2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
3 * Copyright (C) 2014 The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
24 import java.awt.event.*;
26 import javax.swing.border.LineBorder;
27 import javax.swing.event.*;
29 import jalview.datamodel.*;
30 import jalview.schemes.*;
31 import jalview.util.MessageManager;
33 import java.awt.Dimension;
35 public class FeatureColourChooser extends JalviewDialog
37 // FeatureSettings fs;
40 private GraduatedColor cs;
46 * @return the last colour setting selected by user - either oldcs (which may
47 * be a java.awt.Color) or the new GraduatedColor
49 public Object getLastColour()
58 Hashtable oldgroupColours;
62 boolean adjusting = false;
70 public FeatureColourChooser(FeatureRenderer frender, String type)
72 this(frender, false, type);
75 public FeatureColourChooser(FeatureRenderer frender, boolean block,
81 initDialogFrame(this, true, block, "Graduated Feature Colour for "
83 // frame.setLayer(JLayeredPane.PALETTE_LAYER);
84 // Desktop.addInternalFrame(frame, "Graduated Feature Colour for "+type,
87 slider.addChangeListener(new ChangeListener()
89 public void stateChanged(ChangeEvent evt)
93 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
98 slider.addMouseListener(new MouseAdapter()
100 public void mouseReleased(MouseEvent evt)
104 ap.paintAlignment(true);
110 float mm[] = ((float[][]) fr.minmax.get(type))[0];
113 oldcs = fr.featureColours.get(type);
114 if (oldcs instanceof GraduatedColor)
116 if (((GraduatedColor) oldcs).isAutoScale())
119 cs = new GraduatedColor((GraduatedColor) oldcs, min, max);
123 cs = new GraduatedColor((GraduatedColor) oldcs);
128 // promote original color to a graduated color
129 Color bl = Color.black;
130 if (oldcs instanceof Color)
134 // original colour becomes the maximum colour
135 cs = new GraduatedColor(Color.white, bl, mm[0], mm[1]);
136 cs.setColourByLabel(false);
138 minColour.setBackground(oldminColour = cs.getMinColor());
139 maxColour.setBackground(oldmaxColour = cs.getMaxColor());
145 } catch (Exception ex)
148 // update the gui from threshold state
149 thresholdIsMin.setSelected(!cs.isAutoScale());
150 colourByLabel.setSelected(cs.isColourByLabel());
151 if (cs.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
153 // initialise threshold slider and selector
155 .setSelectedIndex(cs.getThreshType() == AnnotationColourGradient.ABOVE_THRESHOLD ? 1
157 slider.setEnabled(true);
158 thresholdValue.setEnabled(true);
159 threshline = new jalview.datamodel.GraphLine((max - min) / 2f,
160 "Threshold", Color.black);
170 public FeatureColourChooser()
175 } catch (Exception ex)
177 ex.printStackTrace();
181 private void jbInit() throws Exception
184 minColour.setFont(JvSwingUtils.getLabelFont());
185 minColour.setBorder(BorderFactory.createLineBorder(Color.black));
186 minColour.setPreferredSize(new Dimension(40, 20));
187 minColour.setToolTipText(MessageManager.getString("label.min_colour"));
188 minColour.addMouseListener(new MouseAdapter()
190 public void mousePressed(MouseEvent e)
192 if (minColour.isEnabled())
194 minColour_actionPerformed();
198 maxColour.setFont(JvSwingUtils.getLabelFont());
199 maxColour.setBorder(BorderFactory.createLineBorder(Color.black));
200 maxColour.setPreferredSize(new Dimension(40, 20));
201 maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
202 maxColour.addMouseListener(new MouseAdapter()
204 public void mousePressed(MouseEvent e)
206 if (maxColour.isEnabled())
208 maxColour_actionPerformed();
212 maxColour.setBorder(new LineBorder(Color.black));
213 minText.setText(MessageManager.getString("label.min"));
214 minText.setFont(JvSwingUtils.getLabelFont());
215 maxText.setText(MessageManager.getString("label.max"));
216 maxText.setFont(JvSwingUtils.getLabelFont());
217 this.setLayout(borderLayout1);
218 jPanel2.setLayout(flowLayout1);
219 jPanel1.setBackground(Color.white);
220 jPanel2.setBackground(Color.white);
221 threshold.addActionListener(new ActionListener()
223 public void actionPerformed(ActionEvent e)
225 threshold_actionPerformed(e);
228 threshold.setToolTipText(MessageManager.getString("label.threshold_feature_display_by_score"));
229 threshold.addItem(MessageManager.getString("label.threshold_feature_no_thereshold")); // index 0
230 threshold.addItem(MessageManager.getString("label.threshold_feature_above_thereshold")); // index 1
231 threshold.addItem(MessageManager.getString("label.threshold_feature_below_thereshold")); // index 2
232 jPanel3.setLayout(flowLayout2);
233 thresholdValue.addActionListener(new ActionListener()
235 public void actionPerformed(ActionEvent e)
237 thresholdValue_actionPerformed(e);
240 slider.setPaintLabels(false);
241 slider.setPaintTicks(true);
242 slider.setBackground(Color.white);
243 slider.setEnabled(false);
244 slider.setOpaque(false);
245 slider.setPreferredSize(new Dimension(100, 32));
246 slider.setToolTipText(MessageManager.getString("label.adjust_thereshold"));
247 thresholdValue.setEnabled(false);
248 thresholdValue.setColumns(7);
249 jPanel3.setBackground(Color.white);
250 thresholdIsMin.setBackground(Color.white);
251 thresholdIsMin.setText(MessageManager.getString("label.threshold_minmax"));
253 .setToolTipText(MessageManager.getString("label.toggle_absolute_relative_display_threshold"));
254 thresholdIsMin.addActionListener(new ActionListener()
256 public void actionPerformed(ActionEvent actionEvent)
258 thresholdIsMin_actionPerformed(actionEvent);
261 colourByLabel.setBackground(Color.white);
262 colourByLabel.setText(MessageManager.getString("label.colour_by_label"));
264 .setToolTipText(MessageManager.getString("label.display_features_same_type_different_label_using_different_colour"));
265 colourByLabel.addActionListener(new ActionListener()
267 public void actionPerformed(ActionEvent actionEvent)
269 colourByLabel_actionPerformed(actionEvent);
272 colourPanel.setBackground(Color.white);
275 jPanel2.add(colourByLabel, java.awt.BorderLayout.WEST);
276 jPanel2.add(colourPanel, java.awt.BorderLayout.EAST);
277 colourPanel.add(minText);
278 colourPanel.add(minColour);
279 colourPanel.add(maxText);
280 colourPanel.add(maxColour);
281 this.add(jPanel3, java.awt.BorderLayout.CENTER);
282 jPanel3.add(threshold);
284 jPanel3.add(thresholdValue);
285 jPanel3.add(thresholdIsMin);
286 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
287 this.add(jPanel2, java.awt.BorderLayout.NORTH);
290 JLabel minText = new JLabel();
292 JLabel maxText = new JLabel();
294 JPanel minColour = new JPanel();
296 JPanel maxColour = new JPanel();
298 JPanel colourPanel = new JPanel();
300 JPanel jPanel1 = new JPanel();
302 JPanel jPanel2 = new JPanel();
304 BorderLayout borderLayout1 = new BorderLayout();
306 JComboBox threshold = new JComboBox();
308 FlowLayout flowLayout1 = new FlowLayout();
310 JPanel jPanel3 = new JPanel();
312 FlowLayout flowLayout2 = new FlowLayout();
314 JSlider slider = new JSlider();
316 JTextField thresholdValue = new JTextField(20);
318 // TODO implement GUI for tolower flag
319 // JCheckBox toLower = new JCheckBox();
321 JCheckBox thresholdIsMin = new JCheckBox();
323 JCheckBox colourByLabel = new JCheckBox();
325 private GraphLine threshline;
327 private Color oldmaxColour;
329 private Color oldminColour;
331 public void minColour_actionPerformed()
333 Color col = JColorChooser.showDialog(this,
334 MessageManager.getString("label.select_colour_minimum_value"), minColour.getBackground());
337 minColour.setBackground(col);
338 minColour.setForeground(col);
344 public void maxColour_actionPerformed()
346 Color col = JColorChooser.showDialog(this,
347 MessageManager.getString("label.select_colour_maximum_value"), maxColour.getBackground());
350 maxColour.setBackground(col);
351 maxColour.setForeground(col);
359 // Check if combobox is still adjusting
365 int aboveThreshold = AnnotationColourGradient.NO_THRESHOLD;
366 if (threshold.getSelectedIndex()==1)
368 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
370 else if (threshold.getSelectedIndex()==2)
372 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
375 slider.setEnabled(true);
376 thresholdValue.setEnabled(true);
379 if (cs.isColourByLabel())
381 acg = new GraduatedColor(oldminColour, oldmaxColour, min, max);
385 acg = new GraduatedColor(oldminColour = minColour.getBackground(),
386 oldmaxColour = maxColour.getBackground(), min, max);
390 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
392 slider.setEnabled(false);
393 thresholdValue.setEnabled(false);
394 thresholdValue.setText("");
395 thresholdIsMin.setEnabled(false);
397 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
398 && threshline == null)
400 // todo visual indication of feature threshold
401 threshline = new jalview.datamodel.GraphLine((max - min) / 2f,
402 "Threshold", Color.black);
405 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
408 acg.setThresh(threshline.value);
410 float range = max * 1000f - min * 1000f;
412 slider.setMinimum((int) (min * 1000));
413 slider.setMaximum((int) (max * 1000));
414 slider.setValue((int) (threshline.value * 1000));
415 thresholdValue.setText(threshline.value + "");
416 slider.setMajorTickSpacing((int) (range / 10f));
417 slider.setEnabled(true);
418 thresholdValue.setEnabled(true);
419 thresholdIsMin.setEnabled(!colourByLabel.isSelected());
423 acg.setThreshType(aboveThreshold);
424 if (thresholdIsMin.isSelected()
425 && aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
427 acg.setAutoScaled(false);
428 if (aboveThreshold == AnnotationColourGradient.ABOVE_THRESHOLD)
430 acg = new GraduatedColor(acg, threshline.value, max);
434 acg = new GraduatedColor(acg, min, threshline.value);
439 acg.setAutoScaled(true);
441 acg.setColourByLabel(colourByLabel.isSelected());
442 if (acg.isColourByLabel())
444 maxColour.setEnabled(false);
445 minColour.setEnabled(false);
446 maxColour.setBackground(this.getBackground());
447 maxColour.setForeground(this.getBackground());
448 minColour.setBackground(this.getBackground());
449 minColour.setForeground(this.getBackground());
454 maxColour.setEnabled(true);
455 minColour.setEnabled(true);
456 maxColour.setBackground(oldmaxColour);
457 minColour.setBackground(oldminColour);
458 maxColour.setForeground(oldmaxColour);
459 minColour.setForeground(oldminColour);
461 fr.featureColours.put(type, acg);
463 ap.paintAlignment(false);
466 protected void raiseClosed()
468 if (this.colourEditor != null)
470 colourEditor.actionPerformed(new ActionEvent(this, 0, "CLOSED"));
474 public void okPressed()
479 public void cancelPressed()
486 fr.featureColours.put(type, oldcs);
487 ap.paintAlignment(false);
491 public void thresholdCheck_actionPerformed(ActionEvent e)
496 public void annotations_actionPerformed(ActionEvent e)
501 public void threshold_actionPerformed(ActionEvent e)
506 public void thresholdValue_actionPerformed(ActionEvent e)
510 float f = Float.parseFloat(thresholdValue.getText());
511 slider.setValue((int) (f * 1000));
512 threshline.value = f;
513 } catch (NumberFormatException ex)
518 public void valueChanged()
520 threshline.value = (float) slider.getValue() / 1000f;
521 cs.setThresh(threshline.value);
523 ap.paintAlignment(false);
526 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)
531 public void colourByLabel_actionPerformed(ActionEvent actionEvent)
536 ActionListener colourEditor = null;
538 public void addActionListener(ActionListener graduatedColorEditor)
540 if (colourEditor != null)
543 .println("IMPLEMENTATION ISSUE: overwriting action listener for FeatureColourChooser");
545 colourEditor = graduatedColorEditor;