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