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.gui.JalviewColourChooser.ColourChooserListener;
28 import jalview.schemes.AnnotationColourGradient;
29 import jalview.schemes.ColourSchemeI;
30 import jalview.util.MessageManager;
32 import java.awt.BorderLayout;
33 import java.awt.Color;
34 import java.awt.Dimension;
35 import java.awt.FlowLayout;
36 import java.awt.event.ActionEvent;
37 import java.awt.event.ActionListener;
38 import java.awt.event.MouseAdapter;
39 import java.awt.event.MouseEvent;
40 import java.util.Hashtable;
41 import java.util.Vector;
43 import javax.swing.BorderFactory;
44 import javax.swing.JButton;
45 import javax.swing.JCheckBox;
46 import javax.swing.JComboBox;
47 import javax.swing.JInternalFrame;
48 import javax.swing.JLayeredPane;
49 import javax.swing.JPanel;
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();
69 private JCheckBox transparency = new JCheckBox();
71 protected static final int MIN_WIDTH = 500;
73 protected static final int MIN_HEIGHT = 240;
75 public AnnotationColourChooser(AlignViewport av, final AlignmentPanel ap)
78 oldcs = av.getGlobalColourScheme();
79 if (av.getAlignment().getGroups() != null)
81 oldgroupColours = new Hashtable<>();
82 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
84 if (sg.getColourScheme() != null)
86 oldgroupColours.put(sg, sg.getColourScheme());
90 frame = new JInternalFrame();
91 frame.setContentPane(this);
92 frame.setLayer(JLayeredPane.PALETTE_LAYER);
93 Desktop.addInternalFrame(frame,
94 MessageManager.getString("label.colour_by_annotation"), 520,
96 frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
97 addSliderChangeListener();
98 addSliderMouseListeners();
100 if (av.getAlignment().getAlignmentAnnotation() == null)
105 // Always get default shading from preferences.
109 if (oldcs instanceof AnnotationColourGradient)
111 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
112 useOriginalColours.setSelected(
113 acg.isPredefinedColours() || acg.getBaseColour() != null);
114 if (!acg.isPredefinedColours() && acg.getBaseColour() == null)
116 minColour.setBackground(acg.getMinColour());
117 maxColour.setBackground(acg.getMaxColour());
119 seqAssociated.setSelected(acg.isSeqAssociated());
122 Vector<String> annotItems = getAnnotationItems(
123 seqAssociated.isSelected());
124 annotations = new JComboBox<>(annotItems);
126 populateThresholdComboBox(threshold);
128 if (oldcs instanceof AnnotationColourGradient)
130 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
131 String label = getAnnotationMenuLabel(acg.getAnnotation());
132 annotations.setSelectedItem(label);
133 switch (acg.getAboveThreshold())
135 case AnnotationColourGradient.NO_THRESHOLD:
136 getThreshold().setSelectedIndex(0);
138 case AnnotationColourGradient.ABOVE_THRESHOLD:
139 getThreshold().setSelectedIndex(1);
141 case AnnotationColourGradient.BELOW_THRESHOLD:
142 getThreshold().setSelectedIndex(2);
145 throw new Error(MessageManager.getString(
146 "error.implementation_error_dont_know_about_threshold_setting"));
148 thresholdIsMin.setSelected(acg.isThresholdIsMinMax());
150 .setText(String.valueOf(acg.getAnnotationThreshold()));
162 protected void jbInit()
166 minColour.setFont(JvSwingUtils.getLabelFont());
167 minColour.setBorder(BorderFactory.createEtchedBorder());
168 minColour.setPreferredSize(new Dimension(40, 20));
169 minColour.setToolTipText(MessageManager.getString("label.min_colour"));
170 minColour.addMouseListener(new MouseAdapter()
173 public void mousePressed(MouseEvent e)
175 if (minColour.isEnabled())
177 showColourChooser(minColour, "label.select_colour_minimum_value");
181 maxColour.setFont(JvSwingUtils.getLabelFont());
182 maxColour.setBorder(BorderFactory.createEtchedBorder());
183 maxColour.setPreferredSize(new Dimension(40, 20));
184 maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
185 maxColour.addMouseListener(new MouseAdapter()
188 public void mousePressed(MouseEvent e)
190 if (maxColour.isEnabled())
192 showColourChooser(maxColour, "label.select_colour_maximum_value");
197 defColours = new JButton();
198 defColours.setOpaque(false);
199 defColours.setText(MessageManager.getString("action.set_defaults"));
200 defColours.setToolTipText(MessageManager
201 .getString("label.reset_min_max_colours_to_defaults"));
202 defColours.addActionListener(new ActionListener()
206 public void actionPerformed(ActionEvent arg0)
208 resetColours_actionPerformed();
212 useOriginalColours.setFont(JvSwingUtils.getLabelFont());
213 useOriginalColours.setOpaque(false);
214 useOriginalColours.setText(
215 MessageManager.getString("label.use_original_colours"));
216 useOriginalColours.addActionListener(new ActionListener()
219 public void actionPerformed(ActionEvent e)
221 originalColours_actionPerformed();
224 thresholdIsMin.setBackground(Color.white);
225 thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
227 .setText(MessageManager.getString("label.threshold_minmax"));
228 thresholdIsMin.addActionListener(new ActionListener()
231 public void actionPerformed(ActionEvent actionEvent)
233 thresholdIsMin_actionPerformed();
236 seqAssociated.setBackground(Color.white);
237 seqAssociated.setFont(JvSwingUtils.getLabelFont());
239 .setText(MessageManager.getString("label.per_sequence_only"));
240 seqAssociated.addActionListener(new ActionListener()
244 public void actionPerformed(ActionEvent arg0)
246 seqAssociated_actionPerformed(annotations);
250 transparency.setBackground(Color.white);
251 transparency.setFont(JvSwingUtils.getLabelFont());
253 .setText(MessageManager.getString("Use Transparency"));
254 transparency.addActionListener(new ActionListener()
258 public void actionPerformed(ActionEvent arg0)
260 transparency_actionPerformed();
263 this.setLayout(new BorderLayout());
264 JPanel jPanel1 = new JPanel();
265 JPanel jPanel2 = new JPanel();
266 jPanel2.setLayout(new MigLayout("", "[left][center][right]", "[][][]"));
267 jPanel1.setBackground(Color.white);
268 jPanel2.setBackground(Color.white);
272 jPanel2.add(annotations, "grow, wrap");
273 jPanel2.add(seqAssociated);
274 jPanel2.add(useOriginalColours);
275 jPanel2.add(transparency);
276 JPanel colpanel = new JPanel(new FlowLayout());
277 colpanel.setBackground(Color.white);
278 colpanel.add(minColour);
279 colpanel.add(maxColour);
280 jPanel2.add(colpanel, "wrap");
281 jPanel2.add(getThreshold());
282 jPanel2.add(defColours, "skip 1, wrap");
283 jPanel2.add(thresholdIsMin);
284 jPanel2.add(slider, "grow");
285 jPanel2.add(thresholdValue, "grow");
286 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
287 this.add(jPanel2, java.awt.BorderLayout.CENTER);
291 protected void transparency_actionPerformed()
295 protected void resetColours_actionPerformed()
301 private void setDefaultMinMax()
303 minColour.setBackground(
304 Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN", Color.orange));
305 maxColour.setBackground(
306 Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX", Color.red));
309 protected void showColourChooser(JPanel colourPanel, String titleKey)
311 String ttl = MessageManager.getString(titleKey);
312 ColourChooserListener listener = new ColourChooserListener()
315 public void colourSelected(Color c)
317 colourPanel.setBackground(c);
318 colourPanel.repaint();
322 JalviewColourChooser.showColourChooser(Desktop.getDesktopPane(), ttl,
323 colourPanel.getBackground(), listener);
329 this.ap.alignFrame.changeColour(oldcs);
330 if (av.getAlignment().getGroups() != null)
333 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
335 sg.setColourScheme(oldgroupColours.get(sg));
341 public void valueChanged(boolean updateAllAnnotation)
343 if (slider.isEnabled())
345 if (useOriginalColours.isSelected() && !(av
346 .getGlobalColourScheme() instanceof AnnotationColourGradient))
350 getCurrentAnnotation().threshold.value = getSliderValue();
351 propagateSeqAssociatedThreshold(updateAllAnnotation,
352 getCurrentAnnotation());
353 ap.paintAlignment(false, false);
357 public void originalColours_actionPerformed()
359 boolean selected = useOriginalColours.isSelected();
364 maxColour.setEnabled(!selected);
365 minColour.setEnabled(!selected);
366 thresholdIsMin.setEnabled(!selected);
371 public void updateView()
373 // Check if combobox is still adjusting
379 setCurrentAnnotation(
380 av.getAlignment().getAlignmentAnnotation()[annmap[annotations
381 .getSelectedIndex()]]);
383 int selectedThresholdItem = getSelectedThresholdItem(
384 getThreshold().getSelectedIndex());
386 slider.setEnabled(true);
387 thresholdValue.setEnabled(true);
388 thresholdIsMin.setEnabled(!useOriginalColours.isSelected());
390 final AlignmentAnnotation currentAnnotation = getCurrentAnnotation();
391 if (selectedThresholdItem == AnnotationColourGradient.NO_THRESHOLD)
393 slider.setEnabled(false);
394 thresholdValue.setEnabled(false);
395 thresholdValue.setText("");
396 thresholdIsMin.setEnabled(false);
398 else if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD
399 && currentAnnotation.threshold == null)
401 currentAnnotation.setThreshold(new GraphLine(
402 (currentAnnotation.graphMax - currentAnnotation.graphMin)
404 "Threshold", Color.black));
407 if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD)
410 setSliderModel(currentAnnotation.graphMin, currentAnnotation.graphMax,
411 currentAnnotation.threshold.value);
412 slider.setEnabled(true);
414 setThresholdValueText();
415 thresholdValue.setEnabled(true);
418 colorAlignmentContaining(currentAnnotation, selectedThresholdItem);
420 ap.alignmentChanged();
423 protected void colorAlignmentContaining(AlignmentAnnotation currentAnn,
424 int selectedThresholdOption)
427 AnnotationColourGradient acg = null;
428 if (useOriginalColours.isSelected())
430 acg = new AnnotationColourGradient(currentAnn,
431 av.getGlobalColourScheme(), selectedThresholdOption);
435 acg = new AnnotationColourGradient(currentAnn,
436 minColour.getBackground(), maxColour.getBackground(),
437 selectedThresholdOption);
439 acg.setSeqAssociated(seqAssociated.isSelected());
441 if (currentAnn.graphMin == 0f && currentAnn.graphMax == 0f)
443 acg.setPredefinedColours(true);
446 acg.setThresholdIsMinMax(thresholdIsMin.isSelected());
448 acg.setPositionToTransparency(transparency.isSelected());
450 this.ap.alignFrame.changeColour(acg);
452 if (av.getAlignment().getGroups() != null)
455 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
462 acg.getInstance(av, sg));
468 protected void sliderDragReleased()
470 super.sliderDragReleased();
471 ap.paintAlignment(true, true);