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 java.awt.BorderLayout;
24 import java.awt.Button;
25 import java.awt.Checkbox;
26 import java.awt.Choice;
27 import java.awt.Color;
28 import java.awt.Dimension;
29 import java.awt.FlowLayout;
31 import java.awt.Frame;
32 import java.awt.Panel;
33 import java.awt.Scrollbar;
34 import java.awt.TextField;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.ActionListener;
37 import java.awt.event.AdjustmentEvent;
38 import java.awt.event.AdjustmentListener;
39 import java.awt.event.ItemEvent;
40 import java.awt.event.ItemListener;
41 import java.awt.event.MouseEvent;
42 import java.awt.event.MouseListener;
43 import java.util.HashMap;
45 import java.util.Vector;
47 import jalview.datamodel.AlignmentAnnotation;
48 import jalview.datamodel.SequenceGroup;
49 import jalview.schemes.AnnotationColourGradient;
50 import jalview.schemes.ColourSchemeI;
51 import jalview.util.MessageManager;
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())
93 oldgroupColours.put(sg, sg.cs);
100 slider.addAdjustmentListener(this);
101 slider.addMouseListener(this);
103 AlignmentAnnotation[] anns = av.getAlignment().getAlignmentAnnotation();
112 if (oldcs instanceof AnnotationColourGradient)
114 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
115 currentColours.setState(acg.isPredefinedColours()
116 || acg.getBaseColour() != null);
117 if (!acg.isPredefinedColours() && acg.getBaseColour() == null)
119 minColour.setBackground(acg.getMinColour());
120 maxColour.setBackground(acg.getMaxColour());
122 // seqAssociated.setState(acg.isSeqAssociated());
125 Vector<String> list = getAnnotationItems();
127 for (int i = 0; i < list.size(); i++)
129 annotations.addItem(list.elementAt(i).toString());
132 threshold.addItem(MessageManager
133 .getString("label.threshold_feature_no_threshold"));
134 threshold.addItem(MessageManager
135 .getString("label.threshold_feature_above_threshold"));
136 threshold.addItem(MessageManager
137 .getString("label.threshold_feature_below_threshold"));
139 if (oldcs instanceof AnnotationColourGradient)
141 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
142 String label = annotationLabels.get(acg.getAnnotation());
143 annotations.select(label);
144 switch (acg.getAboveThreshold())
146 case AnnotationColourGradient.NO_THRESHOLD:
149 case AnnotationColourGradient.ABOVE_THRESHOLD:
152 case AnnotationColourGradient.BELOW_THRESHOLD:
158 .getString("error.implementation_error_dont_know_threshold_annotationcolourgradient"));
160 thresholdIsMin.setState(acg.isThresholdIsMinMax());
161 thresholdValue.setText("" + acg.getAnnotationThreshold());
170 jalview.bin.JalviewLite.addFrame(frame,
171 MessageManager.getString("label.colour_by_annotation"), 560,
177 * Builds and returns a list of menu items (display text) for choice of
178 * annotation. Also builds a map between annotations and their display labels.
182 protected Vector<String> getAnnotationItems()
184 // TODO remove duplication with gui.AnnotationRowFilter
185 // TODO add 'per sequence only' option / parameter
187 annotationLabels = new HashMap<AlignmentAnnotation, String>();
188 Vector<String> list = new Vector<String>();
189 AlignmentAnnotation[] anns = av.getAlignment().getAlignmentAnnotation();
195 for (int i = 0; i < anns.length; i++)
197 String label = anns[i].label;
198 if (anns[i].sequenceRef != null)
201 * be helpful and include sequence id in label for
202 * sequence-associated annotation (JAL-2236)
204 label = label + "_" + anns[i].sequenceRef.getName();
206 if (!list.contains(label))
208 list.addElement(label);
209 annotationLabels.put(anns[i], label);
213 label = label + "_" + (index++);
214 list.addElement(label);
215 annotationLabels.put(anns[i], label);
221 private void setDefaultMinMax()
223 minColour.setBackground(av.applet.getDefaultColourParameter(
224 "ANNOTATIONCOLOUR_MIN", Color.orange));
225 maxColour.setBackground(av.applet.getDefaultColourParameter(
226 "ANNOTATIONCOLOUR_MAX", Color.red));
230 public AnnotationColourChooser()
235 } catch (Exception ex)
237 ex.printStackTrace();
241 private void jbInit() throws Exception
243 minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
244 minColour.setLabel(MessageManager.getString("label.min_colour"));
245 minColour.addActionListener(this);
247 maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
248 maxColour.setLabel(MessageManager.getString("label.max_colour"));
249 maxColour.addActionListener(this);
251 thresholdIsMin.addItemListener(this);
252 ok.setLabel(MessageManager.getString("action.ok"));
253 ok.addActionListener(this);
255 cancel.setLabel(MessageManager.getString("action.cancel"));
256 cancel.addActionListener(this);
258 defColours.setLabel(MessageManager.getString("action.set_defaults"));
259 defColours.addActionListener(this);
261 annotations.addItemListener(this);
263 thresholdValue.addActionListener(this);
264 slider.setBackground(Color.white);
265 slider.setPreferredSize(new Dimension(193, 21));
266 slider.setEnabled(false);
267 thresholdValue.setPreferredSize(new Dimension(79, 22));
268 thresholdValue.setEnabled(false);
269 thresholdValue.setColumns(5);
270 currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
271 currentColours.setLabel(MessageManager
272 .getString("label.use_original_colours"));
273 currentColours.addItemListener(this);
275 thresholdIsMin.setBackground(Color.white);
276 thresholdIsMin.setLabel(MessageManager
277 .getString("label.threshold_minmax"));
279 this.setLayout(borderLayout1);
281 jPanel1.setBackground(Color.white);
283 jPanel2.setLayout(new FlowLayout());
284 jPanel2.setBackground(Color.white);
285 threshold.addItemListener(this);
286 jPanel3.setLayout(new FlowLayout());
287 jPanel3.setBackground(Color.white);
288 Panel jPanel4 = new Panel();
289 jPanel4.setLayout(new BorderLayout());
290 jPanel4.setBackground(Color.white);
295 jPanel2.add(annotations);
296 jPanel2.add(currentColours);
297 jPanel2.add(minColour);
298 jPanel2.add(maxColour);
300 jPanel4.add(thresholdIsMin, BorderLayout.WEST);
301 jPanel4.add(slider, BorderLayout.CENTER);
302 jPanel4.add(thresholdValue, BorderLayout.EAST);
304 Panel jPanel34 = new Panel();
305 jPanel34.setLayout(new BorderLayout());
306 jPanel34.setBackground(Color.white);
307 jPanel34.add(jPanel2, BorderLayout.NORTH);
308 jPanel34.add(threshold, BorderLayout.WEST);
309 jPanel3.add(defColours);
310 jPanel34.add(jPanel3, BorderLayout.EAST);
311 jPanel34.add(jPanel4, BorderLayout.SOUTH);
313 this.add(jPanel34, java.awt.BorderLayout.CENTER);
314 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
318 Choice annotations = new Choice();
320 Button minColour = new Button();
322 Button maxColour = new Button();
324 Button ok = new Button();
326 Button cancel = new Button();
328 Button defColours = new Button();
330 Panel jPanel1 = new Panel();
332 Panel jPanel2 = new Panel();
334 Choice threshold = new Choice();
336 FlowLayout flowLayout1 = new FlowLayout();
338 Panel jPanel3 = new Panel();
340 Scrollbar slider = new Scrollbar(Scrollbar.HORIZONTAL);
342 TextField thresholdValue = new TextField(20);
344 Checkbox currentColours = new Checkbox();
346 BorderLayout borderLayout1 = new BorderLayout();
348 Checkbox thresholdIsMin = new Checkbox();
351 public void actionPerformed(ActionEvent evt)
353 if (evt.getSource() == thresholdValue)
357 float f = new Float(thresholdValue.getText()).floatValue();
358 slider.setValue((int) (f * 1000));
359 adjustmentValueChanged(null);
360 } catch (NumberFormatException ex)
364 else if (evt.getSource() == minColour)
366 minColour_actionPerformed(null);
368 else if (evt.getSource() == maxColour)
370 maxColour_actionPerformed(null);
372 else if (evt.getSource() == defColours)
374 defColour_actionPerformed();
376 else if (evt.getSource() == ok)
378 frame.setVisible(false);
380 else if (evt.getSource() == cancel)
383 ap.paintAlignment(true);
384 frame.setVisible(false);
394 public void itemStateChanged(ItemEvent evt)
396 if (evt.getSource() == currentColours)
398 if (currentColours.getState())
403 maxColour.setEnabled(!currentColours.getState());
404 minColour.setEnabled(!currentColours.getState());
412 public void adjustmentValueChanged(AdjustmentEvent evt)
416 thresholdValue.setText((slider.getValue() / 1000f) + "");
417 if (currentColours.getState()
418 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
423 currentAnnotation.threshold.value = slider.getValue() / 1000f;
424 ap.paintAlignment(false);
428 public void minColour_actionPerformed(Color newCol)
432 minColour.setBackground(newCol);
438 new UserDefinedColours(this, "Min Colour", minColour.getBackground());
443 public void maxColour_actionPerformed(Color newCol)
447 maxColour.setBackground(newCol);
453 new UserDefinedColours(this, "Max Colour", maxColour.getBackground());
457 public void defColour_actionPerformed()
467 // Check if combobox is still adjusting
473 currentAnnotation = av.getAlignment().getAlignmentAnnotation()[annotations
474 .getSelectedIndex()];
476 int aboveThreshold = -1;
477 if (threshold.getSelectedIndex() == 1)
479 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
481 else if (threshold.getSelectedIndex() == 2)
483 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
486 slider.setEnabled(true);
487 thresholdValue.setEnabled(true);
488 thresholdIsMin.setEnabled(true);
490 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
492 slider.setEnabled(false);
493 thresholdValue.setEnabled(false);
494 thresholdIsMin.setEnabled(false);
495 thresholdValue.setText("");
497 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
498 && currentAnnotation.threshold == null)
501 .setThreshold(new jalview.datamodel.GraphLine(
502 (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
503 "Threshold", Color.black));
506 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
510 slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
511 slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
512 slider.setValue((int) (currentAnnotation.threshold.value * 1000));
513 thresholdValue.setText(currentAnnotation.threshold.value + "");
514 slider.setEnabled(true);
515 thresholdValue.setEnabled(true);
519 AnnotationColourGradient acg = null;
520 if (currentColours.getState())
522 acg = new AnnotationColourGradient(currentAnnotation,
523 av.getGlobalColourScheme(), aboveThreshold);
527 acg = new AnnotationColourGradient(currentAnnotation,
528 minColour.getBackground(), maxColour.getBackground(),
532 if (currentAnnotation.graphMin == 0f
533 && currentAnnotation.graphMax == 0f)
535 acg.setPredefinedColours(true);
538 acg.setThresholdIsMinMax(thresholdIsMin.getState());
540 av.setGlobalColourScheme(acg);
542 // TODO: per group colour propagation not always desired
543 if (av.getAlignment().getGroups() != null)
545 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
552 if (currentColours.getState())
554 sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
559 sg.cs = new AnnotationColourGradient(currentAnnotation,
560 minColour.getBackground(), maxColour.getBackground(),
566 // update colours in linked windows
567 ap.alignmentChanged();
568 ap.paintAlignment(true);
573 av.setGlobalColourScheme(oldcs);
574 if (av.getAlignment().getGroups() != null)
576 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
578 sg.cs = oldgroupColours.get(sg);
581 ap.paintAlignment(true);
586 public void mouseClicked(MouseEvent evt)
591 public void mousePressed(MouseEvent evt)
596 public void mouseReleased(MouseEvent evt)
598 ap.paintAlignment(true);
602 public void mouseEntered(MouseEvent evt)
607 public void mouseExited(MouseEvent evt)