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 static final int ONETHOUSAND = 1000;
58 private ColourSchemeI oldcs;
60 private JButton defColours;
62 private Hashtable<SequenceGroup, ColourSchemeI> oldgroupColours;
64 private JCheckBox useOriginalColours = new JCheckBox();
66 JPanel minColour = new JPanel();
68 JPanel maxColour = new JPanel();
70 private JCheckBox thresholdIsMin = new JCheckBox();
71 private JCheckBox transparency = new JCheckBox();
73 protected static final int MIN_WIDTH = 500;
75 protected static final int MIN_HEIGHT = 240;
77 public AnnotationColourChooser(AlignViewport av, final AlignmentPanel ap)
80 oldcs = av.getGlobalColourScheme();
81 if (av.getAlignment().getGroups() != null)
83 oldgroupColours = new Hashtable<>();
84 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
86 if (sg.getColourScheme() != null)
88 oldgroupColours.put(sg, sg.getColourScheme());
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 protected void jbInit()
167 minColour.setFont(JvSwingUtils.getLabelFont());
168 minColour.setBorder(BorderFactory.createEtchedBorder());
169 minColour.setPreferredSize(new Dimension(40, 20));
170 minColour.setToolTipText(MessageManager.getString("label.min_colour"));
171 minColour.addMouseListener(new MouseAdapter()
174 public void mousePressed(MouseEvent e)
176 if (minColour.isEnabled())
178 showColourChooser(minColour, "label.select_colour_minimum_value");
182 maxColour.setFont(JvSwingUtils.getLabelFont());
183 maxColour.setBorder(BorderFactory.createEtchedBorder());
184 maxColour.setPreferredSize(new Dimension(40, 20));
185 maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
186 maxColour.addMouseListener(new MouseAdapter()
189 public void mousePressed(MouseEvent e)
191 if (maxColour.isEnabled())
193 showColourChooser(maxColour, "label.select_colour_maximum_value");
198 defColours = new JButton();
199 defColours.setOpaque(false);
200 defColours.setText(MessageManager.getString("action.set_defaults"));
201 defColours.setToolTipText(MessageManager
202 .getString("label.reset_min_max_colours_to_defaults"));
203 defColours.addActionListener(new ActionListener()
207 public void actionPerformed(ActionEvent arg0)
209 resetColours_actionPerformed();
213 useOriginalColours.setFont(JvSwingUtils.getLabelFont());
214 useOriginalColours.setOpaque(false);
215 useOriginalColours.setText(
216 MessageManager.getString("label.use_original_colours"));
217 useOriginalColours.addActionListener(new ActionListener()
220 public void actionPerformed(ActionEvent e)
222 originalColours_actionPerformed();
225 thresholdIsMin.setBackground(Color.white);
226 thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
228 .setText(MessageManager.getString("label.threshold_minmax"));
229 thresholdIsMin.addActionListener(new ActionListener()
232 public void actionPerformed(ActionEvent actionEvent)
234 thresholdIsMin_actionPerformed();
237 seqAssociated.setBackground(Color.white);
238 seqAssociated.setFont(JvSwingUtils.getLabelFont());
240 .setText(MessageManager.getString("label.per_sequence_only"));
241 seqAssociated.addActionListener(new ActionListener()
245 public void actionPerformed(ActionEvent arg0)
247 seqAssociated_actionPerformed(annotations);
251 transparency.setBackground(Color.white);
252 transparency.setFont(JvSwingUtils.getLabelFont());
254 .setText(MessageManager.getString("Use Transparency"));
255 transparency.addActionListener(new ActionListener()
259 public void actionPerformed(ActionEvent arg0)
261 transparency_actionPerformed();
264 this.setLayout(new BorderLayout());
265 JPanel jPanel1 = new JPanel();
266 JPanel jPanel2 = new JPanel();
267 jPanel2.setLayout(new MigLayout("", "[left][center][right]", "[][][]"));
268 jPanel1.setBackground(Color.white);
269 jPanel2.setBackground(Color.white);
273 jPanel2.add(annotations, "grow, wrap");
274 jPanel2.add(seqAssociated);
275 jPanel2.add(useOriginalColours);
276 jPanel2.add(transparency);
277 JPanel colpanel = new JPanel(new FlowLayout());
278 colpanel.setBackground(Color.white);
279 colpanel.add(minColour);
280 colpanel.add(maxColour);
281 jPanel2.add(colpanel, "wrap");
282 jPanel2.add(getThreshold());
283 jPanel2.add(defColours, "skip 1, wrap");
284 jPanel2.add(thresholdIsMin);
285 jPanel2.add(slider, "grow");
286 jPanel2.add(thresholdValue, "grow");
287 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
288 this.add(jPanel2, java.awt.BorderLayout.CENTER);
292 protected void transparency_actionPerformed()
296 protected void resetColours_actionPerformed()
302 private void setDefaultMinMax()
304 minColour.setBackground(
305 Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN", Color.orange));
306 maxColour.setBackground(
307 Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX", Color.red));
310 protected void showColourChooser(JPanel colourPanel, String titleKey)
312 String ttl = MessageManager.getString(titleKey);
313 ColourChooserListener listener = new ColourChooserListener()
316 public void colourSelected(Color c)
318 colourPanel.setBackground(c);
319 colourPanel.repaint();
323 JalviewColourChooser.showColourChooser(Desktop.getDesktopPane(), ttl,
324 colourPanel.getBackground(), listener);
330 this.ap.alignFrame.changeColour(oldcs);
331 if (av.getAlignment().getGroups() != null)
334 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
336 sg.setColourScheme(oldgroupColours.get(sg));
342 public void valueChanged(boolean updateAllAnnotation)
344 if (slider.isEnabled())
346 if (useOriginalColours.isSelected() && !(av
347 .getGlobalColourScheme() instanceof AnnotationColourGradient))
351 getCurrentAnnotation().threshold.value = slider.getValue() / 1000f;
352 propagateSeqAssociatedThreshold(updateAllAnnotation,
353 getCurrentAnnotation());
354 ap.paintAlignment(false, false);
358 public void originalColours_actionPerformed()
360 boolean selected = useOriginalColours.isSelected();
365 maxColour.setEnabled(!selected);
366 minColour.setEnabled(!selected);
367 thresholdIsMin.setEnabled(!selected);
372 public void updateView()
374 // Check if combobox is still adjusting
380 setCurrentAnnotation(
381 av.getAlignment().getAlignmentAnnotation()[annmap[annotations
382 .getSelectedIndex()]]);
384 int selectedThresholdItem = getSelectedThresholdItem(
385 getThreshold().getSelectedIndex());
387 slider.setEnabled(true);
388 thresholdValue.setEnabled(true);
389 thresholdIsMin.setEnabled(!useOriginalColours.isSelected());
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 && getCurrentAnnotation().threshold == null)
401 getCurrentAnnotation().setThreshold(new GraphLine(
402 (getCurrentAnnotation().graphMax
403 - getCurrentAnnotation().graphMin) / 2f,
404 "Threshold", Color.black));
407 if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD)
410 float range = getCurrentAnnotation().graphMax * ONETHOUSAND
411 - getCurrentAnnotation().graphMin * ONETHOUSAND;
414 (int) (getCurrentAnnotation().graphMin * ONETHOUSAND));
416 (int) (getCurrentAnnotation().graphMax * ONETHOUSAND));
418 (int) (getCurrentAnnotation().threshold.value * ONETHOUSAND));
419 thresholdValue.setText(getCurrentAnnotation().threshold.value + "");
420 slider.setMajorTickSpacing((int) (range / 10f));
421 slider.setEnabled(true);
422 thresholdValue.setEnabled(true);
425 colorAlignmentContaining(getCurrentAnnotation(), selectedThresholdItem);
427 ap.alignmentChanged();
430 protected void colorAlignmentContaining(AlignmentAnnotation currentAnn,
431 int selectedThresholdOption)
434 AnnotationColourGradient acg = null;
435 if (useOriginalColours.isSelected())
437 acg = new AnnotationColourGradient(currentAnn,
438 av.getGlobalColourScheme(), selectedThresholdOption);
442 acg = new AnnotationColourGradient(currentAnn,
443 minColour.getBackground(), maxColour.getBackground(),
444 selectedThresholdOption);
446 acg.setSeqAssociated(seqAssociated.isSelected());
448 if (currentAnn.graphMin == 0f && currentAnn.graphMax == 0f)
450 acg.setPredefinedColours(true);
453 acg.setThresholdIsMinMax(thresholdIsMin.isSelected());
455 acg.setPositionToTransparency(transparency.isSelected());
457 this.ap.alignFrame.changeColour(acg);
459 if (av.getAlignment().getGroups() != null)
462 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
469 acg.getInstance(av, sg));
475 protected void sliderDragReleased()
477 super.sliderDragReleased();
478 ap.paintAlignment(true, true);