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 ColourSchemeI oldcs;
58 private JButton defColours;
60 private Hashtable<SequenceGroup, ColourSchemeI> oldgroupColours;
62 private JCheckBox useOriginalColours = new JCheckBox();
64 private JPanel minColour = new JPanel();
66 private 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 minColour_actionPerformed();
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 maxColour_actionPerformed();
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 public void minColour_actionPerformed()
292 Color col = JColorChooser.showDialog(this,
293 MessageManager.getString("label.select_colour_minimum_value"),
294 minColour.getBackground());
297 minColour.setBackground(col);
303 public void maxColour_actionPerformed()
305 Color col = JColorChooser.showDialog(this,
306 MessageManager.getString("label.select_colour_maximum_value"),
307 maxColour.getBackground());
310 maxColour.setBackground(col);
319 this.ap.alignFrame.changeColour(oldcs);
320 if (av.getAlignment().getGroups() != null)
323 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
325 sg.setColourScheme(oldgroupColours.get(sg));
331 public void valueChanged(boolean updateAllAnnotation)
333 if (slider.isEnabled())
335 if (useOriginalColours.isSelected() && !(av
336 .getGlobalColourScheme() instanceof AnnotationColourGradient))
340 getCurrentAnnotation().threshold.value = getSliderValue();
341 propagateSeqAssociatedThreshold(updateAllAnnotation,
342 getCurrentAnnotation());
343 ap.paintAlignment(false, false);
347 public void originalColours_actionPerformed()
349 boolean selected = useOriginalColours.isSelected();
354 maxColour.setEnabled(!selected);
355 minColour.setEnabled(!selected);
356 thresholdIsMin.setEnabled(!selected);
361 public void updateView()
363 // Check if combobox is still adjusting
369 setCurrentAnnotation(
370 av.getAlignment().getAlignmentAnnotation()[annmap[annotations
371 .getSelectedIndex()]]);
373 int selectedThresholdItem = getSelectedThresholdItem(
374 getThreshold().getSelectedIndex());
376 slider.setEnabled(true);
377 thresholdValue.setEnabled(true);
378 thresholdIsMin.setEnabled(!useOriginalColours.isSelected());
380 final AlignmentAnnotation currentAnnotation = getCurrentAnnotation();
381 if (selectedThresholdItem == AnnotationColourGradient.NO_THRESHOLD)
383 slider.setEnabled(false);
384 thresholdValue.setEnabled(false);
385 thresholdValue.setText("");
386 thresholdIsMin.setEnabled(false);
388 else if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD
389 && currentAnnotation.threshold == null)
391 currentAnnotation.setThreshold(new GraphLine(
392 (currentAnnotation.graphMax - currentAnnotation.graphMin)
394 "Threshold", Color.black));
397 if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD)
400 setSliderModel(currentAnnotation.graphMin, currentAnnotation.graphMax,
401 currentAnnotation.threshold.value);
402 slider.setEnabled(true);
404 setThresholdValueText();
405 thresholdValue.setEnabled(true);
408 colorAlignmentContaining(currentAnnotation, selectedThresholdItem);
410 ap.alignmentChanged();
413 protected void colorAlignmentContaining(AlignmentAnnotation currentAnn,
414 int selectedThresholdOption)
417 AnnotationColourGradient acg = null;
418 if (useOriginalColours.isSelected())
420 acg = new AnnotationColourGradient(currentAnn,
421 av.getGlobalColourScheme(), selectedThresholdOption);
425 acg = new AnnotationColourGradient(currentAnn,
426 minColour.getBackground(), maxColour.getBackground(),
427 selectedThresholdOption);
429 acg.setSeqAssociated(seqAssociated.isSelected());
431 if (currentAnn.graphMin == 0f && currentAnn.graphMax == 0f)
433 acg.setPredefinedColours(true);
436 acg.setThresholdIsMinMax(thresholdIsMin.isSelected());
438 this.ap.alignFrame.changeColour(acg);
440 if (av.getAlignment().getGroups() != null)
443 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
450 acg.getInstance(av, sg));
456 protected void sliderDragReleased()
458 super.sliderDragReleased();
459 ap.paintAlignment(true, true);