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.*;
27 import jalview.util.MessageManager;
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.getAlignment().getGroups() != null)
58 oldgroupColours = new Hashtable();
59 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
63 oldgroupColours.put(sg, sg.cs);
67 oldgroupColours.put(sg, "null");
74 slider.addAdjustmentListener(this);
75 slider.addMouseListener(this);
77 if (av.getAlignment().getAlignmentAnnotation() == null)
84 if (oldcs instanceof AnnotationColourGradient)
86 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
87 currentColours.setState(acg.predefinedColours);
88 if (!acg.predefinedColours)
90 minColour.setBackground(acg.getMinColour());
91 maxColour.setBackground(acg.getMaxColour());
97 Vector list = new Vector();
99 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
101 String label = av.getAlignment().getAlignmentAnnotation()[i].label;
102 if (!list.contains(label))
103 list.addElement(label);
105 list.addElement(label + "_" + (index++));
108 for (int i = 0; i < list.size(); i++)
110 annotations.addItem(list.elementAt(i).toString());
113 threshold.addItem("No Threshold");
114 threshold.addItem("Above Threshold");
115 threshold.addItem("Below Threshold");
117 if (oldcs instanceof AnnotationColourGradient)
119 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
120 annotations.select(acg.getAnnotation());
121 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");
134 "Implementation error: don't know about threshold setting for current AnnotationColourGradient.");
136 thresholdIsMin.setState(acg.thresholdIsMinMax);
137 thresholdValue.setText("" + acg.getAnnotationThreshold());
146 jalview.bin.JalviewLite.addFrame(frame, MessageManager.getString("label.colour_by_annotation"), 560,
151 private void setDefaultMinMax()
153 minColour.setBackground(av.applet.getDefaultColourParameter(
154 "ANNOTATIONCOLOUR_MIN", Color.orange));
155 maxColour.setBackground(av.applet.getDefaultColourParameter(
156 "ANNOTATIONCOLOUR_MAX", Color.red));
160 public AnnotationColourChooser()
165 } catch (Exception ex)
167 ex.printStackTrace();
171 private void jbInit() throws Exception
173 minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
174 minColour.setLabel(MessageManager.getString("label.min_colour"));
175 minColour.addActionListener(this);
177 maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
178 maxColour.setLabel(MessageManager.getString("label.max_colour"));
179 maxColour.addActionListener(this);
181 thresholdIsMin.addItemListener(this);
182 ok.setLabel(MessageManager.getString("action.ok"));
183 ok.addActionListener(this);
185 cancel.setLabel(MessageManager.getString("action.cancel"));
186 cancel.addActionListener(this);
188 defColours.setLabel(MessageManager.getString("action.set_defaults"));
189 defColours.addActionListener(this);
191 annotations.addItemListener(this);
193 thresholdValue.addActionListener(this);
194 slider.setBackground(Color.white);
195 slider.setPreferredSize(new Dimension(193, 21));
196 slider.setEnabled(false);
197 thresholdValue.setPreferredSize(new Dimension(79, 22));
198 thresholdValue.setEnabled(false);
199 thresholdValue.setColumns(5);
200 currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
201 currentColours.setLabel(MessageManager.getString("label.use_original_colours"));
202 currentColours.addItemListener(this);
204 thresholdIsMin.setBackground(Color.white);
205 thresholdIsMin.setLabel(MessageManager.getString("label.threshold_minmax"));
207 this.setLayout(borderLayout1);
209 jPanel1.setBackground(Color.white);
211 jPanel2.setLayout(new FlowLayout());
212 jPanel2.setBackground(Color.white);
213 threshold.addItemListener(this);
214 jPanel3.setLayout(new FlowLayout());
215 jPanel3.setBackground(Color.white);
216 Panel jPanel4 = new Panel();
217 jPanel4.setLayout(new BorderLayout());
218 jPanel4.setBackground(Color.white);
223 jPanel2.add(annotations);
224 jPanel2.add(currentColours);
225 jPanel2.add(minColour);
226 jPanel2.add(maxColour);
228 jPanel4.add(thresholdIsMin, BorderLayout.WEST);
229 jPanel4.add(slider, BorderLayout.CENTER);
230 jPanel4.add(thresholdValue, BorderLayout.EAST);
232 Panel jPanel34 = new Panel();
233 jPanel34.setLayout(new BorderLayout());
234 jPanel34.setBackground(Color.white);
235 jPanel34.add(jPanel2, BorderLayout.NORTH);
236 jPanel34.add(threshold, BorderLayout.WEST);
237 jPanel3.add(defColours);
238 jPanel34.add(jPanel3, BorderLayout.EAST);
239 jPanel34.add(jPanel4, BorderLayout.SOUTH);
241 this.add(jPanel34, java.awt.BorderLayout.CENTER);
242 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
246 Choice annotations = new Choice();
248 Button minColour = new Button();
250 Button maxColour = new Button();
252 Button ok = new Button();
254 Button cancel = new Button();
256 Button defColours = new Button();
258 Panel jPanel1 = new Panel();
260 Panel jPanel2 = new Panel();
262 Choice threshold = new Choice();
264 FlowLayout flowLayout1 = new FlowLayout();
266 Panel jPanel3 = new Panel();
268 Scrollbar slider = new Scrollbar(Scrollbar.HORIZONTAL);
270 TextField thresholdValue = new TextField(20);
272 Checkbox currentColours = new Checkbox();
274 BorderLayout borderLayout1 = new BorderLayout();
276 Checkbox thresholdIsMin = new Checkbox();
278 public void actionPerformed(ActionEvent evt)
280 if (evt.getSource() == thresholdValue)
284 float f = new Float(thresholdValue.getText()).floatValue();
285 slider.setValue((int) (f * 1000));
286 adjustmentValueChanged(null);
287 } catch (NumberFormatException ex)
291 else if (evt.getSource() == minColour)
293 minColour_actionPerformed(null);
295 else if (evt.getSource() == maxColour)
297 maxColour_actionPerformed(null);
299 else if (evt.getSource() == defColours)
301 defColour_actionPerformed();
303 else if (evt.getSource() == ok)
306 frame.setVisible(false);
308 else if (evt.getSource() == cancel)
311 ap.paintAlignment(true);
312 frame.setVisible(false);
321 public void itemStateChanged(ItemEvent evt)
323 if (evt.getSource() == currentColours)
325 if (currentColours.getState())
330 maxColour.setEnabled(!currentColours.getState());
331 minColour.setEnabled(!currentColours.getState());
338 public void adjustmentValueChanged(AdjustmentEvent evt)
342 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
343 if (currentColours.getState()
344 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
349 currentAnnotation.threshold.value = (float) slider.getValue() / 1000f;
350 ap.paintAlignment(false);
354 public void minColour_actionPerformed(Color newCol)
358 minColour.setBackground(newCol);
364 new UserDefinedColours(this, "Min Colour", minColour.getBackground());
369 public void maxColour_actionPerformed(Color newCol)
373 maxColour.setBackground(newCol);
379 new UserDefinedColours(this, "Max Colour", maxColour.getBackground());
383 public void defColour_actionPerformed()
393 // Check if combobox is still adjusting
399 currentAnnotation = av.getAlignment().getAlignmentAnnotation()[annotations
400 .getSelectedIndex()];
402 int aboveThreshold = -1;
403 if (threshold.getSelectedItem().equals("Above Threshold"))
405 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
407 else if (threshold.getSelectedItem().equals("Below Threshold"))
409 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
412 slider.setEnabled(true);
413 thresholdValue.setEnabled(true);
414 thresholdIsMin.setEnabled(true);
416 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
418 slider.setEnabled(false);
419 thresholdValue.setEnabled(false);
420 thresholdIsMin.setEnabled(false);
421 thresholdValue.setText("");
423 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
424 && currentAnnotation.threshold == null)
427 .setThreshold(new jalview.datamodel.GraphLine(
428 (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
429 "Threshold", Color.black));
432 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
436 slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
437 slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
438 slider.setValue((int) (currentAnnotation.threshold.value * 1000));
439 thresholdValue.setText(currentAnnotation.threshold.value + "");
440 slider.setEnabled(true);
441 thresholdValue.setEnabled(true);
445 AnnotationColourGradient acg = null;
446 if (currentColours.getState())
448 acg = new AnnotationColourGradient(currentAnnotation,
449 av.getGlobalColourScheme(), aboveThreshold);
453 acg = new AnnotationColourGradient(currentAnnotation,
454 minColour.getBackground(), maxColour.getBackground(),
458 if (currentAnnotation.graphMin == 0f
459 && currentAnnotation.graphMax == 0f)
461 acg.predefinedColours = true;
464 acg.thresholdIsMinMax = thresholdIsMin.getState();
466 av.setGlobalColourScheme(acg);
468 if (av.getAlignment().getGroups() != null)
470 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
478 if (currentColours.getState())
480 sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
485 sg.cs = new AnnotationColourGradient(currentAnnotation,
486 minColour.getBackground(), maxColour.getBackground(),
493 // update colours in linked windows
494 ap.paintAlignment(true);
499 av.setGlobalColourScheme(oldcs);
500 if (av.getAlignment().getGroups() != null)
502 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
504 Object cs = oldgroupColours.get(sg);
505 if (cs instanceof ColourSchemeI)
507 sg.cs = (ColourSchemeI) cs;
511 // probably the "null" string we set it to if it was null originally.
516 ap.paintAlignment(true);
520 public void mouseClicked(MouseEvent evt)
524 public void mousePressed(MouseEvent evt)
528 public void mouseReleased(MouseEvent evt)
530 ap.paintAlignment(true);
533 public void mouseEntered(MouseEvent evt)
537 public void mouseExited(MouseEvent evt)