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 java.awt.BorderLayout;
24 import java.awt.Color;
25 import java.awt.Dimension;
26 import java.awt.FlowLayout;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
29 import java.awt.event.MouseAdapter;
30 import java.awt.event.MouseEvent;
31 import java.util.Hashtable;
32 import java.util.Vector;
34 import javax.swing.BorderFactory;
35 import javax.swing.JButton;
36 import javax.swing.JCheckBox;
37 import javax.swing.JComboBox;
38 import javax.swing.JInternalFrame;
39 import javax.swing.JLayeredPane;
40 import javax.swing.JPanel;
42 import jalview.bin.Cache;
43 import jalview.datamodel.AlignmentAnnotation;
44 import jalview.datamodel.Annotation;
45 import jalview.datamodel.GraphLine;
46 import jalview.datamodel.SequenceGroup;
47 import jalview.gui.JalviewColourChooser.ColourChooserListener;
48 import jalview.schemes.AnnotationColourGradient;
49 import jalview.schemes.ColourSchemeI;
50 import jalview.util.MessageManager;
51 import net.miginfocom.swing.MigLayout;
53 @SuppressWarnings("serial")
54 public class AnnotationColourChooser extends AnnotationRowFilter
56 private ColourSchemeI oldcs;
58 private JButton defColours;
60 private Hashtable<SequenceGroup, ColourSchemeI> oldgroupColours;
62 private JCheckBox useOriginalColours = new JCheckBox();
64 JPanel minColour = new JPanel();
66 JPanel maxColour = new JPanel();
68 private JCheckBox thresholdIsMin = new JCheckBox();
70 protected static final int MIN_WIDTH = 500;
72 protected static final int MIN_HEIGHT = 240;
74 public AnnotationColourChooser(AlignViewport av, final AlignmentPanel ap)
78 public AnnotationColourChooser(AlignViewport av, final AlignmentPanel ap,AnnotationColourGradient initSettings)
81 oldcs = av.getGlobalColourScheme();
82 if (av.getAlignment().getGroups() != null)
84 oldgroupColours = new Hashtable<>();
85 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
87 if (sg.getColourScheme() != null)
89 oldgroupColours.put(sg, sg.getColourScheme());
93 frame = new JInternalFrame();
94 frame.setFrameIcon(null);
95 frame.setContentPane(this);
96 frame.setLayer(JLayeredPane.PALETTE_LAYER);
97 Desktop.addInternalFrame(frame,
98 MessageManager.getString("label.colour_by_annotation"), 520,
100 frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
101 addSliderChangeListener();
102 addSliderMouseListeners();
104 if (av.getAlignment().getAlignmentAnnotation() == null)
109 // Always get default shading from preferences.
113 if (oldcs instanceof AnnotationColourGradient && initSettings==null)
116 initialiseFrom((AnnotationColourGradient) oldcs);
118 // use initial colour gradient - if any..
119 initialiseFrom(initSettings);
129 private void initialiseFrom(AnnotationColourGradient acg)
133 useOriginalColours.setSelected(
134 acg.isPredefinedColours() || acg.getBaseColour() != null);
135 if (!acg.isPredefinedColours() && acg.getBaseColour() == null)
137 minColour.setBackground(acg.getMinColour());
138 maxColour.setBackground(acg.getMaxColour());
140 seqAssociated.setSelected(acg.isSeqAssociated());
143 Vector<String> annotItems = getAnnotationItems(
144 seqAssociated.isSelected());
145 annotations = new JComboBox<>(annotItems);
147 populateThresholdComboBox(threshold);
151 String label = getAnnotationMenuLabel(acg.getAnnotation());
152 // TODO: workaround below shouldn't be necessary - there's a bug in getAnnotationMenuLabel!
153 if (acg.isSeqAssociated())
155 label = acg.getAnnotation().label;
157 annotations.setSelectedItem(label);
158 switch (acg.getAboveThreshold())
160 case AnnotationColourGradient.NO_THRESHOLD:
161 getThreshold().setSelectedIndex(0);
163 case AnnotationColourGradient.ABOVE_THRESHOLD:
164 getThreshold().setSelectedIndex(1);
166 case AnnotationColourGradient.BELOW_THRESHOLD:
167 getThreshold().setSelectedIndex(2);
170 throw new Error(MessageManager.getString(
171 "error.implementation_error_dont_know_about_threshold_setting"));
173 thresholdIsMin.setSelected(acg.isThresholdIsMinMax());
174 thresholdValue.setText(String.valueOf(acg.getAnnotationThreshold()));
179 protected void jbInit()
183 minColour.setFont(JvSwingUtils.getLabelFont());
184 minColour.setBorder(BorderFactory.createEtchedBorder());
185 minColour.setPreferredSize(new Dimension(40, 20));
186 minColour.setToolTipText(MessageManager.getString("label.min_colour"));
187 minColour.addMouseListener(new MouseAdapter()
190 public void mousePressed(MouseEvent e)
192 if (minColour.isEnabled())
194 showColourChooser(minColour, "label.select_colour_minimum_value");
198 maxColour.setFont(JvSwingUtils.getLabelFont());
199 maxColour.setBorder(BorderFactory.createEtchedBorder());
200 maxColour.setPreferredSize(new Dimension(40, 20));
201 maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
202 maxColour.addMouseListener(new MouseAdapter()
205 public void mousePressed(MouseEvent e)
207 if (maxColour.isEnabled())
209 showColourChooser(maxColour, "label.select_colour_maximum_value");
214 defColours = new JButton();
215 defColours.setOpaque(false);
216 defColours.setText(MessageManager.getString("action.set_defaults"));
217 defColours.setToolTipText(MessageManager
218 .getString("label.reset_min_max_colours_to_defaults"));
219 defColours.addActionListener(new ActionListener()
223 public void actionPerformed(ActionEvent arg0)
225 resetColours_actionPerformed();
229 useOriginalColours.setFont(JvSwingUtils.getLabelFont());
230 useOriginalColours.setOpaque(false);
231 useOriginalColours.setText(
232 MessageManager.getString("label.use_original_colours"));
233 useOriginalColours.addActionListener(new ActionListener()
236 public void actionPerformed(ActionEvent e)
238 originalColours_actionPerformed();
241 thresholdIsMin.setBackground(Color.white);
242 thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
244 .setText(MessageManager.getString("label.threshold_minmax"));
245 thresholdIsMin.addActionListener(new ActionListener()
248 public void actionPerformed(ActionEvent actionEvent)
250 thresholdIsMin_actionPerformed();
253 seqAssociated.setBackground(Color.white);
254 seqAssociated.setFont(JvSwingUtils.getLabelFont());
256 .setText(MessageManager.getString("label.per_sequence_only"));
257 seqAssociated.addActionListener(new ActionListener()
261 public void actionPerformed(ActionEvent arg0)
263 seqAssociated_actionPerformed(annotations);
267 this.setLayout(new BorderLayout());
268 JPanel jPanel1 = new JPanel();
269 JPanel jPanel2 = new JPanel();
270 jPanel2.setLayout(new MigLayout("", "[left][center][right]", "[][][]"));
271 jPanel1.setBackground(Color.white);
272 jPanel2.setBackground(Color.white);
276 jPanel2.add(annotations, "grow, wrap");
277 jPanel2.add(seqAssociated);
278 jPanel2.add(useOriginalColours);
279 JPanel colpanel = new JPanel(new FlowLayout());
280 colpanel.setBackground(Color.white);
281 colpanel.add(minColour);
282 colpanel.add(maxColour);
283 jPanel2.add(colpanel, "wrap");
284 jPanel2.add(getThreshold());
285 jPanel2.add(defColours, "skip 1, wrap");
286 jPanel2.add(thresholdIsMin);
287 jPanel2.add(slider, "grow");
288 jPanel2.add(thresholdValue, "grow");
289 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
290 this.add(jPanel2, java.awt.BorderLayout.CENTER);
294 protected void resetColours_actionPerformed()
300 private void setDefaultMinMax()
302 minColour.setBackground(
303 Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN", Color.orange));
304 maxColour.setBackground(
305 Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX", Color.red));
308 protected void showColourChooser(JPanel colourPanel, String titleKey)
310 String ttl = MessageManager.getString(titleKey);
311 ColourChooserListener listener = new ColourChooserListener()
314 public void colourSelected(Color c)
316 colourPanel.setBackground(c);
317 colourPanel.repaint();
321 JalviewColourChooser.showColourChooser(Desktop.getDesktop(), ttl,
322 colourPanel.getBackground(), listener);
328 this.ap.alignFrame.changeColour(oldcs);
329 if (av.getAlignment().getGroups() != null)
332 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
334 sg.setColourScheme(oldgroupColours.get(sg));
340 public void valueChanged(boolean updateAllAnnotation)
342 if (slider.isEnabled())
344 if (useOriginalColours.isSelected() && !(av
345 .getGlobalColourScheme() instanceof AnnotationColourGradient))
349 getCurrentAnnotation().threshold.value = getSliderValue();
350 propagateSeqAssociatedThreshold(updateAllAnnotation,
351 getCurrentAnnotation());
352 ap.paintAlignment(false, false);
356 public void originalColours_actionPerformed()
358 boolean selected = useOriginalColours.isSelected();
363 maxColour.setEnabled(!selected);
364 minColour.setEnabled(!selected);
365 thresholdIsMin.setEnabled(!selected);
370 public void updateView()
372 // Check if combobox is still adjusting
378 int selIndex = annotations
380 int annIndex = annmap[selIndex];
381 setCurrentAnnotation(
382 av.getAlignment().getAlignmentAnnotation()[annIndex]);
384 int selectedThresholdItem = getSelectedThresholdItem(
385 getThreshold().getSelectedIndex());
387 slider.setEnabled(true);
388 thresholdValue.setEnabled(true);
389 thresholdIsMin.setEnabled(!useOriginalColours.isSelected());
391 final AlignmentAnnotation currentAnnotation = getCurrentAnnotation();
392 if (selectedThresholdItem == AnnotationColourGradient.NO_THRESHOLD)
394 slider.setEnabled(false);
395 thresholdValue.setEnabled(false);
396 thresholdValue.setText("");
397 thresholdIsMin.setEnabled(false);
399 else if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD
400 && currentAnnotation.threshold == null)
402 currentAnnotation.setThreshold(new GraphLine(
403 (currentAnnotation.graphMax - currentAnnotation.graphMin)
405 "Threshold", Color.black));
408 if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD)
411 setSliderModel(currentAnnotation.graphMin, currentAnnotation.graphMax,
412 currentAnnotation.threshold.value);
413 slider.setEnabled(true);
415 setThresholdValueText();
416 thresholdValue.setEnabled(true);
419 colorAlignmentContaining(currentAnnotation, selectedThresholdItem);
421 ap.alignmentChanged();
424 protected void colorAlignmentContaining(AlignmentAnnotation currentAnn,
425 int selectedThresholdOption)
428 AnnotationColourGradient acg = null;
429 if (useOriginalColours.isSelected())
431 acg = new AnnotationColourGradient(currentAnn,
432 av.getGlobalColourScheme(), selectedThresholdOption);
436 acg = new AnnotationColourGradient(currentAnn,
437 minColour.getBackground(), maxColour.getBackground(),
438 selectedThresholdOption);
440 acg.setSeqAssociated(seqAssociated.isSelected());
442 if (currentAnn.graphMin == 0f && currentAnn.graphMax == 0f)
444 acg.setPredefinedColours(true);
447 acg.setThresholdIsMinMax(thresholdIsMin.isSelected());
449 this.ap.alignFrame.changeColour(acg);
451 if (av.getAlignment().getGroups() != null)
454 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
460 sg.setColourScheme(acg.getInstance(av, sg));
466 protected void sliderDragReleased()
468 super.sliderDragReleased();
469 ap.paintAlignment(true, true);
473 * construct and display a colourchooser for a given annotation row
477 * @param alignmentAnnotation
478 * @param perseq - when true, enable per-sequence if alignment annotation is per sequence
480 public static void displayFor(AlignViewport av, AlignmentPanel ap,
481 AlignmentAnnotation alignmentAnnotation, boolean perSeq)
483 ColourSchemeI global = av.getGlobalColourScheme();
484 AnnotationColourGradient newCS = new AnnotationColourGradient(alignmentAnnotation, global, alignmentAnnotation.threshold!=null ? AnnotationColourGradient.ABOVE_THRESHOLD:AnnotationColourGradient.NO_THRESHOLD);
485 if (alignmentAnnotation.sequenceRef!=null)
487 newCS.setSeqAssociated(perSeq);
489 for (int i=0;i<alignmentAnnotation.annotations.length;i++)
491 Annotation ann = alignmentAnnotation.annotations[i];
492 if (ann!=null && ann.colour!=null && !ann.colour.equals(Color.white))
494 newCS.setPredefinedColours(true);
498 AnnotationColourChooser achooser = new AnnotationColourChooser(av,ap,newCS);