Merge branch 'JAL-3878_ws-overhaul-3' into with_ws_overhaul-3
[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   private JCheckBox transparency = new JCheckBox();
70
71   protected static final int MIN_WIDTH = 500;
72
73   protected static final int MIN_HEIGHT = 240;
74
75   public AnnotationColourChooser(AlignViewport av, final AlignmentPanel ap)
76   {
77     super(av, ap);
78     oldcs = av.getGlobalColourScheme();
79     if (av.getAlignment().getGroups() != null)
80     {
81       oldgroupColours = new Hashtable<>();
82       for (SequenceGroup sg : ap.av.getAlignment().getGroups())
83       {
84         if (sg.getColourScheme() != null)
85         {
86           oldgroupColours.put(sg, sg.getColourScheme());
87         }
88       }
89     }
90     frame = new JInternalFrame();
91     frame.setContentPane(this);
92     frame.setLayer(JLayeredPane.PALETTE_LAYER);
93     Desktop.addInternalFrame(frame,
94             MessageManager.getString("label.colour_by_annotation"), 520,
95             215);
96     frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
97     addSliderChangeListener();
98     addSliderMouseListeners();
99
100     if (av.getAlignment().getAlignmentAnnotation() == null)
101     {
102       return;
103     }
104
105     // Always get default shading from preferences.
106     setDefaultMinMax();
107
108     adjusting = true;
109     if (oldcs instanceof AnnotationColourGradient)
110     {
111       AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
112       useOriginalColours.setSelected(
113               acg.isPredefinedColours() || acg.getBaseColour() != null);
114       if (!acg.isPredefinedColours() && acg.getBaseColour() == null)
115       {
116         minColour.setBackground(acg.getMinColour());
117         maxColour.setBackground(acg.getMaxColour());
118       }
119       seqAssociated.setSelected(acg.isSeqAssociated());
120
121     }
122     Vector<String> annotItems = getAnnotationItems(
123             seqAssociated.isSelected());
124     annotations = new JComboBox<>(annotItems);
125
126     populateThresholdComboBox(threshold);
127
128     if (oldcs instanceof AnnotationColourGradient)
129     {
130       AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
131       String label = getAnnotationMenuLabel(acg.getAnnotation());
132       annotations.setSelectedItem(label);
133       switch (acg.getAboveThreshold())
134       {
135       case AnnotationColourGradient.NO_THRESHOLD:
136         getThreshold().setSelectedIndex(0);
137         break;
138       case AnnotationColourGradient.ABOVE_THRESHOLD:
139         getThreshold().setSelectedIndex(1);
140         break;
141       case AnnotationColourGradient.BELOW_THRESHOLD:
142         getThreshold().setSelectedIndex(2);
143         break;
144       default:
145         throw new Error(MessageManager.getString(
146                 "error.implementation_error_dont_know_about_threshold_setting"));
147       }
148       thresholdIsMin.setSelected(acg.isThresholdIsMinMax());
149       thresholdValue.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     transparency.setBackground(Color.white);
250     transparency.setFont(JvSwingUtils.getLabelFont());
251     transparency
252             .setText(MessageManager.getString("Use Transparency"));
253     transparency.addActionListener(new ActionListener()
254     {
255
256       @Override
257       public void actionPerformed(ActionEvent arg0)
258       {
259         transparency_actionPerformed();
260       }
261     });
262     this.setLayout(new BorderLayout());
263     JPanel jPanel1 = new JPanel();
264     JPanel jPanel2 = new JPanel();
265     jPanel2.setLayout(new MigLayout("", "[left][center][right]", "[][][]"));
266     jPanel1.setBackground(Color.white);
267     jPanel2.setBackground(Color.white);
268
269     jPanel1.add(ok);
270     jPanel1.add(cancel);
271     jPanel2.add(annotations, "grow, wrap");
272     jPanel2.add(seqAssociated);
273     jPanel2.add(useOriginalColours);
274     jPanel2.add(transparency);
275     JPanel colpanel = new JPanel(new FlowLayout());
276     colpanel.setBackground(Color.white);
277     colpanel.add(minColour);
278     colpanel.add(maxColour);
279     jPanel2.add(colpanel, "wrap");
280     jPanel2.add(getThreshold());
281     jPanel2.add(defColours, "skip 1, wrap");
282     jPanel2.add(thresholdIsMin);
283     jPanel2.add(slider, "grow");
284     jPanel2.add(thresholdValue, "grow");
285     this.add(jPanel1, java.awt.BorderLayout.SOUTH);
286     this.add(jPanel2, java.awt.BorderLayout.CENTER);
287     this.validate();
288   }
289
290   protected void transparency_actionPerformed()
291   {
292     updateView();
293   }
294   protected void resetColours_actionPerformed()
295   {
296     setDefaultMinMax();
297     updateView();
298   }
299
300   private void setDefaultMinMax()
301   {
302     minColour.setBackground(
303             Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN", Color.orange));
304     maxColour.setBackground(
305             Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX", Color.red));
306   }
307
308   protected void showColourChooser(JPanel colourPanel, String titleKey)
309   {
310     String ttl = MessageManager.getString(titleKey);
311     ColourChooserListener listener = new ColourChooserListener()
312     {
313       @Override
314       public void colourSelected(Color c)
315       {
316         colourPanel.setBackground(c);
317         colourPanel.repaint();
318         updateView();
319       }
320     };
321     JalviewColourChooser.showColourChooser(Desktop.getDesktopPane(), ttl,
322             colourPanel.getBackground(), listener);
323   }
324
325   @Override
326   public void reset()
327   {
328     this.ap.alignFrame.changeColour(oldcs);
329     if (av.getAlignment().getGroups() != null)
330     {
331
332       for (SequenceGroup sg : ap.av.getAlignment().getGroups())
333       {
334         sg.setColourScheme(oldgroupColours.get(sg));
335       }
336     }
337   }
338
339   @Override
340   public void valueChanged(boolean updateAllAnnotation)
341   {
342     if (slider.isEnabled())
343     {
344       if (useOriginalColours.isSelected() && !(av
345               .getGlobalColourScheme() instanceof AnnotationColourGradient))
346       {
347         updateView();
348       }
349       getCurrentAnnotation().threshold.value = getSliderValue();
350       propagateSeqAssociatedThreshold(updateAllAnnotation,
351               getCurrentAnnotation());
352       ap.paintAlignment(false, false);
353     }
354   }
355
356   public void originalColours_actionPerformed()
357   {
358     boolean selected = useOriginalColours.isSelected();
359     if (selected)
360     {
361       reset();
362     }
363     maxColour.setEnabled(!selected);
364     minColour.setEnabled(!selected);
365     thresholdIsMin.setEnabled(!selected);
366     updateView();
367   }
368
369   @Override
370   public void updateView()
371   {
372     // Check if combobox is still adjusting
373     if (adjusting)
374     {
375       return;
376     }
377
378     setCurrentAnnotation(
379             av.getAlignment().getAlignmentAnnotation()[annmap[annotations
380                     .getSelectedIndex()]]);
381
382     int selectedThresholdItem = getSelectedThresholdItem(
383             getThreshold().getSelectedIndex());
384
385     slider.setEnabled(true);
386     thresholdValue.setEnabled(true);
387     thresholdIsMin.setEnabled(!useOriginalColours.isSelected());
388
389     final AlignmentAnnotation currentAnnotation = getCurrentAnnotation();
390     if (selectedThresholdItem == AnnotationColourGradient.NO_THRESHOLD)
391     {
392       slider.setEnabled(false);
393       thresholdValue.setEnabled(false);
394       thresholdValue.setText("");
395       thresholdIsMin.setEnabled(false);
396     }
397     else if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD
398             && currentAnnotation.threshold == null)
399     {
400       currentAnnotation.setThreshold(new GraphLine(
401               (currentAnnotation.graphMax - currentAnnotation.graphMin)
402                       / 2f,
403               "Threshold", Color.black));
404     }
405
406     if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD)
407     {
408       adjusting = true;
409       setSliderModel(currentAnnotation.graphMin, currentAnnotation.graphMax,
410               currentAnnotation.threshold.value);
411       slider.setEnabled(true);
412
413       setThresholdValueText();
414       thresholdValue.setEnabled(true);
415       adjusting = false;
416     }
417     colorAlignmentContaining(currentAnnotation, selectedThresholdItem);
418
419     ap.alignmentChanged();
420   }
421
422   protected void colorAlignmentContaining(AlignmentAnnotation currentAnn,
423           int selectedThresholdOption)
424   {
425
426     AnnotationColourGradient acg = null;
427     if (useOriginalColours.isSelected())
428     {
429       acg = new AnnotationColourGradient(currentAnn,
430               av.getGlobalColourScheme(), selectedThresholdOption);
431     }
432     else
433     {
434       acg = new AnnotationColourGradient(currentAnn,
435               minColour.getBackground(), maxColour.getBackground(),
436               selectedThresholdOption);
437     }
438     acg.setSeqAssociated(seqAssociated.isSelected());
439
440     if (currentAnn.graphMin == 0f && currentAnn.graphMax == 0f)
441     {
442       acg.setPredefinedColours(true);
443     }
444
445     acg.setThresholdIsMinMax(thresholdIsMin.isSelected());
446
447     acg.setPositionToTransparency(transparency.isSelected());
448     
449     this.ap.alignFrame.changeColour(acg);
450
451     if (av.getAlignment().getGroups() != null)
452     {
453
454       for (SequenceGroup sg : ap.av.getAlignment().getGroups())
455       {
456         if (sg.cs == null)
457         {
458           continue;
459         }
460         sg.setColourScheme(acg.getInstance(av, sg));
461       }
462     }
463   }
464
465   @Override
466   protected void sliderDragReleased()
467   {
468     super.sliderDragReleased();
469     ap.paintAlignment(true, true);
470   }
471 }