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 for (SequenceGroup sg: ap.av.getAlignment().getGroups())
65 oldgroupColours.put(sg, sg.cs);
69 oldgroupColours.put(sg, "null");
76 slider.addAdjustmentListener(this);
77 slider.addMouseListener(this);
79 if (av.getAlignment().getAlignmentAnnotation() == null)
86 if (oldcs instanceof AnnotationColourGradient)
88 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
89 currentColours.setState(acg.predefinedColours);
90 if (!acg.predefinedColours) {
91 minColour.setBackground(acg.getMinColour());
92 maxColour.setBackground(acg.getMaxColour());
98 Vector list = new Vector();
100 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
102 String label = av.getAlignment().getAlignmentAnnotation()[i].label;
103 if (!list.contains(label))
104 list.addElement(label);
106 list.addElement(label + "_" + (index++));
109 for (int i = 0; i < list.size(); i++)
111 annotations.addItem(list.elementAt(i).toString());
114 threshold.addItem("No Threshold");
115 threshold.addItem("Above Threshold");
116 threshold.addItem("Below Threshold");
118 if (oldcs instanceof AnnotationColourGradient)
120 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
121 annotations.select(acg.getAnnotation());
122 switch (acg.getAboveThreshold()) {
123 case AnnotationColourGradient.NO_THRESHOLD:
124 threshold.select("No Threshold");
126 case AnnotationColourGradient.ABOVE_THRESHOLD:
127 threshold.select("Above Threshold");
129 case AnnotationColourGradient.BELOW_THRESHOLD:
130 threshold.select("Below Threshold");
133 throw new Error("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("ANNOTATIONCOLOUR_MIN",Color.orange));
153 maxColour.setBackground(av.applet.getDefaultColourParameter("ANNOTATIONCOLOUR_MAX",Color.red));
156 public AnnotationColourChooser()
161 } catch (Exception ex)
163 ex.printStackTrace();
167 private void jbInit() throws Exception
169 minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
170 minColour.setLabel("Min Colour");
171 minColour.addActionListener(this);
173 maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
174 maxColour.setLabel("Max Colour");
175 maxColour.addActionListener(this);
177 thresholdIsMin.addItemListener(this);
179 ok.addActionListener(this);
181 cancel.setLabel("Cancel");
182 cancel.addActionListener(this);
184 defColours.setLabel("Defaults");
185 defColours.addActionListener(this);
187 annotations.addItemListener(this);
189 thresholdValue.addActionListener(this);
190 slider.setBackground(Color.white);
191 slider.setPreferredSize(new Dimension(193,21));
192 slider.setEnabled(false);
193 thresholdValue.setPreferredSize(new Dimension(79,22));
194 thresholdValue.setEnabled(false);
195 thresholdValue.setColumns(5);
196 currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
197 currentColours.setLabel("Use Original Colours");
198 currentColours.addItemListener(this);
200 thresholdIsMin.setBackground(Color.white);
201 thresholdIsMin.setLabel("Threshold is min/max");
203 this.setLayout(borderLayout1);
205 jPanel1.setBackground(Color.white);
207 jPanel2.setLayout(new FlowLayout());
208 jPanel2.setBackground(Color.white);
209 threshold.addItemListener(this);
210 jPanel3.setLayout(new FlowLayout());
211 jPanel3.setBackground(Color.white);
212 Panel jPanel4 = new Panel();
213 jPanel4.setLayout(new BorderLayout());
214 jPanel4.setBackground(Color.white);
220 jPanel2.add(annotations);
221 jPanel2.add(currentColours);
222 jPanel2.add(minColour);
223 jPanel2.add(maxColour);
226 jPanel4.add(thresholdIsMin, BorderLayout.WEST);
227 jPanel4.add(slider, BorderLayout.CENTER);
228 jPanel4.add(thresholdValue, BorderLayout.EAST);
230 Panel jPanel34 = new Panel();
231 jPanel34.setLayout(new BorderLayout());
232 jPanel34.setBackground(Color.white);
233 jPanel34.add(jPanel2, BorderLayout.NORTH);
234 jPanel34.add(threshold, BorderLayout.WEST);
235 jPanel3.add(defColours);
236 jPanel34.add(jPanel3, BorderLayout.EAST);
237 jPanel34.add(jPanel4, BorderLayout.SOUTH);
239 this.add(jPanel34, java.awt.BorderLayout.CENTER);
240 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
244 Choice annotations = new Choice();
246 Button minColour = new Button();
248 Button maxColour = new Button();
250 Button ok = new Button();
252 Button cancel = new Button();
254 Button defColours = new Button();
256 Panel jPanel1 = new Panel();
258 Panel jPanel2 = new Panel();
260 Choice threshold = new Choice();
262 FlowLayout flowLayout1 = new FlowLayout();
264 Panel jPanel3 = new Panel();
266 Scrollbar slider = new Scrollbar(Scrollbar.HORIZONTAL);
268 TextField thresholdValue = new TextField(20);
270 Checkbox currentColours = new Checkbox();
272 BorderLayout borderLayout1 = new BorderLayout();
274 Checkbox thresholdIsMin = new Checkbox();
276 public void actionPerformed(ActionEvent evt)
278 if (evt.getSource() == thresholdValue)
282 float f = new Float(thresholdValue.getText()).floatValue();
283 slider.setValue((int) (f * 1000));
284 adjustmentValueChanged(null);
285 } catch (NumberFormatException ex)
289 else if (evt.getSource() == minColour)
291 minColour_actionPerformed(null);
293 else if (evt.getSource() == maxColour)
295 maxColour_actionPerformed(null);
297 else if (evt.getSource() == defColours)
299 defColour_actionPerformed();
301 else if (evt.getSource() == ok)
304 frame.setVisible(false);
306 else if (evt.getSource() == cancel)
309 ap.paintAlignment(true);
310 frame.setVisible(false);
319 public void itemStateChanged(ItemEvent evt)
321 if (evt.getSource() == currentColours)
323 if (currentColours.getState())
328 maxColour.setEnabled(!currentColours.getState());
329 minColour.setEnabled(!currentColours.getState());
336 public void adjustmentValueChanged(AdjustmentEvent evt)
340 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
341 if (currentColours.getState()
342 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
347 currentAnnotation.threshold.value = (float) slider.getValue() / 1000f;
348 ap.paintAlignment(false);
352 public void minColour_actionPerformed(Color newCol)
356 minColour.setBackground(newCol);
362 new UserDefinedColours(this, "Min Colour", minColour.getBackground());
367 public void maxColour_actionPerformed(Color newCol)
371 maxColour.setBackground(newCol);
377 new UserDefinedColours(this, "Max Colour", maxColour.getBackground());
380 public void defColour_actionPerformed()
389 // Check if combobox is still adjusting
395 currentAnnotation = av.getAlignment().getAlignmentAnnotation()[annotations
396 .getSelectedIndex()];
398 int aboveThreshold = -1;
399 if (threshold.getSelectedItem().equals("Above Threshold"))
401 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
403 else if (threshold.getSelectedItem().equals("Below Threshold"))
405 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
408 slider.setEnabled(true);
409 thresholdValue.setEnabled(true);
410 thresholdIsMin.setEnabled(true);
412 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
414 slider.setEnabled(false);
415 thresholdValue.setEnabled(false);
416 thresholdIsMin.setEnabled(false);
417 thresholdValue.setText("");
419 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
420 && currentAnnotation.threshold == null)
423 .setThreshold(new jalview.datamodel.GraphLine(
424 (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
425 "Threshold", Color.black));
428 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
432 slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
433 slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
434 slider.setValue((int) (currentAnnotation.threshold.value * 1000));
435 thresholdValue.setText(currentAnnotation.threshold.value + "");
436 slider.setEnabled(true);
437 thresholdValue.setEnabled(true);
441 AnnotationColourGradient acg = null;
442 if (currentColours.getState())
444 acg = new AnnotationColourGradient(currentAnnotation,
445 av.getGlobalColourScheme(), aboveThreshold);
449 acg = new AnnotationColourGradient(currentAnnotation,
450 minColour.getBackground(), maxColour.getBackground(),
454 if (currentAnnotation.graphMin == 0f
455 && currentAnnotation.graphMax == 0f)
457 acg.predefinedColours = true;
460 acg.thresholdIsMinMax = thresholdIsMin.getState();
462 av.setGlobalColourScheme(acg);
464 if (av.getAlignment().getGroups() != null)
466 for (SequenceGroup sg:ap.av.getAlignment().getGroups())
474 if (currentColours.getState())
476 sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
481 sg.cs = new AnnotationColourGradient(currentAnnotation,
482 minColour.getBackground(), maxColour.getBackground(),
489 // update colours in linked windows
490 ap.paintAlignment(true);
495 av.setGlobalColourScheme(oldcs);
496 if (av.getAlignment().getGroups() != null)
498 for (SequenceGroup sg:ap.av.getAlignment().getGroups())
500 Object cs = oldgroupColours.get(sg);
501 if (cs instanceof ColourSchemeI)
503 sg.cs = (ColourSchemeI) cs;
507 // probably the "null" string we set it to if it was null originally.
512 ap.paintAlignment(true);
516 public void mouseClicked(MouseEvent evt)
520 public void mousePressed(MouseEvent evt)
524 public void mouseReleased(MouseEvent evt)
526 ap.paintAlignment(true);
529 public void mouseEntered(MouseEvent evt)
533 public void mouseExited(MouseEvent evt)