ad013f5bb1a90478c99967c3ce79a70c6c862b24
[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
149               .setText(String.valueOf(acg.getAnnotationThreshold()));
150     }
151
152     jbInit();
153     adjusting = false;
154
155     updateView();
156     frame.invalidate();
157     frame.pack();
158   }
159
160   @Override
161   protected void jbInit()
162   {
163     super.jbInit();
164
165     minColour.setFont(JvSwingUtils.getLabelFont());
166     minColour.setBorder(BorderFactory.createEtchedBorder());
167     minColour.setPreferredSize(new Dimension(40, 20));
168     minColour.setToolTipText(MessageManager.getString("label.min_colour"));
169     minColour.addMouseListener(new MouseAdapter()
170     {
171       @Override
172       public void mousePressed(MouseEvent e)
173       {
174         if (minColour.isEnabled())
175         {
176           showColourChooser(minColour, "label.select_colour_minimum_value");
177         }
178       }
179     });
180     maxColour.setFont(JvSwingUtils.getLabelFont());
181     maxColour.setBorder(BorderFactory.createEtchedBorder());
182     maxColour.setPreferredSize(new Dimension(40, 20));
183     maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
184     maxColour.addMouseListener(new MouseAdapter()
185     {
186       @Override
187       public void mousePressed(MouseEvent e)
188       {
189         if (maxColour.isEnabled())
190         {
191           showColourChooser(maxColour, "label.select_colour_maximum_value");
192         }
193       }
194     });
195
196     defColours = new JButton();
197     defColours.setOpaque(false);
198     defColours.setText(MessageManager.getString("action.set_defaults"));
199     defColours.setToolTipText(MessageManager
200             .getString("label.reset_min_max_colours_to_defaults"));
201     defColours.addActionListener(new ActionListener()
202     {
203
204       @Override
205       public void actionPerformed(ActionEvent arg0)
206       {
207         resetColours_actionPerformed();
208       }
209     });
210
211     useOriginalColours.setFont(JvSwingUtils.getLabelFont());
212     useOriginalColours.setOpaque(false);
213     useOriginalColours.setText(
214             MessageManager.getString("label.use_original_colours"));
215     useOriginalColours.addActionListener(new ActionListener()
216     {
217       @Override
218       public void actionPerformed(ActionEvent e)
219       {
220         originalColours_actionPerformed();
221       }
222     });
223     thresholdIsMin.setBackground(Color.white);
224     thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
225     thresholdIsMin
226             .setText(MessageManager.getString("label.threshold_minmax"));
227     thresholdIsMin.addActionListener(new ActionListener()
228     {
229       @Override
230       public void actionPerformed(ActionEvent actionEvent)
231       {
232         thresholdIsMin_actionPerformed();
233       }
234     });
235     seqAssociated.setBackground(Color.white);
236     seqAssociated.setFont(JvSwingUtils.getLabelFont());
237     seqAssociated
238             .setText(MessageManager.getString("label.per_sequence_only"));
239     seqAssociated.addActionListener(new ActionListener()
240     {
241
242       @Override
243       public void actionPerformed(ActionEvent arg0)
244       {
245         seqAssociated_actionPerformed(annotations);
246       }
247     });
248
249     this.setLayout(new BorderLayout());
250     JPanel jPanel1 = new JPanel();
251     JPanel jPanel2 = new JPanel();
252     jPanel2.setLayout(new MigLayout("", "[left][center][right]", "[][][]"));
253     jPanel1.setBackground(Color.white);
254     jPanel2.setBackground(Color.white);
255
256     jPanel1.add(ok);
257     jPanel1.add(cancel);
258     jPanel2.add(annotations, "grow, wrap");
259     jPanel2.add(seqAssociated);
260     jPanel2.add(useOriginalColours);
261     JPanel colpanel = new JPanel(new FlowLayout());
262     colpanel.setBackground(Color.white);
263     colpanel.add(minColour);
264     colpanel.add(maxColour);
265     jPanel2.add(colpanel, "wrap");
266     jPanel2.add(getThreshold());
267     jPanel2.add(defColours, "skip 1, wrap");
268     jPanel2.add(thresholdIsMin);
269     jPanel2.add(slider, "grow");
270     jPanel2.add(thresholdValue, "grow");
271     this.add(jPanel1, java.awt.BorderLayout.SOUTH);
272     this.add(jPanel2, java.awt.BorderLayout.CENTER);
273     this.validate();
274   }
275
276   protected void resetColours_actionPerformed()
277   {
278     setDefaultMinMax();
279     updateView();
280   }
281
282   private void setDefaultMinMax()
283   {
284     minColour.setBackground(
285             Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN", Color.orange));
286     maxColour.setBackground(
287             Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX", Color.red));
288   }
289
290   protected void showColourChooser(JPanel colourPanel, String titleKey)
291   {
292     String ttl = MessageManager.getString(titleKey);
293     ColourChooserListener listener = new ColourChooserListener()
294     {
295       @Override
296       public void colourSelected(Color c)
297       {
298         colourPanel.setBackground(c);
299         colourPanel.repaint();
300         updateView();
301       }
302     };
303     JalviewColourChooser.showColourChooser(Desktop.getDesktop(), ttl,
304             colourPanel.getBackground(), listener);
305   }
306
307   @Override
308   public void reset()
309   {
310     this.ap.alignFrame.changeColour(oldcs);
311     if (av.getAlignment().getGroups() != null)
312     {
313
314       for (SequenceGroup sg : ap.av.getAlignment().getGroups())
315       {
316         sg.setColourScheme(oldgroupColours.get(sg));
317       }
318     }
319   }
320
321   @Override
322   public void valueChanged(boolean updateAllAnnotation)
323   {
324     if (slider.isEnabled())
325     {
326       if (useOriginalColours.isSelected() && !(av
327               .getGlobalColourScheme() instanceof AnnotationColourGradient))
328       {
329         updateView();
330       }
331       getCurrentAnnotation().threshold.value = getSliderValue();
332       propagateSeqAssociatedThreshold(updateAllAnnotation,
333               getCurrentAnnotation());
334       ap.paintAlignment(false, false);
335     }
336   }
337
338   public void originalColours_actionPerformed()
339   {
340     boolean selected = useOriginalColours.isSelected();
341     if (selected)
342     {
343       reset();
344     }
345     maxColour.setEnabled(!selected);
346     minColour.setEnabled(!selected);
347     thresholdIsMin.setEnabled(!selected);
348     updateView();
349   }
350
351   @Override
352   public void updateView()
353   {
354     // Check if combobox is still adjusting
355     if (adjusting)
356     {
357       return;
358     }
359
360     setCurrentAnnotation(
361             av.getAlignment().getAlignmentAnnotation()[annmap[annotations
362                     .getSelectedIndex()]]);
363
364     int selectedThresholdItem = getSelectedThresholdItem(
365             getThreshold().getSelectedIndex());
366
367     slider.setEnabled(true);
368     thresholdValue.setEnabled(true);
369     thresholdIsMin.setEnabled(!useOriginalColours.isSelected());
370
371     final AlignmentAnnotation currentAnnotation = getCurrentAnnotation();
372     if (selectedThresholdItem == AnnotationColourGradient.NO_THRESHOLD)
373     {
374       slider.setEnabled(false);
375       thresholdValue.setEnabled(false);
376       thresholdValue.setText("");
377       thresholdIsMin.setEnabled(false);
378     }
379     else if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD
380             && currentAnnotation.threshold == null)
381     {
382       currentAnnotation.setThreshold(new GraphLine(
383               (currentAnnotation.graphMax - currentAnnotation.graphMin)
384                       / 2f,
385               "Threshold", Color.black));
386     }
387
388     if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD)
389     {
390       adjusting = true;
391       setSliderModel(currentAnnotation.graphMin, currentAnnotation.graphMax,
392               currentAnnotation.threshold.value);
393       slider.setEnabled(true);
394
395       setThresholdValueText();
396       thresholdValue.setEnabled(true);
397       adjusting = false;
398     }
399     colorAlignmentContaining(currentAnnotation, selectedThresholdItem);
400
401     ap.alignmentChanged();
402   }
403
404   protected void colorAlignmentContaining(AlignmentAnnotation currentAnn,
405           int selectedThresholdOption)
406   {
407
408     AnnotationColourGradient acg = null;
409     if (useOriginalColours.isSelected())
410     {
411       acg = new AnnotationColourGradient(currentAnn,
412               av.getGlobalColourScheme(), selectedThresholdOption);
413     }
414     else
415     {
416       acg = new AnnotationColourGradient(currentAnn,
417               minColour.getBackground(), maxColour.getBackground(),
418               selectedThresholdOption);
419     }
420     acg.setSeqAssociated(seqAssociated.isSelected());
421
422     if (currentAnn.graphMin == 0f && currentAnn.graphMax == 0f)
423     {
424       acg.setPredefinedColours(true);
425     }
426
427     acg.setThresholdIsMinMax(thresholdIsMin.isSelected());
428
429     this.ap.alignFrame.changeColour(acg);
430
431     if (av.getAlignment().getGroups() != null)
432     {
433
434       for (SequenceGroup sg : ap.av.getAlignment().getGroups())
435       {
436         if (sg.cs == null)
437         {
438           continue;
439         }
440         sg.setColourScheme(
441                 acg.getInstance(av, sg));
442       }
443     }
444   }
445
446   @Override
447   protected void sliderDragReleased()
448   {
449     super.sliderDragReleased();
450     ap.paintAlignment(true, true);
451   }
452 }