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());
108 seqAssociated.setSelected(acg.isSeqAssociated());
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)
164 private void setDefaultMinMax()
166 minColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN", Color.orange));
167 maxColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX", Color.red));
170 public AnnotationColourChooser()
175 } catch (Exception ex)
177 ex.printStackTrace();
181 private void jbInit() throws Exception
183 minColour.setFont(JvSwingUtils.getLabelFont());
184 minColour.setBorder(BorderFactory.createEtchedBorder());
185 minColour.setPreferredSize(new Dimension(40, 20));
186 minColour.setToolTipText("Minimum Colour");
187 minColour.addMouseListener(new MouseAdapter()
189 public void mousePressed(MouseEvent e)
191 if (minColour.isEnabled())
193 minColour_actionPerformed();
197 maxColour.setFont(JvSwingUtils.getLabelFont());
198 maxColour.setBorder(BorderFactory.createEtchedBorder());
199 maxColour.setPreferredSize(new Dimension(40, 20));
200 maxColour.setToolTipText("Maximum Colour");
201 maxColour.addMouseListener(new MouseAdapter()
203 public void mousePressed(MouseEvent e)
205 if (maxColour.isEnabled())
207 maxColour_actionPerformed();
213 ok.addActionListener(new ActionListener()
215 public void actionPerformed(ActionEvent e)
217 ok_actionPerformed(e);
220 cancel.setOpaque(false);
221 cancel.setText("Cancel");
222 cancel.addActionListener(new ActionListener()
224 public void actionPerformed(ActionEvent e)
226 cancel_actionPerformed(e);
229 defColours.setOpaque(false);
230 defColours.setText("Defaults");
231 defColours.setToolTipText("Reset min and max colours to defaults from user preferences.");
232 defColours.addActionListener(new ActionListener()
236 public void actionPerformed(ActionEvent arg0)
238 resetColours_actionPerformed(arg0);
242 annotations.addActionListener(new ActionListener()
244 public void actionPerformed(ActionEvent e)
246 annotations_actionPerformed(e);
249 threshold.addActionListener(new ActionListener()
251 public void actionPerformed(ActionEvent e)
253 threshold_actionPerformed(e);
256 thresholdValue.addActionListener(new ActionListener()
258 public void actionPerformed(ActionEvent e)
260 thresholdValue_actionPerformed(e);
263 slider.setPaintLabels(false);
264 slider.setPaintTicks(true);
265 slider.setBackground(Color.white);
266 slider.setEnabled(false);
267 slider.setOpaque(false);
268 slider.setPreferredSize(new Dimension(100, 32));
269 thresholdValue.setEnabled(false);
270 thresholdValue.setColumns(7);
271 currentColours.setFont(JvSwingUtils.getLabelFont());
272 currentColours.setOpaque(false);
273 currentColours.setText("Use Original Colours");
274 currentColours.addActionListener(new ActionListener()
276 public void actionPerformed(ActionEvent e)
278 currentColours_actionPerformed(e);
281 thresholdIsMin.setBackground(Color.white);
282 thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
283 thresholdIsMin.setText("Threshold is Min/Max");
284 thresholdIsMin.addActionListener(new ActionListener()
286 public void actionPerformed(ActionEvent actionEvent)
288 thresholdIsMin_actionPerformed(actionEvent);
291 seqAssociated.setBackground(Color.white);
292 seqAssociated.setFont(JvSwingUtils.getLabelFont());
293 seqAssociated.setText("Per-sequence only");
294 seqAssociated.addActionListener(new ActionListener()
298 public void actionPerformed(ActionEvent arg0)
300 seqAssociated_actionPerformed(arg0);
304 this.setLayout(borderLayout1);
305 jPanel2.setLayout(new MigLayout("","[left][center][right]","[][][]"));
306 jPanel1.setBackground(Color.white);
307 jPanel2.setBackground(Color.white);
311 jPanel2.add(annotations);
312 jPanel2.add(currentColours);
313 JPanel colpanel = new JPanel(new FlowLayout());
314 colpanel.setBackground(Color.white);
315 colpanel.add(minColour);
316 colpanel.add(maxColour);
317 jPanel2.add(colpanel, "wrap");
318 jPanel2.add(seqAssociated, "wrap");
319 jPanel2.add(threshold);
320 jPanel2.add(defColours,"skip 1, wrap");
321 jPanel2.add(thresholdIsMin);
322 jPanel2.add(slider, "grow");
323 jPanel2.add(thresholdValue, "grow");
324 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
325 this.add(jPanel2, java.awt.BorderLayout.CENTER);
329 protected void seqAssociated_actionPerformed(ActionEvent arg0)
334 protected void resetColours_actionPerformed(ActionEvent arg0)
340 JComboBox annotations;
342 JPanel minColour = new JPanel();
344 JPanel maxColour = new JPanel();
345 JButton defColours = new JButton();
346 JButton ok = new JButton();
348 JButton cancel = new JButton();
350 JPanel jPanel1 = new JPanel();
351 JPanel jPanel2 = new JPanel();
353 BorderLayout borderLayout1 = new BorderLayout();
355 JComboBox threshold = new JComboBox();
358 JSlider slider = new JSlider();
360 JTextField thresholdValue = new JTextField(20);
362 JCheckBox currentColours = new JCheckBox();
364 JCheckBox thresholdIsMin = new JCheckBox();
366 JCheckBox seqAssociated = new JCheckBox();
368 public void minColour_actionPerformed()
370 Color col = JColorChooser.showDialog(this,
371 "Select Colour for Minimum Value", minColour.getBackground());
374 minColour.setBackground(col);
380 public void maxColour_actionPerformed()
382 Color col = JColorChooser.showDialog(this,
383 "Select Colour for Maximum Value", maxColour.getBackground());
386 maxColour.setBackground(col);
394 // Check if combobox is still adjusting
400 currentAnnotation = av.getAlignment().getAlignmentAnnotation()[annotations
401 .getSelectedIndex()];
403 int aboveThreshold = -1;
404 if (threshold.getSelectedItem().equals("Above Threshold"))
406 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
408 else if (threshold.getSelectedItem().equals("Below Threshold"))
410 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
413 slider.setEnabled(true);
414 thresholdValue.setEnabled(true);
415 thresholdIsMin.setEnabled(true);
417 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
419 slider.setEnabled(false);
420 thresholdValue.setEnabled(false);
421 thresholdValue.setText("");
422 thresholdIsMin.setEnabled(false);
424 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
425 && currentAnnotation.threshold == null)
428 .setThreshold(new jalview.datamodel.GraphLine(
429 (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
430 "Threshold", Color.black));
433 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
436 float range = currentAnnotation.graphMax * 1000
437 - currentAnnotation.graphMin * 1000;
439 slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
440 slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
441 slider.setValue((int) (currentAnnotation.threshold.value * 1000));
442 thresholdValue.setText(currentAnnotation.threshold.value + "");
443 slider.setMajorTickSpacing((int) (range / 10f));
444 slider.setEnabled(true);
445 thresholdValue.setEnabled(true);
449 AnnotationColourGradient acg = null;
450 if (currentColours.isSelected())
452 acg = new AnnotationColourGradient(currentAnnotation,
453 av.getGlobalColourScheme(), aboveThreshold);
457 acg = new AnnotationColourGradient(currentAnnotation,
458 minColour.getBackground(), maxColour.getBackground(),
461 acg.setSeqAssociated(seqAssociated.isSelected());
463 if (currentAnnotation.graphMin == 0f
464 && currentAnnotation.graphMax == 0f)
466 acg.predefinedColours = true;
469 acg.thresholdIsMinMax = thresholdIsMin.isSelected();
471 av.setGlobalColourScheme(acg);
473 if (av.getAlignment().getGroups() != null)
476 for (SequenceGroup sg:ap.av.getAlignment().getGroups())
483 if (currentColours.isSelected())
485 sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
487 ((AnnotationColourGradient)sg.cs).setSeqAssociated(seqAssociated.isSelected());
492 sg.cs = new AnnotationColourGradient(currentAnnotation,
493 minColour.getBackground(), maxColour.getBackground(),
495 ((AnnotationColourGradient)sg.cs).setSeqAssociated(seqAssociated.isSelected());
500 ap.alignmentChanged();
501 // ensure all associated views (overviews, structures, etc) are notified of updated colours.
502 ap.paintAlignment(true);
505 public void ok_actionPerformed(ActionEvent e)
510 frame.setClosed(true);
511 } catch (Exception ex)
516 public void cancel_actionPerformed(ActionEvent e)
519 // ensure all original colouring is propagated to listeners.
520 ap.paintAlignment(true);
523 frame.setClosed(true);
524 } catch (Exception ex)
531 av.setGlobalColourScheme(oldcs);
532 if (av.getAlignment().getGroups() != null)
535 for (SequenceGroup sg:ap.av.getAlignment().getGroups())
537 sg.cs = (ColourSchemeI) oldgroupColours.get(sg);
542 public void thresholdCheck_actionPerformed(ActionEvent e)
547 public void annotations_actionPerformed(ActionEvent e)
552 public void threshold_actionPerformed(ActionEvent e)
557 public void thresholdValue_actionPerformed(ActionEvent e)
561 float f = Float.parseFloat(thresholdValue.getText());
562 slider.setValue((int) (f * 1000));
563 } catch (NumberFormatException ex)
568 public void valueChanged()
570 if (currentColours.isSelected()
571 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
576 currentAnnotation.threshold.value = (float) slider.getValue() / 1000f;
577 ap.paintAlignment(false);
580 public void currentColours_actionPerformed(ActionEvent e)
582 if (currentColours.isSelected())
587 maxColour.setEnabled(!currentColours.isSelected());
588 minColour.setEnabled(!currentColours.isSelected());
593 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)