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.
21 package jalview.appletgui;
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.SequenceGroup;
25 import jalview.schemes.AnnotationColourGradient;
26 import jalview.schemes.ColourSchemeI;
27 import jalview.util.MessageManager;
29 import java.awt.BorderLayout;
30 import java.awt.Button;
31 import java.awt.Checkbox;
32 import java.awt.Choice;
33 import java.awt.Color;
34 import java.awt.Dimension;
35 import java.awt.FlowLayout;
37 import java.awt.Frame;
38 import java.awt.Panel;
39 import java.awt.Scrollbar;
40 import java.awt.TextField;
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.awt.event.AdjustmentEvent;
44 import java.awt.event.AdjustmentListener;
45 import java.awt.event.ItemEvent;
46 import java.awt.event.ItemListener;
47 import java.awt.event.MouseEvent;
48 import java.awt.event.MouseListener;
49 import java.util.HashMap;
51 import java.util.Vector;
53 public class AnnotationColourChooser extends Panel implements
54 ActionListener, AdjustmentListener, ItemListener, MouseListener
64 Map<SequenceGroup, ColourSchemeI> oldgroupColours;
67 * map from annotation to its menu item display label
68 * - so we know which item to pre-select on restore
70 private Map<AlignmentAnnotation, String> annotationLabels;
72 AlignmentAnnotation currentAnnotation;
74 boolean adjusting = false;
76 public AnnotationColourChooser(AlignViewport av, AlignmentPanel ap)
81 } catch (Exception ex)
85 oldcs = av.getGlobalColourScheme();
86 if (av.getAlignment().getGroups() != null)
88 oldgroupColours = new HashMap<>();
89 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
91 oldgroupColours.put(sg, sg.getColourScheme());
97 slider.addAdjustmentListener(this);
98 slider.addMouseListener(this);
100 AlignmentAnnotation[] anns = av.getAlignment().getAlignmentAnnotation();
109 if (oldcs instanceof AnnotationColourGradient)
111 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
112 currentColours.setState(
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.setState(acg.isSeqAssociated());
122 Vector<String> list = getAnnotationItems();
124 for (int i = 0; i < list.size(); i++)
126 annotations.addItem(list.elementAt(i).toString());
129 threshold.addItem(MessageManager
130 .getString("label.threshold_feature_no_threshold"));
131 threshold.addItem(MessageManager
132 .getString("label.threshold_feature_above_threshold"));
133 threshold.addItem(MessageManager
134 .getString("label.threshold_feature_below_threshold"));
136 if (oldcs instanceof AnnotationColourGradient)
138 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
139 String label = annotationLabels.get(acg.getAnnotation());
140 annotations.select(label);
141 switch (acg.getAboveThreshold())
143 case AnnotationColourGradient.NO_THRESHOLD:
146 case AnnotationColourGradient.ABOVE_THRESHOLD:
149 case AnnotationColourGradient.BELOW_THRESHOLD:
153 throw new Error(MessageManager.getString(
154 "error.implementation_error_dont_know_threshold_annotationcolourgradient"));
156 thresholdIsMin.setState(acg.isThresholdIsMinMax());
157 thresholdValue.setText("" + acg.getAnnotationThreshold());
166 jalview.bin.JalviewLite.addFrame(frame,
167 MessageManager.getString("label.colour_by_annotation"), 560,
173 * Builds and returns a list of menu items (display text) for choice of
174 * annotation. Also builds a map between annotations and their display labels.
178 protected Vector<String> getAnnotationItems()
180 // TODO remove duplication with gui.AnnotationRowFilter
181 // TODO add 'per sequence only' option / parameter
183 annotationLabels = new HashMap<>();
184 Vector<String> list = new Vector<>();
185 AlignmentAnnotation[] anns = av.getAlignment().getAlignmentAnnotation();
191 for (int i = 0; i < anns.length; i++)
193 String label = anns[i].label;
194 if (anns[i].sequenceRef != null)
197 * be helpful and include sequence id in label for
198 * sequence-associated annotation (JAL-2236)
200 label = label + "_" + anns[i].sequenceRef.getName();
202 if (!list.contains(label))
204 list.addElement(label);
205 annotationLabels.put(anns[i], label);
209 label = label + "_" + (index++);
210 list.addElement(label);
211 annotationLabels.put(anns[i], label);
217 private void setDefaultMinMax()
219 minColour.setBackground(av.applet.getDefaultColourParameter(
220 "ANNOTATIONCOLOUR_MIN", Color.orange));
221 maxColour.setBackground(av.applet
222 .getDefaultColourParameter("ANNOTATIONCOLOUR_MAX", Color.red));
226 public AnnotationColourChooser()
231 } catch (Exception ex)
233 ex.printStackTrace();
237 private void jbInit() throws Exception
239 minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
240 minColour.setLabel(MessageManager.getString("label.min_colour"));
241 minColour.addActionListener(this);
243 maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
244 maxColour.setLabel(MessageManager.getString("label.max_colour"));
245 maxColour.addActionListener(this);
247 thresholdIsMin.addItemListener(this);
248 ok.setLabel(MessageManager.getString("action.ok"));
249 ok.addActionListener(this);
251 cancel.setLabel(MessageManager.getString("action.cancel"));
252 cancel.addActionListener(this);
254 defColours.setLabel(MessageManager.getString("action.set_defaults"));
255 defColours.addActionListener(this);
257 annotations.addItemListener(this);
259 thresholdValue.addActionListener(this);
260 slider.setBackground(Color.white);
261 slider.setPreferredSize(new Dimension(193, 21));
262 slider.setEnabled(false);
263 thresholdValue.setPreferredSize(new Dimension(79, 22));
264 thresholdValue.setEnabled(false);
265 thresholdValue.setColumns(5);
266 currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
267 currentColours.setLabel(
268 MessageManager.getString("label.use_original_colours"));
269 currentColours.addItemListener(this);
271 thresholdIsMin.setBackground(Color.white);
273 .setLabel(MessageManager.getString("label.threshold_minmax"));
275 this.setLayout(borderLayout1);
277 jPanel1.setBackground(Color.white);
279 jPanel2.setLayout(new FlowLayout());
280 jPanel2.setBackground(Color.white);
281 threshold.addItemListener(this);
282 jPanel3.setLayout(new FlowLayout());
283 jPanel3.setBackground(Color.white);
284 Panel jPanel4 = new Panel();
285 jPanel4.setLayout(new BorderLayout());
286 jPanel4.setBackground(Color.white);
291 jPanel2.add(annotations);
292 jPanel2.add(currentColours);
293 jPanel2.add(minColour);
294 jPanel2.add(maxColour);
296 jPanel4.add(thresholdIsMin, BorderLayout.WEST);
297 jPanel4.add(slider, BorderLayout.CENTER);
298 jPanel4.add(thresholdValue, BorderLayout.EAST);
300 Panel jPanel34 = new Panel();
301 jPanel34.setLayout(new BorderLayout());
302 jPanel34.setBackground(Color.white);
303 jPanel34.add(jPanel2, BorderLayout.NORTH);
304 jPanel34.add(threshold, BorderLayout.WEST);
305 jPanel3.add(defColours);
306 jPanel34.add(jPanel3, BorderLayout.EAST);
307 jPanel34.add(jPanel4, BorderLayout.SOUTH);
309 this.add(jPanel34, java.awt.BorderLayout.CENTER);
310 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
314 Choice annotations = new Choice();
316 Button minColour = new Button();
318 Button maxColour = new Button();
320 Button ok = new Button();
322 Button cancel = new Button();
324 Button defColours = new Button();
326 Panel jPanel1 = new Panel();
328 Panel jPanel2 = new Panel();
330 Choice threshold = new Choice();
332 FlowLayout flowLayout1 = new FlowLayout();
334 Panel jPanel3 = new Panel();
336 Scrollbar slider = new Scrollbar(Scrollbar.HORIZONTAL);
338 TextField thresholdValue = new TextField(20);
340 Checkbox currentColours = new Checkbox();
342 BorderLayout borderLayout1 = new BorderLayout();
344 Checkbox thresholdIsMin = new Checkbox();
347 public void actionPerformed(ActionEvent evt)
349 if (evt.getSource() == thresholdValue)
353 float f = Float.valueOf(thresholdValue.getText()).floatValue();
354 slider.setValue((int) (f * 1000));
355 adjustmentValueChanged(null);
356 } catch (NumberFormatException ex)
360 else if (evt.getSource() == minColour)
362 minColour_actionPerformed(null);
364 else if (evt.getSource() == maxColour)
366 maxColour_actionPerformed(null);
368 else if (evt.getSource() == defColours)
370 defColour_actionPerformed();
372 else if (evt.getSource() == ok)
374 frame.setVisible(false);
376 else if (evt.getSource() == cancel)
379 ap.paintAlignment(true, true);
380 frame.setVisible(false);
390 public void itemStateChanged(ItemEvent evt)
392 if (evt.getSource() == currentColours)
394 if (currentColours.getState())
399 maxColour.setEnabled(!currentColours.getState());
400 minColour.setEnabled(!currentColours.getState());
408 public void adjustmentValueChanged(AdjustmentEvent evt)
412 thresholdValue.setText((slider.getValue() / 1000f) + "");
413 if (currentColours.getState() && !(av
414 .getGlobalColourScheme() instanceof AnnotationColourGradient))
419 currentAnnotation.threshold.value = slider.getValue() / 1000f;
420 ap.paintAlignment(false, false);
424 public void minColour_actionPerformed(Color newCol)
428 minColour.setBackground(newCol);
434 new UserDefinedColours(this, "Min Colour", minColour.getBackground());
439 public void maxColour_actionPerformed(Color newCol)
443 maxColour.setBackground(newCol);
449 new UserDefinedColours(this, "Max Colour", maxColour.getBackground());
453 public void defColour_actionPerformed()
463 // Check if combobox is still adjusting
469 currentAnnotation = av.getAlignment()
470 .getAlignmentAnnotation()[annotations.getSelectedIndex()];
472 int aboveThreshold = -1;
473 if (threshold.getSelectedIndex() == 1)
475 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
477 else if (threshold.getSelectedIndex() == 2)
479 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
482 slider.setEnabled(true);
483 thresholdValue.setEnabled(true);
484 thresholdIsMin.setEnabled(true);
486 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
488 slider.setEnabled(false);
489 thresholdValue.setEnabled(false);
490 thresholdIsMin.setEnabled(false);
491 thresholdValue.setText("");
493 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
494 && currentAnnotation.threshold == null)
496 currentAnnotation.setThreshold(new jalview.datamodel.GraphLine(
497 (currentAnnotation.graphMax - currentAnnotation.graphMin)
499 "Threshold", Color.black));
502 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
506 slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
507 slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
508 slider.setValue((int) (currentAnnotation.threshold.value * 1000));
509 thresholdValue.setText(currentAnnotation.threshold.value + "");
510 slider.setEnabled(true);
511 thresholdValue.setEnabled(true);
515 AnnotationColourGradient acg = null;
516 if (currentColours.getState())
521 acg = new AnnotationColourGradient(currentAnnotation,
522 minColour.getBackground(), maxColour.getBackground(),
526 if (currentAnnotation.graphMin == 0f
527 && currentAnnotation.graphMax == 0f)
529 acg.setPredefinedColours(true);
532 acg.setThresholdIsMinMax(thresholdIsMin.getState());
534 av.setGlobalColourScheme(acg);
536 // TODO: per group colour propagation not always desired
537 if (av.getAlignment().getGroups() != null)
539 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
541 if (sg.getColourScheme() == null)
546 if (currentColours.getState())
548 sg.setColourScheme(new AnnotationColourGradient(currentAnnotation,
549 sg.getColourScheme(), aboveThreshold));
553 sg.setColourScheme(new AnnotationColourGradient(currentAnnotation,
554 minColour.getBackground(), maxColour.getBackground(),
560 // update colours in linked windows
561 ap.alignmentChanged();
562 ap.paintAlignment(true, true);
567 av.setGlobalColourScheme(oldcs);
568 if (av.getAlignment().getGroups() != null)
570 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
572 sg.setColourScheme(oldgroupColours.get(sg));
575 ap.paintAlignment(true, true);
579 public void mouseClicked(MouseEvent evt)
584 public void mousePressed(MouseEvent evt)
589 public void mouseReleased(MouseEvent evt)
591 ap.paintAlignment(true, true);
595 public void mouseEntered(MouseEvent evt)
600 public void mouseExited(MouseEvent evt)