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