2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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.*;
28 public class AnnotationColourChooser extends Panel implements
29 ActionListener, AdjustmentListener, ItemListener, MouseListener
39 Hashtable oldgroupColours;
41 jalview.datamodel.AlignmentAnnotation currentAnnotation;
43 boolean adjusting = false;
45 public AnnotationColourChooser(AlignViewport av, AlignmentPanel ap)
50 } catch (Exception ex)
54 oldcs = av.getGlobalColourScheme();
55 if (av.getAlignment().getGroups() != null)
57 oldgroupColours = new Hashtable();
58 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
62 oldgroupColours.put(sg, sg.cs);
66 oldgroupColours.put(sg, "null");
73 slider.addAdjustmentListener(this);
74 slider.addMouseListener(this);
76 if (av.getAlignment().getAlignmentAnnotation() == null)
83 if (oldcs instanceof AnnotationColourGradient)
85 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
86 currentColours.setState(acg.predefinedColours);
87 if (!acg.predefinedColours)
89 minColour.setBackground(acg.getMinColour());
90 maxColour.setBackground(acg.getMaxColour());
96 Vector list = new Vector();
98 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
100 String label = av.getAlignment().getAlignmentAnnotation()[i].label;
101 if (!list.contains(label))
102 list.addElement(label);
104 list.addElement(label + "_" + (index++));
107 for (int i = 0; i < list.size(); i++)
109 annotations.addItem(list.elementAt(i).toString());
112 threshold.addItem("No Threshold");
113 threshold.addItem("Above Threshold");
114 threshold.addItem("Below Threshold");
116 if (oldcs instanceof AnnotationColourGradient)
118 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
119 annotations.select(acg.getAnnotation());
120 switch (acg.getAboveThreshold())
122 case AnnotationColourGradient.NO_THRESHOLD:
123 threshold.select("No Threshold");
125 case AnnotationColourGradient.ABOVE_THRESHOLD:
126 threshold.select("Above Threshold");
128 case AnnotationColourGradient.BELOW_THRESHOLD:
129 threshold.select("Below Threshold");
133 "Implementation error: don't know about threshold setting for current AnnotationColourGradient.");
135 thresholdIsMin.setState(acg.thresholdIsMinMax);
136 thresholdValue.setText("" + acg.getAnnotationThreshold());
145 jalview.bin.JalviewLite.addFrame(frame, "Colour by Annotation", 560,
150 private void setDefaultMinMax()
152 minColour.setBackground(av.applet.getDefaultColourParameter(
153 "ANNOTATIONCOLOUR_MIN", Color.orange));
154 maxColour.setBackground(av.applet.getDefaultColourParameter(
155 "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);
222 jPanel2.add(annotations);
223 jPanel2.add(currentColours);
224 jPanel2.add(minColour);
225 jPanel2.add(maxColour);
227 jPanel4.add(thresholdIsMin, BorderLayout.WEST);
228 jPanel4.add(slider, BorderLayout.CENTER);
229 jPanel4.add(thresholdValue, BorderLayout.EAST);
231 Panel jPanel34 = new Panel();
232 jPanel34.setLayout(new BorderLayout());
233 jPanel34.setBackground(Color.white);
234 jPanel34.add(jPanel2, BorderLayout.NORTH);
235 jPanel34.add(threshold, BorderLayout.WEST);
236 jPanel3.add(defColours);
237 jPanel34.add(jPanel3, BorderLayout.EAST);
238 jPanel34.add(jPanel4, BorderLayout.SOUTH);
240 this.add(jPanel34, java.awt.BorderLayout.CENTER);
241 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
245 Choice annotations = new Choice();
247 Button minColour = new Button();
249 Button maxColour = new Button();
251 Button ok = new Button();
253 Button cancel = new Button();
255 Button defColours = new Button();
257 Panel jPanel1 = new Panel();
259 Panel jPanel2 = new Panel();
261 Choice threshold = new Choice();
263 FlowLayout flowLayout1 = new FlowLayout();
265 Panel jPanel3 = new Panel();
267 Scrollbar slider = new Scrollbar(Scrollbar.HORIZONTAL);
269 TextField thresholdValue = new TextField(20);
271 Checkbox currentColours = new Checkbox();
273 BorderLayout borderLayout1 = new BorderLayout();
275 Checkbox thresholdIsMin = new Checkbox();
277 public void actionPerformed(ActionEvent evt)
279 if (evt.getSource() == thresholdValue)
283 float f = new Float(thresholdValue.getText()).floatValue();
284 slider.setValue((int) (f * 1000));
285 adjustmentValueChanged(null);
286 } catch (NumberFormatException ex)
290 else if (evt.getSource() == minColour)
292 minColour_actionPerformed(null);
294 else if (evt.getSource() == maxColour)
296 maxColour_actionPerformed(null);
298 else if (evt.getSource() == defColours)
300 defColour_actionPerformed();
302 else if (evt.getSource() == ok)
305 frame.setVisible(false);
307 else if (evt.getSource() == cancel)
310 ap.paintAlignment(true);
311 frame.setVisible(false);
320 public void itemStateChanged(ItemEvent evt)
322 if (evt.getSource() == currentColours)
324 if (currentColours.getState())
329 maxColour.setEnabled(!currentColours.getState());
330 minColour.setEnabled(!currentColours.getState());
337 public void adjustmentValueChanged(AdjustmentEvent evt)
341 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
342 if (currentColours.getState()
343 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
348 currentAnnotation.threshold.value = (float) slider.getValue() / 1000f;
349 ap.paintAlignment(false);
353 public void minColour_actionPerformed(Color newCol)
357 minColour.setBackground(newCol);
363 new UserDefinedColours(this, "Min Colour", minColour.getBackground());
368 public void maxColour_actionPerformed(Color newCol)
372 maxColour.setBackground(newCol);
378 new UserDefinedColours(this, "Max Colour", maxColour.getBackground());
382 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 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
477 if (currentColours.getState())
479 sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
484 sg.cs = new AnnotationColourGradient(currentAnnotation,
485 minColour.getBackground(), maxColour.getBackground(),
492 // update colours in linked windows
493 ap.paintAlignment(true);
498 av.setGlobalColourScheme(oldcs);
499 if (av.getAlignment().getGroups() != null)
501 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
503 Object cs = oldgroupColours.get(sg);
504 if (cs instanceof ColourSchemeI)
506 sg.cs = (ColourSchemeI) cs;
510 // probably the "null" string we set it to if it was null originally.
515 ap.paintAlignment(true);
519 public void mouseClicked(MouseEvent evt)
523 public void mousePressed(MouseEvent evt)
527 public void mouseReleased(MouseEvent evt)
529 ap.paintAlignment(true);
532 public void mouseEntered(MouseEvent evt)
536 public void mouseExited(MouseEvent evt)