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.Hashtable;
50 import java.util.Vector;
52 public class AnnotationColourChooser extends Panel implements
53 ActionListener, AdjustmentListener, ItemListener, MouseListener
63 Hashtable oldgroupColours;
65 jalview.datamodel.AlignmentAnnotation currentAnnotation;
67 boolean adjusting = false;
69 public AnnotationColourChooser(AlignViewport av, AlignmentPanel ap)
74 } catch (Exception ex)
78 oldcs = av.getGlobalColourScheme();
79 if (av.getAlignment().getGroups() != null)
81 oldgroupColours = new Hashtable();
82 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
84 if (sg.getColourScheme() != null)
86 oldgroupColours.put(sg, sg.getColourScheme());
90 oldgroupColours.put(sg, "null");
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 = new Vector<String>();
124 for (int i = 0; i < anns.length; i++)
126 String label = anns[i].label;
127 if (anns[i].sequenceRef != null)
129 label = label + "_" + anns[i].sequenceRef.getName();
131 if (!list.contains(label))
133 list.addElement(label);
137 list.addElement(label + "_" + (index++));
141 for (int i = 0; i < list.size(); i++)
143 annotations.addItem(list.elementAt(i).toString());
146 threshold.addItem(MessageManager
147 .getString("label.threshold_feature_no_threshold"));
148 threshold.addItem(MessageManager
149 .getString("label.threshold_feature_above_threshold"));
150 threshold.addItem(MessageManager
151 .getString("label.threshold_feature_below_threshold"));
153 if (oldcs instanceof AnnotationColourGradient)
155 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
156 annotations.select(acg.getAnnotation());
157 switch (acg.getAboveThreshold())
159 case AnnotationColourGradient.NO_THRESHOLD:
162 case AnnotationColourGradient.ABOVE_THRESHOLD:
165 case AnnotationColourGradient.BELOW_THRESHOLD:
171 .getString("error.implementation_error_dont_know_threshold_annotationcolourgradient"));
173 thresholdIsMin.setState(acg.thresholdIsMinMax);
174 thresholdValue.setText("" + acg.getAnnotationThreshold());
183 jalview.bin.JalviewLite.addFrame(frame,
184 MessageManager.getString("label.colour_by_annotation"), 560,
189 private void setDefaultMinMax()
191 minColour.setBackground(av.applet.getDefaultColourParameter(
192 "ANNOTATIONCOLOUR_MIN", Color.orange));
193 maxColour.setBackground(av.applet.getDefaultColourParameter(
194 "ANNOTATIONCOLOUR_MAX", Color.red));
198 public AnnotationColourChooser()
203 } catch (Exception ex)
205 ex.printStackTrace();
209 private void jbInit() throws Exception
211 minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
212 minColour.setLabel(MessageManager.getString("label.min_colour"));
213 minColour.addActionListener(this);
215 maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
216 maxColour.setLabel(MessageManager.getString("label.max_colour"));
217 maxColour.addActionListener(this);
219 thresholdIsMin.addItemListener(this);
220 ok.setLabel(MessageManager.getString("action.ok"));
221 ok.addActionListener(this);
223 cancel.setLabel(MessageManager.getString("action.cancel"));
224 cancel.addActionListener(this);
226 defColours.setLabel(MessageManager.getString("action.set_defaults"));
227 defColours.addActionListener(this);
229 annotations.addItemListener(this);
231 thresholdValue.addActionListener(this);
232 slider.setBackground(Color.white);
233 slider.setPreferredSize(new Dimension(193, 21));
234 slider.setEnabled(false);
235 thresholdValue.setPreferredSize(new Dimension(79, 22));
236 thresholdValue.setEnabled(false);
237 thresholdValue.setColumns(5);
238 currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
239 currentColours.setLabel(MessageManager
240 .getString("label.use_original_colours"));
241 currentColours.addItemListener(this);
243 thresholdIsMin.setBackground(Color.white);
244 thresholdIsMin.setLabel(MessageManager
245 .getString("label.threshold_minmax"));
247 this.setLayout(borderLayout1);
249 jPanel1.setBackground(Color.white);
251 jPanel2.setLayout(new FlowLayout());
252 jPanel2.setBackground(Color.white);
253 threshold.addItemListener(this);
254 jPanel3.setLayout(new FlowLayout());
255 jPanel3.setBackground(Color.white);
256 Panel jPanel4 = new Panel();
257 jPanel4.setLayout(new BorderLayout());
258 jPanel4.setBackground(Color.white);
263 jPanel2.add(annotations);
264 jPanel2.add(currentColours);
265 jPanel2.add(minColour);
266 jPanel2.add(maxColour);
268 jPanel4.add(thresholdIsMin, BorderLayout.WEST);
269 jPanel4.add(slider, BorderLayout.CENTER);
270 jPanel4.add(thresholdValue, BorderLayout.EAST);
272 Panel jPanel34 = new Panel();
273 jPanel34.setLayout(new BorderLayout());
274 jPanel34.setBackground(Color.white);
275 jPanel34.add(jPanel2, BorderLayout.NORTH);
276 jPanel34.add(threshold, BorderLayout.WEST);
277 jPanel3.add(defColours);
278 jPanel34.add(jPanel3, BorderLayout.EAST);
279 jPanel34.add(jPanel4, BorderLayout.SOUTH);
281 this.add(jPanel34, java.awt.BorderLayout.CENTER);
282 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
286 Choice annotations = new Choice();
288 Button minColour = new Button();
290 Button maxColour = new Button();
292 Button ok = new Button();
294 Button cancel = new Button();
296 Button defColours = new Button();
298 Panel jPanel1 = new Panel();
300 Panel jPanel2 = new Panel();
302 Choice threshold = new Choice();
304 FlowLayout flowLayout1 = new FlowLayout();
306 Panel jPanel3 = new Panel();
308 Scrollbar slider = new Scrollbar(Scrollbar.HORIZONTAL);
310 TextField thresholdValue = new TextField(20);
312 Checkbox currentColours = new Checkbox();
314 BorderLayout borderLayout1 = new BorderLayout();
316 Checkbox thresholdIsMin = new Checkbox();
319 public void actionPerformed(ActionEvent evt)
321 if (evt.getSource() == thresholdValue)
325 float f = new Float(thresholdValue.getText()).floatValue();
326 slider.setValue((int) (f * 1000));
327 adjustmentValueChanged(null);
328 } catch (NumberFormatException ex)
332 else if (evt.getSource() == minColour)
334 minColour_actionPerformed(null);
336 else if (evt.getSource() == maxColour)
338 maxColour_actionPerformed(null);
340 else if (evt.getSource() == defColours)
342 defColour_actionPerformed();
344 else if (evt.getSource() == ok)
346 frame.setVisible(false);
348 else if (evt.getSource() == cancel)
351 ap.paintAlignment(true);
352 frame.setVisible(false);
362 public void itemStateChanged(ItemEvent evt)
364 if (evt.getSource() == currentColours)
366 if (currentColours.getState())
371 maxColour.setEnabled(!currentColours.getState());
372 minColour.setEnabled(!currentColours.getState());
380 public void adjustmentValueChanged(AdjustmentEvent evt)
384 thresholdValue.setText((slider.getValue() / 1000f) + "");
385 if (currentColours.getState()
386 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
391 currentAnnotation.threshold.value = slider.getValue() / 1000f;
392 ap.paintAlignment(false);
396 public void minColour_actionPerformed(Color newCol)
400 minColour.setBackground(newCol);
406 new UserDefinedColours(this, "Min Colour", minColour.getBackground());
411 public void maxColour_actionPerformed(Color newCol)
415 maxColour.setBackground(newCol);
421 new UserDefinedColours(this, "Max Colour", maxColour.getBackground());
425 public void defColour_actionPerformed()
435 // Check if combobox is still adjusting
441 currentAnnotation = av.getAlignment().getAlignmentAnnotation()[annotations
442 .getSelectedIndex()];
444 int aboveThreshold = -1;
445 if (threshold.getSelectedIndex() == 1)
447 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
449 else if (threshold.getSelectedIndex() == 2)
451 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
454 slider.setEnabled(true);
455 thresholdValue.setEnabled(true);
456 thresholdIsMin.setEnabled(true);
458 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
460 slider.setEnabled(false);
461 thresholdValue.setEnabled(false);
462 thresholdIsMin.setEnabled(false);
463 thresholdValue.setText("");
465 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
466 && currentAnnotation.threshold == null)
469 .setThreshold(new jalview.datamodel.GraphLine(
470 (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
471 "Threshold", Color.black));
474 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
478 slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
479 slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
480 slider.setValue((int) (currentAnnotation.threshold.value * 1000));
481 thresholdValue.setText(currentAnnotation.threshold.value + "");
482 slider.setEnabled(true);
483 thresholdValue.setEnabled(true);
487 AnnotationColourGradient acg = null;
488 if (currentColours.getState())
493 acg = new AnnotationColourGradient(currentAnnotation,
494 minColour.getBackground(), maxColour.getBackground(),
498 if (currentAnnotation.graphMin == 0f
499 && currentAnnotation.graphMax == 0f)
501 acg.setPredefinedColours(true);
504 acg.thresholdIsMinMax = thresholdIsMin.getState();
506 av.setGlobalColourScheme(acg);
508 // TODO: per group colour propagation not always desired
509 if (av.getAlignment().getGroups() != null)
511 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
514 if (sg.getColourScheme() == null)
519 if (currentColours.getState())
521 sg.setColourScheme(new AnnotationColourGradient(
522 currentAnnotation, sg.getColourScheme(), aboveThreshold));
526 sg.setColourScheme(new AnnotationColourGradient(
527 currentAnnotation, minColour.getBackground(), maxColour
528 .getBackground(), aboveThreshold));
534 // update colours in linked windows
535 ap.alignmentChanged();
536 ap.paintAlignment(true);
541 av.setGlobalColourScheme(oldcs);
542 if (av.getAlignment().getGroups() != null)
544 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
546 Object cs = oldgroupColours.get(sg);
547 if (cs instanceof ColourSchemeI)
549 sg.setColourScheme((ColourSchemeI) cs);
553 // probably the "null" string we set it to if it was null originally.
554 sg.setColourScheme(null);
558 ap.paintAlignment(true);
563 public void mouseClicked(MouseEvent evt)
568 public void mousePressed(MouseEvent evt)
573 public void mouseReleased(MouseEvent evt)
575 ap.paintAlignment(true);
579 public void mouseEntered(MouseEvent evt)
584 public void mouseExited(MouseEvent evt)