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.schemes.AnnotationColourGradient;
28 import jalview.schemes.ColourSchemeI;
29 import jalview.util.MessageManager;
31 import java.awt.BorderLayout;
32 import java.awt.Color;
33 import java.awt.Dimension;
34 import java.awt.FlowLayout;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.ActionListener;
37 import java.awt.event.MouseAdapter;
38 import java.awt.event.MouseEvent;
39 import java.util.Hashtable;
40 import java.util.Vector;
42 import javax.swing.BorderFactory;
43 import javax.swing.JButton;
44 import javax.swing.JCheckBox;
45 import javax.swing.JColorChooser;
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 currentColours = new JCheckBox();
66 private JPanel minColour = new JPanel();
68 private JPanel maxColour = new JPanel();
70 private JCheckBox thresholdIsMin = new JCheckBox();
72 public AnnotationColourChooser(AlignViewport av, final AlignmentPanel ap)
75 oldcs = av.getGlobalColourScheme();
76 if (av.getAlignment().getGroups() != null)
78 oldgroupColours = new Hashtable<SequenceGroup, ColourSchemeI>();
79 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
83 oldgroupColours.put(sg, sg.cs);
87 frame = new JInternalFrame();
88 frame.setContentPane(this);
89 frame.setLayer(JLayeredPane.PALETTE_LAYER);
90 Desktop.addInternalFrame(frame,
91 MessageManager.getString("label.colour_by_annotation"), 520,
94 addSliderChangeListener();
95 addSliderMouseListeners();
97 if (av.getAlignment().getAlignmentAnnotation() == null)
102 // Always get default shading from preferences.
106 if (oldcs instanceof AnnotationColourGradient)
108 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
109 currentColours.setSelected(acg.isPredefinedColours()
110 || acg.getBaseColour() != null);
111 if (!acg.isPredefinedColours() && acg.getBaseColour() == null)
113 minColour.setBackground(acg.getMinColour());
114 maxColour.setBackground(acg.getMaxColour());
116 seqAssociated.setSelected(acg.isSeqAssociated());
119 Vector<String> annotItems = getAnnotationItems(seqAssociated
121 annotations = new JComboBox<String>(annotItems);
123 populateThresholdComboBox(threshold);
125 if (oldcs instanceof AnnotationColourGradient)
127 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
128 String label = getAnnotationMenuLabel(acg.getAnnotation());
129 annotations.setSelectedItem(label);
130 switch (acg.getAboveThreshold())
132 case AnnotationColourGradient.NO_THRESHOLD:
133 getThreshold().setSelectedIndex(0);
135 case AnnotationColourGradient.ABOVE_THRESHOLD:
136 getThreshold().setSelectedIndex(1);
138 case AnnotationColourGradient.BELOW_THRESHOLD:
139 getThreshold().setSelectedIndex(2);
144 .getString("error.implementation_error_dont_know_about_threshold_setting"));
146 thresholdIsMin.setSelected(acg.isThresholdIsMinMax());
147 thresholdValue.setText("" + 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 minColour_actionPerformed();
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 maxColour_actionPerformed();
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 currentColours.setFont(JvSwingUtils.getLabelFont());
210 currentColours.setOpaque(false);
211 currentColours.setText(MessageManager
212 .getString("label.use_original_colours"));
213 currentColours.addActionListener(new ActionListener()
216 public void actionPerformed(ActionEvent e)
218 currentColours_actionPerformed();
221 thresholdIsMin.setBackground(Color.white);
222 thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
223 thresholdIsMin.setText(MessageManager
224 .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());
235 seqAssociated.setText(MessageManager
236 .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(currentColours);
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(Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN",
284 maxColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX",
288 public void minColour_actionPerformed()
290 Color col = JColorChooser.showDialog(this,
291 MessageManager.getString("label.select_colour_minimum_value"),
292 minColour.getBackground());
295 minColour.setBackground(col);
301 public void maxColour_actionPerformed()
303 Color col = JColorChooser.showDialog(this,
304 MessageManager.getString("label.select_colour_maximum_value"),
305 maxColour.getBackground());
308 maxColour.setBackground(col);
317 av.setGlobalColourScheme(oldcs);
318 if (av.getAlignment().getGroups() != null)
321 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
323 sg.cs = oldgroupColours.get(sg);
329 public void valueChanged(boolean updateAllAnnotation)
331 if (slider.isEnabled())
333 if (currentColours.isSelected()
334 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
338 getCurrentAnnotation().threshold.value = slider.getValue() / 1000f;
339 propagateSeqAssociatedThreshold(updateAllAnnotation,
340 getCurrentAnnotation());
341 ap.paintAlignment(false);
345 public void currentColours_actionPerformed()
347 if (currentColours.isSelected())
351 maxColour.setEnabled(!currentColours.isSelected());
352 minColour.setEnabled(!currentColours.isSelected());
357 public void updateView()
359 // Check if combobox is still adjusting
365 setCurrentAnnotation(av.getAlignment().getAlignmentAnnotation()[annmap[annotations
366 .getSelectedIndex()]]);
368 int selectedThresholdItem = getSelectedThresholdItem(getThreshold()
369 .getSelectedIndex());
371 slider.setEnabled(true);
372 thresholdValue.setEnabled(true);
373 thresholdIsMin.setEnabled(true);
375 if (selectedThresholdItem == AnnotationColourGradient.NO_THRESHOLD)
377 slider.setEnabled(false);
378 thresholdValue.setEnabled(false);
379 thresholdValue.setText("");
380 thresholdIsMin.setEnabled(false);
382 else if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD
383 && getCurrentAnnotation().threshold == null)
385 getCurrentAnnotation()
388 (getCurrentAnnotation().graphMax - getCurrentAnnotation().graphMin) / 2f,
389 "Threshold", Color.black));
392 if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD)
395 float range = getCurrentAnnotation().graphMax * ONETHOUSAND
396 - getCurrentAnnotation().graphMin * ONETHOUSAND;
398 slider.setMinimum((int) (getCurrentAnnotation().graphMin * ONETHOUSAND));
399 slider.setMaximum((int) (getCurrentAnnotation().graphMax * ONETHOUSAND));
400 slider.setValue((int) (getCurrentAnnotation().threshold.value * ONETHOUSAND));
401 thresholdValue.setText(getCurrentAnnotation().threshold.value + "");
402 slider.setMajorTickSpacing((int) (range / 10f));
403 slider.setEnabled(true);
404 thresholdValue.setEnabled(true);
407 colorAlignmentContaining(getCurrentAnnotation(), selectedThresholdItem);
409 ap.alignmentChanged();
410 // ensure all associated views (overviews, structures, etc) are notified of
412 ap.paintAlignment(true);
415 protected boolean colorAlignmentContaining(AlignmentAnnotation currentAnn, int selectedThresholdOption)
418 AnnotationColourGradient acg = null;
419 if (currentColours.isSelected())
421 acg = new AnnotationColourGradient(currentAnn,
422 av.getGlobalColourScheme(), selectedThresholdOption);
426 acg = new AnnotationColourGradient(currentAnn,
427 minColour.getBackground(), maxColour.getBackground(),
428 selectedThresholdOption);
430 acg.setSeqAssociated(seqAssociated.isSelected());
432 if (currentAnn.graphMin == 0f && currentAnn.graphMax == 0f)
434 acg.setPredefinedColours(true);
437 acg.setThresholdIsMinMax(thresholdIsMin.isSelected());
439 av.setGlobalColourScheme(acg);
441 if (av.getAlignment().getGroups() != null)
444 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
451 if (currentColours.isSelected())
453 sg.cs = new AnnotationColourGradient(currentAnn, sg.cs,
454 selectedThresholdOption);
455 ((AnnotationColourGradient) sg.cs).setSeqAssociated(seqAssociated
460 sg.cs = new AnnotationColourGradient(currentAnn,
461 minColour.getBackground(), maxColour.getBackground(),
462 selectedThresholdOption);
463 ((AnnotationColourGradient) sg.cs).setSeqAssociated(seqAssociated