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.
23 import jalview.bin.Cache;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.GraphLine;
26 import jalview.datamodel.SequenceGroup;
27 import jalview.schemes.AnnotationColourGradient;
28 import jalview.schemes.ColourSchemeI;
29 import jalview.util.MessageManager;
31 import java.awt.BorderLayout;
32 import java.awt.Color;
33 import java.awt.Dimension;
34 import java.awt.FlowLayout;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.ActionListener;
37 import java.awt.event.MouseAdapter;
38 import java.awt.event.MouseEvent;
39 import java.util.Hashtable;
40 import java.util.Vector;
42 import javax.swing.BorderFactory;
43 import javax.swing.JButton;
44 import javax.swing.JCheckBox;
45 import javax.swing.JColorChooser;
46 import javax.swing.JComboBox;
47 import javax.swing.JInternalFrame;
48 import javax.swing.JLayeredPane;
49 import javax.swing.JPanel;
50 import javax.swing.JSlider;
51 import javax.swing.JTextField;
52 import javax.swing.event.ChangeEvent;
53 import javax.swing.event.ChangeListener;
55 import net.miginfocom.swing.MigLayout;
57 public class AnnotationColourChooser extends JPanel
67 Hashtable oldgroupColours;
69 jalview.datamodel.AlignmentAnnotation currentAnnotation;
71 boolean adjusting = false;
74 * enabled if the user is dragging the slider - try to keep updates to a
77 boolean sliderDragging = false;
79 public AnnotationColourChooser(AlignViewport av, final AlignmentPanel ap)
81 oldcs = av.getGlobalColourScheme();
82 if (av.getAlignment().getGroups() != null)
84 oldgroupColours = new Hashtable();
85 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
89 oldgroupColours.put(sg, sg.cs);
95 frame = new JInternalFrame();
96 frame.setContentPane(this);
97 frame.setLayer(JLayeredPane.PALETTE_LAYER);
98 Desktop.addInternalFrame(frame,
99 MessageManager.getString("label.colour_by_annotation"), 520,
102 slider.addChangeListener(new ChangeListener()
105 public void stateChanged(ChangeEvent evt)
109 thresholdValue.setText((slider.getValue() / 1000f) + "");
110 valueChanged(!sliderDragging);
114 slider.addMouseListener(new MouseAdapter()
117 public void mousePressed(MouseEvent e)
119 sliderDragging = true;
120 super.mousePressed(e);
124 public void mouseDragged(MouseEvent e)
126 sliderDragging = true;
127 super.mouseDragged(e);
131 public void mouseReleased(MouseEvent evt)
135 sliderDragging = false;
138 ap.paintAlignment(true);
142 if (av.getAlignment().getAlignmentAnnotation() == null)
147 // Always get default shading from preferences.
151 if (oldcs instanceof AnnotationColourGradient)
153 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
154 currentColours.setSelected(acg.isPredefinedColours()
155 || acg.getBaseColour() != null);
156 if (!acg.isPredefinedColours() && acg.getBaseColour() == null)
158 minColour.setBackground(acg.getMinColour());
159 maxColour.setBackground(acg.getMaxColour());
161 seqAssociated.setSelected(acg.isSeqAssociated());
164 annotations = new JComboBox(
165 getAnnotationItems(seqAssociated.isSelected()));
167 threshold.addItem(MessageManager
168 .getString("label.threshold_feature_no_thereshold"));
169 threshold.addItem(MessageManager
170 .getString("label.threshold_feature_above_thereshold"));
171 threshold.addItem(MessageManager
172 .getString("label.threshold_feature_below_thereshold"));
174 if (oldcs instanceof AnnotationColourGradient)
176 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
177 annotations.setSelectedItem(acg.getAnnotation());
178 switch (acg.getAboveThreshold())
180 case AnnotationColourGradient.NO_THRESHOLD:
181 threshold.setSelectedIndex(0);
183 case AnnotationColourGradient.ABOVE_THRESHOLD:
184 threshold.setSelectedIndex(1);
186 case AnnotationColourGradient.BELOW_THRESHOLD:
187 threshold.setSelectedIndex(2);
190 throw new Error(MessageManager.getString("error.implementation_error_dont_know_about_thereshold_setting"));
192 thresholdIsMin.setSelected(acg.thresholdIsMinMax);
193 thresholdValue.setText("" + acg.getAnnotationThreshold());
199 } catch (Exception ex)
211 private Vector<String> getAnnotationItems(boolean isSeqAssociated)
213 Vector<String> list = new Vector<String>();
215 int[] anmap = new int[av.getAlignment().getAlignmentAnnotation().length];
216 boolean enableSeqAss = false;
217 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
219 if (av.getAlignment().getAlignmentAnnotation()[i].sequenceRef == null)
230 String label = av.getAlignment().getAlignmentAnnotation()[i].label;
231 if (!list.contains(label))
233 anmap[list.size()] = i;
239 if (!isSeqAssociated)
241 anmap[list.size()] = i;
242 list.add(label + "_" + (index++));
246 seqAssociated.setEnabled(enableSeqAss);
247 this.annmap = new int[list.size()];
248 System.arraycopy(anmap, 0, this.annmap, 0, this.annmap.length);
252 private void setDefaultMinMax()
254 minColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN",
256 maxColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX",
260 public AnnotationColourChooser()
265 } catch (Exception ex)
267 ex.printStackTrace();
271 private void jbInit() throws Exception
273 minColour.setFont(JvSwingUtils.getLabelFont());
274 minColour.setBorder(BorderFactory.createEtchedBorder());
275 minColour.setPreferredSize(new Dimension(40, 20));
276 minColour.setToolTipText(MessageManager.getString("label.min_colour"));
277 minColour.addMouseListener(new MouseAdapter()
280 public void mousePressed(MouseEvent e)
282 if (minColour.isEnabled())
284 minColour_actionPerformed();
288 maxColour.setFont(JvSwingUtils.getLabelFont());
289 maxColour.setBorder(BorderFactory.createEtchedBorder());
290 maxColour.setPreferredSize(new Dimension(40, 20));
291 maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
292 maxColour.addMouseListener(new MouseAdapter()
295 public void mousePressed(MouseEvent e)
297 if (maxColour.isEnabled())
299 maxColour_actionPerformed();
304 ok.setText(MessageManager.getString("action.ok"));
305 ok.addActionListener(new ActionListener()
308 public void actionPerformed(ActionEvent e)
310 ok_actionPerformed(e);
313 cancel.setOpaque(false);
314 cancel.setText(MessageManager.getString("action.cancel"));
315 cancel.addActionListener(new ActionListener()
318 public void actionPerformed(ActionEvent e)
320 cancel_actionPerformed(e);
323 defColours.setOpaque(false);
324 defColours.setText(MessageManager.getString("action.set_defaults"));
325 defColours.setToolTipText(MessageManager
326 .getString("label.reset_min_max_colours_to_defaults"));
327 defColours.addActionListener(new ActionListener()
331 public void actionPerformed(ActionEvent arg0)
333 resetColours_actionPerformed(arg0);
337 annotations.addActionListener(new ActionListener()
340 public void actionPerformed(ActionEvent e)
342 annotations_actionPerformed(e);
345 threshold.addActionListener(new ActionListener()
348 public void actionPerformed(ActionEvent e)
350 threshold_actionPerformed(e);
353 thresholdValue.addActionListener(new ActionListener()
356 public void actionPerformed(ActionEvent e)
358 thresholdValue_actionPerformed(e);
361 slider.setPaintLabels(false);
362 slider.setPaintTicks(true);
363 slider.setBackground(Color.white);
364 slider.setEnabled(false);
365 slider.setOpaque(false);
366 slider.setPreferredSize(new Dimension(100, 32));
367 thresholdValue.setEnabled(false);
368 thresholdValue.setColumns(7);
369 currentColours.setFont(JvSwingUtils.getLabelFont());
370 currentColours.setOpaque(false);
371 currentColours.setText(MessageManager
372 .getString("label.use_original_colours"));
373 currentColours.addActionListener(new ActionListener()
376 public void actionPerformed(ActionEvent e)
378 currentColours_actionPerformed(e);
381 thresholdIsMin.setBackground(Color.white);
382 thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
383 thresholdIsMin.setText(MessageManager
384 .getString("label.threshold_minmax"));
385 thresholdIsMin.addActionListener(new ActionListener()
388 public void actionPerformed(ActionEvent actionEvent)
390 thresholdIsMin_actionPerformed(actionEvent);
393 seqAssociated.setBackground(Color.white);
394 seqAssociated.setFont(JvSwingUtils.getLabelFont());
395 seqAssociated.setText(MessageManager
396 .getString("label.per_sequence_only"));
397 seqAssociated.addActionListener(new ActionListener()
401 public void actionPerformed(ActionEvent arg0)
403 seqAssociated_actionPerformed(arg0);
407 this.setLayout(borderLayout1);
408 jPanel2.setLayout(new MigLayout("", "[left][center][right]", "[][][]"));
409 jPanel1.setBackground(Color.white);
410 jPanel2.setBackground(Color.white);
414 jPanel2.add(annotations, "grow, wrap");
415 jPanel2.add(seqAssociated);
416 jPanel2.add(currentColours);
417 JPanel colpanel = new JPanel(new FlowLayout());
418 colpanel.setBackground(Color.white);
419 colpanel.add(minColour);
420 colpanel.add(maxColour);
421 jPanel2.add(colpanel, "wrap");
422 jPanel2.add(threshold);
423 jPanel2.add(defColours, "skip 1, wrap");
424 jPanel2.add(thresholdIsMin);
425 jPanel2.add(slider, "grow");
426 jPanel2.add(thresholdValue, "grow");
427 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
428 this.add(jPanel2, java.awt.BorderLayout.CENTER);
432 protected void seqAssociated_actionPerformed(ActionEvent arg0)
435 String cursel = (String) annotations.getSelectedItem();
436 boolean isvalid = false, isseqs = seqAssociated.isSelected();
437 this.annotations.removeAllItems();
438 for (String anitem : getAnnotationItems(seqAssociated.isSelected()))
440 if (anitem.equals(cursel) || (isseqs && cursel.startsWith(anitem)))
445 this.annotations.addItem(anitem);
450 this.annotations.setSelectedItem(cursel);
454 if (annotations.getItemCount() > 0)
456 annotations.setSelectedIndex(0);
461 protected void resetColours_actionPerformed(ActionEvent arg0)
467 JComboBox annotations;
471 JPanel minColour = new JPanel();
473 JPanel maxColour = new JPanel();
475 JButton defColours = new JButton();
477 JButton ok = new JButton();
479 JButton cancel = new JButton();
481 JPanel jPanel1 = new JPanel();
483 JPanel jPanel2 = new JPanel();
485 BorderLayout borderLayout1 = new BorderLayout();
487 JComboBox threshold = new JComboBox();
489 JSlider slider = new JSlider();
491 JTextField thresholdValue = new JTextField(20);
493 JCheckBox currentColours = new JCheckBox();
495 JCheckBox thresholdIsMin = new JCheckBox();
497 JCheckBox seqAssociated = new JCheckBox();
499 public void minColour_actionPerformed()
501 Color col = JColorChooser.showDialog(this,
502 MessageManager.getString("label.select_colour_minimum_value"), minColour.getBackground());
505 minColour.setBackground(col);
511 public void maxColour_actionPerformed()
513 Color col = JColorChooser.showDialog(this,
514 MessageManager.getString("label.select_colour_maximum_value"), maxColour.getBackground());
517 maxColour.setBackground(col);
525 // Check if combobox is still adjusting
531 currentAnnotation = av.getAlignment().getAlignmentAnnotation()[annmap[annotations
532 .getSelectedIndex()]];
534 int aboveThreshold = -1;
535 if (threshold.getSelectedIndex() == 1)
537 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
539 else if (threshold.getSelectedIndex() == 2)
541 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
544 slider.setEnabled(true);
545 thresholdValue.setEnabled(true);
546 thresholdIsMin.setEnabled(true);
548 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
550 slider.setEnabled(false);
551 thresholdValue.setEnabled(false);
552 thresholdValue.setText("");
553 thresholdIsMin.setEnabled(false);
555 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
556 && currentAnnotation.threshold == null)
559 .setThreshold(new jalview.datamodel.GraphLine(
560 (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
561 "Threshold", Color.black));
564 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
567 float range = currentAnnotation.graphMax * 1000
568 - currentAnnotation.graphMin * 1000;
570 slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
571 slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
572 slider.setValue((int) (currentAnnotation.threshold.value * 1000));
573 thresholdValue.setText(currentAnnotation.threshold.value + "");
574 slider.setMajorTickSpacing((int) (range / 10f));
575 slider.setEnabled(true);
576 thresholdValue.setEnabled(true);
580 AnnotationColourGradient acg = null;
581 if (currentColours.isSelected())
583 acg = new AnnotationColourGradient(currentAnnotation,
584 av.getGlobalColourScheme(), aboveThreshold);
588 acg = new AnnotationColourGradient(currentAnnotation,
589 minColour.getBackground(), maxColour.getBackground(),
592 acg.setSeqAssociated(seqAssociated.isSelected());
594 if (currentAnnotation.graphMin == 0f
595 && currentAnnotation.graphMax == 0f)
597 acg.setPredefinedColours(true);
600 acg.thresholdIsMinMax = thresholdIsMin.isSelected();
602 av.setGlobalColourScheme(acg);
604 if (av.getAlignment().getGroups() != null)
607 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
614 if (currentColours.isSelected())
616 sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
618 ((AnnotationColourGradient) sg.cs).setSeqAssociated(seqAssociated
624 sg.cs = new AnnotationColourGradient(currentAnnotation,
625 minColour.getBackground(), maxColour.getBackground(),
627 ((AnnotationColourGradient) sg.cs).setSeqAssociated(seqAssociated
633 ap.alignmentChanged();
634 // ensure all associated views (overviews, structures, etc) are notified of
636 ap.paintAlignment(true);
639 public void ok_actionPerformed(ActionEvent e)
644 frame.setClosed(true);
645 } catch (Exception ex)
650 public void cancel_actionPerformed(ActionEvent e)
653 // ensure all original colouring is propagated to listeners.
654 ap.paintAlignment(true);
657 frame.setClosed(true);
658 } catch (Exception ex)
665 av.setGlobalColourScheme(oldcs);
666 if (av.getAlignment().getGroups() != null)
669 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
671 sg.cs = (ColourSchemeI) oldgroupColours.get(sg);
676 public void thresholdCheck_actionPerformed(ActionEvent e)
681 public void annotations_actionPerformed(ActionEvent e)
686 public void threshold_actionPerformed(ActionEvent e)
691 public void thresholdValue_actionPerformed(ActionEvent e)
695 float f = Float.parseFloat(thresholdValue.getText());
696 slider.setValue((int) (f * 1000));
697 } catch (NumberFormatException ex)
702 public void valueChanged(boolean updateAllAnnotation)
704 if (currentColours.isSelected()
705 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
709 currentAnnotation.threshold.value = slider.getValue() / 1000f;
710 propagateSeqAssociatedThreshold(updateAllAnnotation);
711 ap.paintAlignment(false);
714 private void propagateSeqAssociatedThreshold(boolean allAnnotation)
716 if (currentAnnotation.sequenceRef == null
717 || currentAnnotation.threshold == null)
721 // TODO: JAL-1327 only update visible annotation thresholds if allAnnotation
722 // is false, since we only need to provide a quick visual indicator
724 float thr = currentAnnotation.threshold.value;
725 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
727 AlignmentAnnotation aa = av.getAlignment().getAlignmentAnnotation()[i];
728 if (aa.label.equals(currentAnnotation.label)
729 && (currentAnnotation.getCalcId() == null ? aa.getCalcId() == null
730 : currentAnnotation.getCalcId()
731 .equals(aa.getCalcId())))
733 if (aa.threshold == null)
735 aa.threshold = new GraphLine(currentAnnotation.threshold);
739 aa.threshold.value = thr;
745 public void currentColours_actionPerformed(ActionEvent e)
747 if (currentColours.isSelected())
752 maxColour.setEnabled(!currentColours.isSelected());
753 minColour.setEnabled(!currentColours.isSelected());
758 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)