2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
26 import java.awt.event.*;
29 import javax.swing.event.*;
31 import net.miginfocom.swing.MigLayout;
33 import jalview.bin.Cache;
34 import jalview.datamodel.*;
35 import jalview.schemes.*;
36 import jalview.util.MessageManager;
38 import java.awt.Dimension;
40 public class AnnotationColourChooser extends JPanel
50 Hashtable oldgroupColours;
52 jalview.datamodel.AlignmentAnnotation currentAnnotation;
54 boolean adjusting = false;
57 * enabled if the user is dragging the slider - try to keep updates to a
60 boolean sliderDragging = false;
62 public AnnotationColourChooser(AlignViewport av, final AlignmentPanel ap)
64 oldcs = av.getGlobalColourScheme();
65 if (av.getAlignment().getGroups() != null)
67 oldgroupColours = new Hashtable();
68 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
72 oldgroupColours.put(sg, sg.cs);
78 frame = new JInternalFrame();
79 frame.setContentPane(this);
80 frame.setLayer(JLayeredPane.PALETTE_LAYER);
81 Desktop.addInternalFrame(frame,
82 MessageManager.getString("label.colour_by_annotation"), 520,
85 slider.addChangeListener(new ChangeListener()
87 public void stateChanged(ChangeEvent evt)
91 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
92 valueChanged(!sliderDragging);
96 slider.addMouseListener(new MouseAdapter()
99 public void mousePressed(MouseEvent e)
101 sliderDragging = true;
102 super.mousePressed(e);
106 public void mouseDragged(MouseEvent e)
108 sliderDragging = true;
109 super.mouseDragged(e);
112 public void mouseReleased(MouseEvent evt)
116 sliderDragging = false;
119 ap.paintAlignment(true);
123 if (av.getAlignment().getAlignmentAnnotation() == null)
128 // Always get default shading from preferences.
132 if (oldcs instanceof AnnotationColourGradient)
134 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
135 currentColours.setSelected(acg.isPredefinedColours()
136 || acg.getBaseColour() != null);
137 if (!acg.isPredefinedColours() && acg.getBaseColour() == null)
139 minColour.setBackground(acg.getMinColour());
140 maxColour.setBackground(acg.getMaxColour());
142 seqAssociated.setSelected(acg.isSeqAssociated());
145 annotations = new JComboBox(
146 getAnnotationItems(seqAssociated.isSelected()));
148 threshold.addItem(MessageManager
149 .getString("label.threshold_feature_no_thereshold"));
150 threshold.addItem(MessageManager
151 .getString("label.threshold_feature_above_thereshold"));
152 threshold.addItem(MessageManager
153 .getString("label.threshold_feature_below_thereshold"));
155 if (oldcs instanceof AnnotationColourGradient)
157 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
158 annotations.setSelectedItem(acg.getAnnotation());
159 switch (acg.getAboveThreshold())
161 case AnnotationColourGradient.NO_THRESHOLD:
162 threshold.setSelectedIndex(0);
164 case AnnotationColourGradient.ABOVE_THRESHOLD:
165 threshold.setSelectedIndex(1);
167 case AnnotationColourGradient.BELOW_THRESHOLD:
168 threshold.setSelectedIndex(2);
172 "Implementation error: don't know about threshold setting for current AnnotationColourGradient.");
174 thresholdIsMin.setSelected(acg.thresholdIsMinMax);
175 thresholdValue.setText("" + acg.getAnnotationThreshold());
181 } catch (Exception ex)
193 private Vector<String> getAnnotationItems(boolean isSeqAssociated)
195 Vector<String> list = new Vector<String>();
197 int[] anmap = new int[av.getAlignment().getAlignmentAnnotation().length];
198 boolean enableSeqAss = false;
199 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
201 if (av.getAlignment().getAlignmentAnnotation()[i].sequenceRef == null)
212 String label = av.getAlignment().getAlignmentAnnotation()[i].label;
213 if (!list.contains(label))
215 anmap[list.size()] = i;
216 list.addElement(label);
221 if (!isSeqAssociated)
223 anmap[list.size()] = i;
224 list.addElement(label + "_" + (index++));
228 seqAssociated.setEnabled(enableSeqAss);
229 annmap = new int[list.size()];
230 System.arraycopy(anmap, 0, annmap, 0, annmap.length);
234 private void setDefaultMinMax()
236 minColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN",
238 maxColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX",
242 public AnnotationColourChooser()
247 } catch (Exception ex)
249 ex.printStackTrace();
253 private void jbInit() throws Exception
255 minColour.setFont(JvSwingUtils.getLabelFont());
256 minColour.setBorder(BorderFactory.createEtchedBorder());
257 minColour.setPreferredSize(new Dimension(40, 20));
258 minColour.setToolTipText(MessageManager.getString("label.min_colour"));
259 minColour.addMouseListener(new MouseAdapter()
261 public void mousePressed(MouseEvent e)
263 if (minColour.isEnabled())
265 minColour_actionPerformed();
269 maxColour.setFont(JvSwingUtils.getLabelFont());
270 maxColour.setBorder(BorderFactory.createEtchedBorder());
271 maxColour.setPreferredSize(new Dimension(40, 20));
272 maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
273 maxColour.addMouseListener(new MouseAdapter()
275 public void mousePressed(MouseEvent e)
277 if (maxColour.isEnabled())
279 maxColour_actionPerformed();
284 ok.setText(MessageManager.getString("action.ok"));
285 ok.addActionListener(new ActionListener()
287 public void actionPerformed(ActionEvent e)
289 ok_actionPerformed(e);
292 cancel.setOpaque(false);
293 cancel.setText(MessageManager.getString("action.cancel"));
294 cancel.addActionListener(new ActionListener()
296 public void actionPerformed(ActionEvent e)
298 cancel_actionPerformed(e);
301 defColours.setOpaque(false);
302 defColours.setText(MessageManager.getString("action.set_defaults"));
303 defColours.setToolTipText(MessageManager
304 .getString("label.reset_min_max_colours_to_defaults"));
305 defColours.addActionListener(new ActionListener()
309 public void actionPerformed(ActionEvent arg0)
311 resetColours_actionPerformed(arg0);
315 annotations.addActionListener(new ActionListener()
317 public void actionPerformed(ActionEvent e)
319 annotations_actionPerformed(e);
322 threshold.addActionListener(new ActionListener()
324 public void actionPerformed(ActionEvent e)
326 threshold_actionPerformed(e);
329 thresholdValue.addActionListener(new ActionListener()
331 public void actionPerformed(ActionEvent e)
333 thresholdValue_actionPerformed(e);
336 slider.setPaintLabels(false);
337 slider.setPaintTicks(true);
338 slider.setBackground(Color.white);
339 slider.setEnabled(false);
340 slider.setOpaque(false);
341 slider.setPreferredSize(new Dimension(100, 32));
342 thresholdValue.setEnabled(false);
343 thresholdValue.setColumns(7);
344 currentColours.setFont(JvSwingUtils.getLabelFont());
345 currentColours.setOpaque(false);
346 currentColours.setText(MessageManager
347 .getString("label.use_original_colours"));
348 currentColours.addActionListener(new ActionListener()
350 public void actionPerformed(ActionEvent e)
352 currentColours_actionPerformed(e);
355 thresholdIsMin.setBackground(Color.white);
356 thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
357 thresholdIsMin.setText(MessageManager
358 .getString("label.threshold_minmax"));
359 thresholdIsMin.addActionListener(new ActionListener()
361 public void actionPerformed(ActionEvent actionEvent)
363 thresholdIsMin_actionPerformed(actionEvent);
366 seqAssociated.setBackground(Color.white);
367 seqAssociated.setFont(JvSwingUtils.getLabelFont());
368 seqAssociated.setText(MessageManager
369 .getString("label.per_sequence_only"));
370 seqAssociated.addActionListener(new ActionListener()
374 public void actionPerformed(ActionEvent arg0)
376 seqAssociated_actionPerformed(arg0);
380 this.setLayout(borderLayout1);
381 jPanel2.setLayout(new MigLayout("", "[left][center][right]", "[][][]"));
382 jPanel1.setBackground(Color.white);
383 jPanel2.setBackground(Color.white);
387 jPanel2.add(annotations, "grow, wrap");
388 jPanel2.add(seqAssociated);
389 jPanel2.add(currentColours);
390 JPanel colpanel = new JPanel(new FlowLayout());
391 colpanel.setBackground(Color.white);
392 colpanel.add(minColour);
393 colpanel.add(maxColour);
394 jPanel2.add(colpanel, "wrap");
395 jPanel2.add(threshold);
396 jPanel2.add(defColours, "skip 1, wrap");
397 jPanel2.add(thresholdIsMin);
398 jPanel2.add(slider, "grow");
399 jPanel2.add(thresholdValue, "grow");
400 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
401 this.add(jPanel2, java.awt.BorderLayout.CENTER);
405 protected void seqAssociated_actionPerformed(ActionEvent arg0)
408 String cursel = (String) annotations.getSelectedItem();
409 boolean isvalid = false, isseqs = seqAssociated.isSelected();
410 this.annotations.removeAllItems();
411 for (String anitem : getAnnotationItems(seqAssociated.isSelected()))
413 if (anitem.equals(cursel) || (isseqs && cursel.startsWith(anitem)))
418 this.annotations.addItem(anitem);
423 this.annotations.setSelectedItem(cursel);
427 if (annotations.getItemCount() > 0)
429 annotations.setSelectedIndex(0);
434 protected void resetColours_actionPerformed(ActionEvent arg0)
440 JComboBox annotations;
444 JPanel minColour = new JPanel();
446 JPanel maxColour = new JPanel();
448 JButton defColours = new JButton();
450 JButton ok = new JButton();
452 JButton cancel = new JButton();
454 JPanel jPanel1 = new JPanel();
456 JPanel jPanel2 = new JPanel();
458 BorderLayout borderLayout1 = new BorderLayout();
460 JComboBox threshold = new JComboBox();
462 JSlider slider = new JSlider();
464 JTextField thresholdValue = new JTextField(20);
466 JCheckBox currentColours = new JCheckBox();
468 JCheckBox thresholdIsMin = new JCheckBox();
470 JCheckBox seqAssociated = new JCheckBox();
472 public void minColour_actionPerformed()
474 Color col = JColorChooser.showDialog(this,
475 "Select Colour for Minimum Value", minColour.getBackground());
478 minColour.setBackground(col);
484 public void maxColour_actionPerformed()
486 Color col = JColorChooser.showDialog(this,
487 "Select Colour for Maximum Value", maxColour.getBackground());
490 maxColour.setBackground(col);
498 // Check if combobox is still adjusting
504 currentAnnotation = av.getAlignment().getAlignmentAnnotation()[annmap[annotations
505 .getSelectedIndex()]];
507 int aboveThreshold = -1;
508 if (threshold.getSelectedIndex() == 1)
510 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
512 else if (threshold.getSelectedIndex() == 2)
514 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
517 slider.setEnabled(true);
518 thresholdValue.setEnabled(true);
519 thresholdIsMin.setEnabled(true);
521 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
523 slider.setEnabled(false);
524 thresholdValue.setEnabled(false);
525 thresholdValue.setText("");
526 thresholdIsMin.setEnabled(false);
528 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
529 && currentAnnotation.threshold == null)
532 .setThreshold(new jalview.datamodel.GraphLine(
533 (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
534 "Threshold", Color.black));
537 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
540 float range = currentAnnotation.graphMax * 1000
541 - currentAnnotation.graphMin * 1000;
543 slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
544 slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
545 slider.setValue((int) (currentAnnotation.threshold.value * 1000));
546 thresholdValue.setText(currentAnnotation.threshold.value + "");
547 slider.setMajorTickSpacing((int) (range / 10f));
548 slider.setEnabled(true);
549 thresholdValue.setEnabled(true);
553 AnnotationColourGradient acg = null;
554 if (currentColours.isSelected())
556 acg = new AnnotationColourGradient(currentAnnotation,
557 av.getGlobalColourScheme(), aboveThreshold);
561 acg = new AnnotationColourGradient(currentAnnotation,
562 minColour.getBackground(), maxColour.getBackground(),
565 acg.setSeqAssociated(seqAssociated.isSelected());
567 if (currentAnnotation.graphMin == 0f
568 && currentAnnotation.graphMax == 0f)
570 acg.setPredefinedColours(true);
573 acg.thresholdIsMinMax = thresholdIsMin.isSelected();
575 av.setGlobalColourScheme(acg);
577 if (av.getAlignment().getGroups() != null)
580 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
587 if (currentColours.isSelected())
589 sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
591 ((AnnotationColourGradient) sg.cs).setSeqAssociated(seqAssociated
597 sg.cs = new AnnotationColourGradient(currentAnnotation,
598 minColour.getBackground(), maxColour.getBackground(),
600 ((AnnotationColourGradient) sg.cs).setSeqAssociated(seqAssociated
606 ap.alignmentChanged();
607 // ensure all associated views (overviews, structures, etc) are notified of
609 ap.paintAlignment(true);
612 public void ok_actionPerformed(ActionEvent e)
617 frame.setClosed(true);
618 } catch (Exception ex)
623 public void cancel_actionPerformed(ActionEvent e)
626 // ensure all original colouring is propagated to listeners.
627 ap.paintAlignment(true);
630 frame.setClosed(true);
631 } catch (Exception ex)
638 av.setGlobalColourScheme(oldcs);
639 if (av.getAlignment().getGroups() != null)
642 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
644 sg.cs = (ColourSchemeI) oldgroupColours.get(sg);
649 public void thresholdCheck_actionPerformed(ActionEvent e)
654 public void annotations_actionPerformed(ActionEvent e)
659 public void threshold_actionPerformed(ActionEvent e)
664 public void thresholdValue_actionPerformed(ActionEvent e)
668 float f = Float.parseFloat(thresholdValue.getText());
669 slider.setValue((int) (f * 1000));
670 } catch (NumberFormatException ex)
675 public void valueChanged(boolean updateAllAnnotation)
677 if (currentColours.isSelected()
678 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
682 currentAnnotation.threshold.value = (float) slider.getValue() / 1000f;
683 propagateSeqAssociatedThreshold(updateAllAnnotation);
684 ap.paintAlignment(false);
687 private void propagateSeqAssociatedThreshold(boolean allAnnotation)
689 if (currentAnnotation.sequenceRef == null
690 || currentAnnotation.threshold == null)
694 // TODO: JAL-1327 only update visible annotation thresholds if allAnnotation
695 // is false, since we only need to provide a quick visual indicator
697 float thr = currentAnnotation.threshold.value;
698 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
700 AlignmentAnnotation aa = av.getAlignment().getAlignmentAnnotation()[i];
701 if (aa.label.equals(currentAnnotation.label))
703 aa.threshold.value = thr;
708 public void currentColours_actionPerformed(ActionEvent e)
710 if (currentColours.isSelected())
715 maxColour.setEnabled(!currentColours.isSelected());
716 minColour.setEnabled(!currentColours.isSelected());
721 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)