2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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;
26 import java.awt.event.*;
28 import jalview.datamodel.*;
29 import jalview.schemes.*;
30 import jalview.util.MessageManager;
32 public class AnnotationColourChooser extends Panel implements
33 ActionListener, AdjustmentListener, ItemListener, MouseListener
43 Hashtable oldgroupColours;
45 jalview.datamodel.AlignmentAnnotation currentAnnotation;
47 boolean adjusting = false;
49 public AnnotationColourChooser(AlignViewport av, AlignmentPanel ap)
54 } catch (Exception ex)
58 oldcs = av.getGlobalColourScheme();
59 if (av.getAlignment().getGroups() != null)
61 oldgroupColours = new Hashtable();
62 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
66 oldgroupColours.put(sg, sg.cs);
70 oldgroupColours.put(sg, "null");
77 slider.addAdjustmentListener(this);
78 slider.addMouseListener(this);
80 if (av.getAlignment().getAlignmentAnnotation() == null)
88 if (oldcs instanceof AnnotationColourGradient)
90 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
91 currentColours.setState(acg.isPredefinedColours()
92 || acg.getBaseColour() != null);
93 if (!acg.isPredefinedColours() && acg.getBaseColour() == null)
95 minColour.setBackground(acg.getMinColour());
96 maxColour.setBackground(acg.getMaxColour());
98 // seqAssociated.setState(acg.isSeqAssociated());
101 Vector list = new Vector();
103 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
105 String label = av.getAlignment().getAlignmentAnnotation()[i].label;
106 if (!list.contains(label))
107 list.addElement(label);
109 list.addElement(label + "_" + (index++));
112 for (int i = 0; i < list.size(); i++)
114 annotations.addItem(list.elementAt(i).toString());
117 threshold.addItem(MessageManager
118 .getString("label.threshold_feature_no_thereshold"));
119 threshold.addItem(MessageManager
120 .getString("label.threshold_feature_above_thereshold"));
121 threshold.addItem(MessageManager
122 .getString("label.threshold_feature_below_thereshold"));
124 if (oldcs instanceof AnnotationColourGradient)
126 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
127 annotations.select(acg.getAnnotation());
128 switch (acg.getAboveThreshold())
130 case AnnotationColourGradient.NO_THRESHOLD:
133 case AnnotationColourGradient.ABOVE_THRESHOLD:
136 case AnnotationColourGradient.BELOW_THRESHOLD:
141 "Implementation error: don't know about threshold setting for current AnnotationColourGradient.");
143 thresholdIsMin.setState(acg.thresholdIsMinMax);
144 thresholdValue.setText("" + acg.getAnnotationThreshold());
153 jalview.bin.JalviewLite.addFrame(frame,
154 MessageManager.getString("label.colour_by_annotation"), 560,
159 private void setDefaultMinMax()
161 minColour.setBackground(av.applet.getDefaultColourParameter(
162 "ANNOTATIONCOLOUR_MIN", Color.orange));
163 maxColour.setBackground(av.applet.getDefaultColourParameter(
164 "ANNOTATIONCOLOUR_MAX", Color.red));
168 public AnnotationColourChooser()
173 } catch (Exception ex)
175 ex.printStackTrace();
179 private void jbInit() throws Exception
181 minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
182 minColour.setLabel(MessageManager.getString("label.min_colour"));
183 minColour.addActionListener(this);
185 maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
186 maxColour.setLabel(MessageManager.getString("label.max_colour"));
187 maxColour.addActionListener(this);
189 thresholdIsMin.addItemListener(this);
190 ok.setLabel(MessageManager.getString("action.ok"));
191 ok.addActionListener(this);
193 cancel.setLabel(MessageManager.getString("action.cancel"));
194 cancel.addActionListener(this);
196 defColours.setLabel(MessageManager.getString("action.set_defaults"));
197 defColours.addActionListener(this);
199 annotations.addItemListener(this);
201 thresholdValue.addActionListener(this);
202 slider.setBackground(Color.white);
203 slider.setPreferredSize(new Dimension(193, 21));
204 slider.setEnabled(false);
205 thresholdValue.setPreferredSize(new Dimension(79, 22));
206 thresholdValue.setEnabled(false);
207 thresholdValue.setColumns(5);
208 currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
209 currentColours.setLabel(MessageManager
210 .getString("label.use_original_colours"));
211 currentColours.addItemListener(this);
213 thresholdIsMin.setBackground(Color.white);
214 thresholdIsMin.setLabel(MessageManager
215 .getString("label.threshold_minmax"));
217 this.setLayout(borderLayout1);
219 jPanel1.setBackground(Color.white);
221 jPanel2.setLayout(new FlowLayout());
222 jPanel2.setBackground(Color.white);
223 threshold.addItemListener(this);
224 jPanel3.setLayout(new FlowLayout());
225 jPanel3.setBackground(Color.white);
226 Panel jPanel4 = new Panel();
227 jPanel4.setLayout(new BorderLayout());
228 jPanel4.setBackground(Color.white);
233 jPanel2.add(annotations);
234 jPanel2.add(currentColours);
235 jPanel2.add(minColour);
236 jPanel2.add(maxColour);
238 jPanel4.add(thresholdIsMin, BorderLayout.WEST);
239 jPanel4.add(slider, BorderLayout.CENTER);
240 jPanel4.add(thresholdValue, BorderLayout.EAST);
242 Panel jPanel34 = new Panel();
243 jPanel34.setLayout(new BorderLayout());
244 jPanel34.setBackground(Color.white);
245 jPanel34.add(jPanel2, BorderLayout.NORTH);
246 jPanel34.add(threshold, BorderLayout.WEST);
247 jPanel3.add(defColours);
248 jPanel34.add(jPanel3, BorderLayout.EAST);
249 jPanel34.add(jPanel4, BorderLayout.SOUTH);
251 this.add(jPanel34, java.awt.BorderLayout.CENTER);
252 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
256 Choice annotations = new Choice();
258 Button minColour = new Button();
260 Button maxColour = new Button();
262 Button ok = new Button();
264 Button cancel = new Button();
266 Button defColours = new Button();
268 Panel jPanel1 = new Panel();
270 Panel jPanel2 = new Panel();
272 Choice threshold = new Choice();
274 FlowLayout flowLayout1 = new FlowLayout();
276 Panel jPanel3 = new Panel();
278 Scrollbar slider = new Scrollbar(Scrollbar.HORIZONTAL);
280 TextField thresholdValue = new TextField(20);
282 Checkbox currentColours = new Checkbox();
284 BorderLayout borderLayout1 = new BorderLayout();
286 Checkbox thresholdIsMin = new Checkbox();
288 public void actionPerformed(ActionEvent evt)
290 if (evt.getSource() == thresholdValue)
294 float f = new Float(thresholdValue.getText()).floatValue();
295 slider.setValue((int) (f * 1000));
296 adjustmentValueChanged(null);
297 } catch (NumberFormatException ex)
301 else if (evt.getSource() == minColour)
303 minColour_actionPerformed(null);
305 else if (evt.getSource() == maxColour)
307 maxColour_actionPerformed(null);
309 else if (evt.getSource() == defColours)
311 defColour_actionPerformed();
313 else if (evt.getSource() == ok)
316 frame.setVisible(false);
318 else if (evt.getSource() == cancel)
321 ap.paintAlignment(true);
322 frame.setVisible(false);
331 public void itemStateChanged(ItemEvent evt)
333 if (evt.getSource() == currentColours)
335 if (currentColours.getState())
340 maxColour.setEnabled(!currentColours.getState());
341 minColour.setEnabled(!currentColours.getState());
348 public void adjustmentValueChanged(AdjustmentEvent evt)
352 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
353 if (currentColours.getState()
354 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
359 currentAnnotation.threshold.value = (float) slider.getValue() / 1000f;
360 ap.paintAlignment(false);
364 public void minColour_actionPerformed(Color newCol)
368 minColour.setBackground(newCol);
374 new UserDefinedColours(this, "Min Colour", minColour.getBackground());
379 public void maxColour_actionPerformed(Color newCol)
383 maxColour.setBackground(newCol);
389 new UserDefinedColours(this, "Max Colour", maxColour.getBackground());
393 public void defColour_actionPerformed()
403 // Check if combobox is still adjusting
409 currentAnnotation = av.getAlignment().getAlignmentAnnotation()[annotations
410 .getSelectedIndex()];
412 int aboveThreshold = -1;
413 if (threshold.getSelectedIndex() == 1)
415 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
417 else if (threshold.getSelectedIndex() == 2)
419 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
422 slider.setEnabled(true);
423 thresholdValue.setEnabled(true);
424 thresholdIsMin.setEnabled(true);
426 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
428 slider.setEnabled(false);
429 thresholdValue.setEnabled(false);
430 thresholdIsMin.setEnabled(false);
431 thresholdValue.setText("");
433 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
434 && currentAnnotation.threshold == null)
437 .setThreshold(new jalview.datamodel.GraphLine(
438 (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
439 "Threshold", Color.black));
442 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
446 slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
447 slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
448 slider.setValue((int) (currentAnnotation.threshold.value * 1000));
449 thresholdValue.setText(currentAnnotation.threshold.value + "");
450 slider.setEnabled(true);
451 thresholdValue.setEnabled(true);
455 AnnotationColourGradient acg = null;
456 if (currentColours.getState())
458 acg = new AnnotationColourGradient(currentAnnotation,
459 av.getGlobalColourScheme(), aboveThreshold);
463 acg = new AnnotationColourGradient(currentAnnotation,
464 minColour.getBackground(), maxColour.getBackground(),
468 if (currentAnnotation.graphMin == 0f
469 && currentAnnotation.graphMax == 0f)
471 acg.setPredefinedColours(true);
474 acg.thresholdIsMinMax = thresholdIsMin.getState();
476 av.setGlobalColourScheme(acg);
478 // TODO: per group colour propagation not always desired
479 if (av.getAlignment().getGroups() != null)
481 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
489 if (currentColours.getState())
491 sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
496 sg.cs = new AnnotationColourGradient(currentAnnotation,
497 minColour.getBackground(), maxColour.getBackground(),
504 // update colours in linked windows
505 ap.alignmentChanged();
506 ap.paintAlignment(true);
511 av.setGlobalColourScheme(oldcs);
512 if (av.getAlignment().getGroups() != null)
514 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
516 Object cs = oldgroupColours.get(sg);
517 if (cs instanceof ColourSchemeI)
519 sg.cs = (ColourSchemeI) cs;
523 // probably the "null" string we set it to if it was null originally.
528 ap.paintAlignment(true);
532 public void mouseClicked(MouseEvent evt)
536 public void mousePressed(MouseEvent evt)
540 public void mouseReleased(MouseEvent evt)
542 ap.paintAlignment(true);
545 public void mouseEntered(MouseEvent evt)
549 public void mouseExited(MouseEvent evt)