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.api.FeatureColourI;
24 import jalview.datamodel.GraphLine;
25 import jalview.schemes.FeatureColour;
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.FocusAdapter;
35 import java.awt.event.FocusEvent;
36 import java.awt.event.MouseAdapter;
37 import java.awt.event.MouseEvent;
39 import javax.swing.BorderFactory;
40 import javax.swing.JCheckBox;
41 import javax.swing.JColorChooser;
42 import javax.swing.JComboBox;
43 import javax.swing.JLabel;
44 import javax.swing.JPanel;
45 import javax.swing.JSlider;
46 import javax.swing.JTextField;
47 import javax.swing.border.LineBorder;
48 import javax.swing.event.ChangeEvent;
49 import javax.swing.event.ChangeListener;
51 public class FeatureColourChooser extends JalviewDialog
53 // FeatureSettings fs;
54 private FeatureRenderer fr;
56 private FeatureColourI cs;
58 private FeatureColourI oldcs;
60 private AlignmentPanel ap;
62 private boolean adjusting = false;
64 final private float min;
66 final private float max;
68 final private float scaleFactor;
70 private String type = null;
72 private JPanel minColour = new JPanel();
74 private JPanel maxColour = new JPanel();
76 private JComboBox<String> threshold = new JComboBox<>();
78 private JSlider slider = new JSlider();
80 private JTextField thresholdValue = new JTextField(20);
82 // TODO implement GUI for tolower flag
83 // JCheckBox toLower = new JCheckBox();
85 private JCheckBox thresholdIsMin = new JCheckBox();
87 private JCheckBox colourByLabel = new JCheckBox();
89 private GraphLine threshline;
91 private Color oldmaxColour;
93 private Color oldminColour;
95 private ActionListener colourEditor = null;
103 public FeatureColourChooser(FeatureRenderer frender, String theType)
105 this(frender, false, theType);
109 * Constructor, with option to make a blocking dialog (has to complete in the
110 * AWT event queue thread). Currently this option is always set to false.
116 FeatureColourChooser(FeatureRenderer frender, boolean blocking,
122 String title = MessageManager.formatMessage(
123 "label.graduated_color_for_params", new String[] { theType });
124 initDialogFrame(this, true, blocking, title, 480, 185);
126 slider.addChangeListener(new ChangeListener()
129 public void stateChanged(ChangeEvent evt)
133 thresholdValue.setText((slider.getValue() / scaleFactor) + "");
134 sliderValueChanged();
138 slider.addMouseListener(new MouseAdapter()
141 public void mouseReleased(MouseEvent evt)
144 * only update Overview and/or structure colouring
145 * when threshold slider drag ends (mouse up)
149 ap.paintAlignment(true);
154 float mm[] = fr.getMinMax().get(theType)[0];
159 * ensure scale factor allows a scaled range with
160 * 10 integer divisions ('ticks'); if we have got here,
161 * we should expect that max != min
163 scaleFactor = (max == min) ? 1f : 100f / (max - min);
165 oldcs = fr.getFeatureColours().get(theType);
166 if (!oldcs.isSimpleColour())
168 if (oldcs.isAutoScaled())
171 cs = new FeatureColour((FeatureColour) oldcs, min, max);
175 cs = new FeatureColour((FeatureColour) oldcs);
180 // promote original color to a graduated color
181 Color bl = oldcs.getColour();
186 // original colour becomes the maximum colour
187 cs = new FeatureColour(Color.white, bl, mm[0], mm[1]);
188 cs.setColourByLabel(false);
190 minColour.setBackground(oldminColour = cs.getMinColour());
191 maxColour.setBackground(oldmaxColour = cs.getMaxColour());
197 } catch (Exception ex)
200 // update the gui from threshold state
201 thresholdIsMin.setSelected(!cs.isAutoScaled());
202 colourByLabel.setSelected(cs.isColourByLabel());
203 if (cs.hasThreshold())
205 // initialise threshold slider and selector
206 threshold.setSelectedIndex(cs.isAboveThreshold() ? 1 : 2);
207 slider.setEnabled(true);
208 slider.setValue((int) (cs.getThreshold() * scaleFactor));
209 thresholdValue.setEnabled(true);
210 threshline = new GraphLine((max - min) / 2f, "Threshold", Color.black);
211 threshline.value = cs.getThreshold();
220 private void jbInit() throws Exception
223 minColour.setFont(JvSwingUtils.getLabelFont());
224 minColour.setBorder(BorderFactory.createLineBorder(Color.black));
225 minColour.setPreferredSize(new Dimension(40, 20));
226 minColour.setToolTipText(MessageManager.getString("label.min_colour"));
227 minColour.addMouseListener(new MouseAdapter()
230 public void mousePressed(MouseEvent e)
232 if (minColour.isEnabled())
234 minColour_actionPerformed();
238 maxColour.setFont(JvSwingUtils.getLabelFont());
239 maxColour.setBorder(BorderFactory.createLineBorder(Color.black));
240 maxColour.setPreferredSize(new Dimension(40, 20));
241 maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
242 maxColour.addMouseListener(new MouseAdapter()
245 public void mousePressed(MouseEvent e)
247 if (maxColour.isEnabled())
249 maxColour_actionPerformed();
253 maxColour.setBorder(new LineBorder(Color.black));
254 JLabel minText = new JLabel(MessageManager.getString("label.min"));
255 minText.setFont(JvSwingUtils.getLabelFont());
256 JLabel maxText = new JLabel(MessageManager.getString("label.max"));
257 maxText.setFont(JvSwingUtils.getLabelFont());
258 this.setLayout(new BorderLayout());
259 JPanel jPanel1 = new JPanel();
260 jPanel1.setBackground(Color.white);
261 JPanel jPanel2 = new JPanel();
262 jPanel2.setLayout(new FlowLayout());
263 jPanel2.setBackground(Color.white);
264 threshold.addActionListener(new ActionListener()
267 public void actionPerformed(ActionEvent e)
269 threshold_actionPerformed();
272 threshold.setToolTipText(MessageManager
273 .getString("label.threshold_feature_display_by_score"));
274 threshold.addItem(MessageManager
275 .getString("label.threshold_feature_no_threshold")); // index 0
276 threshold.addItem(MessageManager
277 .getString("label.threshold_feature_above_threshold")); // index 1
278 threshold.addItem(MessageManager
279 .getString("label.threshold_feature_below_threshold")); // index 2
281 JPanel jPanel3 = new JPanel();
282 jPanel3.setLayout(new FlowLayout());
283 thresholdValue.addActionListener(new ActionListener()
286 public void actionPerformed(ActionEvent e)
288 thresholdValue_actionPerformed();
291 thresholdValue.addFocusListener(new FocusAdapter()
294 public void focusLost(FocusEvent e)
296 thresholdValue_actionPerformed();
299 slider.setPaintLabels(false);
300 slider.setPaintTicks(true);
301 slider.setBackground(Color.white);
302 slider.setEnabled(false);
303 slider.setOpaque(false);
304 slider.setPreferredSize(new Dimension(100, 32));
305 slider.setToolTipText(MessageManager
306 .getString("label.adjust_threshold"));
307 thresholdValue.setEnabled(false);
308 thresholdValue.setColumns(7);
309 jPanel3.setBackground(Color.white);
310 thresholdIsMin.setBackground(Color.white);
311 thresholdIsMin.setText(MessageManager
312 .getString("label.threshold_minmax"));
313 thresholdIsMin.setToolTipText(MessageManager
314 .getString("label.toggle_absolute_relative_display_threshold"));
315 thresholdIsMin.addActionListener(new ActionListener()
318 public void actionPerformed(ActionEvent actionEvent)
320 thresholdIsMin_actionPerformed();
323 colourByLabel.setBackground(Color.white);
325 .setText(MessageManager.getString("label.colour_by_label"));
327 .setToolTipText(MessageManager
328 .getString("label.display_features_same_type_different_label_using_different_colour"));
329 colourByLabel.addActionListener(new ActionListener()
332 public void actionPerformed(ActionEvent actionEvent)
334 colourByLabel_actionPerformed();
338 JPanel colourPanel = new JPanel();
339 colourPanel.setBackground(Color.white);
342 jPanel2.add(colourByLabel, BorderLayout.WEST);
343 jPanel2.add(colourPanel, BorderLayout.EAST);
344 colourPanel.add(minText);
345 colourPanel.add(minColour);
346 colourPanel.add(maxText);
347 colourPanel.add(maxColour);
348 this.add(jPanel3, BorderLayout.CENTER);
349 jPanel3.add(threshold);
351 jPanel3.add(thresholdValue);
352 jPanel3.add(thresholdIsMin);
353 this.add(jPanel1, BorderLayout.SOUTH);
354 this.add(jPanel2, BorderLayout.NORTH);
358 * Action on clicking the 'minimum colour' - open a colour chooser dialog, and
359 * set the selected colour (if the user does not cancel out of the dialog)
361 protected void minColour_actionPerformed()
363 Color col = JColorChooser.showDialog(this,
364 MessageManager.getString("label.select_colour_minimum_value"),
365 minColour.getBackground());
368 minColour.setBackground(col);
369 minColour.setForeground(col);
376 * Action on clicking the 'maximum colour' - open a colour chooser dialog, and
377 * set the selected colour (if the user does not cancel out of the dialog)
379 protected void maxColour_actionPerformed()
381 Color col = JColorChooser.showDialog(this,
382 MessageManager.getString("label.select_colour_maximum_value"),
383 maxColour.getBackground());
386 maxColour.setBackground(col);
387 maxColour.setForeground(col);
394 * Constructs and sets the selected colour options as the colour for the
395 * feature type, and repaints the alignment, and optionally the Overview
396 * and/or structure viewer if open
398 * @param updateOverview
400 void changeColour(boolean updateOverview)
402 // Check if combobox is still adjusting
408 boolean aboveThreshold = false;
409 boolean belowThreshold = false;
410 if (threshold.getSelectedIndex() == 1)
412 aboveThreshold = true;
414 else if (threshold.getSelectedIndex() == 2)
416 belowThreshold = true;
418 boolean hasThreshold = aboveThreshold || belowThreshold;
420 slider.setEnabled(true);
421 thresholdValue.setEnabled(true);
424 if (cs.isColourByLabel())
426 acg = new FeatureColour(oldminColour, oldmaxColour, min, max);
430 acg = new FeatureColour(oldminColour = minColour.getBackground(),
431 oldmaxColour = maxColour.getBackground(), min, max);
436 slider.setEnabled(false);
437 thresholdValue.setEnabled(false);
438 thresholdValue.setText("");
439 thresholdIsMin.setEnabled(false);
441 else if (threshline == null)
444 * todo not yet implemented: visual indication of feature threshold
446 threshline = new GraphLine((max - min) / 2f, "Threshold", Color.black);
452 acg.setThreshold(threshline.value);
454 float range = (max - min) * scaleFactor;
456 slider.setMinimum((int) (min * scaleFactor));
457 slider.setMaximum((int) (max * scaleFactor));
458 // slider.setValue((int) (threshline.value * scaleFactor));
459 slider.setValue(Math.round(threshline.value * scaleFactor));
460 thresholdValue.setText(threshline.value + "");
461 slider.setMajorTickSpacing((int) (range / 10f));
462 slider.setEnabled(true);
463 thresholdValue.setEnabled(true);
464 thresholdIsMin.setEnabled(!colourByLabel.isSelected());
468 acg.setAboveThreshold(aboveThreshold);
469 acg.setBelowThreshold(belowThreshold);
470 if (thresholdIsMin.isSelected() && hasThreshold)
472 acg.setAutoScaled(false);
475 acg = new FeatureColour((FeatureColour) acg, threshline.value, max);
479 acg = new FeatureColour((FeatureColour) acg, min, threshline.value);
484 acg.setAutoScaled(true);
486 acg.setColourByLabel(colourByLabel.isSelected());
487 if (acg.isColourByLabel())
489 maxColour.setEnabled(false);
490 minColour.setEnabled(false);
491 maxColour.setBackground(this.getBackground());
492 maxColour.setForeground(this.getBackground());
493 minColour.setBackground(this.getBackground());
494 minColour.setForeground(this.getBackground());
499 maxColour.setEnabled(true);
500 minColour.setEnabled(true);
501 maxColour.setBackground(oldmaxColour);
502 minColour.setBackground(oldminColour);
503 maxColour.setForeground(oldmaxColour);
504 minColour.setForeground(oldminColour);
506 fr.setColour(type, acg);
508 ap.paintAlignment(updateOverview);
512 protected void raiseClosed()
514 if (this.colourEditor != null)
516 colourEditor.actionPerformed(new ActionEvent(this, 0, "CLOSED"));
521 public void okPressed()
527 public void cancelPressed()
533 * Action when the user cancels the dialog. All previous settings should be
534 * restored and rendered on the alignment, and any linked Overview window or
539 fr.setColour(type, oldcs);
540 ap.paintAlignment(true);
545 * Action on change of choice of No / Above / Below Threshold
547 protected void threshold_actionPerformed()
553 * Action on text entry of a threshold value
555 protected void thresholdValue_actionPerformed()
559 float f = Float.parseFloat(thresholdValue.getText());
560 slider.setValue((int) (f * scaleFactor));
561 threshline.value = f;
564 * force repaint of any Overview window or structure
566 ap.paintAlignment(true);
567 } catch (NumberFormatException ex)
573 * Action on change of threshold slider value. This may be done interactively
574 * (by moving the slider), or programmatically (to update the slider after
575 * manual input of a threshold value).
577 protected void sliderValueChanged()
580 * squash rounding errors by forcing min/max of slider to
581 * actual min/max of feature score range
583 int value = slider.getValue();
584 threshline.value = value == slider.getMaximum() ? max
585 : (value == slider.getMinimum() ? min : value / scaleFactor);
586 cs.setThreshold(threshline.value);
589 * repaint alignment, but not Overview or structure,
590 * to avoid overload while dragging the slider
595 protected void thresholdIsMin_actionPerformed()
600 protected void colourByLabel_actionPerformed()
605 void addActionListener(ActionListener graduatedColorEditor)
607 if (colourEditor != null)
610 .println("IMPLEMENTATION ISSUE: overwriting action listener for FeatureColourChooser");
612 colourEditor = graduatedColorEditor;
616 * Answers the last colour setting selected by user - either oldcs (which may
617 * be a java.awt.Color) or the new GraduatedColor
621 FeatureColourI getLastColour()