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;
56 * enabled if the user is dragging the slider - try to keep updates to a minimun
58 boolean sliderDragging = false;
60 public AnnotationColourChooser(AlignViewport av, final AlignmentPanel ap)
62 oldcs = av.getGlobalColourScheme();
63 if (av.getAlignment().getGroups() != null)
65 oldgroupColours = new Hashtable();
66 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
70 oldgroupColours.put(sg, sg.cs);
76 frame = new JInternalFrame();
77 frame.setContentPane(this);
78 frame.setLayer(JLayeredPane.PALETTE_LAYER);
79 Desktop.addInternalFrame(frame, MessageManager.getString("label.colour_by_annotation"), 520, 215);
81 slider.addChangeListener(new ChangeListener()
83 public void stateChanged(ChangeEvent evt)
87 thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
88 valueChanged(!sliderDragging);
92 slider.addMouseListener(new MouseAdapter()
95 public void mousePressed(MouseEvent e)
98 super.mousePressed(e);
102 public void mouseDragged(MouseEvent e)
105 super.mouseDragged(e);
107 public void mouseReleased(MouseEvent evt)
111 sliderDragging=false;
114 ap.paintAlignment(true);
118 if (av.getAlignment().getAlignmentAnnotation() == null)
123 // Always get default shading from preferences.
127 if (oldcs instanceof AnnotationColourGradient)
129 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
130 currentColours.setSelected(acg.isPredefinedColours() || acg.getBaseColour()!=null);
131 if (!acg.isPredefinedColours() && acg.getBaseColour()==null)
133 minColour.setBackground(acg.getMinColour());
134 maxColour.setBackground(acg.getMaxColour());
136 seqAssociated.setSelected(acg.isSeqAssociated());
139 annotations = new JComboBox(
140 getAnnotationItems(seqAssociated.isSelected()));
142 threshold.addItem(MessageManager.getString("label.threshold_feature_no_thereshold"));
143 threshold.addItem(MessageManager.getString("label.threshold_feature_above_thereshold"));
144 threshold.addItem(MessageManager.getString("label.threshold_feature_below_thereshold"));
146 if (oldcs instanceof AnnotationColourGradient)
148 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
149 annotations.setSelectedItem(acg.getAnnotation());
150 switch (acg.getAboveThreshold())
152 case AnnotationColourGradient.NO_THRESHOLD:
153 threshold.setSelectedIndex(0);
155 case AnnotationColourGradient.ABOVE_THRESHOLD:
156 threshold.setSelectedIndex(1);
158 case AnnotationColourGradient.BELOW_THRESHOLD:
159 threshold.setSelectedIndex(2);
163 "Implementation error: don't know about threshold setting for current AnnotationColourGradient.");
165 thresholdIsMin.setSelected(acg.thresholdIsMinMax);
166 thresholdValue.setText("" + acg.getAnnotationThreshold());
172 } catch (Exception ex)
184 private Vector<String> getAnnotationItems(boolean isSeqAssociated)
186 Vector<String> list = new Vector<String>();
188 int[] anmap = new int[av.getAlignment().getAlignmentAnnotation().length];
189 boolean enableSeqAss = false;
190 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
192 if (av.getAlignment().getAlignmentAnnotation()[i].sequenceRef == null)
203 String label = av.getAlignment().getAlignmentAnnotation()[i].label;
204 if (!list.contains(label))
206 anmap[list.size()] = i;
207 list.addElement(label);
212 if (!isSeqAssociated)
214 anmap[list.size()] = i;
215 list.addElement(label + "_" + (index++));
219 seqAssociated.setEnabled(enableSeqAss);
220 annmap = new int[list.size()];
221 System.arraycopy(anmap, 0, annmap, 0, annmap.length);
224 private void setDefaultMinMax()
226 minColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN",
228 maxColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX",
232 public AnnotationColourChooser()
237 } catch (Exception ex)
239 ex.printStackTrace();
243 private void jbInit() throws Exception
245 minColour.setFont(JvSwingUtils.getLabelFont());
246 minColour.setBorder(BorderFactory.createEtchedBorder());
247 minColour.setPreferredSize(new Dimension(40, 20));
248 minColour.setToolTipText(MessageManager.getString("label.min_colour"));
249 minColour.addMouseListener(new MouseAdapter()
251 public void mousePressed(MouseEvent e)
253 if (minColour.isEnabled())
255 minColour_actionPerformed();
259 maxColour.setFont(JvSwingUtils.getLabelFont());
260 maxColour.setBorder(BorderFactory.createEtchedBorder());
261 maxColour.setPreferredSize(new Dimension(40, 20));
262 maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
263 maxColour.addMouseListener(new MouseAdapter()
265 public void mousePressed(MouseEvent e)
267 if (maxColour.isEnabled())
269 maxColour_actionPerformed();
274 ok.setText(MessageManager.getString("action.ok"));
275 ok.addActionListener(new ActionListener()
277 public void actionPerformed(ActionEvent e)
279 ok_actionPerformed(e);
282 cancel.setOpaque(false);
283 cancel.setText(MessageManager.getString("action.cancel"));
284 cancel.addActionListener(new ActionListener()
286 public void actionPerformed(ActionEvent e)
288 cancel_actionPerformed(e);
291 defColours.setOpaque(false);
292 defColours.setText(MessageManager.getString("action.set_defaults"));
294 .setToolTipText(MessageManager.getString("label.reset_min_max_colours_to_defaults"));
295 defColours.addActionListener(new ActionListener()
299 public void actionPerformed(ActionEvent arg0)
301 resetColours_actionPerformed(arg0);
305 annotations.addActionListener(new ActionListener()
307 public void actionPerformed(ActionEvent e)
309 annotations_actionPerformed(e);
312 threshold.addActionListener(new ActionListener()
314 public void actionPerformed(ActionEvent e)
316 threshold_actionPerformed(e);
319 thresholdValue.addActionListener(new ActionListener()
321 public void actionPerformed(ActionEvent e)
323 thresholdValue_actionPerformed(e);
326 slider.setPaintLabels(false);
327 slider.setPaintTicks(true);
328 slider.setBackground(Color.white);
329 slider.setEnabled(false);
330 slider.setOpaque(false);
331 slider.setPreferredSize(new Dimension(100, 32));
332 thresholdValue.setEnabled(false);
333 thresholdValue.setColumns(7);
334 currentColours.setFont(JvSwingUtils.getLabelFont());
335 currentColours.setOpaque(false);
336 currentColours.setText(MessageManager.getString("label.use_original_colours"));
337 currentColours.addActionListener(new ActionListener()
339 public void actionPerformed(ActionEvent e)
341 currentColours_actionPerformed(e);
344 thresholdIsMin.setBackground(Color.white);
345 thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
346 thresholdIsMin.setText(MessageManager.getString("label.threshold_minmax"));
347 thresholdIsMin.addActionListener(new ActionListener()
349 public void actionPerformed(ActionEvent actionEvent)
351 thresholdIsMin_actionPerformed(actionEvent);
354 seqAssociated.setBackground(Color.white);
355 seqAssociated.setFont(JvSwingUtils.getLabelFont());
356 seqAssociated.setText(MessageManager.getString("label.per_sequence_only"));
357 seqAssociated.addActionListener(new ActionListener()
361 public void actionPerformed(ActionEvent arg0)
363 seqAssociated_actionPerformed(arg0);
367 this.setLayout(borderLayout1);
368 jPanel2.setLayout(new MigLayout("", "[left][center][right]", "[][][]"));
369 jPanel1.setBackground(Color.white);
370 jPanel2.setBackground(Color.white);
374 jPanel2.add(annotations, "grow, wrap");
375 jPanel2.add(seqAssociated);
376 jPanel2.add(currentColours);
377 JPanel colpanel = new JPanel(new FlowLayout());
378 colpanel.setBackground(Color.white);
379 colpanel.add(minColour);
380 colpanel.add(maxColour);
381 jPanel2.add(colpanel, "wrap");
382 jPanel2.add(threshold);
383 jPanel2.add(defColours, "skip 1, wrap");
384 jPanel2.add(thresholdIsMin);
385 jPanel2.add(slider, "grow");
386 jPanel2.add(thresholdValue, "grow");
387 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
388 this.add(jPanel2, java.awt.BorderLayout.CENTER);
392 protected void seqAssociated_actionPerformed(ActionEvent arg0)
395 String cursel = (String) annotations.getSelectedItem();
396 boolean isvalid = false, isseqs = seqAssociated.isSelected();
397 this.annotations.removeAllItems();
398 for (String anitem : getAnnotationItems(seqAssociated.isSelected()))
400 if (anitem.equals(cursel) || (isseqs && cursel.startsWith(anitem)))
405 this.annotations.addItem(anitem);
410 this.annotations.setSelectedItem(cursel);
414 if (annotations.getItemCount() > 0)
416 annotations.setSelectedIndex(0);
421 protected void resetColours_actionPerformed(ActionEvent arg0)
427 JComboBox annotations;
431 JPanel minColour = new JPanel();
433 JPanel maxColour = new JPanel();
435 JButton defColours = new JButton();
437 JButton ok = new JButton();
439 JButton cancel = new JButton();
441 JPanel jPanel1 = new JPanel();
443 JPanel jPanel2 = new JPanel();
445 BorderLayout borderLayout1 = new BorderLayout();
447 JComboBox threshold = new JComboBox();
449 JSlider slider = new JSlider();
451 JTextField thresholdValue = new JTextField(20);
453 JCheckBox currentColours = new JCheckBox();
455 JCheckBox thresholdIsMin = new JCheckBox();
457 JCheckBox seqAssociated = new JCheckBox();
459 public void minColour_actionPerformed()
461 Color col = JColorChooser.showDialog(this,
462 "Select Colour for Minimum Value", minColour.getBackground());
465 minColour.setBackground(col);
471 public void maxColour_actionPerformed()
473 Color col = JColorChooser.showDialog(this,
474 "Select Colour for Maximum Value", maxColour.getBackground());
477 maxColour.setBackground(col);
485 // Check if combobox is still adjusting
491 currentAnnotation = av.getAlignment().getAlignmentAnnotation()[annmap[annotations
492 .getSelectedIndex()]];
494 int aboveThreshold = -1;
495 if (threshold.getSelectedIndex()==1)
497 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
499 else if (threshold.getSelectedIndex()==2)
501 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
504 slider.setEnabled(true);
505 thresholdValue.setEnabled(true);
506 thresholdIsMin.setEnabled(true);
508 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
510 slider.setEnabled(false);
511 thresholdValue.setEnabled(false);
512 thresholdValue.setText("");
513 thresholdIsMin.setEnabled(false);
515 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
516 && currentAnnotation.threshold == null)
519 .setThreshold(new jalview.datamodel.GraphLine(
520 (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
521 "Threshold", Color.black));
524 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
527 float range = currentAnnotation.graphMax * 1000
528 - currentAnnotation.graphMin * 1000;
530 slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
531 slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
532 slider.setValue((int) (currentAnnotation.threshold.value * 1000));
533 thresholdValue.setText(currentAnnotation.threshold.value + "");
534 slider.setMajorTickSpacing((int) (range / 10f));
535 slider.setEnabled(true);
536 thresholdValue.setEnabled(true);
540 AnnotationColourGradient acg = null;
541 if (currentColours.isSelected())
543 acg = new AnnotationColourGradient(currentAnnotation,
544 av.getGlobalColourScheme(), aboveThreshold);
548 acg = new AnnotationColourGradient(currentAnnotation,
549 minColour.getBackground(), maxColour.getBackground(),
552 acg.setSeqAssociated(seqAssociated.isSelected());
554 if (currentAnnotation.graphMin == 0f
555 && currentAnnotation.graphMax == 0f)
557 acg.setPredefinedColours(true);
560 acg.thresholdIsMinMax = thresholdIsMin.isSelected();
562 av.setGlobalColourScheme(acg);
564 if (av.getAlignment().getGroups() != null)
567 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
574 if (currentColours.isSelected())
576 sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
578 ((AnnotationColourGradient) sg.cs).setSeqAssociated(seqAssociated
584 sg.cs = new AnnotationColourGradient(currentAnnotation,
585 minColour.getBackground(), maxColour.getBackground(),
587 ((AnnotationColourGradient) sg.cs).setSeqAssociated(seqAssociated
593 ap.alignmentChanged();
594 // ensure all associated views (overviews, structures, etc) are notified of
596 ap.paintAlignment(true);
599 public void ok_actionPerformed(ActionEvent e)
604 frame.setClosed(true);
605 } catch (Exception ex)
610 public void cancel_actionPerformed(ActionEvent e)
613 // ensure all original colouring is propagated to listeners.
614 ap.paintAlignment(true);
617 frame.setClosed(true);
618 } catch (Exception ex)
625 av.setGlobalColourScheme(oldcs);
626 if (av.getAlignment().getGroups() != null)
629 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
631 sg.cs = (ColourSchemeI) oldgroupColours.get(sg);
636 public void thresholdCheck_actionPerformed(ActionEvent e)
641 public void annotations_actionPerformed(ActionEvent e)
646 public void threshold_actionPerformed(ActionEvent e)
651 public void thresholdValue_actionPerformed(ActionEvent e)
655 float f = Float.parseFloat(thresholdValue.getText());
656 slider.setValue((int) (f * 1000));
657 } catch (NumberFormatException ex)
662 public void valueChanged(boolean updateAllAnnotation)
664 if (currentColours.isSelected()
665 && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
669 currentAnnotation.threshold.value = (float) slider.getValue() / 1000f;
670 propagateSeqAssociatedThreshold(updateAllAnnotation);
671 ap.paintAlignment(false);
674 private void propagateSeqAssociatedThreshold(boolean allAnnotation)
676 if (currentAnnotation.sequenceRef == null
677 || currentAnnotation.threshold == null)
681 // TODO: JAL-1327 only update visible annotation thresholds if allAnnotation is false, since we only need to provide a quick visual indicator
683 float thr = currentAnnotation.threshold.value;
684 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
686 AlignmentAnnotation aa = av.getAlignment().getAlignmentAnnotation()[i];
687 if (aa.label.equals(currentAnnotation.label))
689 aa.threshold.value = thr;
694 public void currentColours_actionPerformed(ActionEvent e)
696 if (currentColours.isSelected())
701 maxColour.setEnabled(!currentColours.isSelected());
702 minColour.setEnabled(!currentColours.isSelected());
707 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)