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();
70 protected static final int MIN_WIDTH = 500;
72 protected static final int MIN_HEIGHT = 240;
74 public AnnotationColourChooser(AlignViewport av, final AlignmentPanel ap)
77 oldcs = av.getGlobalColourScheme();
78 if (av.getAlignment().getGroups() != null)
80 oldgroupColours = new Hashtable<>();
81 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
83 if (sg.getColourScheme() != null)
85 oldgroupColours.put(sg, sg.getColourScheme());
89 frame = new JInternalFrame();
90 frame.setContentPane(this);
91 frame.setLayer(JLayeredPane.PALETTE_LAYER);
92 Desktop.addInternalFrame(frame,
93 MessageManager.getString("label.colour_by_annotation"), 520,
95 frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
96 addSliderChangeListener();
97 addSliderMouseListeners();
99 if (av.getAlignment().getAlignmentAnnotation() == null)
104 // Always get default shading from preferences.
108 if (oldcs instanceof AnnotationColourGradient)
110 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
111 useOriginalColours.setSelected(
112 acg.isPredefinedColours() || acg.getBaseColour() != null);
113 if (!acg.isPredefinedColours() && acg.getBaseColour() == null)
115 minColour.setBackground(acg.getMinColour());
116 maxColour.setBackground(acg.getMaxColour());
118 seqAssociated.setSelected(acg.isSeqAssociated());
121 Vector<String> annotItems = getAnnotationItems(
122 seqAssociated.isSelected());
123 annotations = new JComboBox<>(annotItems);
125 populateThresholdComboBox(threshold);
127 if (oldcs instanceof AnnotationColourGradient)
129 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
130 String label = getAnnotationMenuLabel(acg.getAnnotation());
131 annotations.setSelectedItem(label);
132 switch (acg.getAboveThreshold())
134 case AnnotationColourGradient.NO_THRESHOLD:
135 getThreshold().setSelectedIndex(0);
137 case AnnotationColourGradient.ABOVE_THRESHOLD:
138 getThreshold().setSelectedIndex(1);
140 case AnnotationColourGradient.BELOW_THRESHOLD:
141 getThreshold().setSelectedIndex(2);
144 throw new Error(MessageManager.getString(
145 "error.implementation_error_dont_know_about_threshold_setting"));
147 thresholdIsMin.setSelected(acg.isThresholdIsMinMax());
149 .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 this.setLayout(new BorderLayout());
250 JPanel jPanel1 = new JPanel();
251 JPanel jPanel2 = new JPanel();
252 jPanel2.setLayout(new MigLayout("", "[left][center][right]", "[][][]"));
253 jPanel1.setBackground(Color.white);
254 jPanel2.setBackground(Color.white);
258 jPanel2.add(annotations, "grow, wrap");
259 jPanel2.add(seqAssociated);
260 jPanel2.add(useOriginalColours);
261 JPanel colpanel = new JPanel(new FlowLayout());
262 colpanel.setBackground(Color.white);
263 colpanel.add(minColour);
264 colpanel.add(maxColour);
265 jPanel2.add(colpanel, "wrap");
266 jPanel2.add(getThreshold());
267 jPanel2.add(defColours, "skip 1, wrap");
268 jPanel2.add(thresholdIsMin);
269 jPanel2.add(slider, "grow");
270 jPanel2.add(thresholdValue, "grow");
271 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
272 this.add(jPanel2, java.awt.BorderLayout.CENTER);
276 protected void resetColours_actionPerformed()
282 private void setDefaultMinMax()
284 minColour.setBackground(
285 Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN", Color.orange));
286 maxColour.setBackground(
287 Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX", Color.red));
290 protected void showColourChooser(JPanel colourPanel, String titleKey)
292 String ttl = MessageManager.getString(titleKey);
293 ColourChooserListener listener = new ColourChooserListener()
296 public void colourSelected(Color c)
298 colourPanel.setBackground(c);
299 colourPanel.repaint();
303 JalviewColourChooser.showColourChooser(Desktop.getDesktop(), ttl,
304 colourPanel.getBackground(), listener);
310 this.ap.alignFrame.changeColour(oldcs);
311 if (av.getAlignment().getGroups() != null)
314 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
316 sg.setColourScheme(oldgroupColours.get(sg));
322 public void valueChanged(boolean updateAllAnnotation)
324 if (slider.isEnabled())
326 if (useOriginalColours.isSelected() && !(av
327 .getGlobalColourScheme() instanceof AnnotationColourGradient))
331 getCurrentAnnotation().threshold.value = getSliderValue();
332 propagateSeqAssociatedThreshold(updateAllAnnotation,
333 getCurrentAnnotation());
334 ap.paintAlignment(false, false);
338 public void originalColours_actionPerformed()
340 boolean selected = useOriginalColours.isSelected();
345 maxColour.setEnabled(!selected);
346 minColour.setEnabled(!selected);
347 thresholdIsMin.setEnabled(!selected);
352 public void updateView()
354 // Check if combobox is still adjusting
360 setCurrentAnnotation(
361 av.getAlignment().getAlignmentAnnotation()[annmap[annotations
362 .getSelectedIndex()]]);
364 int selectedThresholdItem = getSelectedThresholdItem(
365 getThreshold().getSelectedIndex());
367 slider.setEnabled(true);
368 thresholdValue.setEnabled(true);
369 thresholdIsMin.setEnabled(!useOriginalColours.isSelected());
371 final AlignmentAnnotation currentAnnotation = getCurrentAnnotation();
372 if (selectedThresholdItem == AnnotationColourGradient.NO_THRESHOLD)
374 slider.setEnabled(false);
375 thresholdValue.setEnabled(false);
376 thresholdValue.setText("");
377 thresholdIsMin.setEnabled(false);
379 else if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD
380 && currentAnnotation.threshold == null)
382 currentAnnotation.setThreshold(new GraphLine(
383 (currentAnnotation.graphMax - currentAnnotation.graphMin)
385 "Threshold", Color.black));
388 if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD)
391 setSliderModel(currentAnnotation.graphMin, currentAnnotation.graphMax,
392 currentAnnotation.threshold.value);
393 slider.setEnabled(true);
395 setThresholdValueText();
396 thresholdValue.setEnabled(true);
399 colorAlignmentContaining(currentAnnotation, selectedThresholdItem);
401 ap.alignmentChanged();
404 protected void colorAlignmentContaining(AlignmentAnnotation currentAnn,
405 int selectedThresholdOption)
408 AnnotationColourGradient acg = null;
409 if (useOriginalColours.isSelected())
411 acg = new AnnotationColourGradient(currentAnn,
412 av.getGlobalColourScheme(), selectedThresholdOption);
416 acg = new AnnotationColourGradient(currentAnn,
417 minColour.getBackground(), maxColour.getBackground(),
418 selectedThresholdOption);
420 acg.setSeqAssociated(seqAssociated.isSelected());
422 if (currentAnn.graphMin == 0f && currentAnn.graphMax == 0f)
424 acg.setPredefinedColours(true);
427 acg.setThresholdIsMinMax(thresholdIsMin.isSelected());
429 this.ap.alignFrame.changeColour(acg);
431 if (av.getAlignment().getGroups() != null)
434 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
441 acg.getInstance(av, sg));
447 protected void sliderDragReleased()
449 super.sliderDragReleased();
450 ap.paintAlignment(true, true);