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.bin.JalviewLite;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.GraphLine;
26 import jalview.datamodel.SequenceGroup;
27 import jalview.schemes.AnnotationColourGradient;
28 import jalview.schemes.ColourSchemeI;
29 import jalview.util.MessageManager;
31 import java.awt.BorderLayout;
32 import awt2swing.Button;
33 import awt2swing.Checkbox;
34 import awt2swing.Choice;
35 import java.awt.Color;
36 import java.awt.Dimension;
37 import java.awt.FlowLayout;
39 import awt2swing.Frame;
40 import awt2swing.Panel;
41 import awt2swing.Scrollbar;
42 import awt2swing.TextField;
43 import java.awt.event.ActionEvent;
44 import java.awt.event.ActionListener;
45 import java.awt.event.AdjustmentEvent;
46 import java.awt.event.AdjustmentListener;
47 import java.awt.event.ItemEvent;
48 import java.awt.event.ItemListener;
49 import java.awt.event.MouseEvent;
50 import java.awt.event.MouseListener;
51 import java.util.Hashtable;
52 import java.util.Vector;
54 public class AnnotationColourChooser extends Panel implements
55 ActionListener, AdjustmentListener, ItemListener, MouseListener
65 Hashtable oldgroupColours;
67 AlignmentAnnotation currentAnnotation;
69 boolean adjusting = false;
71 public AnnotationColourChooser(AlignViewport av, AlignmentPanel ap)
76 } catch (Exception ex)
80 oldcs = av.getGlobalColourScheme();
81 if (av.getAlignment().getGroups() != null)
83 oldgroupColours = new Hashtable();
84 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
88 oldgroupColours.put(sg, sg.cs);
92 oldgroupColours.put(sg, "null");
99 slider.addAdjustmentListener(this);
100 slider.addMouseListener(this);
102 if (av.getAlignment().getAlignmentAnnotation() == null)
110 if (oldcs instanceof AnnotationColourGradient)
112 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
113 currentColours.setState(acg.isPredefinedColours()
114 || acg.getBaseColour() != null);
115 if (!acg.isPredefinedColours() && acg.getBaseColour() == null)
117 minColour.setBackground(acg.getMinColour());
118 maxColour.setBackground(acg.getMaxColour());
120 // seqAssociated.setState(acg.isSeqAssociated());
123 Vector list = new Vector();
125 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
127 String label = av.getAlignment().getAlignmentAnnotation()[i].label;
128 if (!list.contains(label))
130 list.addElement(label);
134 list.addElement(label + "_" + (index++));
138 for (int i = 0; i < list.size(); i++)
140 annotations.addItem(list.elementAt(i).toString());
143 threshold.addItem(MessageManager
144 .getString("label.threshold_feature_no_thereshold"));
145 threshold.addItem(MessageManager
146 .getString("label.threshold_feature_above_thereshold"));
147 threshold.addItem(MessageManager
148 .getString("label.threshold_feature_below_thereshold"));
150 if (oldcs instanceof AnnotationColourGradient)
152 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
153 annotations.select(acg.getAnnotation());
154 switch (acg.getAboveThreshold())
156 case AnnotationColourGradient.NO_THRESHOLD:
159 case AnnotationColourGradient.ABOVE_THRESHOLD:
162 case AnnotationColourGradient.BELOW_THRESHOLD:
168 .getString("error.implementation_error_dont_know_thereshold_annotationcolourgradient"));
170 thresholdIsMin.setState(acg.thresholdIsMinMax);
171 thresholdValue.setText("" + acg.getAnnotationThreshold());
180 JalviewLite.addFrame(frame,
181 MessageManager.getString("label.colour_by_annotation"), 560,
186 private void setDefaultMinMax()
188 minColour.setBackground(av.applet.getDefaultColourParameter(
189 "ANNOTATIONCOLOUR_MIN", Color.orange));
190 maxColour.setBackground(av.applet.getDefaultColourParameter(
191 "ANNOTATIONCOLOUR_MAX", Color.red));
195 public AnnotationColourChooser()
200 } catch (Exception ex)
202 ex.printStackTrace();
206 private void jbInit() throws Exception
208 minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
209 minColour.setLabel(MessageManager.getString("label.min_colour"));
210 minColour.addActionListener(this);
212 maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
213 maxColour.setLabel(MessageManager.getString("label.max_colour"));
214 maxColour.addActionListener(this);
216 thresholdIsMin.addItemListener(this);
217 ok.setLabel(MessageManager.getString("action.ok"));
218 ok.addActionListener(this);
220 cancel.setLabel(MessageManager.getString("action.cancel"));
221 cancel.addActionListener(this);
223 defColours.setLabel(MessageManager.getString("action.set_defaults"));
224 defColours.addActionListener(this);
226 annotations.addItemListener(this);
228 thresholdValue.addActionListener(this);
229 slider.setBackground(Color.white);
230 slider.setPreferredSize(new Dimension(193, 21));
231 slider.setEnabled(false);
232 thresholdValue.setPreferredSize(new Dimension(79, 22));
233 thresholdValue.setEnabled(false);
234 thresholdValue.setColumns(5);
235 currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
236 currentColours.setLabel(MessageManager
237 .getString("label.use_original_colours"));
238 currentColours.addItemListener(this);
240 thresholdIsMin.setBackground(Color.white);
241 thresholdIsMin.setLabel(MessageManager
242 .getString("label.threshold_minmax"));
244 this.setLayout(borderLayout1);
246 jPanel1.setBackground(Color.white);
248 jPanel2.setLayout(new FlowLayout());
249 jPanel2.setBackground(Color.white);
250 threshold.addItemListener(this);
251 jPanel3.setLayout(new FlowLayout());
252 jPanel3.setBackground(Color.white);
253 Panel jPanel4 = new Panel();
254 jPanel4.setLayout(new BorderLayout());
255 jPanel4.setBackground(Color.white);
260 jPanel2.add(annotations);
261 jPanel2.add(currentColours);
262 jPanel2.add(minColour);
263 jPanel2.add(maxColour);
265 jPanel4.add(thresholdIsMin, BorderLayout.WEST);
266 jPanel4.add(slider, BorderLayout.CENTER);
267 jPanel4.add(thresholdValue, BorderLayout.EAST);
269 Panel jPanel34 = new Panel();
270 jPanel34.setLayout(new BorderLayout());
271 jPanel34.setBackground(Color.white);
272 jPanel34.add(jPanel2, BorderLayout.NORTH);
273 jPanel34.add(threshold, BorderLayout.WEST);
274 jPanel3.add(defColours);
275 jPanel34.add(jPanel3, BorderLayout.EAST);
276 jPanel34.add(jPanel4, BorderLayout.SOUTH);
278 this.add(jPanel34, java.awt.BorderLayout.CENTER);
279 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
283 Choice annotations = new Choice();
285 Button minColour = new Button();
287 Button maxColour = new Button();
289 Button ok = new Button();
291 Button cancel = new Button();
293 Button defColours = new Button();
295 Panel jPanel1 = new Panel();
297 Panel jPanel2 = new Panel();
299 Choice threshold = new Choice();
301 FlowLayout flowLayout1 = new FlowLayout();
303 Panel jPanel3 = new Panel();
305 Scrollbar slider = new Scrollbar(Scrollbar.HORIZONTAL);
307 TextField thresholdValue = new TextField(20);
309 Checkbox currentColours = new Checkbox();
311 BorderLayout borderLayout1 = new BorderLayout();
313 Checkbox thresholdIsMin = new Checkbox();
315 public void actionPerformed(ActionEvent evt)
317 if (evt.getSource() == thresholdValue)
321 float f = new Float(thresholdValue.getText()).floatValue();
322 slider.setValue((int) (f * 1000));
323 adjustmentValueChanged(null);
324 } catch (NumberFormatException ex)
328 else if (evt.getSource() == minColour)
330 minColour_actionPerformed(null);
332 else if (evt.getSource() == maxColour)
334 maxColour_actionPerformed(null);
336 else if (evt.getSource() == defColours)
338 defColour_actionPerformed();
340 else if (evt.getSource() == ok)
343 frame.setVisible(false);
345 else if (evt.getSource() == cancel)
348 ap.paintAlignment(true);
349 frame.setVisible(false);
358 public void itemStateChanged(ItemEvent evt)
360 if (evt.getSource() == currentColours)
362 if (currentColours.getState())
367 maxColour.setEnabled(!currentColours.getState());
368 minColour.setEnabled(!currentColours.getState());
375 public void adjustmentValueChanged(AdjustmentEvent evt)
379 thresholdValue.setText((slider.getValue() / 1000f) + "");
380 if (currentColours.getState()
381 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
386 currentAnnotation.threshold.value = slider.getValue() / 1000f;
387 ap.paintAlignment(false);
391 public void minColour_actionPerformed(Color newCol)
395 minColour.setBackground(newCol);
401 new UserDefinedColours(this, "Min Colour", minColour.getBackground());
406 public void maxColour_actionPerformed(Color newCol)
410 maxColour.setBackground(newCol);
416 new UserDefinedColours(this, "Max Colour", maxColour.getBackground());
420 public void defColour_actionPerformed()
430 // Check if combobox is still adjusting
436 currentAnnotation = av.getAlignment().getAlignmentAnnotation()[annotations
437 .getSelectedIndex()];
439 int aboveThreshold = -1;
440 if (threshold.getSelectedIndex() == 1)
442 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
444 else if (threshold.getSelectedIndex() == 2)
446 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
449 slider.setEnabled(true);
450 thresholdValue.setEnabled(true);
451 thresholdIsMin.setEnabled(true);
453 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
455 slider.setEnabled(false);
456 thresholdValue.setEnabled(false);
457 thresholdIsMin.setEnabled(false);
458 thresholdValue.setText("");
460 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
461 && currentAnnotation.threshold == null)
464 .setThreshold(new GraphLine(
465 (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
466 "Threshold", Color.black));
469 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
473 slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
474 slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
475 slider.setValue((int) (currentAnnotation.threshold.value * 1000));
476 thresholdValue.setText(currentAnnotation.threshold.value + "");
477 slider.setEnabled(true);
478 thresholdValue.setEnabled(true);
482 AnnotationColourGradient acg = null;
483 if (currentColours.getState())
485 acg = new AnnotationColourGradient(currentAnnotation,
486 av.getGlobalColourScheme(), aboveThreshold);
490 acg = new AnnotationColourGradient(currentAnnotation,
491 minColour.getBackground(), maxColour.getBackground(),
495 if (currentAnnotation.graphMin == 0f
496 && currentAnnotation.graphMax == 0f)
498 acg.setPredefinedColours(true);
501 acg.thresholdIsMinMax = thresholdIsMin.getState();
503 av.setGlobalColourScheme(acg);
505 // TODO: per group colour propagation not always desired
506 if (av.getAlignment().getGroups() != null)
508 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
516 if (currentColours.getState())
518 sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
523 sg.cs = new AnnotationColourGradient(currentAnnotation,
524 minColour.getBackground(), maxColour.getBackground(),
531 // update colours in linked windows
532 ap.alignmentChanged();
533 ap.paintAlignment(true);
538 av.setGlobalColourScheme(oldcs);
539 if (av.getAlignment().getGroups() != null)
541 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
543 Object cs = oldgroupColours.get(sg);
544 if (cs instanceof ColourSchemeI)
546 sg.cs = (ColourSchemeI) cs;
550 // probably the "null" string we set it to if it was null originally.
555 ap.paintAlignment(true);
559 public void mouseClicked(MouseEvent evt)
563 public void mousePressed(MouseEvent evt)
567 public void mouseReleased(MouseEvent evt)
569 ap.paintAlignment(true);
572 public void mouseEntered(MouseEvent evt)
576 public void mouseExited(MouseEvent evt)