2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.datamodel.GraphLine;
24 import jalview.schemes.AnnotationColourGradient;
25 import jalview.schemes.GraduatedColor;
26 import jalview.util.MessageManager;
28 import java.awt.BorderLayout;
29 import java.awt.Color;
30 import java.awt.Dimension;
31 import java.awt.FlowLayout;
32 import java.awt.event.ActionEvent;
33 import java.awt.event.ActionListener;
34 import java.awt.event.MouseAdapter;
35 import java.awt.event.MouseEvent;
36 import java.util.Hashtable;
38 import javax.swing.BorderFactory;
39 import javax.swing.JCheckBox;
40 import javax.swing.JColorChooser;
41 import javax.swing.JComboBox;
42 import javax.swing.JLabel;
43 import javax.swing.JPanel;
44 import javax.swing.JSlider;
45 import javax.swing.JTextField;
46 import javax.swing.border.LineBorder;
47 import javax.swing.event.ChangeEvent;
48 import javax.swing.event.ChangeListener;
50 public class FeatureColourChooser extends JalviewDialog
52 // FeatureSettings fs;
55 private GraduatedColor cs;
61 * @return the last colour setting selected by user - either oldcs (which may
62 * be a java.awt.Color) or the new GraduatedColor
64 public Object getLastColour()
73 Hashtable oldgroupColours;
77 boolean adjusting = false;
85 public FeatureColourChooser(FeatureRenderer frender, String type)
87 this(frender, false, type);
90 public FeatureColourChooser(FeatureRenderer frender, boolean block,
96 initDialogFrame(this, true, block, "Graduated Feature Colour for "
98 // frame.setLayer(JLayeredPane.PALETTE_LAYER);
99 // Desktop.addInternalFrame(frame, "Graduated Feature Colour for "+type,
102 slider.addChangeListener(new ChangeListener()
104 public void stateChanged(ChangeEvent evt)
108 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
113 slider.addMouseListener(new MouseAdapter()
115 public void mouseReleased(MouseEvent evt)
119 ap.paintAlignment(true);
125 float mm[] = ((float[][]) fr.getMinMax().get(type))[0];
128 oldcs = fr.getFeatureColours().get(type);
129 if (oldcs instanceof GraduatedColor)
131 if (((GraduatedColor) oldcs).isAutoScale())
134 cs = new GraduatedColor((GraduatedColor) oldcs, min, max);
138 cs = new GraduatedColor((GraduatedColor) oldcs);
143 // promote original color to a graduated color
144 Color bl = Color.black;
145 if (oldcs instanceof Color)
149 // original colour becomes the maximum colour
150 cs = new GraduatedColor(Color.white, bl, mm[0], mm[1]);
151 cs.setColourByLabel(false);
153 minColour.setBackground(oldminColour = cs.getMinColor());
154 maxColour.setBackground(oldmaxColour = cs.getMaxColor());
160 } catch (Exception ex)
163 // update the gui from threshold state
164 thresholdIsMin.setSelected(!cs.isAutoScale());
165 colourByLabel.setSelected(cs.isColourByLabel());
166 if (cs.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
168 // initialise threshold slider and selector
170 .setSelectedIndex(cs.getThreshType() == AnnotationColourGradient.ABOVE_THRESHOLD ? 1
172 slider.setEnabled(true);
173 thresholdValue.setEnabled(true);
174 threshline = new jalview.datamodel.GraphLine((max - min) / 2f,
175 "Threshold", Color.black);
185 public FeatureColourChooser()
190 } catch (Exception ex)
192 ex.printStackTrace();
196 private void jbInit() throws Exception
199 minColour.setFont(JvSwingUtils.getLabelFont());
200 minColour.setBorder(BorderFactory.createLineBorder(Color.black));
201 minColour.setPreferredSize(new Dimension(40, 20));
202 minColour.setToolTipText(MessageManager.getString("label.min_colour"));
203 minColour.addMouseListener(new MouseAdapter()
205 public void mousePressed(MouseEvent e)
207 if (minColour.isEnabled())
209 minColour_actionPerformed();
213 maxColour.setFont(JvSwingUtils.getLabelFont());
214 maxColour.setBorder(BorderFactory.createLineBorder(Color.black));
215 maxColour.setPreferredSize(new Dimension(40, 20));
216 maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
217 maxColour.addMouseListener(new MouseAdapter()
219 public void mousePressed(MouseEvent e)
221 if (maxColour.isEnabled())
223 maxColour_actionPerformed();
227 maxColour.setBorder(new LineBorder(Color.black));
228 minText.setText(MessageManager.getString("label.min"));
229 minText.setFont(JvSwingUtils.getLabelFont());
230 maxText.setText(MessageManager.getString("label.max"));
231 maxText.setFont(JvSwingUtils.getLabelFont());
232 this.setLayout(borderLayout1);
233 jPanel2.setLayout(flowLayout1);
234 jPanel1.setBackground(Color.white);
235 jPanel2.setBackground(Color.white);
236 threshold.addActionListener(new ActionListener()
238 public void actionPerformed(ActionEvent e)
240 threshold_actionPerformed(e);
243 threshold.setToolTipText(MessageManager
244 .getString("label.threshold_feature_display_by_score"));
245 threshold.addItem(MessageManager
246 .getString("label.threshold_feature_no_thereshold")); // index 0
247 threshold.addItem(MessageManager
248 .getString("label.threshold_feature_above_thereshold")); // index 1
249 threshold.addItem(MessageManager
250 .getString("label.threshold_feature_below_thereshold")); // index 2
251 jPanel3.setLayout(flowLayout2);
252 thresholdValue.addActionListener(new ActionListener()
254 public void actionPerformed(ActionEvent e)
256 thresholdValue_actionPerformed(e);
259 slider.setPaintLabels(false);
260 slider.setPaintTicks(true);
261 slider.setBackground(Color.white);
262 slider.setEnabled(false);
263 slider.setOpaque(false);
264 slider.setPreferredSize(new Dimension(100, 32));
265 slider.setToolTipText(MessageManager
266 .getString("label.adjust_thereshold"));
267 thresholdValue.setEnabled(false);
268 thresholdValue.setColumns(7);
269 jPanel3.setBackground(Color.white);
270 thresholdIsMin.setBackground(Color.white);
271 thresholdIsMin.setText(MessageManager
272 .getString("label.threshold_minmax"));
273 thresholdIsMin.setToolTipText(MessageManager
274 .getString("label.toggle_absolute_relative_display_threshold"));
275 thresholdIsMin.addActionListener(new ActionListener()
277 public void actionPerformed(ActionEvent actionEvent)
279 thresholdIsMin_actionPerformed(actionEvent);
282 colourByLabel.setBackground(Color.white);
284 .setText(MessageManager.getString("label.colour_by_label"));
286 .setToolTipText(MessageManager
287 .getString("label.display_features_same_type_different_label_using_different_colour"));
288 colourByLabel.addActionListener(new ActionListener()
290 public void actionPerformed(ActionEvent actionEvent)
292 colourByLabel_actionPerformed(actionEvent);
295 colourPanel.setBackground(Color.white);
298 jPanel2.add(colourByLabel, java.awt.BorderLayout.WEST);
299 jPanel2.add(colourPanel, java.awt.BorderLayout.EAST);
300 colourPanel.add(minText);
301 colourPanel.add(minColour);
302 colourPanel.add(maxText);
303 colourPanel.add(maxColour);
304 this.add(jPanel3, java.awt.BorderLayout.CENTER);
305 jPanel3.add(threshold);
307 jPanel3.add(thresholdValue);
308 jPanel3.add(thresholdIsMin);
309 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
310 this.add(jPanel2, java.awt.BorderLayout.NORTH);
313 JLabel minText = new JLabel();
315 JLabel maxText = new JLabel();
317 JPanel minColour = new JPanel();
319 JPanel maxColour = new JPanel();
321 JPanel colourPanel = new JPanel();
323 JPanel jPanel1 = new JPanel();
325 JPanel jPanel2 = new JPanel();
327 BorderLayout borderLayout1 = new BorderLayout();
329 JComboBox threshold = new JComboBox();
331 FlowLayout flowLayout1 = new FlowLayout();
333 JPanel jPanel3 = new JPanel();
335 FlowLayout flowLayout2 = new FlowLayout();
337 JSlider slider = new JSlider();
339 JTextField thresholdValue = new JTextField(20);
341 // TODO implement GUI for tolower flag
342 // JCheckBox toLower = new JCheckBox();
344 JCheckBox thresholdIsMin = new JCheckBox();
346 JCheckBox colourByLabel = new JCheckBox();
348 private GraphLine threshline;
350 private Color oldmaxColour;
352 private Color oldminColour;
354 public void minColour_actionPerformed()
356 Color col = JColorChooser.showDialog(this,
357 MessageManager.getString("label.select_colour_minimum_value"),
358 minColour.getBackground());
361 minColour.setBackground(col);
362 minColour.setForeground(col);
368 public void maxColour_actionPerformed()
370 Color col = JColorChooser.showDialog(this,
371 MessageManager.getString("label.select_colour_maximum_value"),
372 maxColour.getBackground());
375 maxColour.setBackground(col);
376 maxColour.setForeground(col);
384 // Check if combobox is still adjusting
390 int aboveThreshold = AnnotationColourGradient.NO_THRESHOLD;
391 if (threshold.getSelectedIndex() == 1)
393 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
395 else if (threshold.getSelectedIndex() == 2)
397 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
400 slider.setEnabled(true);
401 thresholdValue.setEnabled(true);
404 if (cs.isColourByLabel())
406 acg = new GraduatedColor(oldminColour, oldmaxColour, min, max);
410 acg = new GraduatedColor(oldminColour = minColour.getBackground(),
411 oldmaxColour = maxColour.getBackground(), min, max);
415 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
417 slider.setEnabled(false);
418 thresholdValue.setEnabled(false);
419 thresholdValue.setText("");
420 thresholdIsMin.setEnabled(false);
422 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
423 && threshline == null)
425 // todo visual indication of feature threshold
426 threshline = new jalview.datamodel.GraphLine((max - min) / 2f,
427 "Threshold", Color.black);
430 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
433 acg.setThresh(threshline.value);
435 float range = max * 1000f - min * 1000f;
437 slider.setMinimum((int) (min * 1000));
438 slider.setMaximum((int) (max * 1000));
439 slider.setValue((int) (threshline.value * 1000));
440 thresholdValue.setText(threshline.value + "");
441 slider.setMajorTickSpacing((int) (range / 10f));
442 slider.setEnabled(true);
443 thresholdValue.setEnabled(true);
444 thresholdIsMin.setEnabled(!colourByLabel.isSelected());
448 acg.setThreshType(aboveThreshold);
449 if (thresholdIsMin.isSelected()
450 && aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
452 acg.setAutoScaled(false);
453 if (aboveThreshold == AnnotationColourGradient.ABOVE_THRESHOLD)
455 acg = new GraduatedColor(acg, threshline.value, max);
459 acg = new GraduatedColor(acg, min, threshline.value);
464 acg.setAutoScaled(true);
466 acg.setColourByLabel(colourByLabel.isSelected());
467 if (acg.isColourByLabel())
469 maxColour.setEnabled(false);
470 minColour.setEnabled(false);
471 maxColour.setBackground(this.getBackground());
472 maxColour.setForeground(this.getBackground());
473 minColour.setBackground(this.getBackground());
474 minColour.setForeground(this.getBackground());
479 maxColour.setEnabled(true);
480 minColour.setEnabled(true);
481 maxColour.setBackground(oldmaxColour);
482 minColour.setBackground(oldminColour);
483 maxColour.setForeground(oldmaxColour);
484 minColour.setForeground(oldminColour);
486 fr.setColour(type, acg);
488 ap.paintAlignment(false);
491 protected void raiseClosed()
493 if (this.colourEditor != null)
495 colourEditor.actionPerformed(new ActionEvent(this, 0, "CLOSED"));
499 public void okPressed()
504 public void cancelPressed()
511 fr.setColour(type, oldcs);
512 ap.paintAlignment(false);
516 public void thresholdCheck_actionPerformed(ActionEvent e)
521 public void annotations_actionPerformed(ActionEvent e)
526 public void threshold_actionPerformed(ActionEvent e)
531 public void thresholdValue_actionPerformed(ActionEvent e)
535 float f = Float.parseFloat(thresholdValue.getText());
536 slider.setValue((int) (f * 1000));
537 threshline.value = f;
538 } catch (NumberFormatException ex)
543 public void valueChanged()
545 threshline.value = (float) slider.getValue() / 1000f;
546 cs.setThresh(threshline.value);
548 ap.paintAlignment(false);
551 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)
556 public void colourByLabel_actionPerformed(ActionEvent actionEvent)
561 ActionListener colourEditor = null;
563 public void addActionListener(ActionListener graduatedColorEditor)
565 if (colourEditor != null)
568 .println("IMPLEMENTATION ISSUE: overwriting action listener for FeatureColourChooser");
570 colourEditor = graduatedColorEditor;