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;
30 import jalview.viewmodel.AlignmentViewport;
32 import java.awt.BorderLayout;
33 import java.awt.Color;
34 import java.awt.Dimension;
35 import java.awt.FlowLayout;
36 import java.awt.event.ActionEvent;
37 import java.awt.event.ActionListener;
38 import java.awt.event.MouseAdapter;
39 import java.awt.event.MouseEvent;
40 import java.util.Hashtable;
41 import java.util.Vector;
43 import javax.swing.BorderFactory;
44 import javax.swing.JButton;
45 import javax.swing.JCheckBox;
46 import javax.swing.JColorChooser;
47 import javax.swing.JComboBox;
48 import javax.swing.JInternalFrame;
49 import javax.swing.JLayeredPane;
50 import javax.swing.JPanel;
51 import javax.swing.JSlider;
52 import javax.swing.JTextField;
53 import javax.swing.event.ChangeEvent;
54 import javax.swing.event.ChangeListener;
56 import net.miginfocom.swing.MigLayout;
58 public class AnnotationColourChooser extends JPanel
68 Hashtable oldgroupColours;
70 jalview.datamodel.AlignmentAnnotation currentAnnotation;
72 boolean adjusting = false;
75 * enabled if the user is dragging the slider - try to keep updates to a
78 boolean sliderDragging = false;
80 public AnnotationColourChooser(AlignmentViewport av, final AlignmentPanel ap)
82 oldcs = av.getGlobalColourScheme();
83 if (av.getAlignment().getGroups() != null)
85 oldgroupColours = new Hashtable();
86 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
90 oldgroupColours.put(sg, sg.cs);
96 frame = new JInternalFrame();
97 frame.setContentPane(this);
98 frame.setLayer(JLayeredPane.PALETTE_LAYER);
99 Desktop.addInternalFrame(frame,
100 MessageManager.getString("label.colour_by_annotation"), 520,
103 slider.addChangeListener(new ChangeListener()
106 public void stateChanged(ChangeEvent evt)
110 thresholdValue.setText((slider.getValue() / 1000f) + "");
111 valueChanged(!sliderDragging);
115 slider.addMouseListener(new MouseAdapter()
118 public void mousePressed(MouseEvent e)
120 sliderDragging = true;
121 super.mousePressed(e);
125 public void mouseDragged(MouseEvent e)
127 sliderDragging = true;
128 super.mouseDragged(e);
132 public void mouseReleased(MouseEvent evt)
136 sliderDragging = false;
139 ap.paintAlignment(true);
143 if (av.getAlignment().getAlignmentAnnotation() == null)
148 // Always get default shading from preferences.
152 if (oldcs instanceof AnnotationColourGradient)
154 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
155 currentColours.setSelected(acg.isPredefinedColours()
156 || acg.getBaseColour() != null);
157 if (!acg.isPredefinedColours() && acg.getBaseColour() == null)
159 minColour.setBackground(acg.getMinColour());
160 maxColour.setBackground(acg.getMaxColour());
162 seqAssociated.setSelected(acg.isSeqAssociated());
165 annotations = new JComboBox(
166 getAnnotationItems(seqAssociated.isSelected()));
168 threshold.addItem(MessageManager
169 .getString("label.threshold_feature_no_thereshold"));
170 threshold.addItem(MessageManager
171 .getString("label.threshold_feature_above_thereshold"));
172 threshold.addItem(MessageManager
173 .getString("label.threshold_feature_below_thereshold"));
175 if (oldcs instanceof AnnotationColourGradient)
177 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
178 annotations.setSelectedItem(acg.getAnnotation());
179 switch (acg.getAboveThreshold())
181 case AnnotationColourGradient.NO_THRESHOLD:
182 threshold.setSelectedIndex(0);
184 case AnnotationColourGradient.ABOVE_THRESHOLD:
185 threshold.setSelectedIndex(1);
187 case AnnotationColourGradient.BELOW_THRESHOLD:
188 threshold.setSelectedIndex(2);
191 throw new Error(MessageManager.getString("error.implementation_error_dont_know_about_thereshold_setting"));
193 thresholdIsMin.setSelected(acg.thresholdIsMinMax);
194 thresholdValue.setText("" + acg.getAnnotationThreshold());
200 } catch (Exception ex)
212 private Vector<String> getAnnotationItems(boolean isSeqAssociated)
214 Vector<String> list = new Vector<String>();
216 int[] anmap = new int[av.getAlignment().getAlignmentAnnotation().length];
217 boolean enableSeqAss = false;
218 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
220 if (av.getAlignment().getAlignmentAnnotation()[i].sequenceRef == null)
231 String label = av.getAlignment().getAlignmentAnnotation()[i].label;
232 if (!list.contains(label))
234 anmap[list.size()] = i;
240 if (!isSeqAssociated)
242 anmap[list.size()] = i;
243 list.add(label + "_" + (index++));
247 seqAssociated.setEnabled(enableSeqAss);
248 this.annmap = new int[list.size()];
249 System.arraycopy(anmap, 0, this.annmap, 0, this.annmap.length);
253 private void setDefaultMinMax()
255 minColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN",
257 maxColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX",
261 public AnnotationColourChooser()
266 } catch (Exception ex)
268 ex.printStackTrace();
272 private void jbInit() throws Exception
274 minColour.setFont(JvSwingUtils.getLabelFont());
275 minColour.setBorder(BorderFactory.createEtchedBorder());
276 minColour.setPreferredSize(new Dimension(40, 20));
277 minColour.setToolTipText(MessageManager.getString("label.min_colour"));
278 minColour.addMouseListener(new MouseAdapter()
281 public void mousePressed(MouseEvent e)
283 if (minColour.isEnabled())
285 minColour_actionPerformed();
289 maxColour.setFont(JvSwingUtils.getLabelFont());
290 maxColour.setBorder(BorderFactory.createEtchedBorder());
291 maxColour.setPreferredSize(new Dimension(40, 20));
292 maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
293 maxColour.addMouseListener(new MouseAdapter()
296 public void mousePressed(MouseEvent e)
298 if (maxColour.isEnabled())
300 maxColour_actionPerformed();
305 ok.setText(MessageManager.getString("action.ok"));
306 ok.addActionListener(new ActionListener()
309 public void actionPerformed(ActionEvent e)
311 ok_actionPerformed(e);
314 cancel.setOpaque(false);
315 cancel.setText(MessageManager.getString("action.cancel"));
316 cancel.addActionListener(new ActionListener()
319 public void actionPerformed(ActionEvent e)
321 cancel_actionPerformed(e);
324 defColours.setOpaque(false);
325 defColours.setText(MessageManager.getString("action.set_defaults"));
326 defColours.setToolTipText(MessageManager
327 .getString("label.reset_min_max_colours_to_defaults"));
328 defColours.addActionListener(new ActionListener()
332 public void actionPerformed(ActionEvent arg0)
334 resetColours_actionPerformed(arg0);
338 annotations.addActionListener(new ActionListener()
341 public void actionPerformed(ActionEvent e)
343 annotations_actionPerformed(e);
346 threshold.addActionListener(new ActionListener()
349 public void actionPerformed(ActionEvent e)
351 threshold_actionPerformed(e);
354 thresholdValue.addActionListener(new ActionListener()
357 public void actionPerformed(ActionEvent e)
359 thresholdValue_actionPerformed(e);
362 slider.setPaintLabels(false);
363 slider.setPaintTicks(true);
364 slider.setBackground(Color.white);
365 slider.setEnabled(false);
366 slider.setOpaque(false);
367 slider.setPreferredSize(new Dimension(100, 32));
368 thresholdValue.setEnabled(false);
369 thresholdValue.setColumns(7);
370 currentColours.setFont(JvSwingUtils.getLabelFont());
371 currentColours.setOpaque(false);
372 currentColours.setText(MessageManager
373 .getString("label.use_original_colours"));
374 currentColours.addActionListener(new ActionListener()
377 public void actionPerformed(ActionEvent e)
379 currentColours_actionPerformed(e);
382 thresholdIsMin.setBackground(Color.white);
383 thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
384 thresholdIsMin.setText(MessageManager
385 .getString("label.threshold_minmax"));
386 thresholdIsMin.addActionListener(new ActionListener()
389 public void actionPerformed(ActionEvent actionEvent)
391 thresholdIsMin_actionPerformed(actionEvent);
394 seqAssociated.setBackground(Color.white);
395 seqAssociated.setFont(JvSwingUtils.getLabelFont());
396 seqAssociated.setText(MessageManager
397 .getString("label.per_sequence_only"));
398 seqAssociated.addActionListener(new ActionListener()
402 public void actionPerformed(ActionEvent arg0)
404 seqAssociated_actionPerformed(arg0);
408 this.setLayout(borderLayout1);
409 jPanel2.setLayout(new MigLayout("", "[left][center][right]", "[][][]"));
410 jPanel1.setBackground(Color.white);
411 jPanel2.setBackground(Color.white);
415 jPanel2.add(annotations, "grow, wrap");
416 jPanel2.add(seqAssociated);
417 jPanel2.add(currentColours);
418 JPanel colpanel = new JPanel(new FlowLayout());
419 colpanel.setBackground(Color.white);
420 colpanel.add(minColour);
421 colpanel.add(maxColour);
422 jPanel2.add(colpanel, "wrap");
423 jPanel2.add(threshold);
424 jPanel2.add(defColours, "skip 1, wrap");
425 jPanel2.add(thresholdIsMin);
426 jPanel2.add(slider, "grow");
427 jPanel2.add(thresholdValue, "grow");
428 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
429 this.add(jPanel2, java.awt.BorderLayout.CENTER);
433 protected void seqAssociated_actionPerformed(ActionEvent arg0)
436 String cursel = (String) annotations.getSelectedItem();
437 boolean isvalid = false, isseqs = seqAssociated.isSelected();
438 this.annotations.removeAllItems();
439 for (String anitem : getAnnotationItems(seqAssociated.isSelected()))
441 if (anitem.equals(cursel) || (isseqs && cursel.startsWith(anitem)))
446 this.annotations.addItem(anitem);
451 this.annotations.setSelectedItem(cursel);
455 if (annotations.getItemCount() > 0)
457 annotations.setSelectedIndex(0);
462 protected void resetColours_actionPerformed(ActionEvent arg0)
468 JComboBox annotations;
472 JPanel minColour = new JPanel();
474 JPanel maxColour = new JPanel();
476 JButton defColours = new JButton();
478 JButton ok = new JButton();
480 JButton cancel = new JButton();
482 JPanel jPanel1 = new JPanel();
484 JPanel jPanel2 = new JPanel();
486 BorderLayout borderLayout1 = new BorderLayout();
488 JComboBox threshold = new JComboBox();
490 JSlider slider = new JSlider();
492 JTextField thresholdValue = new JTextField(20);
494 JCheckBox currentColours = new JCheckBox();
496 JCheckBox thresholdIsMin = new JCheckBox();
498 JCheckBox seqAssociated = new JCheckBox();
500 public void minColour_actionPerformed()
502 Color col = JColorChooser.showDialog(this,
503 MessageManager.getString("label.select_colour_minimum_value"), minColour.getBackground());
506 minColour.setBackground(col);
512 public void maxColour_actionPerformed()
514 Color col = JColorChooser.showDialog(this,
515 MessageManager.getString("label.select_colour_maximum_value"), maxColour.getBackground());
518 maxColour.setBackground(col);
526 // Check if combobox is still adjusting
532 currentAnnotation = av.getAlignment().getAlignmentAnnotation()[annmap[annotations
533 .getSelectedIndex()]];
535 int aboveThreshold = -1;
536 if (threshold.getSelectedIndex() == 1)
538 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
540 else if (threshold.getSelectedIndex() == 2)
542 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
545 slider.setEnabled(true);
546 thresholdValue.setEnabled(true);
547 thresholdIsMin.setEnabled(true);
549 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
551 slider.setEnabled(false);
552 thresholdValue.setEnabled(false);
553 thresholdValue.setText("");
554 thresholdIsMin.setEnabled(false);
556 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
557 && currentAnnotation.threshold == null)
560 .setThreshold(new jalview.datamodel.GraphLine(
561 (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
562 "Threshold", Color.black));
565 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
568 float range = currentAnnotation.graphMax * 1000
569 - currentAnnotation.graphMin * 1000;
571 slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
572 slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
573 slider.setValue((int) (currentAnnotation.threshold.value * 1000));
574 thresholdValue.setText(currentAnnotation.threshold.value + "");
575 slider.setMajorTickSpacing((int) (range / 10f));
576 slider.setEnabled(true);
577 thresholdValue.setEnabled(true);
581 AnnotationColourGradient acg = null;
582 if (currentColours.isSelected())
584 acg = new AnnotationColourGradient(currentAnnotation,
585 av.getGlobalColourScheme(), aboveThreshold);
589 acg = new AnnotationColourGradient(currentAnnotation,
590 minColour.getBackground(), maxColour.getBackground(),
593 acg.setSeqAssociated(seqAssociated.isSelected());
595 if (currentAnnotation.graphMin == 0f
596 && currentAnnotation.graphMax == 0f)
598 acg.setPredefinedColours(true);
601 acg.thresholdIsMinMax = thresholdIsMin.isSelected();
603 av.setGlobalColourScheme(acg);
605 if (av.getAlignment().getGroups() != null)
608 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
615 if (currentColours.isSelected())
617 sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
619 ((AnnotationColourGradient) sg.cs).setSeqAssociated(seqAssociated
625 sg.cs = new AnnotationColourGradient(currentAnnotation,
626 minColour.getBackground(), maxColour.getBackground(),
628 ((AnnotationColourGradient) sg.cs).setSeqAssociated(seqAssociated
634 ap.alignmentChanged();
635 // ensure all associated views (overviews, structures, etc) are notified of
637 ap.paintAlignment(true);
640 public void ok_actionPerformed(ActionEvent e)
645 frame.setClosed(true);
646 } catch (Exception ex)
651 public void cancel_actionPerformed(ActionEvent e)
654 // ensure all original colouring is propagated to listeners.
655 ap.paintAlignment(true);
658 frame.setClosed(true);
659 } catch (Exception ex)
666 av.setGlobalColourScheme(oldcs);
667 if (av.getAlignment().getGroups() != null)
670 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
672 sg.cs = (ColourSchemeI) oldgroupColours.get(sg);
677 public void thresholdCheck_actionPerformed(ActionEvent e)
682 public void annotations_actionPerformed(ActionEvent e)
687 public void threshold_actionPerformed(ActionEvent e)
692 public void thresholdValue_actionPerformed(ActionEvent e)
696 float f = Float.parseFloat(thresholdValue.getText());
697 slider.setValue((int) (f * 1000));
698 } catch (NumberFormatException ex)
703 public void valueChanged(boolean updateAllAnnotation)
705 if (currentColours.isSelected()
706 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
710 currentAnnotation.threshold.value = slider.getValue() / 1000f;
711 propagateSeqAssociatedThreshold(updateAllAnnotation);
712 ap.paintAlignment(false);
715 private void propagateSeqAssociatedThreshold(boolean allAnnotation)
717 if (currentAnnotation.sequenceRef == null
718 || currentAnnotation.threshold == null)
722 // TODO: JAL-1327 only update visible annotation thresholds if allAnnotation
723 // is false, since we only need to provide a quick visual indicator
725 float thr = currentAnnotation.threshold.value;
726 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
728 AlignmentAnnotation aa = av.getAlignment().getAlignmentAnnotation()[i];
729 if (aa.label.equals(currentAnnotation.label)
730 && (currentAnnotation.getCalcId() == null ? aa.getCalcId() == null
731 : currentAnnotation.getCalcId()
732 .equals(aa.getCalcId())))
734 if (aa.threshold == null)
736 aa.threshold = new GraphLine(currentAnnotation.threshold);
740 aa.threshold.value = thr;
746 public void currentColours_actionPerformed(ActionEvent e)
748 if (currentColours.isSelected())
753 maxColour.setEnabled(!currentColours.isSelected());
754 minColour.setEnabled(!currentColours.isSelected());
759 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)