2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, 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 public class AnnotationColourChooser extends Panel implements
30 ActionListener, AdjustmentListener, ItemListener, MouseListener
40 Hashtable oldgroupColours;
42 jalview.datamodel.AlignmentAnnotation currentAnnotation;
44 boolean adjusting = false;
46 public AnnotationColourChooser(AlignViewport av, AlignmentPanel ap)
51 } catch (Exception ex)
55 oldcs = av.getGlobalColourScheme();
56 if (av.alignment.getGroups() != null)
58 oldgroupColours = new Hashtable();
59 Vector allGroups = ap.av.alignment.getGroups();
61 for (int g = 0; g < allGroups.size(); g++)
63 sg = (SequenceGroup) allGroups.elementAt(g);
66 oldgroupColours.put(sg, sg.cs);
70 oldgroupColours.put(sg, "null");
77 slider.addAdjustmentListener(this);
78 slider.addMouseListener(this);
80 if (av.alignment.getAlignmentAnnotation() == null)
85 minColour.setBackground(av.applet.getDefaultColourParameter("ANNOTATIONCOLOUR_MIN",Color.orange));
86 maxColour.setBackground(av.applet.getDefaultColourParameter("ANNOTATIONCOLOUR_MAX",Color.red));
88 if (oldcs instanceof AnnotationColourGradient)
90 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
91 currentColours.setState(acg.predefinedColours);
92 if (!acg.predefinedColours) {
93 minColour.setBackground(acg.getMinColour());
94 maxColour.setBackground(acg.getMaxColour());
100 Vector list = new Vector();
102 for (int i = 0; i < av.alignment.getAlignmentAnnotation().length; i++)
104 String label = av.alignment.getAlignmentAnnotation()[i].label;
105 if (!list.contains(label))
106 list.addElement(label);
108 list.addElement(label + "_" + (index++));
111 for (int i = 0; i < list.size(); i++)
113 annotations.addItem(list.elementAt(i).toString());
116 threshold.addItem("No Threshold");
117 threshold.addItem("Above Threshold");
118 threshold.addItem("Below Threshold");
120 if (oldcs instanceof AnnotationColourGradient)
122 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
123 annotations.select(acg.getAnnotation());
124 switch (acg.getAboveThreshold()) {
125 case AnnotationColourGradient.NO_THRESHOLD:
126 threshold.select("No Threshold");
128 case AnnotationColourGradient.ABOVE_THRESHOLD:
129 threshold.select("Above Threshold");
131 case AnnotationColourGradient.BELOW_THRESHOLD:
132 threshold.select("Below Threshold");
135 throw new Error("Implementation error: don't know about threshold setting for current AnnotationColourGradient.");
137 thresholdIsMin.setState(acg.thresholdIsMinMax);
138 thresholdValue.setText(""+acg.getAnnotationThreshold());
147 jalview.bin.JalviewLite.addFrame(frame, "Colour by Annotation", 480,
152 public AnnotationColourChooser()
157 } catch (Exception ex)
159 ex.printStackTrace();
163 private void jbInit() throws Exception
165 minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
166 minColour.setLabel("Min Colour");
167 minColour.addActionListener(this);
169 maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
170 maxColour.setLabel("Max Colour");
171 maxColour.addActionListener(this);
173 thresholdIsMin.addItemListener(this);
175 ok.addActionListener(this);
177 cancel.setLabel("Cancel");
178 cancel.addActionListener(this);
180 this.setLayout(borderLayout1);
181 jPanel2.setLayout(flowLayout1);
182 annotations.addItemListener(this);
184 jPanel1.setBackground(Color.white);
185 jPanel2.setBackground(Color.white);
186 threshold.addItemListener(this);
187 jPanel3.setLayout(null);
188 thresholdValue.addActionListener(this);
190 slider.setBackground(Color.white);
191 slider.setEnabled(false);
192 slider.setBounds(new Rectangle(153, 3, 93, 21));
193 thresholdValue.setEnabled(false);
194 thresholdValue.setBounds(new Rectangle(248, 2, 79, 22));
195 thresholdValue.setColumns(5);
196 jPanel3.setBackground(Color.white);
197 currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
198 currentColours.setLabel("Use Original Colours");
199 currentColours.addItemListener(this);
201 threshold.setBounds(new Rectangle(11, 3, 139, 22));
202 thresholdIsMin.setBackground(Color.white);
203 thresholdIsMin.setLabel("Threshold is min/max");
204 thresholdIsMin.setBounds(new Rectangle(328, 3, 135, 23));
207 jPanel2.add(annotations);
208 jPanel2.add(currentColours);
209 jPanel2.add(minColour);
210 jPanel2.add(maxColour);
211 jPanel3.add(threshold);
213 jPanel3.add(thresholdValue);
214 jPanel3.add(thresholdIsMin);
215 this.add(jPanel2, java.awt.BorderLayout.NORTH);
216 this.add(jPanel3, java.awt.BorderLayout.CENTER);
217 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
220 Choice annotations = new Choice();
222 Button minColour = new Button();
224 Button maxColour = new Button();
226 Button ok = new Button();
228 Button cancel = new Button();
230 Panel jPanel1 = new Panel();
232 Panel jPanel2 = new Panel();
234 Choice threshold = new Choice();
236 FlowLayout flowLayout1 = new FlowLayout();
238 Panel jPanel3 = new Panel();
240 Scrollbar slider = new Scrollbar(Scrollbar.HORIZONTAL);
242 TextField thresholdValue = new TextField(20);
244 Checkbox currentColours = new Checkbox();
246 BorderLayout borderLayout1 = new BorderLayout();
248 Checkbox thresholdIsMin = new Checkbox();
250 public void actionPerformed(ActionEvent evt)
252 if (evt.getSource() == thresholdValue)
256 float f = new Float(thresholdValue.getText()).floatValue();
257 slider.setValue((int) (f * 1000));
258 adjustmentValueChanged(null);
259 } catch (NumberFormatException ex)
263 else if (evt.getSource() == minColour)
265 minColour_actionPerformed(null);
267 else if (evt.getSource() == maxColour)
269 maxColour_actionPerformed(null);
272 else if (evt.getSource() == ok)
275 frame.setVisible(false);
277 else if (evt.getSource() == cancel)
280 ap.paintAlignment(true);
281 frame.setVisible(false);
290 public void itemStateChanged(ItemEvent evt)
292 if (evt.getSource() == currentColours)
294 if (currentColours.getState())
299 maxColour.setEnabled(!currentColours.getState());
300 minColour.setEnabled(!currentColours.getState());
307 public void adjustmentValueChanged(AdjustmentEvent evt)
311 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
312 if (currentColours.getState()
313 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
318 currentAnnotation.threshold.value = (float) slider.getValue() / 1000f;
319 ap.paintAlignment(false);
323 public void minColour_actionPerformed(Color newCol)
327 minColour.setBackground(newCol);
333 new UserDefinedColours(this, "Min Colour", minColour.getBackground());
338 public void maxColour_actionPerformed(Color newCol)
342 maxColour.setBackground(newCol);
348 new UserDefinedColours(this, "Max Colour", maxColour.getBackground());
354 // Check if combobox is still adjusting
360 currentAnnotation = av.alignment.getAlignmentAnnotation()[annotations
361 .getSelectedIndex()];
363 int aboveThreshold = -1;
364 if (threshold.getSelectedItem().equals("Above Threshold"))
366 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
368 else if (threshold.getSelectedItem().equals("Below Threshold"))
370 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
373 slider.setEnabled(true);
374 thresholdValue.setEnabled(true);
376 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
378 slider.setEnabled(false);
379 thresholdValue.setEnabled(false);
380 thresholdValue.setText("");
382 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
383 && currentAnnotation.threshold == null)
386 .setThreshold(new jalview.datamodel.GraphLine(
387 (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
388 "Threshold", Color.black));
391 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
395 slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
396 slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
397 slider.setValue((int) (currentAnnotation.threshold.value * 1000));
398 thresholdValue.setText(currentAnnotation.threshold.value + "");
399 slider.setEnabled(true);
400 thresholdValue.setEnabled(true);
404 AnnotationColourGradient acg = null;
405 if (currentColours.getState())
407 acg = new AnnotationColourGradient(currentAnnotation,
408 av.getGlobalColourScheme(), aboveThreshold);
412 acg = new AnnotationColourGradient(currentAnnotation,
413 minColour.getBackground(), maxColour.getBackground(),
417 if (currentAnnotation.graphMin == 0f
418 && currentAnnotation.graphMax == 0f)
420 acg.predefinedColours = true;
423 acg.thresholdIsMinMax = thresholdIsMin.getState();
425 av.setGlobalColourScheme(acg);
427 if (av.alignment.getGroups() != null)
429 Vector allGroups = ap.av.alignment.getGroups();
431 for (int g = 0; g < allGroups.size(); g++)
433 sg = (SequenceGroup) allGroups.elementAt(g);
440 if (currentColours.getState())
442 sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
447 sg.cs = new AnnotationColourGradient(currentAnnotation,
448 minColour.getBackground(), maxColour.getBackground(),
455 // update colours in linked windows
456 ap.paintAlignment(true);
461 av.setGlobalColourScheme(oldcs);
462 if (av.alignment.getGroups() != null)
464 Vector allGroups = ap.av.alignment.getGroups();
466 for (int g = 0; g < allGroups.size(); g++)
468 sg = (SequenceGroup) allGroups.elementAt(g);
469 Object cs = oldgroupColours.get(sg);
470 if (cs instanceof ColourSchemeI)
472 sg.cs = (ColourSchemeI) cs;
476 // probably the "null" string we set it to if it was null originally.
481 ap.paintAlignment(true);
485 public void mouseClicked(MouseEvent evt)
489 public void mousePressed(MouseEvent evt)
493 public void mouseReleased(MouseEvent evt)
495 ap.paintAlignment(true);
498 public void mouseEntered(MouseEvent evt)
502 public void mouseExited(MouseEvent evt)