2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.appletgui;
23 import jalview.datamodel.SequenceGroup;
24 import jalview.schemes.AnnotationColourGradient;
25 import jalview.schemes.ColourSchemeI;
26 import jalview.util.MessageManager;
28 import java.awt.BorderLayout;
29 import java.awt.Button;
30 import java.awt.Checkbox;
31 import java.awt.Choice;
32 import java.awt.Color;
33 import java.awt.Dimension;
34 import java.awt.FlowLayout;
36 import java.awt.Frame;
37 import java.awt.Panel;
38 import java.awt.Scrollbar;
39 import java.awt.TextField;
40 import java.awt.event.ActionEvent;
41 import java.awt.event.ActionListener;
42 import java.awt.event.AdjustmentEvent;
43 import java.awt.event.AdjustmentListener;
44 import java.awt.event.ItemEvent;
45 import java.awt.event.ItemListener;
46 import java.awt.event.MouseEvent;
47 import java.awt.event.MouseListener;
48 import java.util.Hashtable;
49 import java.util.Vector;
51 public class AnnotationColourChooser extends Panel implements
52 ActionListener, AdjustmentListener, ItemListener, MouseListener
62 Hashtable oldgroupColours;
64 jalview.datamodel.AlignmentAnnotation currentAnnotation;
66 boolean adjusting = false;
68 public AnnotationColourChooser(AlignViewport av, AlignmentPanel ap)
73 } catch (Exception ex)
77 oldcs = av.getGlobalColourScheme();
78 if (av.getAlignment().getGroups() != null)
80 oldgroupColours = new Hashtable();
81 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
85 oldgroupColours.put(sg, sg.cs);
89 oldgroupColours.put(sg, "null");
96 slider.addAdjustmentListener(this);
97 slider.addMouseListener(this);
99 if (av.getAlignment().getAlignmentAnnotation() == null)
107 if (oldcs instanceof AnnotationColourGradient)
109 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
110 currentColours.setState(acg.isPredefinedColours()
111 || acg.getBaseColour() != null);
112 if (!acg.isPredefinedColours() && acg.getBaseColour() == null)
114 minColour.setBackground(acg.getMinColour());
115 maxColour.setBackground(acg.getMaxColour());
117 // seqAssociated.setState(acg.isSeqAssociated());
120 Vector list = new Vector();
122 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
124 String label = av.getAlignment().getAlignmentAnnotation()[i].label;
125 if (!list.contains(label))
127 list.addElement(label);
131 list.addElement(label + "_" + (index++));
135 for (int i = 0; i < list.size(); i++)
137 annotations.addItem(list.elementAt(i).toString());
140 threshold.addItem(MessageManager
141 .getString("label.threshold_feature_no_thereshold"));
142 threshold.addItem(MessageManager
143 .getString("label.threshold_feature_above_thereshold"));
144 threshold.addItem(MessageManager
145 .getString("label.threshold_feature_below_thereshold"));
147 if (oldcs instanceof AnnotationColourGradient)
149 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
150 annotations.select(acg.getAnnotation());
151 switch (acg.getAboveThreshold())
153 case AnnotationColourGradient.NO_THRESHOLD:
156 case AnnotationColourGradient.ABOVE_THRESHOLD:
159 case AnnotationColourGradient.BELOW_THRESHOLD:
165 .getString("error.implementation_error_dont_know_thereshold_annotationcolourgradient"));
167 thresholdIsMin.setState(acg.thresholdIsMinMax);
168 thresholdValue.setText("" + acg.getAnnotationThreshold());
177 jalview.bin.JalviewLite.addFrame(frame,
178 MessageManager.getString("label.colour_by_annotation"), 560,
183 private void setDefaultMinMax()
185 minColour.setBackground(av.applet.getDefaultColourParameter(
186 "ANNOTATIONCOLOUR_MIN", Color.orange));
187 maxColour.setBackground(av.applet.getDefaultColourParameter(
188 "ANNOTATIONCOLOUR_MAX", Color.red));
192 public AnnotationColourChooser()
197 } catch (Exception ex)
199 ex.printStackTrace();
203 private void jbInit() throws Exception
205 minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
206 minColour.setLabel(MessageManager.getString("label.min_colour"));
207 minColour.addActionListener(this);
209 maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
210 maxColour.setLabel(MessageManager.getString("label.max_colour"));
211 maxColour.addActionListener(this);
213 thresholdIsMin.addItemListener(this);
214 ok.setLabel(MessageManager.getString("action.ok"));
215 ok.addActionListener(this);
217 cancel.setLabel(MessageManager.getString("action.cancel"));
218 cancel.addActionListener(this);
220 defColours.setLabel(MessageManager.getString("action.set_defaults"));
221 defColours.addActionListener(this);
223 annotations.addItemListener(this);
225 thresholdValue.addActionListener(this);
226 slider.setBackground(Color.white);
227 slider.setPreferredSize(new Dimension(193, 21));
228 slider.setEnabled(false);
229 thresholdValue.setPreferredSize(new Dimension(79, 22));
230 thresholdValue.setEnabled(false);
231 thresholdValue.setColumns(5);
232 currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
233 currentColours.setLabel(MessageManager
234 .getString("label.use_original_colours"));
235 currentColours.addItemListener(this);
237 thresholdIsMin.setBackground(Color.white);
238 thresholdIsMin.setLabel(MessageManager
239 .getString("label.threshold_minmax"));
241 this.setLayout(borderLayout1);
243 jPanel1.setBackground(Color.white);
245 jPanel2.setLayout(new FlowLayout());
246 jPanel2.setBackground(Color.white);
247 threshold.addItemListener(this);
248 jPanel3.setLayout(new FlowLayout());
249 jPanel3.setBackground(Color.white);
250 Panel jPanel4 = new Panel();
251 jPanel4.setLayout(new BorderLayout());
252 jPanel4.setBackground(Color.white);
257 jPanel2.add(annotations);
258 jPanel2.add(currentColours);
259 jPanel2.add(minColour);
260 jPanel2.add(maxColour);
262 jPanel4.add(thresholdIsMin, BorderLayout.WEST);
263 jPanel4.add(slider, BorderLayout.CENTER);
264 jPanel4.add(thresholdValue, BorderLayout.EAST);
266 Panel jPanel34 = new Panel();
267 jPanel34.setLayout(new BorderLayout());
268 jPanel34.setBackground(Color.white);
269 jPanel34.add(jPanel2, BorderLayout.NORTH);
270 jPanel34.add(threshold, BorderLayout.WEST);
271 jPanel3.add(defColours);
272 jPanel34.add(jPanel3, BorderLayout.EAST);
273 jPanel34.add(jPanel4, BorderLayout.SOUTH);
275 this.add(jPanel34, java.awt.BorderLayout.CENTER);
276 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
280 Choice annotations = new Choice();
282 Button minColour = new Button();
284 Button maxColour = new Button();
286 Button ok = new Button();
288 Button cancel = new Button();
290 Button defColours = new Button();
292 Panel jPanel1 = new Panel();
294 Panel jPanel2 = new Panel();
296 Choice threshold = new Choice();
298 FlowLayout flowLayout1 = new FlowLayout();
300 Panel jPanel3 = new Panel();
302 Scrollbar slider = new Scrollbar(Scrollbar.HORIZONTAL);
304 TextField thresholdValue = new TextField(20);
306 Checkbox currentColours = new Checkbox();
308 BorderLayout borderLayout1 = new BorderLayout();
310 Checkbox thresholdIsMin = new Checkbox();
312 public void actionPerformed(ActionEvent evt)
314 if (evt.getSource() == thresholdValue)
318 float f = new Float(thresholdValue.getText()).floatValue();
319 slider.setValue((int) (f * 1000));
320 adjustmentValueChanged(null);
321 } catch (NumberFormatException ex)
325 else if (evt.getSource() == minColour)
327 minColour_actionPerformed(null);
329 else if (evt.getSource() == maxColour)
331 maxColour_actionPerformed(null);
333 else if (evt.getSource() == defColours)
335 defColour_actionPerformed();
337 else if (evt.getSource() == ok)
340 frame.setVisible(false);
342 else if (evt.getSource() == cancel)
345 ap.paintAlignment(true);
346 frame.setVisible(false);
355 public void itemStateChanged(ItemEvent evt)
357 if (evt.getSource() == currentColours)
359 if (currentColours.getState())
364 maxColour.setEnabled(!currentColours.getState());
365 minColour.setEnabled(!currentColours.getState());
372 public void adjustmentValueChanged(AdjustmentEvent evt)
376 thresholdValue.setText((slider.getValue() / 1000f) + "");
377 if (currentColours.getState()
378 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
383 currentAnnotation.threshold.value = slider.getValue() / 1000f;
384 ap.paintAlignment(false);
388 public void minColour_actionPerformed(Color newCol)
392 minColour.setBackground(newCol);
398 new UserDefinedColours(this, "Min Colour", minColour.getBackground());
403 public void maxColour_actionPerformed(Color newCol)
407 maxColour.setBackground(newCol);
413 new UserDefinedColours(this, "Max Colour", maxColour.getBackground());
417 public void defColour_actionPerformed()
427 // Check if combobox is still adjusting
433 currentAnnotation = av.getAlignment().getAlignmentAnnotation()[annotations
434 .getSelectedIndex()];
436 int aboveThreshold = -1;
437 if (threshold.getSelectedIndex() == 1)
439 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
441 else if (threshold.getSelectedIndex() == 2)
443 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
446 slider.setEnabled(true);
447 thresholdValue.setEnabled(true);
448 thresholdIsMin.setEnabled(true);
450 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
452 slider.setEnabled(false);
453 thresholdValue.setEnabled(false);
454 thresholdIsMin.setEnabled(false);
455 thresholdValue.setText("");
457 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
458 && currentAnnotation.threshold == null)
461 .setThreshold(new jalview.datamodel.GraphLine(
462 (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
463 "Threshold", Color.black));
466 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
470 slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
471 slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
472 slider.setValue((int) (currentAnnotation.threshold.value * 1000));
473 thresholdValue.setText(currentAnnotation.threshold.value + "");
474 slider.setEnabled(true);
475 thresholdValue.setEnabled(true);
479 AnnotationColourGradient acg = null;
480 if (currentColours.getState())
482 acg = new AnnotationColourGradient(currentAnnotation,
483 av.getGlobalColourScheme(), aboveThreshold);
487 acg = new AnnotationColourGradient(currentAnnotation,
488 minColour.getBackground(), maxColour.getBackground(),
492 if (currentAnnotation.graphMin == 0f
493 && currentAnnotation.graphMax == 0f)
495 acg.setPredefinedColours(true);
498 acg.thresholdIsMinMax = thresholdIsMin.getState();
500 av.setGlobalColourScheme(acg);
502 // TODO: per group colour propagation not always desired
503 if (av.getAlignment().getGroups() != null)
505 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
513 if (currentColours.getState())
515 sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
520 sg.cs = new AnnotationColourGradient(currentAnnotation,
521 minColour.getBackground(), maxColour.getBackground(),
528 // update colours in linked windows
529 ap.alignmentChanged();
530 ap.paintAlignment(true);
535 av.setGlobalColourScheme(oldcs);
536 if (av.getAlignment().getGroups() != null)
538 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
540 Object cs = oldgroupColours.get(sg);
541 if (cs instanceof ColourSchemeI)
543 sg.cs = (ColourSchemeI) cs;
547 // probably the "null" string we set it to if it was null originally.
552 ap.paintAlignment(true);
556 public void mouseClicked(MouseEvent evt)
560 public void mousePressed(MouseEvent evt)
564 public void mouseReleased(MouseEvent evt)
566 ap.paintAlignment(true);
569 public void mouseEntered(MouseEvent evt)
573 public void mouseExited(MouseEvent evt)