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.GraphLine;
45 import jalview.datamodel.SequenceGroup;
46 import jalview.gui.JalviewColourChooser.ColourChooserListener;
47 import jalview.schemes.AnnotationColourGradient;
48 import jalview.schemes.ColourSchemeI;
49 import jalview.util.MessageManager;
50 import net.miginfocom.swing.MigLayout;
52 @SuppressWarnings("serial")
53 public class AnnotationColourChooser extends AnnotationRowFilter
55 private ColourSchemeI oldcs;
57 private JButton defColours;
59 private Hashtable<SequenceGroup, ColourSchemeI> oldgroupColours;
61 private JCheckBox useOriginalColours = new JCheckBox();
63 JPanel minColour = new JPanel();
65 JPanel maxColour = new JPanel();
67 private JCheckBox thresholdIsMin = new JCheckBox();
69 protected static final int MIN_WIDTH = 500;
71 protected static final int MIN_HEIGHT = 240;
73 public AnnotationColourChooser(AlignViewport av, final AlignmentPanel ap)
76 oldcs = av.getGlobalColourScheme();
77 if (av.getAlignment().getGroups() != null)
79 oldgroupColours = new Hashtable<>();
80 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
82 if (sg.getColourScheme() != null)
84 oldgroupColours.put(sg, sg.getColourScheme());
88 frame = new JInternalFrame();
89 frame.setContentPane(this);
90 frame.setLayer(JLayeredPane.PALETTE_LAYER);
91 Desktop.addInternalFrame(frame,
92 MessageManager.getString("label.colour_by_annotation"), 520,
94 frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
95 addSliderChangeListener();
96 addSliderMouseListeners();
98 if (av.getAlignment().getAlignmentAnnotation() == null)
103 // Always get default shading from preferences.
107 if (oldcs instanceof AnnotationColourGradient)
109 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
110 useOriginalColours.setSelected(
111 acg.isPredefinedColours() || acg.getBaseColour() != null);
112 if (!acg.isPredefinedColours() && acg.getBaseColour() == null)
114 minColour.setBackground(acg.getMinColour());
115 maxColour.setBackground(acg.getMaxColour());
117 seqAssociated.setSelected(acg.isSeqAssociated());
120 Vector<String> annotItems = getAnnotationItems(
121 seqAssociated.isSelected());
122 annotations = new JComboBox<>(annotItems);
124 populateThresholdComboBox(threshold);
126 if (oldcs instanceof AnnotationColourGradient)
128 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
129 String label = getAnnotationMenuLabel(acg.getAnnotation());
130 annotations.setSelectedItem(label);
131 switch (acg.getAboveThreshold())
133 case AnnotationColourGradient.NO_THRESHOLD:
134 getThreshold().setSelectedIndex(0);
136 case AnnotationColourGradient.ABOVE_THRESHOLD:
137 getThreshold().setSelectedIndex(1);
139 case AnnotationColourGradient.BELOW_THRESHOLD:
140 getThreshold().setSelectedIndex(2);
143 throw new Error(MessageManager.getString(
144 "error.implementation_error_dont_know_about_threshold_setting"));
146 thresholdIsMin.setSelected(acg.isThresholdIsMinMax());
147 thresholdValue.setText(String.valueOf(acg.getAnnotationThreshold()));
159 protected void jbInit()
163 minColour.setFont(JvSwingUtils.getLabelFont());
164 minColour.setBorder(BorderFactory.createEtchedBorder());
165 minColour.setPreferredSize(new Dimension(40, 20));
166 minColour.setToolTipText(MessageManager.getString("label.min_colour"));
167 minColour.addMouseListener(new MouseAdapter()
170 public void mousePressed(MouseEvent e)
172 if (minColour.isEnabled())
174 showColourChooser(minColour, "label.select_colour_minimum_value");
178 maxColour.setFont(JvSwingUtils.getLabelFont());
179 maxColour.setBorder(BorderFactory.createEtchedBorder());
180 maxColour.setPreferredSize(new Dimension(40, 20));
181 maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
182 maxColour.addMouseListener(new MouseAdapter()
185 public void mousePressed(MouseEvent e)
187 if (maxColour.isEnabled())
189 showColourChooser(maxColour, "label.select_colour_maximum_value");
194 defColours = new JButton();
195 defColours.setOpaque(false);
196 defColours.setText(MessageManager.getString("action.set_defaults"));
197 defColours.setToolTipText(MessageManager
198 .getString("label.reset_min_max_colours_to_defaults"));
199 defColours.addActionListener(new ActionListener()
203 public void actionPerformed(ActionEvent arg0)
205 resetColours_actionPerformed();
209 useOriginalColours.setFont(JvSwingUtils.getLabelFont());
210 useOriginalColours.setOpaque(false);
211 useOriginalColours.setText(
212 MessageManager.getString("label.use_original_colours"));
213 useOriginalColours.addActionListener(new ActionListener()
216 public void actionPerformed(ActionEvent e)
218 originalColours_actionPerformed();
221 thresholdIsMin.setBackground(Color.white);
222 thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
224 .setText(MessageManager.getString("label.threshold_minmax"));
225 thresholdIsMin.addActionListener(new ActionListener()
228 public void actionPerformed(ActionEvent actionEvent)
230 thresholdIsMin_actionPerformed();
233 seqAssociated.setBackground(Color.white);
234 seqAssociated.setFont(JvSwingUtils.getLabelFont());
236 .setText(MessageManager.getString("label.per_sequence_only"));
237 seqAssociated.addActionListener(new ActionListener()
241 public void actionPerformed(ActionEvent arg0)
243 seqAssociated_actionPerformed(annotations);
247 this.setLayout(new BorderLayout());
248 JPanel jPanel1 = new JPanel();
249 JPanel jPanel2 = new JPanel();
250 jPanel2.setLayout(new MigLayout("", "[left][center][right]", "[][][]"));
251 jPanel1.setBackground(Color.white);
252 jPanel2.setBackground(Color.white);
256 jPanel2.add(annotations, "grow, wrap");
257 jPanel2.add(seqAssociated);
258 jPanel2.add(useOriginalColours);
259 JPanel colpanel = new JPanel(new FlowLayout());
260 colpanel.setBackground(Color.white);
261 colpanel.add(minColour);
262 colpanel.add(maxColour);
263 jPanel2.add(colpanel, "wrap");
264 jPanel2.add(getThreshold());
265 jPanel2.add(defColours, "skip 1, wrap");
266 jPanel2.add(thresholdIsMin);
267 jPanel2.add(slider, "grow");
268 jPanel2.add(thresholdValue, "grow");
269 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
270 this.add(jPanel2, java.awt.BorderLayout.CENTER);
274 protected void resetColours_actionPerformed()
280 private void setDefaultMinMax()
282 minColour.setBackground(
283 Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN", Color.orange));
284 maxColour.setBackground(
285 Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX", Color.red));
288 protected void showColourChooser(JPanel colourPanel, String titleKey)
290 String ttl = MessageManager.getString(titleKey);
291 ColourChooserListener listener = new ColourChooserListener()
294 public void colourSelected(Color c)
296 colourPanel.setBackground(c);
297 colourPanel.repaint();
301 JalviewColourChooser.showColourChooser(Desktop.getDesktop(), ttl,
302 colourPanel.getBackground(), listener);
308 this.ap.alignFrame.changeColour(oldcs);
309 if (av.getAlignment().getGroups() != null)
312 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
314 sg.setColourScheme(oldgroupColours.get(sg));
320 public void valueChanged(boolean updateAllAnnotation)
322 if (slider.isEnabled())
324 if (useOriginalColours.isSelected() && !(av
325 .getGlobalColourScheme() instanceof AnnotationColourGradient))
329 getCurrentAnnotation().threshold.value = getSliderValue();
330 propagateSeqAssociatedThreshold(updateAllAnnotation,
331 getCurrentAnnotation());
332 ap.paintAlignment(false, false);
336 public void originalColours_actionPerformed()
338 boolean selected = useOriginalColours.isSelected();
343 maxColour.setEnabled(!selected);
344 minColour.setEnabled(!selected);
345 thresholdIsMin.setEnabled(!selected);
350 public void updateView()
352 // Check if combobox is still adjusting
358 setCurrentAnnotation(
359 av.getAlignment().getAlignmentAnnotation()[annmap[annotations
360 .getSelectedIndex()]]);
362 int selectedThresholdItem = getSelectedThresholdItem(
363 getThreshold().getSelectedIndex());
365 slider.setEnabled(true);
366 thresholdValue.setEnabled(true);
367 thresholdIsMin.setEnabled(!useOriginalColours.isSelected());
369 final AlignmentAnnotation currentAnnotation = getCurrentAnnotation();
370 if (selectedThresholdItem == AnnotationColourGradient.NO_THRESHOLD)
372 slider.setEnabled(false);
373 thresholdValue.setEnabled(false);
374 thresholdValue.setText("");
375 thresholdIsMin.setEnabled(false);
377 else if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD
378 && currentAnnotation.threshold == null)
380 currentAnnotation.setThreshold(new GraphLine(
381 (currentAnnotation.graphMax - currentAnnotation.graphMin)
383 "Threshold", Color.black));
386 if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD)
389 setSliderModel(currentAnnotation.graphMin, currentAnnotation.graphMax,
390 currentAnnotation.threshold.value);
391 slider.setEnabled(true);
393 setThresholdValueText();
394 thresholdValue.setEnabled(true);
397 colorAlignmentContaining(currentAnnotation, selectedThresholdItem);
399 ap.alignmentChanged();
402 protected void colorAlignmentContaining(AlignmentAnnotation currentAnn,
403 int selectedThresholdOption)
406 AnnotationColourGradient acg = null;
407 if (useOriginalColours.isSelected())
409 acg = new AnnotationColourGradient(currentAnn,
410 av.getGlobalColourScheme(), selectedThresholdOption);
414 acg = new AnnotationColourGradient(currentAnn,
415 minColour.getBackground(), maxColour.getBackground(),
416 selectedThresholdOption);
418 acg.setSeqAssociated(seqAssociated.isSelected());
420 if (currentAnn.graphMin == 0f && currentAnn.graphMax == 0f)
422 acg.setPredefinedColours(true);
425 acg.setThresholdIsMinMax(thresholdIsMin.isSelected());
427 this.ap.alignFrame.changeColour(acg);
429 if (av.getAlignment().getGroups() != null)
432 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
438 sg.setColourScheme(acg.getInstance(av, sg));
444 protected void sliderDragReleased()
446 super.sliderDragReleased();
447 ap.paintAlignment(true, true);