2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
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.bin.Cache;
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 // Always get default shading from preferences.
97 minColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN", Color.orange));
98 maxColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX", Color.red));
100 if (oldcs instanceof AnnotationColourGradient)
102 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
103 currentColours.setSelected(acg.predefinedColours);
104 if (!acg.predefinedColours)
106 minColour.setBackground(acg.getMinColour());
107 maxColour.setBackground(acg.getMaxColour());
112 Vector list = new Vector();
114 for (int i = 0; i < av.alignment.getAlignmentAnnotation().length; i++)
116 String label = av.alignment.getAlignmentAnnotation()[i].label;
117 if (!list.contains(label))
118 list.addElement(label);
120 list.addElement(label + "_" + (index++));
123 annotations = new JComboBox(list);
125 threshold.addItem("No Threshold");
126 threshold.addItem("Above Threshold");
127 threshold.addItem("Below Threshold");
129 if (oldcs instanceof AnnotationColourGradient)
131 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
132 annotations.setSelectedItem(acg.getAnnotation());
133 switch (acg.getAboveThreshold()) {
134 case AnnotationColourGradient.NO_THRESHOLD:
135 threshold.setSelectedItem("No Threshold");
137 case AnnotationColourGradient.ABOVE_THRESHOLD:
138 threshold.setSelectedItem("Above Threshold");
140 case AnnotationColourGradient.BELOW_THRESHOLD:
141 threshold.setSelectedItem("Below Threshold");
144 throw new Error("Implementation error: don't know about threshold setting for current AnnotationColourGradient.");
146 thresholdIsMin.setSelected(acg.thresholdIsMinMax);
147 thresholdValue.setText(""+acg.getAnnotationThreshold());
153 } catch (Exception ex)
164 public AnnotationColourChooser()
169 } catch (Exception ex)
171 ex.printStackTrace();
175 private void jbInit() throws Exception
177 minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
178 minColour.setBorder(BorderFactory.createEtchedBorder());
179 minColour.setPreferredSize(new Dimension(40, 20));
180 minColour.setToolTipText("Minimum Colour");
181 minColour.addMouseListener(new MouseAdapter()
183 public void mousePressed(MouseEvent e)
185 if (minColour.isEnabled())
187 minColour_actionPerformed();
191 maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
192 maxColour.setBorder(BorderFactory.createEtchedBorder());
193 maxColour.setPreferredSize(new Dimension(40, 20));
194 maxColour.setToolTipText("Maximum Colour");
195 maxColour.addMouseListener(new MouseAdapter()
197 public void mousePressed(MouseEvent e)
199 if (maxColour.isEnabled())
201 maxColour_actionPerformed();
207 ok.addActionListener(new ActionListener()
209 public void actionPerformed(ActionEvent e)
211 ok_actionPerformed(e);
214 cancel.setOpaque(false);
215 cancel.setText("Cancel");
216 cancel.addActionListener(new ActionListener()
218 public void actionPerformed(ActionEvent e)
220 cancel_actionPerformed(e);
223 this.setLayout(borderLayout1);
224 jPanel2.setLayout(flowLayout1);
225 annotations.addActionListener(new ActionListener()
227 public void actionPerformed(ActionEvent e)
229 annotations_actionPerformed(e);
232 jPanel1.setBackground(Color.white);
233 jPanel2.setBackground(Color.white);
234 threshold.addActionListener(new ActionListener()
236 public void actionPerformed(ActionEvent e)
238 threshold_actionPerformed(e);
241 jPanel3.setLayout(flowLayout2);
242 thresholdValue.addActionListener(new ActionListener()
244 public void actionPerformed(ActionEvent e)
246 thresholdValue_actionPerformed(e);
249 slider.setPaintLabels(false);
250 slider.setPaintTicks(true);
251 slider.setBackground(Color.white);
252 slider.setEnabled(false);
253 slider.setOpaque(false);
254 slider.setPreferredSize(new Dimension(100, 32));
255 thresholdValue.setEnabled(false);
256 thresholdValue.setColumns(7);
257 jPanel3.setBackground(Color.white);
258 currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
259 currentColours.setOpaque(false);
260 currentColours.setText("Use Original Colours");
261 currentColours.addActionListener(new ActionListener()
263 public void actionPerformed(ActionEvent e)
265 currentColours_actionPerformed(e);
268 thresholdIsMin.setBackground(Color.white);
269 thresholdIsMin.setText("Threshold is Min/Max");
270 thresholdIsMin.addActionListener(new ActionListener()
272 public void actionPerformed(ActionEvent actionEvent)
274 thresholdIsMin_actionPerformed(actionEvent);
279 jPanel2.add(annotations);
280 jPanel2.add(currentColours);
281 jPanel2.add(minColour);
282 jPanel2.add(maxColour);
283 this.add(jPanel3, java.awt.BorderLayout.CENTER);
284 jPanel3.add(threshold);
286 jPanel3.add(thresholdValue);
287 jPanel3.add(thresholdIsMin);
288 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
289 this.add(jPanel2, java.awt.BorderLayout.NORTH);
292 JComboBox annotations;
294 JPanel minColour = new JPanel();
296 JPanel maxColour = new JPanel();
298 JButton ok = new JButton();
300 JButton cancel = new JButton();
302 JPanel jPanel1 = new JPanel();
304 JPanel jPanel2 = new JPanel();
306 BorderLayout borderLayout1 = new BorderLayout();
308 JComboBox threshold = new JComboBox();
310 FlowLayout flowLayout1 = new FlowLayout();
312 JPanel jPanel3 = new JPanel();
314 FlowLayout flowLayout2 = new FlowLayout();
316 JSlider slider = new JSlider();
318 JTextField thresholdValue = new JTextField(20);
320 JCheckBox currentColours = new JCheckBox();
322 JCheckBox thresholdIsMin = new JCheckBox();
324 public void minColour_actionPerformed()
326 Color col = JColorChooser.showDialog(this,
327 "Select Colour for Minimum Value", minColour.getBackground());
330 minColour.setBackground(col);
336 public void maxColour_actionPerformed()
338 Color col = JColorChooser.showDialog(this,
339 "Select Colour for Maximum Value", maxColour.getBackground());
342 maxColour.setBackground(col);
350 // Check if combobox is still adjusting
356 currentAnnotation = av.alignment.getAlignmentAnnotation()[annotations
357 .getSelectedIndex()];
359 int aboveThreshold = -1;
360 if (threshold.getSelectedItem().equals("Above Threshold"))
362 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
364 else if (threshold.getSelectedItem().equals("Below Threshold"))
366 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
369 slider.setEnabled(true);
370 thresholdValue.setEnabled(true);
372 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
374 slider.setEnabled(false);
375 thresholdValue.setEnabled(false);
376 thresholdValue.setText("");
378 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
379 && currentAnnotation.threshold == null)
382 .setThreshold(new jalview.datamodel.GraphLine(
383 (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
384 "Threshold", Color.black));
387 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
390 float range = currentAnnotation.graphMax * 1000
391 - currentAnnotation.graphMin * 1000;
393 slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
394 slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
395 slider.setValue((int) (currentAnnotation.threshold.value * 1000));
396 thresholdValue.setText(currentAnnotation.threshold.value + "");
397 slider.setMajorTickSpacing((int) (range / 10f));
398 slider.setEnabled(true);
399 thresholdValue.setEnabled(true);
403 AnnotationColourGradient acg = null;
404 if (currentColours.isSelected())
406 acg = new AnnotationColourGradient(currentAnnotation,
407 av.getGlobalColourScheme(), aboveThreshold);
411 acg = new AnnotationColourGradient(currentAnnotation,
412 minColour.getBackground(), maxColour.getBackground(),
416 if (currentAnnotation.graphMin == 0f
417 && currentAnnotation.graphMax == 0f)
419 acg.predefinedColours = true;
422 acg.thresholdIsMinMax = thresholdIsMin.isSelected();
424 av.setGlobalColourScheme(acg);
426 if (av.alignment.getGroups() != null)
428 Vector allGroups = ap.av.alignment.getGroups();
430 for (int g = 0; g < allGroups.size(); g++)
432 sg = (SequenceGroup) allGroups.get(g);
439 if (currentColours.isSelected())
441 sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
446 sg.cs = new AnnotationColourGradient(currentAnnotation,
447 minColour.getBackground(), maxColour.getBackground(),
453 // ensure all associated views (overviews, structures, etc) are notified of updated colours.
454 ap.paintAlignment(true);
457 public void ok_actionPerformed(ActionEvent e)
462 frame.setClosed(true);
463 } catch (Exception ex)
468 public void cancel_actionPerformed(ActionEvent e)
473 frame.setClosed(true);
474 } catch (Exception ex)
481 av.setGlobalColourScheme(oldcs);
482 if (av.alignment.getGroups() != null)
484 Vector allGroups = ap.av.alignment.getGroups();
486 for (int g = 0; g < allGroups.size(); g++)
488 sg = (SequenceGroup) allGroups.get(g);
489 sg.cs = (ColourSchemeI) oldgroupColours.get(sg);
494 public void thresholdCheck_actionPerformed(ActionEvent e)
499 public void annotations_actionPerformed(ActionEvent e)
504 public void threshold_actionPerformed(ActionEvent e)
509 public void thresholdValue_actionPerformed(ActionEvent e)
513 float f = Float.parseFloat(thresholdValue.getText());
514 slider.setValue((int) (f * 1000));
515 } catch (NumberFormatException ex)
520 public void valueChanged()
522 if (currentColours.isSelected()
523 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
528 currentAnnotation.threshold.value = (float) slider.getValue() / 1000f;
529 ap.paintAlignment(false);
532 public void currentColours_actionPerformed(ActionEvent e)
534 if (currentColours.isSelected())
539 maxColour.setEnabled(!currentColours.isSelected());
540 minColour.setEnabled(!currentColours.isSelected());
545 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)