Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.gui

File AnnotationColourChooser.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
56% of files have more coverage

Code metrics

40
178
18
1
452
377
45
0.25
9.89
18
2.5

Classes

Class Line # Actions
AnnotationColourChooser 54 178 45
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
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.
11    *
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.
16    *
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.
20    */
21    package jalview.gui;
22   
23    import jalview.bin.Cache;
24    import jalview.datamodel.AlignmentAnnotation;
25    import jalview.datamodel.GraphLine;
26    import jalview.datamodel.SequenceGroup;
27    import jalview.gui.JalviewColourChooser.ColourChooserListener;
28    import jalview.schemes.AnnotationColourGradient;
29    import jalview.schemes.ColourSchemeI;
30    import jalview.util.MessageManager;
31   
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;
42   
43    import javax.swing.BorderFactory;
44    import javax.swing.JButton;
45    import javax.swing.JCheckBox;
46    import javax.swing.JComboBox;
47    import javax.swing.JInternalFrame;
48    import javax.swing.JLayeredPane;
49    import javax.swing.JPanel;
50   
51    import net.miginfocom.swing.MigLayout;
52   
53    @SuppressWarnings("serial")
 
54    public class AnnotationColourChooser extends AnnotationRowFilter
55    {
56    private ColourSchemeI oldcs;
57   
58    private JButton defColours;
59   
60    private Hashtable<SequenceGroup, ColourSchemeI> oldgroupColours;
61   
62    private JCheckBox useOriginalColours = new JCheckBox();
63   
64    JPanel minColour = new JPanel();
65   
66    JPanel maxColour = new JPanel();
67   
68    private JCheckBox thresholdIsMin = new JCheckBox();
69   
70    protected static final int MIN_WIDTH = 500;
71   
72    protected static final int MIN_HEIGHT = 240;
73   
 
74  0 toggle public AnnotationColourChooser(AlignViewport av, final AlignmentPanel ap)
75    {
76  0 super(av, ap);
77  0 oldcs = av.getGlobalColourScheme();
78  0 if (av.getAlignment().getGroups() != null)
79    {
80  0 oldgroupColours = new Hashtable<>();
81  0 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
82    {
83  0 if (sg.getColourScheme() != null)
84    {
85  0 oldgroupColours.put(sg, sg.getColourScheme());
86    }
87    }
88    }
89  0 frame = new JInternalFrame();
90  0 frame.setContentPane(this);
91  0 frame.setLayer(JLayeredPane.PALETTE_LAYER);
92  0 Desktop.addInternalFrame(frame,
93    MessageManager.getString("label.colour_by_annotation"), 520,
94    215);
95  0 frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
96  0 addSliderChangeListener();
97  0 addSliderMouseListeners();
98   
99  0 if (av.getAlignment().getAlignmentAnnotation() == null)
100    {
101  0 return;
102    }
103   
104    // Always get default shading from preferences.
105  0 setDefaultMinMax();
106   
107  0 adjusting = true;
108  0 if (oldcs instanceof AnnotationColourGradient)
109    {
110  0 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
111  0 useOriginalColours.setSelected(
112    acg.isPredefinedColours() || acg.getBaseColour() != null);
113  0 if (!acg.isPredefinedColours() && acg.getBaseColour() == null)
114    {
115  0 minColour.setBackground(acg.getMinColour());
116  0 maxColour.setBackground(acg.getMaxColour());
117    }
118  0 seqAssociated.setSelected(acg.isSeqAssociated());
119   
120    }
121  0 Vector<String> annotItems = getAnnotationItems(
122    seqAssociated.isSelected());
123  0 annotations = new JComboBox<>(annotItems);
124   
125  0 populateThresholdComboBox(threshold);
126   
127  0 if (oldcs instanceof AnnotationColourGradient)
128    {
129  0 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
130  0 String label = getAnnotationMenuLabel(acg.getAnnotation());
131  0 annotations.setSelectedItem(label);
132  0 switch (acg.getAboveThreshold())
133    {
134  0 case AnnotationColourGradient.NO_THRESHOLD:
135  0 getThreshold().setSelectedIndex(0);
136  0 break;
137  0 case AnnotationColourGradient.ABOVE_THRESHOLD:
138  0 getThreshold().setSelectedIndex(1);
139  0 break;
140  0 case AnnotationColourGradient.BELOW_THRESHOLD:
141  0 getThreshold().setSelectedIndex(2);
142  0 break;
143  0 default:
144  0 throw new Error(MessageManager.getString(
145    "error.implementation_error_dont_know_about_threshold_setting"));
146    }
147  0 thresholdIsMin.setSelected(acg.isThresholdIsMinMax());
148  0 thresholdValue
149    .setText(String.valueOf(acg.getAnnotationThreshold()));
150    }
151   
152  0 jbInit();
153  0 adjusting = false;
154   
155  0 updateView();
156  0 frame.invalidate();
157  0 frame.pack();
158    }
159   
 
160  0 toggle @Override
161    protected void jbInit()
162    {
163  0 super.jbInit();
164   
165  0 minColour.setFont(JvSwingUtils.getLabelFont());
166  0 minColour.setBorder(BorderFactory.createEtchedBorder());
167  0 minColour.setPreferredSize(new Dimension(40, 20));
168  0 minColour.setToolTipText(MessageManager.getString("label.min_colour"));
169  0 minColour.addMouseListener(new MouseAdapter()
170    {
 
171  0 toggle @Override
172    public void mousePressed(MouseEvent e)
173    {
174  0 if (minColour.isEnabled())
175    {
176  0 showColourChooser(minColour, "label.select_colour_minimum_value");
177    }
178    }
179    });
180  0 maxColour.setFont(JvSwingUtils.getLabelFont());
181  0 maxColour.setBorder(BorderFactory.createEtchedBorder());
182  0 maxColour.setPreferredSize(new Dimension(40, 20));
183  0 maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
184  0 maxColour.addMouseListener(new MouseAdapter()
185    {
 
186  0 toggle @Override
187    public void mousePressed(MouseEvent e)
188    {
189  0 if (maxColour.isEnabled())
190    {
191  0 showColourChooser(maxColour, "label.select_colour_maximum_value");
192    }
193    }
194    });
195   
196  0 defColours = new JButton();
197  0 defColours.setOpaque(false);
198  0 defColours.setText(MessageManager.getString("action.set_defaults"));
199  0 defColours.setToolTipText(MessageManager
200    .getString("label.reset_min_max_colours_to_defaults"));
201  0 defColours.addActionListener(new ActionListener()
202    {
203   
 
204  0 toggle @Override
205    public void actionPerformed(ActionEvent arg0)
206    {
207  0 resetColours_actionPerformed();
208    }
209    });
210   
211  0 useOriginalColours.setFont(JvSwingUtils.getLabelFont());
212  0 useOriginalColours.setOpaque(false);
213  0 useOriginalColours.setText(
214    MessageManager.getString("label.use_original_colours"));
215  0 useOriginalColours.addActionListener(new ActionListener()
216    {
 
217  0 toggle @Override
218    public void actionPerformed(ActionEvent e)
219    {
220  0 originalColours_actionPerformed();
221    }
222    });
223  0 thresholdIsMin.setBackground(Color.white);
224  0 thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
225  0 thresholdIsMin
226    .setText(MessageManager.getString("label.threshold_minmax"));
227  0 thresholdIsMin.addActionListener(new ActionListener()
228    {
 
229  0 toggle @Override
230    public void actionPerformed(ActionEvent actionEvent)
231    {
232  0 thresholdIsMin_actionPerformed();
233    }
234    });
235  0 seqAssociated.setBackground(Color.white);
236  0 seqAssociated.setFont(JvSwingUtils.getLabelFont());
237  0 seqAssociated
238    .setText(MessageManager.getString("label.per_sequence_only"));
239  0 seqAssociated.addActionListener(new ActionListener()
240    {
241   
 
242  0 toggle @Override
243    public void actionPerformed(ActionEvent arg0)
244    {
245  0 seqAssociated_actionPerformed(annotations);
246    }
247    });
248   
249  0 this.setLayout(new BorderLayout());
250  0 JPanel jPanel1 = new JPanel();
251  0 JPanel jPanel2 = new JPanel();
252  0 jPanel2.setLayout(new MigLayout("", "[left][center][right]", "[][][]"));
253  0 jPanel1.setBackground(Color.white);
254  0 jPanel2.setBackground(Color.white);
255   
256  0 jPanel1.add(ok);
257  0 jPanel1.add(cancel);
258  0 jPanel2.add(annotations, "grow, wrap");
259  0 jPanel2.add(seqAssociated);
260  0 jPanel2.add(useOriginalColours);
261  0 JPanel colpanel = new JPanel(new FlowLayout());
262  0 colpanel.setBackground(Color.white);
263  0 colpanel.add(minColour);
264  0 colpanel.add(maxColour);
265  0 jPanel2.add(colpanel, "wrap");
266  0 jPanel2.add(getThreshold());
267  0 jPanel2.add(defColours, "skip 1, wrap");
268  0 jPanel2.add(thresholdIsMin);
269  0 jPanel2.add(slider, "grow");
270  0 jPanel2.add(thresholdValue, "grow");
271  0 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
272  0 this.add(jPanel2, java.awt.BorderLayout.CENTER);
273  0 this.validate();
274    }
275   
 
276  0 toggle protected void resetColours_actionPerformed()
277    {
278  0 setDefaultMinMax();
279  0 updateView();
280    }
281   
 
282  0 toggle private void setDefaultMinMax()
283    {
284  0 minColour.setBackground(
285    Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN", Color.orange));
286  0 maxColour.setBackground(
287    Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX", Color.red));
288    }
289   
 
290  0 toggle protected void showColourChooser(JPanel colourPanel, String titleKey)
291    {
292  0 String ttl = MessageManager.getString(titleKey);
293  0 ColourChooserListener listener = new ColourChooserListener()
294    {
 
295  0 toggle @Override
296    public void colourSelected(Color c)
297    {
298  0 colourPanel.setBackground(c);
299  0 colourPanel.repaint();
300  0 updateView();
301    }
302    };
303  0 JalviewColourChooser.showColourChooser(Desktop.getDesktop(), ttl,
304    colourPanel.getBackground(), listener);
305    }
306   
 
307  0 toggle @Override
308    public void reset()
309    {
310  0 this.ap.alignFrame.changeColour(oldcs);
311  0 if (av.getAlignment().getGroups() != null)
312    {
313   
314  0 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
315    {
316  0 sg.setColourScheme(oldgroupColours.get(sg));
317    }
318    }
319    }
320   
 
321  0 toggle @Override
322    public void valueChanged(boolean updateAllAnnotation)
323    {
324  0 if (slider.isEnabled())
325    {
326  0 if (useOriginalColours.isSelected() && !(av
327    .getGlobalColourScheme() instanceof AnnotationColourGradient))
328    {
329  0 updateView();
330    }
331  0 getCurrentAnnotation().threshold.value = getSliderValue();
332  0 propagateSeqAssociatedThreshold(updateAllAnnotation,
333    getCurrentAnnotation());
334  0 ap.paintAlignment(false, false);
335    }
336    }
337   
 
338  0 toggle public void originalColours_actionPerformed()
339    {
340  0 boolean selected = useOriginalColours.isSelected();
341  0 if (selected)
342    {
343  0 reset();
344    }
345  0 maxColour.setEnabled(!selected);
346  0 minColour.setEnabled(!selected);
347  0 thresholdIsMin.setEnabled(!selected);
348  0 updateView();
349    }
350   
 
351  0 toggle @Override
352    public void updateView()
353    {
354    // Check if combobox is still adjusting
355  0 if (adjusting)
356    {
357  0 return;
358    }
359   
360  0 setCurrentAnnotation(
361    av.getAlignment().getAlignmentAnnotation()[annmap[annotations
362    .getSelectedIndex()]]);
363   
364  0 int selectedThresholdItem = getSelectedThresholdItem(
365    getThreshold().getSelectedIndex());
366   
367  0 slider.setEnabled(true);
368  0 thresholdValue.setEnabled(true);
369  0 thresholdIsMin.setEnabled(!useOriginalColours.isSelected());
370   
371  0 final AlignmentAnnotation currentAnnotation = getCurrentAnnotation();
372  0 if (selectedThresholdItem == AnnotationColourGradient.NO_THRESHOLD)
373    {
374  0 slider.setEnabled(false);
375  0 thresholdValue.setEnabled(false);
376  0 thresholdValue.setText("");
377  0 thresholdIsMin.setEnabled(false);
378    }
379  0 else if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD
380    && currentAnnotation.threshold == null)
381    {
382  0 currentAnnotation.setThreshold(new GraphLine(
383    (currentAnnotation.graphMax - currentAnnotation.graphMin)
384    / 2f,
385    "Threshold", Color.black));
386    }
387   
388  0 if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD)
389    {
390  0 adjusting = true;
391  0 setSliderModel(currentAnnotation.graphMin, currentAnnotation.graphMax,
392    currentAnnotation.threshold.value);
393  0 slider.setEnabled(true);
394   
395  0 setThresholdValueText();
396  0 thresholdValue.setEnabled(true);
397  0 adjusting = false;
398    }
399  0 colorAlignmentContaining(currentAnnotation, selectedThresholdItem);
400   
401  0 ap.alignmentChanged();
402    }
403   
 
404  0 toggle protected void colorAlignmentContaining(AlignmentAnnotation currentAnn,
405    int selectedThresholdOption)
406    {
407   
408  0 AnnotationColourGradient acg = null;
409  0 if (useOriginalColours.isSelected())
410    {
411  0 acg = new AnnotationColourGradient(currentAnn,
412    av.getGlobalColourScheme(), selectedThresholdOption);
413    }
414    else
415    {
416  0 acg = new AnnotationColourGradient(currentAnn,
417    minColour.getBackground(), maxColour.getBackground(),
418    selectedThresholdOption);
419    }
420  0 acg.setSeqAssociated(seqAssociated.isSelected());
421   
422  0 if (currentAnn.graphMin == 0f && currentAnn.graphMax == 0f)
423    {
424  0 acg.setPredefinedColours(true);
425    }
426   
427  0 acg.setThresholdIsMinMax(thresholdIsMin.isSelected());
428   
429  0 this.ap.alignFrame.changeColour(acg);
430   
431  0 if (av.getAlignment().getGroups() != null)
432    {
433   
434  0 for (SequenceGroup sg : ap.av.getAlignment().getGroups())
435    {
436  0 if (sg.cs == null)
437    {
438  0 continue;
439    }
440  0 sg.setColourScheme(
441    acg.getInstance(av, sg));
442    }
443    }
444    }
445   
 
446  0 toggle @Override
447    protected void sliderDragReleased()
448    {
449  0 super.sliderDragReleased();
450  0 ap.paintAlignment(true, true);
451    }
452    }