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 java.awt.Color;
33 import java.awt.Dimension;
34 import java.awt.FlowLayout;
36 import java.awt.event.ActionEvent;
37 import java.awt.event.ActionListener;
38 import java.awt.event.AdjustmentEvent;
39 import java.awt.event.AdjustmentListener;
40 import java.awt.event.ItemEvent;
41 import java.awt.event.ItemListener;
42 import java.awt.event.MouseEvent;
43 import java.awt.event.MouseListener;
44 import java.util.Hashtable;
45 import java.util.Vector;
47 import javax.swing.JButton;
48 import javax.swing.JCheckBox;
49 import javax.swing.JComboBox;
50 import javax.swing.JFrame;
51 import javax.swing.JPanel;
52 import javax.swing.JScrollBar;
53 import javax.swing.JTextField;
55 public class AnnotationColourChooser extends JPanel implements
56 ActionListener, AdjustmentListener, ItemListener, MouseListener
66 Hashtable oldgroupColours;
68 AlignmentAnnotation currentAnnotation;
70 boolean adjusting = false;
72 public AnnotationColourChooser(AlignViewport av, AlignmentPanel ap)
77 } catch (Exception ex)
81 oldcs = av.getGlobalColourScheme();
82 if (av.getAlignment().getGroups() != null)
84 oldgroupColours = new Hashtable();
85 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
89 oldgroupColours.put(sg, sg.cs);
93 oldgroupColours.put(sg, "null");
100 slider.addAdjustmentListener(this);
101 slider.addMouseListener(this);
103 if (av.getAlignment().getAlignmentAnnotation() == null)
111 if (oldcs instanceof AnnotationColourGradient)
113 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
114 currentColours.setSelected(acg.isPredefinedColours()
115 || acg.getBaseColour() != null);
116 if (!acg.isPredefinedColours() && acg.getBaseColour() == null)
118 minColour.setBackground(acg.getMinColour());
119 maxColour.setBackground(acg.getMaxColour());
121 // seqAssociated.setState(acg.isSeqAssociated());
124 Vector list = new Vector();
126 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
128 String label = av.getAlignment().getAlignmentAnnotation()[i].label;
129 if (!list.contains(label))
131 list.addElement(label);
135 list.addElement(label + "_" + (index++));
139 for (int i = 0; i < list.size(); i++)
141 annotations.addItem(list.elementAt(i).toString());
144 threshold.addItem(MessageManager
145 .getString("label.threshold_feature_no_thereshold"));
146 threshold.addItem(MessageManager
147 .getString("label.threshold_feature_above_thereshold"));
148 threshold.addItem(MessageManager
149 .getString("label.threshold_feature_below_thereshold"));
151 if (oldcs instanceof AnnotationColourGradient)
153 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
154 annotations.select(acg.getAnnotation());
155 switch (acg.getAboveThreshold())
157 case AnnotationColourGradient.NO_THRESHOLD:
160 case AnnotationColourGradient.ABOVE_THRESHOLD:
163 case AnnotationColourGradient.BELOW_THRESHOLD:
169 .getString("error.implementation_error_dont_know_thereshold_annotationcolourgradient"));
171 thresholdIsMin.setSelected(acg.thresholdIsMinMax);
172 thresholdValue.setText("" + acg.getAnnotationThreshold());
179 frame = new JFrame();
181 JalviewLite.addFrame(frame,
182 MessageManager.getString("label.colour_by_annotation"), 560,
187 private void setDefaultMinMax()
189 minColour.setBackground(av.applet.getDefaultColourParameter(
190 "ANNOTATIONCOLOUR_MIN", Color.orange));
191 maxColour.setBackground(av.applet.getDefaultColourParameter(
192 "ANNOTATIONCOLOUR_MAX", Color.red));
196 public AnnotationColourChooser()
201 } catch (Exception ex)
203 ex.printStackTrace();
207 private void jbInit() throws Exception
209 minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
210 minColour.setLabel(MessageManager.getString("label.min_colour"));
211 minColour.addActionListener(this);
213 maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
214 maxColour.setLabel(MessageManager.getString("label.max_colour"));
215 maxColour.addActionListener(this);
217 thresholdIsMin.addItemListener(this);
218 ok.setLabel(MessageManager.getString("action.ok"));
219 ok.addActionListener(this);
221 cancel.setLabel(MessageManager.getString("action.cancel"));
222 cancel.addActionListener(this);
224 defColours.setLabel(MessageManager.getString("action.set_defaults"));
225 defColours.addActionListener(this);
227 annotations.addItemListener(this);
229 thresholdValue.addActionListener(this);
230 slider.setBackground(Color.white);
231 slider.setPreferredSize(new Dimension(193, 21));
232 slider.setEnabled(false);
233 thresholdValue.setPreferredSize(new Dimension(79, 22));
234 thresholdValue.setEnabled(false);
235 thresholdValue.setColumns(5);
236 currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
237 currentColours.setLabel(MessageManager
238 .getString("label.use_original_colours"));
239 currentColours.addItemListener(this);
241 thresholdIsMin.setBackground(Color.white);
242 thresholdIsMin.setLabel(MessageManager
243 .getString("label.threshold_minmax"));
245 this.setLayout(borderLayout1);
247 jPanel1.setBackground(Color.white);
249 jPanel2.setLayout(new FlowLayout());
250 jPanel2.setBackground(Color.white);
251 threshold.addItemListener(this);
252 jPanel3.setLayout(new FlowLayout());
253 jPanel3.setBackground(Color.white);
254 JPanel jPanel4 = new JPanel();
255 jPanel4.setLayout(new BorderLayout());
256 jPanel4.setBackground(Color.white);
261 jPanel2.add(annotations);
262 jPanel2.add(currentColours);
263 jPanel2.add(minColour);
264 jPanel2.add(maxColour);
266 jPanel4.add(thresholdIsMin, BorderLayout.WEST);
267 jPanel4.add(slider, BorderLayout.CENTER);
268 jPanel4.add(thresholdValue, BorderLayout.EAST);
270 JPanel jPanel34 = new JPanel();
271 jPanel34.setLayout(new BorderLayout());
272 jPanel34.setBackground(Color.white);
273 jPanel34.add(jPanel2, BorderLayout.NORTH);
274 jPanel34.add(threshold, BorderLayout.WEST);
275 jPanel3.add(defColours);
276 jPanel34.add(jPanel3, BorderLayout.EAST);
277 jPanel34.add(jPanel4, BorderLayout.SOUTH);
279 this.add(jPanel34, java.awt.BorderLayout.CENTER);
280 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
284 Choice annotations = new Choice();
286 JButton minColour = new JButton();
288 JButton maxColour = new JButton();
290 JButton ok = new JButton();
292 JButton cancel = new JButton();
294 JButton defColours = new JButton();
296 JPanel jPanel1 = new JPanel();
298 JPanel jPanel2 = new JPanel();
300 Choice threshold = new Choice();
302 FlowLayout flowLayout1 = new FlowLayout();
304 JPanel jPanel3 = new JPanel();
306 JScrollBar slider = new JScrollBar(JScrollBar.HORIZONTAL);
308 JTextField thresholdValue = new JTextField(20);
310 JCheckBox currentColours = new JCheckBox();
312 BorderLayout borderLayout1 = new BorderLayout();
314 JCheckBox thresholdIsMin = new JCheckBox();
316 public void actionPerformed(ActionEvent evt)
318 if (evt.getSource() == thresholdValue)
322 float f = new Float(thresholdValue.getText()).floatValue();
323 slider.setValue((int) (f * 1000));
324 adjustmentValueChanged(null);
325 } catch (NumberFormatException ex)
329 else if (evt.getSource() == minColour)
331 minColour_actionPerformed(null);
333 else if (evt.getSource() == maxColour)
335 maxColour_actionPerformed(null);
337 else if (evt.getSource() == defColours)
339 defColour_actionPerformed();
341 else if (evt.getSource() == ok)
344 frame.setVisible(false);
346 else if (evt.getSource() == cancel)
349 ap.paintAlignment(true);
350 frame.setVisible(false);
359 public void itemStateChanged(ItemEvent evt)
361 if (evt.getSource() == currentColours)
363 if (currentColours.isSelected())
368 maxColour.setEnabled(!currentColours.isSelected());
369 minColour.setEnabled(!currentColours.isSelected());
376 public void adjustmentValueChanged(AdjustmentEvent evt)
380 thresholdValue.setText((slider.getValue() / 1000f) + "");
381 if (currentColours.isSelected()
382 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
387 currentAnnotation.threshold.value = slider.getValue() / 1000f;
388 ap.paintAlignment(false);
392 public void minColour_actionPerformed(Color newCol)
396 minColour.setBackground(newCol);
402 new UserDefinedColours(this, "Min Colour", minColour.getBackground());
407 public void maxColour_actionPerformed(Color newCol)
411 maxColour.setBackground(newCol);
417 new UserDefinedColours(this, "Max Colour", maxColour.getBackground());
421 public void defColour_actionPerformed()
431 // Check if combobox is still adjusting
437 currentAnnotation = av.getAlignment().getAlignmentAnnotation()[annotations
438 .getSelectedIndex()];
440 int aboveThreshold = -1;
441 if (threshold.getSelectedIndex() == 1)
443 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
445 else if (threshold.getSelectedIndex() == 2)
447 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
450 slider.setEnabled(true);
451 thresholdValue.setEnabled(true);
452 thresholdIsMin.setEnabled(true);
454 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
456 slider.setEnabled(false);
457 thresholdValue.setEnabled(false);
458 thresholdIsMin.setEnabled(false);
459 thresholdValue.setText("");
461 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
462 && currentAnnotation.threshold == null)
465 .setThreshold(new GraphLine(
466 (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
467 "Threshold", Color.black));
470 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
474 slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
475 slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
476 slider.setValue((int) (currentAnnotation.threshold.value * 1000));
477 thresholdValue.setText(currentAnnotation.threshold.value + "");
478 slider.setEnabled(true);
479 thresholdValue.setEnabled(true);
483 AnnotationColourGradient acg = null;
484 if (currentColours.isSelected())
486 acg = new AnnotationColourGradient(currentAnnotation,
487 av.getGlobalColourScheme(), aboveThreshold);
491 acg = new AnnotationColourGradient(currentAnnotation,
492 minColour.getBackground(), maxColour.getBackground(),
496 if (currentAnnotation.graphMin == 0f
497 && currentAnnotation.graphMax == 0f)
499 acg.setPredefinedColours(true);
502 acg.thresholdIsMinMax = thresholdIsMin.isSelected();
504 av.setGlobalColourScheme(acg);
506 // TODO: per group colour propagation not always desired
507 if (av.getAlignment().getGroups() != null)
509 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
517 if (currentColours.isSelected())
519 sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
524 sg.cs = new AnnotationColourGradient(currentAnnotation,
525 minColour.getBackground(), maxColour.getBackground(),
532 // update colours in linked windows
533 ap.alignmentChanged();
534 ap.paintAlignment(true);
539 av.setGlobalColourScheme(oldcs);
540 if (av.getAlignment().getGroups() != null)
542 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
544 Object cs = oldgroupColours.get(sg);
545 if (cs instanceof ColourSchemeI)
547 sg.cs = (ColourSchemeI) cs;
551 // probably the "null" string we set it to if it was null originally.
556 ap.paintAlignment(true);
560 public void mouseClicked(MouseEvent evt)
564 public void mousePressed(MouseEvent evt)
568 public void mouseReleased(MouseEvent evt)
570 ap.paintAlignment(true);
573 public void mouseEntered(MouseEvent evt)
577 public void mouseExited(MouseEvent evt)