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:
140 throw new Error(MessageManager.getString("error.implementation_error_dont_know_thereshold_annotationcolourgradient"));
142 thresholdIsMin.setState(acg.thresholdIsMinMax);
143 thresholdValue.setText("" + acg.getAnnotationThreshold());
152 jalview.bin.JalviewLite.addFrame(frame,
153 MessageManager.getString("label.colour_by_annotation"), 560,
158 private void setDefaultMinMax()
160 minColour.setBackground(av.applet.getDefaultColourParameter(
161 "ANNOTATIONCOLOUR_MIN", Color.orange));
162 maxColour.setBackground(av.applet.getDefaultColourParameter(
163 "ANNOTATIONCOLOUR_MAX", Color.red));
167 public AnnotationColourChooser()
172 } catch (Exception ex)
174 ex.printStackTrace();
178 private void jbInit() throws Exception
180 minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
181 minColour.setLabel(MessageManager.getString("label.min_colour"));
182 minColour.addActionListener(this);
184 maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
185 maxColour.setLabel(MessageManager.getString("label.max_colour"));
186 maxColour.addActionListener(this);
188 thresholdIsMin.addItemListener(this);
189 ok.setLabel(MessageManager.getString("action.ok"));
190 ok.addActionListener(this);
192 cancel.setLabel(MessageManager.getString("action.cancel"));
193 cancel.addActionListener(this);
195 defColours.setLabel(MessageManager.getString("action.set_defaults"));
196 defColours.addActionListener(this);
198 annotations.addItemListener(this);
200 thresholdValue.addActionListener(this);
201 slider.setBackground(Color.white);
202 slider.setPreferredSize(new Dimension(193, 21));
203 slider.setEnabled(false);
204 thresholdValue.setPreferredSize(new Dimension(79, 22));
205 thresholdValue.setEnabled(false);
206 thresholdValue.setColumns(5);
207 currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
208 currentColours.setLabel(MessageManager
209 .getString("label.use_original_colours"));
210 currentColours.addItemListener(this);
212 thresholdIsMin.setBackground(Color.white);
213 thresholdIsMin.setLabel(MessageManager
214 .getString("label.threshold_minmax"));
216 this.setLayout(borderLayout1);
218 jPanel1.setBackground(Color.white);
220 jPanel2.setLayout(new FlowLayout());
221 jPanel2.setBackground(Color.white);
222 threshold.addItemListener(this);
223 jPanel3.setLayout(new FlowLayout());
224 jPanel3.setBackground(Color.white);
225 Panel jPanel4 = new Panel();
226 jPanel4.setLayout(new BorderLayout());
227 jPanel4.setBackground(Color.white);
232 jPanel2.add(annotations);
233 jPanel2.add(currentColours);
234 jPanel2.add(minColour);
235 jPanel2.add(maxColour);
237 jPanel4.add(thresholdIsMin, BorderLayout.WEST);
238 jPanel4.add(slider, BorderLayout.CENTER);
239 jPanel4.add(thresholdValue, BorderLayout.EAST);
241 Panel jPanel34 = new Panel();
242 jPanel34.setLayout(new BorderLayout());
243 jPanel34.setBackground(Color.white);
244 jPanel34.add(jPanel2, BorderLayout.NORTH);
245 jPanel34.add(threshold, BorderLayout.WEST);
246 jPanel3.add(defColours);
247 jPanel34.add(jPanel3, BorderLayout.EAST);
248 jPanel34.add(jPanel4, BorderLayout.SOUTH);
250 this.add(jPanel34, java.awt.BorderLayout.CENTER);
251 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
255 Choice annotations = new Choice();
257 Button minColour = new Button();
259 Button maxColour = new Button();
261 Button ok = new Button();
263 Button cancel = new Button();
265 Button defColours = new Button();
267 Panel jPanel1 = new Panel();
269 Panel jPanel2 = new Panel();
271 Choice threshold = new Choice();
273 FlowLayout flowLayout1 = new FlowLayout();
275 Panel jPanel3 = new Panel();
277 Scrollbar slider = new Scrollbar(Scrollbar.HORIZONTAL);
279 TextField thresholdValue = new TextField(20);
281 Checkbox currentColours = new Checkbox();
283 BorderLayout borderLayout1 = new BorderLayout();
285 Checkbox thresholdIsMin = new Checkbox();
287 public void actionPerformed(ActionEvent evt)
289 if (evt.getSource() == thresholdValue)
293 float f = new Float(thresholdValue.getText()).floatValue();
294 slider.setValue((int) (f * 1000));
295 adjustmentValueChanged(null);
296 } catch (NumberFormatException ex)
300 else if (evt.getSource() == minColour)
302 minColour_actionPerformed(null);
304 else if (evt.getSource() == maxColour)
306 maxColour_actionPerformed(null);
308 else if (evt.getSource() == defColours)
310 defColour_actionPerformed();
312 else if (evt.getSource() == ok)
315 frame.setVisible(false);
317 else if (evt.getSource() == cancel)
320 ap.paintAlignment(true);
321 frame.setVisible(false);
330 public void itemStateChanged(ItemEvent evt)
332 if (evt.getSource() == currentColours)
334 if (currentColours.getState())
339 maxColour.setEnabled(!currentColours.getState());
340 minColour.setEnabled(!currentColours.getState());
347 public void adjustmentValueChanged(AdjustmentEvent evt)
351 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
352 if (currentColours.getState()
353 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
358 currentAnnotation.threshold.value = (float) slider.getValue() / 1000f;
359 ap.paintAlignment(false);
363 public void minColour_actionPerformed(Color newCol)
367 minColour.setBackground(newCol);
373 new UserDefinedColours(this, "Min Colour", minColour.getBackground());
378 public void maxColour_actionPerformed(Color newCol)
382 maxColour.setBackground(newCol);
388 new UserDefinedColours(this, "Max Colour", maxColour.getBackground());
392 public void defColour_actionPerformed()
402 // Check if combobox is still adjusting
408 currentAnnotation = av.getAlignment().getAlignmentAnnotation()[annotations
409 .getSelectedIndex()];
411 int aboveThreshold = -1;
412 if (threshold.getSelectedIndex() == 1)
414 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
416 else if (threshold.getSelectedIndex() == 2)
418 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
421 slider.setEnabled(true);
422 thresholdValue.setEnabled(true);
423 thresholdIsMin.setEnabled(true);
425 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
427 slider.setEnabled(false);
428 thresholdValue.setEnabled(false);
429 thresholdIsMin.setEnabled(false);
430 thresholdValue.setText("");
432 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
433 && currentAnnotation.threshold == null)
436 .setThreshold(new jalview.datamodel.GraphLine(
437 (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
438 "Threshold", Color.black));
441 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
445 slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
446 slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
447 slider.setValue((int) (currentAnnotation.threshold.value * 1000));
448 thresholdValue.setText(currentAnnotation.threshold.value + "");
449 slider.setEnabled(true);
450 thresholdValue.setEnabled(true);
454 AnnotationColourGradient acg = null;
455 if (currentColours.getState())
457 acg = new AnnotationColourGradient(currentAnnotation,
458 av.getGlobalColourScheme(), aboveThreshold);
462 acg = new AnnotationColourGradient(currentAnnotation,
463 minColour.getBackground(), maxColour.getBackground(),
467 if (currentAnnotation.graphMin == 0f
468 && currentAnnotation.graphMax == 0f)
470 acg.setPredefinedColours(true);
473 acg.thresholdIsMinMax = thresholdIsMin.getState();
475 av.setGlobalColourScheme(acg);
477 // TODO: per group colour propagation not always desired
478 if (av.getAlignment().getGroups() != null)
480 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
488 if (currentColours.getState())
490 sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
495 sg.cs = new AnnotationColourGradient(currentAnnotation,
496 minColour.getBackground(), maxColour.getBackground(),
503 // update colours in linked windows
504 ap.alignmentChanged();
505 ap.paintAlignment(true);
510 av.setGlobalColourScheme(oldcs);
511 if (av.getAlignment().getGroups() != null)
513 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
515 Object cs = oldgroupColours.get(sg);
516 if (cs instanceof ColourSchemeI)
518 sg.cs = (ColourSchemeI) cs;
522 // probably the "null" string we set it to if it was null originally.
527 ap.paintAlignment(true);
531 public void mouseClicked(MouseEvent evt)
535 public void mousePressed(MouseEvent evt)
539 public void mouseReleased(MouseEvent evt)
541 ap.paintAlignment(true);
544 public void mouseEntered(MouseEvent evt)
548 public void mouseExited(MouseEvent evt)