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<SequenceGroup, ColourSchemeI>();
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(acg.isPredefinedColours()
113 || 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:
155 .getString("error.implementation_error_dont_know_threshold_annotationcolourgradient"));
157 thresholdIsMin.setState(acg.isThresholdIsMinMax());
158 thresholdValue.setText("" + acg.getAnnotationThreshold());
167 jalview.bin.JalviewLite.addFrame(frame,
168 MessageManager.getString("label.colour_by_annotation"), 560,
174 * Builds and returns a list of menu items (display text) for choice of
175 * annotation. Also builds a map between annotations and their display labels.
179 protected Vector<String> getAnnotationItems()
181 // TODO remove duplication with gui.AnnotationRowFilter
182 // TODO add 'per sequence only' option / parameter
184 annotationLabels = new HashMap<AlignmentAnnotation, String>();
185 Vector<String> list = new Vector<String>();
186 AlignmentAnnotation[] anns = av.getAlignment().getAlignmentAnnotation();
192 for (int i = 0; i < anns.length; i++)
194 String label = anns[i].label;
195 if (anns[i].sequenceRef != null)
198 * be helpful and include sequence id in label for
199 * sequence-associated annotation (JAL-2236)
201 label = label + "_" + anns[i].sequenceRef.getName();
203 if (!list.contains(label))
205 list.addElement(label);
206 annotationLabels.put(anns[i], label);
210 label = label + "_" + (index++);
211 list.addElement(label);
212 annotationLabels.put(anns[i], label);
218 private void setDefaultMinMax()
220 minColour.setBackground(av.applet.getDefaultColourParameter(
221 "ANNOTATIONCOLOUR_MIN", Color.orange));
222 maxColour.setBackground(av.applet.getDefaultColourParameter(
223 "ANNOTATIONCOLOUR_MAX", Color.red));
227 public AnnotationColourChooser()
232 } catch (Exception ex)
234 ex.printStackTrace();
238 private void jbInit() throws Exception
240 minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
241 minColour.setLabel(MessageManager.getString("label.min_colour"));
242 minColour.addActionListener(this);
244 maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
245 maxColour.setLabel(MessageManager.getString("label.max_colour"));
246 maxColour.addActionListener(this);
248 thresholdIsMin.addItemListener(this);
249 ok.setLabel(MessageManager.getString("action.ok"));
250 ok.addActionListener(this);
252 cancel.setLabel(MessageManager.getString("action.cancel"));
253 cancel.addActionListener(this);
255 defColours.setLabel(MessageManager.getString("action.set_defaults"));
256 defColours.addActionListener(this);
258 annotations.addItemListener(this);
260 thresholdValue.addActionListener(this);
261 slider.setBackground(Color.white);
262 slider.setPreferredSize(new Dimension(193, 21));
263 slider.setEnabled(false);
264 thresholdValue.setPreferredSize(new Dimension(79, 22));
265 thresholdValue.setEnabled(false);
266 thresholdValue.setColumns(5);
267 currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
268 currentColours.setLabel(MessageManager
269 .getString("label.use_original_colours"));
270 currentColours.addItemListener(this);
272 thresholdIsMin.setBackground(Color.white);
273 thresholdIsMin.setLabel(MessageManager
274 .getString("label.threshold_minmax"));
276 this.setLayout(borderLayout1);
278 jPanel1.setBackground(Color.white);
280 jPanel2.setLayout(new FlowLayout());
281 jPanel2.setBackground(Color.white);
282 threshold.addItemListener(this);
283 jPanel3.setLayout(new FlowLayout());
284 jPanel3.setBackground(Color.white);
285 Panel jPanel4 = new Panel();
286 jPanel4.setLayout(new BorderLayout());
287 jPanel4.setBackground(Color.white);
292 jPanel2.add(annotations);
293 jPanel2.add(currentColours);
294 jPanel2.add(minColour);
295 jPanel2.add(maxColour);
297 jPanel4.add(thresholdIsMin, BorderLayout.WEST);
298 jPanel4.add(slider, BorderLayout.CENTER);
299 jPanel4.add(thresholdValue, BorderLayout.EAST);
301 Panel jPanel34 = new Panel();
302 jPanel34.setLayout(new BorderLayout());
303 jPanel34.setBackground(Color.white);
304 jPanel34.add(jPanel2, BorderLayout.NORTH);
305 jPanel34.add(threshold, BorderLayout.WEST);
306 jPanel3.add(defColours);
307 jPanel34.add(jPanel3, BorderLayout.EAST);
308 jPanel34.add(jPanel4, BorderLayout.SOUTH);
310 this.add(jPanel34, java.awt.BorderLayout.CENTER);
311 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
315 Choice annotations = new Choice();
317 Button minColour = new Button();
319 Button maxColour = new Button();
321 Button ok = new Button();
323 Button cancel = new Button();
325 Button defColours = new Button();
327 Panel jPanel1 = new Panel();
329 Panel jPanel2 = new Panel();
331 Choice threshold = new Choice();
333 FlowLayout flowLayout1 = new FlowLayout();
335 Panel jPanel3 = new Panel();
337 Scrollbar slider = new Scrollbar(Scrollbar.HORIZONTAL);
339 TextField thresholdValue = new TextField(20);
341 Checkbox currentColours = new Checkbox();
343 BorderLayout borderLayout1 = new BorderLayout();
345 Checkbox thresholdIsMin = new Checkbox();
348 public void actionPerformed(ActionEvent evt)
350 if (evt.getSource() == thresholdValue)
354 float f = new Float(thresholdValue.getText()).floatValue();
355 slider.setValue((int) (f * 1000));
356 adjustmentValueChanged(null);
357 } catch (NumberFormatException ex)
361 else if (evt.getSource() == minColour)
363 minColour_actionPerformed(null);
365 else if (evt.getSource() == maxColour)
367 maxColour_actionPerformed(null);
369 else if (evt.getSource() == defColours)
371 defColour_actionPerformed();
373 else if (evt.getSource() == ok)
375 frame.setVisible(false);
377 else if (evt.getSource() == cancel)
380 ap.paintAlignment(true);
381 frame.setVisible(false);
391 public void itemStateChanged(ItemEvent evt)
393 if (evt.getSource() == currentColours)
395 if (currentColours.getState())
400 maxColour.setEnabled(!currentColours.getState());
401 minColour.setEnabled(!currentColours.getState());
409 public void adjustmentValueChanged(AdjustmentEvent evt)
413 thresholdValue.setText((slider.getValue() / 1000f) + "");
414 if (currentColours.getState()
415 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
420 currentAnnotation.threshold.value = slider.getValue() / 1000f;
421 ap.paintAlignment(false);
425 public void minColour_actionPerformed(Color newCol)
429 minColour.setBackground(newCol);
435 new UserDefinedColours(this, "Min Colour", minColour.getBackground());
440 public void maxColour_actionPerformed(Color newCol)
444 maxColour.setBackground(newCol);
450 new UserDefinedColours(this, "Max Colour", maxColour.getBackground());
454 public void defColour_actionPerformed()
464 // Check if combobox is still adjusting
470 currentAnnotation = av.getAlignment().getAlignmentAnnotation()[annotations
471 .getSelectedIndex()];
473 int aboveThreshold = -1;
474 if (threshold.getSelectedIndex() == 1)
476 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
478 else if (threshold.getSelectedIndex() == 2)
480 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
483 slider.setEnabled(true);
484 thresholdValue.setEnabled(true);
485 thresholdIsMin.setEnabled(true);
487 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
489 slider.setEnabled(false);
490 thresholdValue.setEnabled(false);
491 thresholdIsMin.setEnabled(false);
492 thresholdValue.setText("");
494 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
495 && currentAnnotation.threshold == null)
498 .setThreshold(new jalview.datamodel.GraphLine(
499 (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
500 "Threshold", Color.black));
503 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
507 slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
508 slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
509 slider.setValue((int) (currentAnnotation.threshold.value * 1000));
510 thresholdValue.setText(currentAnnotation.threshold.value + "");
511 slider.setEnabled(true);
512 thresholdValue.setEnabled(true);
516 AnnotationColourGradient acg = null;
517 if (currentColours.getState())
522 acg = new AnnotationColourGradient(currentAnnotation,
523 minColour.getBackground(), maxColour.getBackground(),
527 if (currentAnnotation.graphMin == 0f
528 && currentAnnotation.graphMax == 0f)
530 acg.setPredefinedColours(true);
533 acg.setThresholdIsMinMax(thresholdIsMin.getState());
535 av.setGlobalColourScheme(acg);
537 // TODO: per group colour propagation not always desired
538 if (av.getAlignment().getGroups() != null)
540 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
542 if (sg.getColourScheme() == null)
547 if (currentColours.getState())
549 sg.setColourScheme(new AnnotationColourGradient(
550 currentAnnotation, sg.getColourScheme(), aboveThreshold));
554 sg.setColourScheme(new AnnotationColourGradient(
555 currentAnnotation, minColour.getBackground(), maxColour
556 .getBackground(), aboveThreshold));
561 // update colours in linked windows
562 ap.alignmentChanged();
563 ap.paintAlignment(true);
568 av.setGlobalColourScheme(oldcs);
569 if (av.getAlignment().getGroups() != null)
571 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
573 sg.setColourScheme(oldgroupColours.get(sg));
576 ap.paintAlignment(true);
580 public void mouseClicked(MouseEvent evt)
585 public void mousePressed(MouseEvent evt)
590 public void mouseReleased(MouseEvent evt)
592 ap.paintAlignment(true);
596 public void mouseEntered(MouseEvent evt)
601 public void mouseExited(MouseEvent evt)