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