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());
149 thresholdValue.setText(String.valueOf(acg.getAnnotationThreshold()));
161 protected void jbInit()
165 minColour.setFont(JvSwingUtils.getLabelFont());
166 minColour.setBorder(BorderFactory.createEtchedBorder());
167 minColour.setPreferredSize(new Dimension(40, 20));
168 minColour.setToolTipText(MessageManager.getString("label.min_colour"));
169 minColour.addMouseListener(new MouseAdapter()
172 public void mousePressed(MouseEvent e)
174 if (minColour.isEnabled())
176 showColourChooser(minColour, "label.select_colour_minimum_value");
180 maxColour.setFont(JvSwingUtils.getLabelFont());
181 maxColour.setBorder(BorderFactory.createEtchedBorder());
182 maxColour.setPreferredSize(new Dimension(40, 20));
183 maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
184 maxColour.addMouseListener(new MouseAdapter()
187 public void mousePressed(MouseEvent e)
189 if (maxColour.isEnabled())
191 showColourChooser(maxColour, "label.select_colour_maximum_value");
196 defColours = new JButton();
197 defColours.setOpaque(false);
198 defColours.setText(MessageManager.getString("action.set_defaults"));
199 defColours.setToolTipText(MessageManager
200 .getString("label.reset_min_max_colours_to_defaults"));
201 defColours.addActionListener(new ActionListener()
205 public void actionPerformed(ActionEvent arg0)
207 resetColours_actionPerformed();
211 useOriginalColours.setFont(JvSwingUtils.getLabelFont());
212 useOriginalColours.setOpaque(false);
213 useOriginalColours.setText(
214 MessageManager.getString("label.use_original_colours"));
215 useOriginalColours.addActionListener(new ActionListener()
218 public void actionPerformed(ActionEvent e)
220 originalColours_actionPerformed();
223 thresholdIsMin.setBackground(Color.white);
224 thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
226 .setText(MessageManager.getString("label.threshold_minmax"));
227 thresholdIsMin.addActionListener(new ActionListener()
230 public void actionPerformed(ActionEvent actionEvent)
232 thresholdIsMin_actionPerformed();
235 seqAssociated.setBackground(Color.white);
236 seqAssociated.setFont(JvSwingUtils.getLabelFont());
238 .setText(MessageManager.getString("label.per_sequence_only"));
239 seqAssociated.addActionListener(new ActionListener()
243 public void actionPerformed(ActionEvent arg0)
245 seqAssociated_actionPerformed(annotations);
249 transparency.setBackground(Color.white);
250 transparency.setFont(JvSwingUtils.getLabelFont());
252 .setText(MessageManager.getString("Use Transparency"));
253 transparency.addActionListener(new ActionListener()
257 public void actionPerformed(ActionEvent arg0)
259 transparency_actionPerformed();
262 this.setLayout(new BorderLayout());
263 JPanel jPanel1 = new JPanel();
264 JPanel jPanel2 = new JPanel();
265 jPanel2.setLayout(new MigLayout("", "[left][center][right]", "[][][]"));
266 jPanel1.setBackground(Color.white);
267 jPanel2.setBackground(Color.white);
271 jPanel2.add(annotations, "grow, wrap");
272 jPanel2.add(seqAssociated);
273 jPanel2.add(useOriginalColours);
274 jPanel2.add(transparency);
275 JPanel colpanel = new JPanel(new FlowLayout());
276 colpanel.setBackground(Color.white);
277 colpanel.add(minColour);
278 colpanel.add(maxColour);
279 jPanel2.add(colpanel, "wrap");
280 jPanel2.add(getThreshold());
281 jPanel2.add(defColours, "skip 1, wrap");
282 jPanel2.add(thresholdIsMin);
283 jPanel2.add(slider, "grow");
284 jPanel2.add(thresholdValue, "grow");
285 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
286 this.add(jPanel2, java.awt.BorderLayout.CENTER);
290 protected void transparency_actionPerformed()
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.getDesktopPane(), 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 setCurrentAnnotation(
379 av.getAlignment().getAlignmentAnnotation()[annmap[annotations
380 .getSelectedIndex()]]);
382 int selectedThresholdItem = getSelectedThresholdItem(
383 getThreshold().getSelectedIndex());
385 slider.setEnabled(true);
386 thresholdValue.setEnabled(true);
387 thresholdIsMin.setEnabled(!useOriginalColours.isSelected());
389 final AlignmentAnnotation currentAnnotation = getCurrentAnnotation();
390 if (selectedThresholdItem == AnnotationColourGradient.NO_THRESHOLD)
392 slider.setEnabled(false);
393 thresholdValue.setEnabled(false);
394 thresholdValue.setText("");
395 thresholdIsMin.setEnabled(false);
397 else if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD
398 && currentAnnotation.threshold == null)
400 currentAnnotation.setThreshold(new GraphLine(
401 (currentAnnotation.graphMax - currentAnnotation.graphMin)
403 "Threshold", Color.black));
406 if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD)
409 setSliderModel(currentAnnotation.graphMin, currentAnnotation.graphMax,
410 currentAnnotation.threshold.value);
411 slider.setEnabled(true);
413 setThresholdValueText();
414 thresholdValue.setEnabled(true);
417 colorAlignmentContaining(currentAnnotation, selectedThresholdItem);
419 ap.alignmentChanged();
422 protected void colorAlignmentContaining(AlignmentAnnotation currentAnn,
423 int selectedThresholdOption)
426 AnnotationColourGradient acg = null;
427 if (useOriginalColours.isSelected())
429 acg = new AnnotationColourGradient(currentAnn,
430 av.getGlobalColourScheme(), selectedThresholdOption);
434 acg = new AnnotationColourGradient(currentAnn,
435 minColour.getBackground(), maxColour.getBackground(),
436 selectedThresholdOption);
438 acg.setSeqAssociated(seqAssociated.isSelected());
440 if (currentAnn.graphMin == 0f && currentAnn.graphMax == 0f)
442 acg.setPredefinedColours(true);
445 acg.setThresholdIsMinMax(thresholdIsMin.isSelected());
447 acg.setPositionToTransparency(transparency.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);