2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
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/>.
23 import java.awt.event.*;
25 import javax.swing.event.*;
27 import jalview.datamodel.*;
28 import jalview.schemes.*;
29 import java.awt.Dimension;
31 public class AnnotationColourChooser extends JPanel
41 Hashtable oldgroupColours;
43 jalview.datamodel.AlignmentAnnotation currentAnnotation;
45 boolean adjusting = false;
47 public AnnotationColourChooser(AlignViewport av, final AlignmentPanel ap)
49 oldcs = av.getGlobalColourScheme();
50 if (av.alignment.getGroups() != null)
52 oldgroupColours = new Hashtable();
53 Vector allGroups = ap.av.alignment.getGroups();
55 for (int g = 0; g < allGroups.size(); g++)
57 sg = (SequenceGroup) allGroups.get(g);
60 oldgroupColours.put(sg, sg.cs);
66 frame = new JInternalFrame();
67 frame.setContentPane(this);
68 frame.setLayer(JLayeredPane.PALETTE_LAYER);
69 Desktop.addInternalFrame(frame, "Colour by Annotation", 480, 145);
71 slider.addChangeListener(new ChangeListener()
73 public void stateChanged(ChangeEvent evt)
77 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
82 slider.addMouseListener(new MouseAdapter()
84 public void mouseReleased(MouseEvent evt)
86 ap.paintAlignment(true);
90 if (av.alignment.getAlignmentAnnotation() == null)
95 if (oldcs instanceof AnnotationColourGradient)
97 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
98 minColour.setBackground(acg.getMinColour());
99 maxColour.setBackground(acg.getMaxColour());
103 minColour.setBackground(Color.orange);
104 maxColour.setBackground(Color.red);
108 Vector list = new Vector();
110 for (int i = 0; i < av.alignment.getAlignmentAnnotation().length; i++)
112 String label = av.alignment.getAlignmentAnnotation()[i].label;
113 if (!list.contains(label))
114 list.addElement(label);
116 list.addElement(label + "_" + (index++));
119 annotations = new JComboBox(list);
121 threshold.addItem("No Threshold");
122 threshold.addItem("Above Threshold");
123 threshold.addItem("Below Threshold");
128 } catch (Exception ex)
138 public AnnotationColourChooser()
143 } catch (Exception ex)
145 ex.printStackTrace();
149 private void jbInit() throws Exception
151 minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
152 minColour.setBorder(BorderFactory.createEtchedBorder());
153 minColour.setPreferredSize(new Dimension(40, 20));
154 minColour.setToolTipText("Minimum Colour");
155 minColour.addMouseListener(new MouseAdapter()
157 public void mousePressed(MouseEvent e)
159 if (minColour.isEnabled())
161 minColour_actionPerformed();
165 maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
166 maxColour.setBorder(BorderFactory.createEtchedBorder());
167 maxColour.setPreferredSize(new Dimension(40, 20));
168 maxColour.setToolTipText("Maximum Colour");
169 maxColour.addMouseListener(new MouseAdapter()
171 public void mousePressed(MouseEvent e)
173 if (maxColour.isEnabled())
175 maxColour_actionPerformed();
181 ok.addActionListener(new ActionListener()
183 public void actionPerformed(ActionEvent e)
185 ok_actionPerformed(e);
188 cancel.setOpaque(false);
189 cancel.setText("Cancel");
190 cancel.addActionListener(new ActionListener()
192 public void actionPerformed(ActionEvent e)
194 cancel_actionPerformed(e);
197 this.setLayout(borderLayout1);
198 jPanel2.setLayout(flowLayout1);
199 annotations.addActionListener(new ActionListener()
201 public void actionPerformed(ActionEvent e)
203 annotations_actionPerformed(e);
206 jPanel1.setBackground(Color.white);
207 jPanel2.setBackground(Color.white);
208 threshold.addActionListener(new ActionListener()
210 public void actionPerformed(ActionEvent e)
212 threshold_actionPerformed(e);
215 jPanel3.setLayout(flowLayout2);
216 thresholdValue.addActionListener(new ActionListener()
218 public void actionPerformed(ActionEvent e)
220 thresholdValue_actionPerformed(e);
223 slider.setPaintLabels(false);
224 slider.setPaintTicks(true);
225 slider.setBackground(Color.white);
226 slider.setEnabled(false);
227 slider.setOpaque(false);
228 slider.setPreferredSize(new Dimension(100, 32));
229 thresholdValue.setEnabled(false);
230 thresholdValue.setColumns(7);
231 jPanel3.setBackground(Color.white);
232 currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
233 currentColours.setOpaque(false);
234 currentColours.setText("Use Original Colours");
235 currentColours.addActionListener(new ActionListener()
237 public void actionPerformed(ActionEvent e)
239 currentColours_actionPerformed(e);
242 thresholdIsMin.setBackground(Color.white);
243 thresholdIsMin.setText("Threshold is Min/Max");
244 thresholdIsMin.addActionListener(new ActionListener()
246 public void actionPerformed(ActionEvent actionEvent)
248 thresholdIsMin_actionPerformed(actionEvent);
253 jPanel2.add(annotations);
254 jPanel2.add(currentColours);
255 jPanel2.add(minColour);
256 jPanel2.add(maxColour);
257 this.add(jPanel3, java.awt.BorderLayout.CENTER);
258 jPanel3.add(threshold);
260 jPanel3.add(thresholdValue);
261 jPanel3.add(thresholdIsMin);
262 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
263 this.add(jPanel2, java.awt.BorderLayout.NORTH);
266 JComboBox annotations;
268 JPanel minColour = new JPanel();
270 JPanel maxColour = new JPanel();
272 JButton ok = new JButton();
274 JButton cancel = new JButton();
276 JPanel jPanel1 = new JPanel();
278 JPanel jPanel2 = new JPanel();
280 BorderLayout borderLayout1 = new BorderLayout();
282 JComboBox threshold = new JComboBox();
284 FlowLayout flowLayout1 = new FlowLayout();
286 JPanel jPanel3 = new JPanel();
288 FlowLayout flowLayout2 = new FlowLayout();
290 JSlider slider = new JSlider();
292 JTextField thresholdValue = new JTextField(20);
294 JCheckBox currentColours = new JCheckBox();
296 JCheckBox thresholdIsMin = new JCheckBox();
298 public void minColour_actionPerformed()
300 Color col = JColorChooser.showDialog(this,
301 "Select Colour for Minimum Value", minColour.getBackground());
304 minColour.setBackground(col);
310 public void maxColour_actionPerformed()
312 Color col = JColorChooser.showDialog(this,
313 "Select Colour for Maximum Value", maxColour.getBackground());
316 maxColour.setBackground(col);
324 // Check if combobox is still adjusting
330 currentAnnotation = av.alignment.getAlignmentAnnotation()[annotations
331 .getSelectedIndex()];
333 int aboveThreshold = -1;
334 if (threshold.getSelectedItem().equals("Above Threshold"))
336 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
338 else if (threshold.getSelectedItem().equals("Below Threshold"))
340 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
343 slider.setEnabled(true);
344 thresholdValue.setEnabled(true);
346 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
348 slider.setEnabled(false);
349 thresholdValue.setEnabled(false);
350 thresholdValue.setText("");
352 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
353 && currentAnnotation.threshold == null)
356 .setThreshold(new jalview.datamodel.GraphLine(
357 (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
358 "Threshold", Color.black));
361 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
364 float range = currentAnnotation.graphMax * 1000
365 - currentAnnotation.graphMin * 1000;
367 slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
368 slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
369 slider.setValue((int) (currentAnnotation.threshold.value * 1000));
370 thresholdValue.setText(currentAnnotation.threshold.value + "");
371 slider.setMajorTickSpacing((int) (range / 10f));
372 slider.setEnabled(true);
373 thresholdValue.setEnabled(true);
377 AnnotationColourGradient acg = null;
378 if (currentColours.isSelected())
380 acg = new AnnotationColourGradient(currentAnnotation, av
381 .getGlobalColourScheme(), aboveThreshold);
385 acg = new AnnotationColourGradient(currentAnnotation, minColour
386 .getBackground(), maxColour.getBackground(), aboveThreshold);
389 if (currentAnnotation.graphMin == 0f
390 && currentAnnotation.graphMax == 0f)
392 acg.predefinedColours = true;
395 acg.thresholdIsMinMax = thresholdIsMin.isSelected();
397 av.setGlobalColourScheme(acg);
399 if (av.alignment.getGroups() != null)
401 Vector allGroups = ap.av.alignment.getGroups();
403 for (int g = 0; g < allGroups.size(); g++)
405 sg = (SequenceGroup) allGroups.get(g);
412 if (currentColours.isSelected())
414 sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
419 sg.cs = new AnnotationColourGradient(currentAnnotation, minColour
420 .getBackground(), maxColour.getBackground(),
427 ap.paintAlignment(false);
430 public void ok_actionPerformed(ActionEvent e)
435 frame.setClosed(true);
436 } catch (Exception ex)
441 public void cancel_actionPerformed(ActionEvent e)
446 frame.setClosed(true);
447 } catch (Exception ex)
454 av.setGlobalColourScheme(oldcs);
455 if (av.alignment.getGroups() != null)
457 Vector allGroups = ap.av.alignment.getGroups();
459 for (int g = 0; g < allGroups.size(); g++)
461 sg = (SequenceGroup) allGroups.get(g);
462 sg.cs = (ColourSchemeI) oldgroupColours.get(sg);
467 public void thresholdCheck_actionPerformed(ActionEvent e)
472 public void annotations_actionPerformed(ActionEvent e)
477 public void threshold_actionPerformed(ActionEvent e)
482 public void thresholdValue_actionPerformed(ActionEvent e)
486 float f = Float.parseFloat(thresholdValue.getText());
487 slider.setValue((int) (f * 1000));
488 } catch (NumberFormatException ex)
493 public void valueChanged()
495 if (currentColours.isSelected()
496 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
501 currentAnnotation.threshold.value = (float) slider.getValue() / 1000f;
502 ap.paintAlignment(false);
505 public void currentColours_actionPerformed(ActionEvent e)
507 if (currentColours.isSelected())
512 maxColour.setEnabled(!currentColours.isSelected());
513 minColour.setEnabled(!currentColours.isSelected());
518 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)