2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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.*;
26 import javax.swing.event.*;
28 import net.miginfocom.swing.MigLayout;
30 import jalview.bin.Cache;
31 import jalview.datamodel.*;
32 import jalview.schemes.*;
33 import java.awt.Dimension;
35 public class AnnotationColourChooser extends JPanel
45 Hashtable oldgroupColours;
47 jalview.datamodel.AlignmentAnnotation currentAnnotation;
49 boolean adjusting = false;
51 public AnnotationColourChooser(AlignViewport av, final AlignmentPanel ap)
53 oldcs = av.getGlobalColourScheme();
54 if (av.getAlignment().getGroups() != null)
56 oldgroupColours = new Hashtable();
57 for (SequenceGroup sg:ap.av.getAlignment().getGroups())
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", 520, 215);
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.getAlignment().getAlignmentAnnotation() == null)
96 // Always get default shading from preferences.
99 if (oldcs instanceof AnnotationColourGradient)
101 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
102 currentColours.setSelected(acg.predefinedColours);
103 if (!acg.predefinedColours)
105 minColour.setBackground(acg.getMinColour());
106 maxColour.setBackground(acg.getMaxColour());
111 Vector list = new Vector();
113 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
115 String label = av.getAlignment().getAlignmentAnnotation()[i].label;
116 if (!list.contains(label))
117 list.addElement(label);
119 list.addElement(label + "_" + (index++));
122 annotations = new JComboBox(list);
124 threshold.addItem("No Threshold");
125 threshold.addItem("Above Threshold");
126 threshold.addItem("Below Threshold");
128 if (oldcs instanceof AnnotationColourGradient)
130 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
131 annotations.setSelectedItem(acg.getAnnotation());
132 switch (acg.getAboveThreshold()) {
133 case AnnotationColourGradient.NO_THRESHOLD:
134 threshold.setSelectedItem("No Threshold");
136 case AnnotationColourGradient.ABOVE_THRESHOLD:
137 threshold.setSelectedItem("Above Threshold");
139 case AnnotationColourGradient.BELOW_THRESHOLD:
140 threshold.setSelectedItem("Below Threshold");
143 throw new Error("Implementation error: don't know about threshold setting for current AnnotationColourGradient.");
145 thresholdIsMin.setSelected(acg.thresholdIsMinMax);
146 thresholdValue.setText(""+acg.getAnnotationThreshold());
152 } catch (Exception ex)
163 private void setDefaultMinMax()
165 minColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN", Color.orange));
166 maxColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX", Color.red));
169 public AnnotationColourChooser()
174 } catch (Exception ex)
176 ex.printStackTrace();
180 private void jbInit() throws Exception
182 minColour.setFont(JvSwingUtils.getLabelFont());
183 minColour.setBorder(BorderFactory.createEtchedBorder());
184 minColour.setPreferredSize(new Dimension(40, 20));
185 minColour.setToolTipText("Minimum Colour");
186 minColour.addMouseListener(new MouseAdapter()
188 public void mousePressed(MouseEvent e)
190 if (minColour.isEnabled())
192 minColour_actionPerformed();
196 maxColour.setFont(JvSwingUtils.getLabelFont());
197 maxColour.setBorder(BorderFactory.createEtchedBorder());
198 maxColour.setPreferredSize(new Dimension(40, 20));
199 maxColour.setToolTipText("Maximum Colour");
200 maxColour.addMouseListener(new MouseAdapter()
202 public void mousePressed(MouseEvent e)
204 if (maxColour.isEnabled())
206 maxColour_actionPerformed();
212 ok.addActionListener(new ActionListener()
214 public void actionPerformed(ActionEvent e)
216 ok_actionPerformed(e);
219 cancel.setOpaque(false);
220 cancel.setText("Cancel");
221 cancel.addActionListener(new ActionListener()
223 public void actionPerformed(ActionEvent e)
225 cancel_actionPerformed(e);
228 defColours.setOpaque(false);
229 defColours.setText("Defaults");
230 defColours.setToolTipText("Reset min and max colours to defaults from user preferences.");
231 defColours.addActionListener(new ActionListener()
235 public void actionPerformed(ActionEvent arg0)
237 resetColours_actionPerformed(arg0);
241 annotations.addActionListener(new ActionListener()
243 public void actionPerformed(ActionEvent e)
245 annotations_actionPerformed(e);
248 threshold.addActionListener(new ActionListener()
250 public void actionPerformed(ActionEvent e)
252 threshold_actionPerformed(e);
255 thresholdValue.addActionListener(new ActionListener()
257 public void actionPerformed(ActionEvent e)
259 thresholdValue_actionPerformed(e);
262 slider.setPaintLabels(false);
263 slider.setPaintTicks(true);
264 slider.setBackground(Color.white);
265 slider.setEnabled(false);
266 slider.setOpaque(false);
267 slider.setPreferredSize(new Dimension(100, 32));
268 thresholdValue.setEnabled(false);
269 thresholdValue.setColumns(7);
270 currentColours.setFont(JvSwingUtils.getLabelFont());
271 currentColours.setOpaque(false);
272 currentColours.setText("Use Original Colours");
273 currentColours.addActionListener(new ActionListener()
275 public void actionPerformed(ActionEvent e)
277 currentColours_actionPerformed(e);
280 thresholdIsMin.setBackground(Color.white);
281 thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
282 thresholdIsMin.setText("Threshold is Min/Max");
283 thresholdIsMin.addActionListener(new ActionListener()
285 public void actionPerformed(ActionEvent actionEvent)
287 thresholdIsMin_actionPerformed(actionEvent);
290 this.setLayout(borderLayout1);
291 jPanel2.setLayout(new MigLayout("","[left][center][right]","[][][]"));
292 jPanel1.setBackground(Color.white);
293 jPanel2.setBackground(Color.white);
297 jPanel2.add(annotations);
298 jPanel2.add(currentColours);
299 JPanel colpanel = new JPanel(new FlowLayout());
300 colpanel.setBackground(Color.white);
301 colpanel.add(minColour);
302 colpanel.add(maxColour);
303 jPanel2.add(colpanel, "wrap");
305 jPanel2.add(threshold);
306 jPanel2.add(defColours,"skip 1, wrap");
307 jPanel2.add(thresholdIsMin);
308 jPanel2.add(slider, "grow");
309 jPanel2.add(thresholdValue, "grow");
310 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
311 this.add(jPanel2, java.awt.BorderLayout.CENTER);
314 protected void resetColours_actionPerformed(ActionEvent arg0)
320 JComboBox annotations;
322 JPanel minColour = new JPanel();
324 JPanel maxColour = new JPanel();
325 JButton defColours = new JButton();
326 JButton ok = new JButton();
328 JButton cancel = new JButton();
330 JPanel jPanel1 = new JPanel();
331 JPanel jPanel2 = new JPanel();
333 BorderLayout borderLayout1 = new BorderLayout();
335 JComboBox threshold = new JComboBox();
338 JSlider slider = new JSlider();
340 JTextField thresholdValue = new JTextField(20);
342 JCheckBox currentColours = new JCheckBox();
344 JCheckBox thresholdIsMin = new JCheckBox();
346 public void minColour_actionPerformed()
348 Color col = JColorChooser.showDialog(this,
349 "Select Colour for Minimum Value", minColour.getBackground());
352 minColour.setBackground(col);
358 public void maxColour_actionPerformed()
360 Color col = JColorChooser.showDialog(this,
361 "Select Colour for Maximum Value", maxColour.getBackground());
364 maxColour.setBackground(col);
372 // Check if combobox is still adjusting
378 currentAnnotation = av.getAlignment().getAlignmentAnnotation()[annotations
379 .getSelectedIndex()];
381 int aboveThreshold = -1;
382 if (threshold.getSelectedItem().equals("Above Threshold"))
384 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
386 else if (threshold.getSelectedItem().equals("Below Threshold"))
388 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
391 slider.setEnabled(true);
392 thresholdValue.setEnabled(true);
393 thresholdIsMin.setEnabled(true);
395 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
397 slider.setEnabled(false);
398 thresholdValue.setEnabled(false);
399 thresholdValue.setText("");
400 thresholdIsMin.setEnabled(false);
402 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
403 && currentAnnotation.threshold == null)
406 .setThreshold(new jalview.datamodel.GraphLine(
407 (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
408 "Threshold", Color.black));
411 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
414 float range = currentAnnotation.graphMax * 1000
415 - currentAnnotation.graphMin * 1000;
417 slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
418 slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
419 slider.setValue((int) (currentAnnotation.threshold.value * 1000));
420 thresholdValue.setText(currentAnnotation.threshold.value + "");
421 slider.setMajorTickSpacing((int) (range / 10f));
422 slider.setEnabled(true);
423 thresholdValue.setEnabled(true);
427 AnnotationColourGradient acg = null;
428 if (currentColours.isSelected())
430 acg = new AnnotationColourGradient(currentAnnotation,
431 av.getGlobalColourScheme(), aboveThreshold);
435 acg = new AnnotationColourGradient(currentAnnotation,
436 minColour.getBackground(), maxColour.getBackground(),
440 if (currentAnnotation.graphMin == 0f
441 && currentAnnotation.graphMax == 0f)
443 acg.predefinedColours = true;
446 acg.thresholdIsMinMax = thresholdIsMin.isSelected();
448 av.setGlobalColourScheme(acg);
450 if (av.getAlignment().getGroups() != null)
453 for (SequenceGroup sg:ap.av.getAlignment().getGroups())
460 if (currentColours.isSelected())
462 sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
467 sg.cs = new AnnotationColourGradient(currentAnnotation,
468 minColour.getBackground(), maxColour.getBackground(),
474 // ensure all associated views (overviews, structures, etc) are notified of updated colours.
475 ap.paintAlignment(true);
478 public void ok_actionPerformed(ActionEvent e)
483 frame.setClosed(true);
484 } catch (Exception ex)
489 public void cancel_actionPerformed(ActionEvent e)
492 // ensure all original colouring is propagated to listeners.
493 ap.paintAlignment(true);
496 frame.setClosed(true);
497 } catch (Exception ex)
504 av.setGlobalColourScheme(oldcs);
505 if (av.getAlignment().getGroups() != null)
508 for (SequenceGroup sg:ap.av.getAlignment().getGroups())
510 sg.cs = (ColourSchemeI) oldgroupColours.get(sg);
515 public void thresholdCheck_actionPerformed(ActionEvent e)
520 public void annotations_actionPerformed(ActionEvent e)
525 public void threshold_actionPerformed(ActionEvent e)
530 public void thresholdValue_actionPerformed(ActionEvent e)
534 float f = Float.parseFloat(thresholdValue.getText());
535 slider.setValue((int) (f * 1000));
536 } catch (NumberFormatException ex)
541 public void valueChanged()
543 if (currentColours.isSelected()
544 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
549 currentAnnotation.threshold.value = (float) slider.getValue() / 1000f;
550 ap.paintAlignment(false);
553 public void currentColours_actionPerformed(ActionEvent e)
555 if (currentColours.isSelected())
560 maxColour.setEnabled(!currentColours.isSelected());
561 minColour.setEnabled(!currentColours.isSelected());
566 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)