2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.appletgui;
24 import java.awt.event.*;
26 import jalview.datamodel.*;
27 import jalview.schemes.*;
28 import jalview.util.MessageManager;
30 public class AnnotationColourChooser extends Panel implements
31 ActionListener, AdjustmentListener, ItemListener, MouseListener
41 Hashtable oldgroupColours;
43 jalview.datamodel.AlignmentAnnotation currentAnnotation;
45 boolean adjusting = false;
47 public AnnotationColourChooser(AlignViewport av, AlignmentPanel ap)
52 } catch (Exception ex)
56 oldcs = av.getGlobalColourScheme();
57 if (av.getAlignment().getGroups() != null)
59 oldgroupColours = new Hashtable();
60 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
64 oldgroupColours.put(sg, sg.cs);
68 oldgroupColours.put(sg, "null");
75 slider.addAdjustmentListener(this);
76 slider.addMouseListener(this);
78 if (av.getAlignment().getAlignmentAnnotation() == null)
86 if (oldcs instanceof AnnotationColourGradient)
88 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
89 currentColours.setState(acg.isPredefinedColours() || acg.getBaseColour()!=null);
90 if (!acg.isPredefinedColours() && acg.getBaseColour()==null)
92 minColour.setBackground(acg.getMinColour());
93 maxColour.setBackground(acg.getMaxColour());
95 // seqAssociated.setState(acg.isSeqAssociated());
99 Vector list = new Vector();
101 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
103 String label = av.getAlignment().getAlignmentAnnotation()[i].label;
104 if (!list.contains(label))
105 list.addElement(label);
107 list.addElement(label + "_" + (index++));
110 for (int i = 0; i < list.size(); i++)
112 annotations.addItem(list.elementAt(i).toString());
115 threshold.addItem(MessageManager.getString("label.threshold_feature_no_thereshold"));
116 threshold.addItem(MessageManager.getString("label.threshold_feature_above_thereshold"));
117 threshold.addItem(MessageManager.getString("label.threshold_feature_below_thereshold"));
119 if (oldcs instanceof AnnotationColourGradient)
121 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
122 annotations.select(acg.getAnnotation());
123 switch (acg.getAboveThreshold())
125 case AnnotationColourGradient.NO_THRESHOLD:
128 case AnnotationColourGradient.ABOVE_THRESHOLD:
131 case AnnotationColourGradient.BELOW_THRESHOLD:
136 "Implementation error: don't know about threshold setting for current AnnotationColourGradient.");
138 thresholdIsMin.setState(acg.thresholdIsMinMax);
139 thresholdValue.setText("" + acg.getAnnotationThreshold());
148 jalview.bin.JalviewLite.addFrame(frame, MessageManager.getString("label.colour_by_annotation"), 560,
153 private void setDefaultMinMax()
155 minColour.setBackground(av.applet.getDefaultColourParameter(
156 "ANNOTATIONCOLOUR_MIN", Color.orange));
157 maxColour.setBackground(av.applet.getDefaultColourParameter(
158 "ANNOTATIONCOLOUR_MAX", Color.red));
162 public AnnotationColourChooser()
167 } catch (Exception ex)
169 ex.printStackTrace();
173 private void jbInit() throws Exception
175 minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
176 minColour.setLabel(MessageManager.getString("label.min_colour"));
177 minColour.addActionListener(this);
179 maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
180 maxColour.setLabel(MessageManager.getString("label.max_colour"));
181 maxColour.addActionListener(this);
183 thresholdIsMin.addItemListener(this);
184 ok.setLabel(MessageManager.getString("action.ok"));
185 ok.addActionListener(this);
187 cancel.setLabel(MessageManager.getString("action.cancel"));
188 cancel.addActionListener(this);
190 defColours.setLabel(MessageManager.getString("action.set_defaults"));
191 defColours.addActionListener(this);
193 annotations.addItemListener(this);
195 thresholdValue.addActionListener(this);
196 slider.setBackground(Color.white);
197 slider.setPreferredSize(new Dimension(193, 21));
198 slider.setEnabled(false);
199 thresholdValue.setPreferredSize(new Dimension(79, 22));
200 thresholdValue.setEnabled(false);
201 thresholdValue.setColumns(5);
202 currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
203 currentColours.setLabel(MessageManager.getString("label.use_original_colours"));
204 currentColours.addItemListener(this);
206 thresholdIsMin.setBackground(Color.white);
207 thresholdIsMin.setLabel(MessageManager.getString("label.threshold_minmax"));
209 this.setLayout(borderLayout1);
211 jPanel1.setBackground(Color.white);
213 jPanel2.setLayout(new FlowLayout());
214 jPanel2.setBackground(Color.white);
215 threshold.addItemListener(this);
216 jPanel3.setLayout(new FlowLayout());
217 jPanel3.setBackground(Color.white);
218 Panel jPanel4 = new Panel();
219 jPanel4.setLayout(new BorderLayout());
220 jPanel4.setBackground(Color.white);
225 jPanel2.add(annotations);
226 jPanel2.add(currentColours);
227 jPanel2.add(minColour);
228 jPanel2.add(maxColour);
230 jPanel4.add(thresholdIsMin, BorderLayout.WEST);
231 jPanel4.add(slider, BorderLayout.CENTER);
232 jPanel4.add(thresholdValue, BorderLayout.EAST);
234 Panel jPanel34 = new Panel();
235 jPanel34.setLayout(new BorderLayout());
236 jPanel34.setBackground(Color.white);
237 jPanel34.add(jPanel2, BorderLayout.NORTH);
238 jPanel34.add(threshold, BorderLayout.WEST);
239 jPanel3.add(defColours);
240 jPanel34.add(jPanel3, BorderLayout.EAST);
241 jPanel34.add(jPanel4, BorderLayout.SOUTH);
243 this.add(jPanel34, java.awt.BorderLayout.CENTER);
244 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
248 Choice annotations = new Choice();
250 Button minColour = new Button();
252 Button maxColour = new Button();
254 Button ok = new Button();
256 Button cancel = new Button();
258 Button defColours = new Button();
260 Panel jPanel1 = new Panel();
262 Panel jPanel2 = new Panel();
264 Choice threshold = new Choice();
266 FlowLayout flowLayout1 = new FlowLayout();
268 Panel jPanel3 = new Panel();
270 Scrollbar slider = new Scrollbar(Scrollbar.HORIZONTAL);
272 TextField thresholdValue = new TextField(20);
274 Checkbox currentColours = new Checkbox();
276 BorderLayout borderLayout1 = new BorderLayout();
278 Checkbox thresholdIsMin = new Checkbox();
280 public void actionPerformed(ActionEvent evt)
282 if (evt.getSource() == thresholdValue)
286 float f = new Float(thresholdValue.getText()).floatValue();
287 slider.setValue((int) (f * 1000));
288 adjustmentValueChanged(null);
289 } catch (NumberFormatException ex)
293 else if (evt.getSource() == minColour)
295 minColour_actionPerformed(null);
297 else if (evt.getSource() == maxColour)
299 maxColour_actionPerformed(null);
301 else if (evt.getSource() == defColours)
303 defColour_actionPerformed();
305 else if (evt.getSource() == ok)
308 frame.setVisible(false);
310 else if (evt.getSource() == cancel)
313 ap.paintAlignment(true);
314 frame.setVisible(false);
323 public void itemStateChanged(ItemEvent evt)
325 if (evt.getSource() == currentColours)
327 if (currentColours.getState())
332 maxColour.setEnabled(!currentColours.getState());
333 minColour.setEnabled(!currentColours.getState());
340 public void adjustmentValueChanged(AdjustmentEvent evt)
344 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
345 if (currentColours.getState()
346 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
351 currentAnnotation.threshold.value = (float) slider.getValue() / 1000f;
352 ap.paintAlignment(false);
356 public void minColour_actionPerformed(Color newCol)
360 minColour.setBackground(newCol);
366 new UserDefinedColours(this, "Min Colour", minColour.getBackground());
371 public void maxColour_actionPerformed(Color newCol)
375 maxColour.setBackground(newCol);
381 new UserDefinedColours(this, "Max Colour", maxColour.getBackground());
385 public void defColour_actionPerformed()
395 // Check if combobox is still adjusting
401 currentAnnotation = av.getAlignment().getAlignmentAnnotation()[annotations
402 .getSelectedIndex()];
404 int aboveThreshold = -1;
405 if (threshold.getSelectedIndex()==1)
407 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
409 else if (threshold.getSelectedIndex()==2)
411 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
414 slider.setEnabled(true);
415 thresholdValue.setEnabled(true);
416 thresholdIsMin.setEnabled(true);
418 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
420 slider.setEnabled(false);
421 thresholdValue.setEnabled(false);
422 thresholdIsMin.setEnabled(false);
423 thresholdValue.setText("");
425 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
426 && currentAnnotation.threshold == null)
429 .setThreshold(new jalview.datamodel.GraphLine(
430 (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
431 "Threshold", Color.black));
434 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
438 slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
439 slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
440 slider.setValue((int) (currentAnnotation.threshold.value * 1000));
441 thresholdValue.setText(currentAnnotation.threshold.value + "");
442 slider.setEnabled(true);
443 thresholdValue.setEnabled(true);
447 AnnotationColourGradient acg = null;
448 if (currentColours.getState())
450 acg = new AnnotationColourGradient(currentAnnotation,
451 av.getGlobalColourScheme(), aboveThreshold);
455 acg = new AnnotationColourGradient(currentAnnotation,
456 minColour.getBackground(), maxColour.getBackground(),
460 if (currentAnnotation.graphMin == 0f
461 && currentAnnotation.graphMax == 0f)
463 acg.setPredefinedColours(true);
466 acg.thresholdIsMinMax = thresholdIsMin.getState();
468 av.setGlobalColourScheme(acg);
470 // TODO: per group colour propagation not always desired
471 if (av.getAlignment().getGroups() != null)
473 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
481 if (currentColours.getState())
483 sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
488 sg.cs = new AnnotationColourGradient(currentAnnotation,
489 minColour.getBackground(), maxColour.getBackground(),
496 // update colours in linked windows
497 ap.alignmentChanged();
498 ap.paintAlignment(true);
503 av.setGlobalColourScheme(oldcs);
504 if (av.getAlignment().getGroups() != null)
506 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
508 Object cs = oldgroupColours.get(sg);
509 if (cs instanceof ColourSchemeI)
511 sg.cs = (ColourSchemeI) cs;
515 // probably the "null" string we set it to if it was null originally.
520 ap.paintAlignment(true);
524 public void mouseClicked(MouseEvent evt)
528 public void mousePressed(MouseEvent evt)
532 public void mouseReleased(MouseEvent evt)
534 ap.paintAlignment(true);
537 public void mouseEntered(MouseEvent evt)
541 public void mouseExited(MouseEvent evt)