2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 import java.awt.event.*;
26 import javax.swing.event.*;
28 import jalview.datamodel.*;
29 import jalview.schemes.*;
30 import java.awt.Dimension;
32 public class AnnotationColourChooser extends JPanel
42 Hashtable oldgroupColours;
44 jalview.datamodel.AlignmentAnnotation currentAnnotation;
46 boolean adjusting = false;
48 public AnnotationColourChooser(AlignViewport av, final AlignmentPanel ap)
50 oldcs = av.getGlobalColourScheme();
51 if (av.alignment.getGroups() != null)
53 oldgroupColours = new Hashtable();
54 Vector allGroups = ap.av.alignment.getGroups();
56 for (int g = 0; g < allGroups.size(); g++)
58 sg = (SequenceGroup) allGroups.get(g);
61 oldgroupColours.put(sg, sg.cs);
67 frame = new JInternalFrame();
68 frame.setContentPane(this);
69 frame.setLayer(JLayeredPane.PALETTE_LAYER);
70 Desktop.addInternalFrame(frame, "Colour by Annotation", 480, 145);
72 slider.addChangeListener(new ChangeListener()
74 public void stateChanged(ChangeEvent evt)
78 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
83 slider.addMouseListener(new MouseAdapter()
85 public void mouseReleased(MouseEvent evt)
87 ap.paintAlignment(true);
91 if (av.alignment.getAlignmentAnnotation() == null)
96 if (oldcs instanceof AnnotationColourGradient)
98 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
99 minColour.setBackground(acg.getMinColour());
100 maxColour.setBackground(acg.getMaxColour());
104 minColour.setBackground(Color.orange);
105 maxColour.setBackground(Color.red);
109 Vector list = new Vector();
111 for (int i = 0; i < av.alignment.getAlignmentAnnotation().length; i++)
113 String label = av.alignment.getAlignmentAnnotation()[i].label;
114 if (!list.contains(label))
115 list.addElement(label);
117 list.addElement(label + "_" + (index++));
120 annotations = new JComboBox(list);
122 threshold.addItem("No Threshold");
123 threshold.addItem("Above Threshold");
124 threshold.addItem("Below Threshold");
129 } catch (Exception ex)
139 public AnnotationColourChooser()
144 } catch (Exception ex)
146 ex.printStackTrace();
150 private void jbInit() throws Exception
152 minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
153 minColour.setBorder(BorderFactory.createEtchedBorder());
154 minColour.setPreferredSize(new Dimension(40, 20));
155 minColour.setToolTipText("Minimum Colour");
156 minColour.addMouseListener(new MouseAdapter()
158 public void mousePressed(MouseEvent e)
160 if (minColour.isEnabled())
162 minColour_actionPerformed();
166 maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
167 maxColour.setBorder(BorderFactory.createEtchedBorder());
168 maxColour.setPreferredSize(new Dimension(40, 20));
169 maxColour.setToolTipText("Maximum Colour");
170 maxColour.addMouseListener(new MouseAdapter()
172 public void mousePressed(MouseEvent e)
174 if (maxColour.isEnabled())
176 maxColour_actionPerformed();
182 ok.addActionListener(new ActionListener()
184 public void actionPerformed(ActionEvent e)
186 ok_actionPerformed(e);
189 cancel.setOpaque(false);
190 cancel.setText("Cancel");
191 cancel.addActionListener(new ActionListener()
193 public void actionPerformed(ActionEvent e)
195 cancel_actionPerformed(e);
198 this.setLayout(borderLayout1);
199 jPanel2.setLayout(flowLayout1);
200 annotations.addActionListener(new ActionListener()
202 public void actionPerformed(ActionEvent e)
204 annotations_actionPerformed(e);
207 jPanel1.setBackground(Color.white);
208 jPanel2.setBackground(Color.white);
209 threshold.addActionListener(new ActionListener()
211 public void actionPerformed(ActionEvent e)
213 threshold_actionPerformed(e);
216 jPanel3.setLayout(flowLayout2);
217 thresholdValue.addActionListener(new ActionListener()
219 public void actionPerformed(ActionEvent e)
221 thresholdValue_actionPerformed(e);
224 slider.setPaintLabels(false);
225 slider.setPaintTicks(true);
226 slider.setBackground(Color.white);
227 slider.setEnabled(false);
228 slider.setOpaque(false);
229 slider.setPreferredSize(new Dimension(100, 32));
230 thresholdValue.setEnabled(false);
231 thresholdValue.setColumns(7);
232 jPanel3.setBackground(Color.white);
233 currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
234 currentColours.setOpaque(false);
235 currentColours.setText("Use Original Colours");
236 currentColours.addActionListener(new ActionListener()
238 public void actionPerformed(ActionEvent e)
240 currentColours_actionPerformed(e);
243 thresholdIsMin.setBackground(Color.white);
244 thresholdIsMin.setText("Threshold is Min/Max");
245 thresholdIsMin.addActionListener(new ActionListener()
247 public void actionPerformed(ActionEvent actionEvent)
249 thresholdIsMin_actionPerformed(actionEvent);
254 jPanel2.add(annotations);
255 jPanel2.add(currentColours);
256 jPanel2.add(minColour);
257 jPanel2.add(maxColour);
258 this.add(jPanel3, java.awt.BorderLayout.CENTER);
259 jPanel3.add(threshold);
261 jPanel3.add(thresholdValue);
262 jPanel3.add(thresholdIsMin);
263 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
264 this.add(jPanel2, java.awt.BorderLayout.NORTH);
267 JComboBox annotations;
269 JPanel minColour = new JPanel();
271 JPanel maxColour = new JPanel();
273 JButton ok = new JButton();
275 JButton cancel = new JButton();
277 JPanel jPanel1 = new JPanel();
279 JPanel jPanel2 = new JPanel();
281 BorderLayout borderLayout1 = new BorderLayout();
283 JComboBox threshold = new JComboBox();
285 FlowLayout flowLayout1 = new FlowLayout();
287 JPanel jPanel3 = new JPanel();
289 FlowLayout flowLayout2 = new FlowLayout();
291 JSlider slider = new JSlider();
293 JTextField thresholdValue = new JTextField(20);
295 JCheckBox currentColours = new JCheckBox();
297 JCheckBox thresholdIsMin = new JCheckBox();
299 public void minColour_actionPerformed()
301 Color col = JColorChooser.showDialog(this,
302 "Select Colour for Minimum Value", minColour.getBackground());
305 minColour.setBackground(col);
311 public void maxColour_actionPerformed()
313 Color col = JColorChooser.showDialog(this,
314 "Select Colour for Maximum Value", maxColour.getBackground());
317 maxColour.setBackground(col);
325 // Check if combobox is still adjusting
331 currentAnnotation = av.alignment.getAlignmentAnnotation()[annotations
332 .getSelectedIndex()];
334 int aboveThreshold = -1;
335 if (threshold.getSelectedItem().equals("Above Threshold"))
337 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
339 else if (threshold.getSelectedItem().equals("Below Threshold"))
341 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
344 slider.setEnabled(true);
345 thresholdValue.setEnabled(true);
347 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
349 slider.setEnabled(false);
350 thresholdValue.setEnabled(false);
351 thresholdValue.setText("");
353 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
354 && currentAnnotation.threshold == null)
357 .setThreshold(new jalview.datamodel.GraphLine(
358 (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
359 "Threshold", Color.black));
362 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
365 float range = currentAnnotation.graphMax * 1000
366 - currentAnnotation.graphMin * 1000;
368 slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
369 slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
370 slider.setValue((int) (currentAnnotation.threshold.value * 1000));
371 thresholdValue.setText(currentAnnotation.threshold.value + "");
372 slider.setMajorTickSpacing((int) (range / 10f));
373 slider.setEnabled(true);
374 thresholdValue.setEnabled(true);
378 AnnotationColourGradient acg = null;
379 if (currentColours.isSelected())
381 acg = new AnnotationColourGradient(currentAnnotation, av
382 .getGlobalColourScheme(), aboveThreshold);
386 acg = new AnnotationColourGradient(currentAnnotation, minColour
387 .getBackground(), maxColour.getBackground(), aboveThreshold);
390 if (currentAnnotation.graphMin == 0f
391 && currentAnnotation.graphMax == 0f)
393 acg.predefinedColours = true;
396 acg.thresholdIsMinMax = thresholdIsMin.isSelected();
398 av.setGlobalColourScheme(acg);
400 if (av.alignment.getGroups() != null)
402 Vector allGroups = ap.av.alignment.getGroups();
404 for (int g = 0; g < allGroups.size(); g++)
406 sg = (SequenceGroup) allGroups.get(g);
413 if (currentColours.isSelected())
415 sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
420 sg.cs = new AnnotationColourGradient(currentAnnotation, minColour
421 .getBackground(), maxColour.getBackground(),
428 ap.paintAlignment(false);
431 public void ok_actionPerformed(ActionEvent e)
436 frame.setClosed(true);
437 } catch (Exception ex)
442 public void cancel_actionPerformed(ActionEvent e)
447 frame.setClosed(true);
448 } catch (Exception ex)
455 av.setGlobalColourScheme(oldcs);
456 if (av.alignment.getGroups() != null)
458 Vector allGroups = ap.av.alignment.getGroups();
460 for (int g = 0; g < allGroups.size(); g++)
462 sg = (SequenceGroup) allGroups.get(g);
463 sg.cs = (ColourSchemeI) oldgroupColours.get(sg);
468 public void thresholdCheck_actionPerformed(ActionEvent e)
473 public void annotations_actionPerformed(ActionEvent e)
478 public void threshold_actionPerformed(ActionEvent e)
483 public void thresholdValue_actionPerformed(ActionEvent e)
487 float f = Float.parseFloat(thresholdValue.getText());
488 slider.setValue((int) (f * 1000));
489 } catch (NumberFormatException ex)
494 public void valueChanged()
496 if (currentColours.isSelected()
497 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
502 currentAnnotation.threshold.value = (float) slider.getValue() / 1000f;
503 ap.paintAlignment(false);
506 public void currentColours_actionPerformed(ActionEvent e)
508 if (currentColours.isSelected())
513 maxColour.setEnabled(!currentColours.isSelected());
514 minColour.setEnabled(!currentColours.isSelected());
519 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)