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.bin.Cache;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.GraphLine;
26 import jalview.datamodel.SequenceGroup;
27 import jalview.schemes.AnnotationColourGradient;
28 import jalview.schemes.ColourSchemeI;
29 import jalview.util.MessageManager;
31 import java.awt.BorderLayout;
32 import java.awt.Color;
33 import java.awt.Dimension;
34 import java.awt.FlowLayout;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.ActionListener;
37 import java.awt.event.MouseAdapter;
38 import java.awt.event.MouseEvent;
39 import java.util.HashMap;
41 import java.util.Map.Entry;
42 import java.util.Vector;
44 import javax.swing.BorderFactory;
45 import javax.swing.JButton;
46 import javax.swing.JCheckBox;
47 import javax.swing.JColorChooser;
48 import javax.swing.JComboBox;
49 import javax.swing.JInternalFrame;
50 import javax.swing.JLayeredPane;
51 import javax.swing.JPanel;
53 import net.miginfocom.swing.MigLayout;
55 @SuppressWarnings("serial")
56 public class AnnotationColourChooser extends AnnotationRowFilter
58 private static final int ONETHOUSAND = 1000;
60 private ColourSchemeI oldcs;
62 private JButton defColours;
64 private Map<SequenceGroup, ColourSchemeI> oldgroupColours;
66 private Map<AlignmentAnnotation, GraphLine> oldThresholds;
68 private JCheckBox useOriginalColours = new JCheckBox();
70 private JPanel minColour = new JPanel();
72 private JPanel maxColour = new JPanel();
74 private JCheckBox thresholdIsMin = new JCheckBox();
76 protected static final int MIN_WIDTH = 500;
78 protected static final int MIN_HEIGHT = 240;
86 public AnnotationColourChooser(AlignViewport av, final AlignmentPanel ap)
92 frame = new JInternalFrame();
93 frame.setContentPane(this);
94 frame.setLayer(JLayeredPane.PALETTE_LAYER);
95 Desktop.addInternalFrame(frame,
96 MessageManager.getString("label.colour_by_annotation"), 520,
98 frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
99 addSliderChangeListener();
100 addSliderMouseListeners();
102 if (av.getAlignment().getAlignmentAnnotation() == null)
107 // Always get default shading from preferences.
111 if (oldcs instanceof AnnotationColourGradient)
113 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
114 useOriginalColours.setSelected(
115 acg.isPredefinedColours() || acg.getBaseColour() != null);
116 if (!acg.isPredefinedColours() && acg.getBaseColour() == null)
118 minColour.setBackground(acg.getMinColour());
119 maxColour.setBackground(acg.getMaxColour());
121 seqAssociated.setSelected(acg.isSeqAssociated());
124 Vector<String> annotItems = getAnnotationItems(
125 seqAssociated.isSelected());
126 annotations = new JComboBox<>(annotItems);
128 populateThresholdComboBox(threshold);
130 if (oldcs instanceof AnnotationColourGradient)
132 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
133 String label = getAnnotationMenuLabel(acg.getAnnotation());
134 annotations.setSelectedItem(label);
135 switch (acg.getAboveThreshold())
137 case AnnotationColourGradient.NO_THRESHOLD:
138 getThreshold().setSelectedIndex(0);
140 case AnnotationColourGradient.ABOVE_THRESHOLD:
141 getThreshold().setSelectedIndex(1);
143 case AnnotationColourGradient.BELOW_THRESHOLD:
144 getThreshold().setSelectedIndex(2);
147 throw new Error(MessageManager.getString(
148 "error.implementation_error_dont_know_about_threshold_setting"));
150 thresholdIsMin.setSelected(acg.isThresholdIsMinMax());
151 thresholdValue.setText("" + acg.getAnnotationThreshold());
163 * Saves the global and group colour schemes as they are when the dialog is
164 * opened, so they can be restored on Cancel. Note we also save the threshold
165 * value for each annotation, in case this gets modified.
167 void saveInitialState()
169 oldcs = av.getGlobalColourScheme();
170 if (av.getAlignment().getGroups() != null)
172 oldgroupColours = new HashMap<>();
173 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
175 if (sg.getColourScheme() != null)
177 oldgroupColours.put(sg, sg.getColourScheme());
183 * save any existing annotation threshold settings; note we make a copy
184 * of each in case the current threshold value gets amended
186 oldThresholds = new HashMap<>();
187 AlignmentAnnotation[] anns = av.getAlignment().getAlignmentAnnotation();
190 for (AlignmentAnnotation ann : anns)
192 GraphLine thresh = ann.getThreshold();
193 oldThresholds.put(ann,
194 thresh == null ? null : new GraphLine(thresh));
200 protected void jbInit()
204 minColour.setFont(JvSwingUtils.getLabelFont());
205 minColour.setBorder(BorderFactory.createEtchedBorder());
206 minColour.setPreferredSize(new Dimension(40, 20));
207 minColour.setToolTipText(MessageManager.getString("label.min_colour"));
208 minColour.addMouseListener(new MouseAdapter()
211 public void mousePressed(MouseEvent e)
213 if (minColour.isEnabled())
215 minColour_actionPerformed();
219 maxColour.setFont(JvSwingUtils.getLabelFont());
220 maxColour.setBorder(BorderFactory.createEtchedBorder());
221 maxColour.setPreferredSize(new Dimension(40, 20));
222 maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
223 maxColour.addMouseListener(new MouseAdapter()
226 public void mousePressed(MouseEvent e)
228 if (maxColour.isEnabled())
230 maxColour_actionPerformed();
235 defColours = new JButton();
236 defColours.setOpaque(false);
237 defColours.setText(MessageManager.getString("action.set_defaults"));
238 defColours.setToolTipText(MessageManager
239 .getString("label.reset_min_max_colours_to_defaults"));
240 defColours.addActionListener(new ActionListener()
244 public void actionPerformed(ActionEvent arg0)
246 resetColours_actionPerformed();
250 useOriginalColours.setFont(JvSwingUtils.getLabelFont());
251 useOriginalColours.setOpaque(false);
252 useOriginalColours.setText(
253 MessageManager.getString("label.use_original_colours"));
254 useOriginalColours.addActionListener(new ActionListener()
257 public void actionPerformed(ActionEvent e)
259 originalColours_actionPerformed();
262 thresholdIsMin.setBackground(Color.white);
263 thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
265 .setText(MessageManager.getString("label.threshold_minmax"));
266 thresholdIsMin.addActionListener(new ActionListener()
269 public void actionPerformed(ActionEvent actionEvent)
271 thresholdIsMin_actionPerformed();
274 seqAssociated.setBackground(Color.white);
275 seqAssociated.setFont(JvSwingUtils.getLabelFont());
277 .setText(MessageManager.getString("label.per_sequence_only"));
278 seqAssociated.addActionListener(new ActionListener()
282 public void actionPerformed(ActionEvent arg0)
284 seqAssociated_actionPerformed(annotations);
288 this.setLayout(new BorderLayout());
289 JPanel jPanel1 = new JPanel();
290 JPanel jPanel2 = new JPanel();
291 jPanel2.setLayout(new MigLayout("", "[left][center][right]", "[][][]"));
292 jPanel1.setBackground(Color.white);
293 jPanel2.setBackground(Color.white);
297 jPanel2.add(annotations, "grow, wrap");
298 jPanel2.add(seqAssociated);
299 jPanel2.add(useOriginalColours);
300 JPanel colpanel = new JPanel(new FlowLayout());
301 colpanel.setBackground(Color.white);
302 colpanel.add(minColour);
303 colpanel.add(maxColour);
304 jPanel2.add(colpanel, "wrap");
305 jPanel2.add(getThreshold());
306 jPanel2.add(defColours, "skip 1, wrap");
307 jPanel2.add(thresholdIsMin);
308 jPanel2.add(slider, "grow");
309 jPanel2.add(thresholdValue, "grow");
310 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
311 this.add(jPanel2, java.awt.BorderLayout.CENTER);
315 protected void resetColours_actionPerformed()
321 private void setDefaultMinMax()
323 minColour.setBackground(
324 Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN", Color.orange));
325 maxColour.setBackground(
326 Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX", Color.red));
329 public void minColour_actionPerformed()
331 Color col = JColorChooser.showDialog(this,
332 MessageManager.getString("label.select_colour_minimum_value"),
333 minColour.getBackground());
336 minColour.setBackground(col);
342 public void maxColour_actionPerformed()
344 Color col = JColorChooser.showDialog(this,
345 MessageManager.getString("label.select_colour_maximum_value"),
346 maxColour.getBackground());
349 maxColour.setBackground(col);
358 av.setGlobalColourScheme(oldcs);
359 if (av.getAlignment().getGroups() != null)
362 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
364 sg.setColourScheme(oldgroupColours.get(sg));
367 for (Entry<AlignmentAnnotation, GraphLine> entry : oldThresholds
370 entry.getKey().setThreshold(entry.getValue());
375 public void valueChanged(boolean updateAllAnnotation)
377 if (slider.isEnabled())
379 if (useOriginalColours.isSelected() && !(av
380 .getGlobalColourScheme() instanceof AnnotationColourGradient))
384 getCurrentAnnotation().threshold.value = slider.getValue() / 1000f;
385 propagateSeqAssociatedThreshold(updateAllAnnotation,
386 getCurrentAnnotation());
387 ap.paintAlignment(false, false);
391 public void originalColours_actionPerformed()
393 boolean selected = useOriginalColours.isSelected();
398 maxColour.setEnabled(!selected);
399 minColour.setEnabled(!selected);
400 thresholdIsMin.setEnabled(!selected);
405 public void updateView()
407 // Check if combobox is still adjusting
413 setCurrentAnnotation(
414 av.getAlignment().getAlignmentAnnotation()[annmap[annotations
415 .getSelectedIndex()]]);
417 int selectedThresholdItem = getSelectedThresholdItem(
418 getThreshold().getSelectedIndex());
420 slider.setEnabled(true);
421 thresholdValue.setEnabled(true);
422 thresholdIsMin.setEnabled(!useOriginalColours.isSelected());
424 if (selectedThresholdItem == AnnotationColourGradient.NO_THRESHOLD)
426 slider.setEnabled(false);
427 thresholdValue.setEnabled(false);
428 thresholdValue.setText("");
429 thresholdIsMin.setEnabled(false);
431 else if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD
432 && getCurrentAnnotation().threshold == null)
434 getCurrentAnnotation().setThreshold(new GraphLine(
435 (getCurrentAnnotation().graphMax
436 - getCurrentAnnotation().graphMin) / 2f,
437 "Threshold", Color.black));
440 if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD)
443 float range = getCurrentAnnotation().graphMax * ONETHOUSAND
444 - getCurrentAnnotation().graphMin * ONETHOUSAND;
447 (int) (getCurrentAnnotation().graphMin * ONETHOUSAND));
449 (int) (getCurrentAnnotation().graphMax * ONETHOUSAND));
451 (int) (getCurrentAnnotation().threshold.value * ONETHOUSAND));
452 thresholdValue.setText(getCurrentAnnotation().threshold.value + "");
453 slider.setMajorTickSpacing((int) (range / 10f));
454 slider.setEnabled(true);
455 thresholdValue.setEnabled(true);
458 colorAlignmentContaining(getCurrentAnnotation(), selectedThresholdItem);
460 ap.alignmentChanged();
463 protected void colorAlignmentContaining(AlignmentAnnotation currentAnn,
464 int selectedThresholdOption)
467 AnnotationColourGradient acg = null;
468 if (useOriginalColours.isSelected())
470 acg = new AnnotationColourGradient(currentAnn,
471 av.getGlobalColourScheme(), selectedThresholdOption);
475 acg = new AnnotationColourGradient(currentAnn,
476 minColour.getBackground(), maxColour.getBackground(),
477 selectedThresholdOption);
479 acg.setSeqAssociated(seqAssociated.isSelected());
481 if (currentAnn.graphMin == 0f && currentAnn.graphMax == 0f)
483 acg.setPredefinedColours(true);
486 acg.setThresholdIsMinMax(thresholdIsMin.isSelected());
488 av.setGlobalColourScheme(acg);
490 if (av.getAlignment().getGroups() != null)
493 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
500 acg.getInstance(sg, ap.av.getHiddenRepSequences()));
506 protected void sliderDragReleased()
508 super.sliderDragReleased();
509 ap.paintAlignment(true, true);