2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.appletgui;
23 import java.awt.event.*;
25 import jalview.datamodel.*;
26 import jalview.schemes.*;
27 import java.awt.Rectangle;
29 import javax.swing.BoxLayout;
31 public class AnnotationColourChooser extends Panel implements
32 ActionListener, AdjustmentListener, ItemListener, MouseListener
42 Hashtable oldgroupColours;
44 jalview.datamodel.AlignmentAnnotation currentAnnotation;
46 boolean adjusting = false;
48 public AnnotationColourChooser(AlignViewport av, AlignmentPanel ap)
53 } catch (Exception ex)
57 oldcs = av.getGlobalColourScheme();
58 if (av.getAlignment().getGroups() != null)
60 oldgroupColours = new Hashtable();
61 Vector allGroups = ap.av.getAlignment().getGroups();
63 for (int g = 0; g < allGroups.size(); g++)
65 sg = (SequenceGroup) allGroups.elementAt(g);
68 oldgroupColours.put(sg, sg.cs);
72 oldgroupColours.put(sg, "null");
79 slider.addAdjustmentListener(this);
80 slider.addMouseListener(this);
82 if (av.getAlignment().getAlignmentAnnotation() == null)
89 if (oldcs instanceof AnnotationColourGradient)
91 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
92 currentColours.setState(acg.predefinedColours);
93 if (!acg.predefinedColours) {
94 minColour.setBackground(acg.getMinColour());
95 maxColour.setBackground(acg.getMaxColour());
101 Vector list = new Vector();
103 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
105 String label = av.getAlignment().getAlignmentAnnotation()[i].label;
106 if (!list.contains(label))
107 list.addElement(label);
109 list.addElement(label + "_" + (index++));
112 for (int i = 0; i < list.size(); i++)
114 annotations.addItem(list.elementAt(i).toString());
117 threshold.addItem("No Threshold");
118 threshold.addItem("Above Threshold");
119 threshold.addItem("Below Threshold");
121 if (oldcs instanceof AnnotationColourGradient)
123 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
124 annotations.select(acg.getAnnotation());
125 switch (acg.getAboveThreshold()) {
126 case AnnotationColourGradient.NO_THRESHOLD:
127 threshold.select("No Threshold");
129 case AnnotationColourGradient.ABOVE_THRESHOLD:
130 threshold.select("Above Threshold");
132 case AnnotationColourGradient.BELOW_THRESHOLD:
133 threshold.select("Below Threshold");
136 throw new Error("Implementation error: don't know about threshold setting for current AnnotationColourGradient.");
138 thresholdIsMin.setState(acg.thresholdIsMinMax);
139 thresholdValue.setText(""+acg.getAnnotationThreshold());
148 jalview.bin.JalviewLite.addFrame(frame, "Colour by Annotation", 560,
153 private void setDefaultMinMax()
155 minColour.setBackground(av.applet.getDefaultColourParameter("ANNOTATIONCOLOUR_MIN",Color.orange));
156 maxColour.setBackground(av.applet.getDefaultColourParameter("ANNOTATIONCOLOUR_MAX",Color.red));
159 public AnnotationColourChooser()
164 } catch (Exception ex)
166 ex.printStackTrace();
170 private void jbInit() throws Exception
172 minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
173 minColour.setLabel("Min Colour");
174 minColour.addActionListener(this);
176 maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
177 maxColour.setLabel("Max Colour");
178 maxColour.addActionListener(this);
180 thresholdIsMin.addItemListener(this);
182 ok.addActionListener(this);
184 cancel.setLabel("Cancel");
185 cancel.addActionListener(this);
187 defColours.setLabel("Defaults");
188 defColours.addActionListener(this);
190 annotations.addItemListener(this);
192 thresholdValue.addActionListener(this);
193 slider.setBackground(Color.white);
194 slider.setPreferredSize(new Dimension(193,21));
195 slider.setEnabled(false);
196 thresholdValue.setPreferredSize(new Dimension(79,22));
197 thresholdValue.setEnabled(false);
198 thresholdValue.setColumns(5);
199 currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
200 currentColours.setLabel("Use Original Colours");
201 currentColours.addItemListener(this);
203 thresholdIsMin.setBackground(Color.white);
204 thresholdIsMin.setLabel("Threshold is min/max");
206 this.setLayout(borderLayout1);
208 jPanel1.setBackground(Color.white);
210 jPanel2.setLayout(new FlowLayout());
211 jPanel2.setBackground(Color.white);
212 threshold.addItemListener(this);
213 jPanel3.setLayout(new FlowLayout());
214 jPanel3.setBackground(Color.white);
215 Panel jPanel4 = new Panel();
216 jPanel4.setLayout(new BorderLayout());
217 jPanel4.setBackground(Color.white);
223 jPanel2.add(annotations);
224 jPanel2.add(currentColours);
225 jPanel2.add(minColour);
226 jPanel2.add(maxColour);
229 jPanel4.add(thresholdIsMin, BorderLayout.WEST);
230 jPanel4.add(slider, BorderLayout.CENTER);
231 jPanel4.add(thresholdValue, BorderLayout.EAST);
233 Panel jPanel34 = new Panel();
234 jPanel34.setLayout(new BorderLayout());
235 jPanel34.setBackground(Color.white);
236 jPanel34.add(jPanel2, BorderLayout.NORTH);
237 jPanel34.add(threshold, BorderLayout.WEST);
238 jPanel3.add(defColours);
239 jPanel34.add(jPanel3, BorderLayout.EAST);
240 jPanel34.add(jPanel4, BorderLayout.SOUTH);
242 this.add(jPanel34, java.awt.BorderLayout.CENTER);
243 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
247 Choice annotations = new Choice();
249 Button minColour = new Button();
251 Button maxColour = new Button();
253 Button ok = new Button();
255 Button cancel = new Button();
257 Button defColours = new Button();
259 Panel jPanel1 = new Panel();
261 Panel jPanel2 = new Panel();
263 Choice threshold = new Choice();
265 FlowLayout flowLayout1 = new FlowLayout();
267 Panel jPanel3 = new Panel();
269 Scrollbar slider = new Scrollbar(Scrollbar.HORIZONTAL);
271 TextField thresholdValue = new TextField(20);
273 Checkbox currentColours = new Checkbox();
275 BorderLayout borderLayout1 = new BorderLayout();
277 Checkbox thresholdIsMin = new Checkbox();
279 public void actionPerformed(ActionEvent evt)
281 if (evt.getSource() == thresholdValue)
285 float f = new Float(thresholdValue.getText()).floatValue();
286 slider.setValue((int) (f * 1000));
287 adjustmentValueChanged(null);
288 } catch (NumberFormatException ex)
292 else if (evt.getSource() == minColour)
294 minColour_actionPerformed(null);
296 else if (evt.getSource() == maxColour)
298 maxColour_actionPerformed(null);
300 else if (evt.getSource() == defColours)
302 defColour_actionPerformed();
304 else if (evt.getSource() == ok)
307 frame.setVisible(false);
309 else if (evt.getSource() == cancel)
312 ap.paintAlignment(true);
313 frame.setVisible(false);
322 public void itemStateChanged(ItemEvent evt)
324 if (evt.getSource() == currentColours)
326 if (currentColours.getState())
331 maxColour.setEnabled(!currentColours.getState());
332 minColour.setEnabled(!currentColours.getState());
339 public void adjustmentValueChanged(AdjustmentEvent evt)
343 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
344 if (currentColours.getState()
345 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
350 currentAnnotation.threshold.value = (float) slider.getValue() / 1000f;
351 ap.paintAlignment(false);
355 public void minColour_actionPerformed(Color newCol)
359 minColour.setBackground(newCol);
365 new UserDefinedColours(this, "Min Colour", minColour.getBackground());
370 public void maxColour_actionPerformed(Color newCol)
374 maxColour.setBackground(newCol);
380 new UserDefinedColours(this, "Max Colour", maxColour.getBackground());
383 public void defColour_actionPerformed()
392 // Check if combobox is still adjusting
398 currentAnnotation = av.getAlignment().getAlignmentAnnotation()[annotations
399 .getSelectedIndex()];
401 int aboveThreshold = -1;
402 if (threshold.getSelectedItem().equals("Above Threshold"))
404 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
406 else if (threshold.getSelectedItem().equals("Below Threshold"))
408 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
411 slider.setEnabled(true);
412 thresholdValue.setEnabled(true);
413 thresholdIsMin.setEnabled(true);
415 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
417 slider.setEnabled(false);
418 thresholdValue.setEnabled(false);
419 thresholdIsMin.setEnabled(false);
420 thresholdValue.setText("");
422 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
423 && currentAnnotation.threshold == null)
426 .setThreshold(new jalview.datamodel.GraphLine(
427 (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
428 "Threshold", Color.black));
431 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
435 slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
436 slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
437 slider.setValue((int) (currentAnnotation.threshold.value * 1000));
438 thresholdValue.setText(currentAnnotation.threshold.value + "");
439 slider.setEnabled(true);
440 thresholdValue.setEnabled(true);
444 AnnotationColourGradient acg = null;
445 if (currentColours.getState())
447 acg = new AnnotationColourGradient(currentAnnotation,
448 av.getGlobalColourScheme(), aboveThreshold);
452 acg = new AnnotationColourGradient(currentAnnotation,
453 minColour.getBackground(), maxColour.getBackground(),
457 if (currentAnnotation.graphMin == 0f
458 && currentAnnotation.graphMax == 0f)
460 acg.predefinedColours = true;
463 acg.thresholdIsMinMax = thresholdIsMin.getState();
465 av.setGlobalColourScheme(acg);
467 if (av.getAlignment().getGroups() != null)
469 Vector allGroups = ap.av.getAlignment().getGroups();
471 for (int g = 0; g < allGroups.size(); g++)
473 sg = (SequenceGroup) allGroups.elementAt(g);
480 if (currentColours.getState())
482 sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
487 sg.cs = new AnnotationColourGradient(currentAnnotation,
488 minColour.getBackground(), maxColour.getBackground(),
495 // update colours in linked windows
496 ap.paintAlignment(true);
501 av.setGlobalColourScheme(oldcs);
502 if (av.getAlignment().getGroups() != null)
504 Vector allGroups = ap.av.getAlignment().getGroups();
506 for (int g = 0; g < allGroups.size(); g++)
508 sg = (SequenceGroup) allGroups.elementAt(g);
509 Object cs = oldgroupColours.get(sg);
510 if (cs instanceof ColourSchemeI)
512 sg.cs = (ColourSchemeI) cs;
516 // probably the "null" string we set it to if it was null originally.
521 ap.paintAlignment(true);
525 public void mouseClicked(MouseEvent evt)
529 public void mousePressed(MouseEvent evt)
533 public void mouseReleased(MouseEvent evt)
535 ap.paintAlignment(true);
538 public void mouseEntered(MouseEvent evt)
542 public void mouseExited(MouseEvent evt)