JAL-2346 restore choice of annotation for colouring faithfully
[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 java.awt.BorderLayout;
24 import java.awt.Color;
25 import java.awt.Dimension;
26 import java.awt.FlowLayout;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
29 import java.awt.event.MouseAdapter;
30 import java.awt.event.MouseEvent;
31 import java.util.Hashtable;
32 import java.util.Vector;
33
34 import jalview.bin.Cache;
35 import jalview.datamodel.SequenceGroup;
36 import jalview.schemes.AnnotationColourGradient;
37 import jalview.schemes.ColourSchemeI;
38 import jalview.util.MessageManager;
39
40 import javax.swing.BorderFactory;
41 import javax.swing.JButton;
42 import javax.swing.JColorChooser;
43 import javax.swing.JComboBox;
44 import javax.swing.JInternalFrame;
45 import javax.swing.JLayeredPane;
46 import javax.swing.JPanel;
47
48 import net.miginfocom.swing.MigLayout;
49
50 @SuppressWarnings("serial")
51 public class AnnotationColourChooser extends AnnotationRowFilter
52 {
53
54   ColourSchemeI oldcs;
55
56   Hashtable<SequenceGroup, ColourSchemeI> oldgroupColours;
57
58   /**
59    * enabled if the user is dragging the slider - try to keep updates to a
60    * minimun
61    */
62
63   JComboBox<String> annotations;
64
65   JButton defColours = new JButton();
66
67   JPanel jPanel1 = new JPanel();
68
69   JPanel jPanel2 = new JPanel();
70
71   BorderLayout borderLayout1 = new BorderLayout();
72
73   private JComboBox<String> threshold = new JComboBox<String>();
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<SequenceGroup, ColourSchemeI>();
82       for (SequenceGroup sg : ap.av.getAlignment().getGroups())
83       {
84         if (sg.cs != null)
85         {
86           oldgroupColours.put(sg, sg.cs);
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
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       currentColours.setSelected(acg.isPredefinedColours()
113               || 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(seqAssociated
123             .isSelected());
124     annotations = new JComboBox<String>(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(
146                 MessageManager
147                         .getString("error.implementation_error_dont_know_about_threshold_setting"));
148       }
149       thresholdIsMin.setSelected(acg.isThresholdIsMinMax());
150       thresholdValue.setText("" + acg.getAnnotationThreshold());
151     }
152
153     try
154     {
155       jbInit();
156     } catch (Exception ex)
157     {
158     }
159     adjusting = false;
160
161     updateView();
162     frame.invalidate();
163     frame.pack();
164   }
165
166   public AnnotationColourChooser()
167   {
168     try
169     {
170       jbInit();
171     } catch (Exception ex)
172     {
173       ex.printStackTrace();
174     }
175   }
176
177   private void jbInit() throws Exception
178   {
179     minColour.setFont(JvSwingUtils.getLabelFont());
180     minColour.setBorder(BorderFactory.createEtchedBorder());
181     minColour.setPreferredSize(new Dimension(40, 20));
182     minColour.setToolTipText(MessageManager.getString("label.min_colour"));
183     minColour.addMouseListener(new MouseAdapter()
184     {
185       @Override
186       public void mousePressed(MouseEvent e)
187       {
188         if (minColour.isEnabled())
189         {
190           minColour_actionPerformed();
191         }
192       }
193     });
194     maxColour.setFont(JvSwingUtils.getLabelFont());
195     maxColour.setBorder(BorderFactory.createEtchedBorder());
196     maxColour.setPreferredSize(new Dimension(40, 20));
197     maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
198     maxColour.addMouseListener(new MouseAdapter()
199     {
200       @Override
201       public void mousePressed(MouseEvent e)
202       {
203         if (maxColour.isEnabled())
204         {
205           maxColour_actionPerformed();
206         }
207       }
208     });
209     ok.setOpaque(false);
210     ok.setText(MessageManager.getString("action.ok"));
211     ok.addActionListener(new ActionListener()
212     {
213       @Override
214       public void actionPerformed(ActionEvent e)
215       {
216         ok_actionPerformed();
217       }
218     });
219     cancel.setOpaque(false);
220     cancel.setText(MessageManager.getString("action.cancel"));
221     cancel.addActionListener(new ActionListener()
222     {
223       @Override
224       public void actionPerformed(ActionEvent e)
225       {
226         cancel_actionPerformed();
227       }
228     });
229     defColours.setOpaque(false);
230     defColours.setText(MessageManager.getString("action.set_defaults"));
231     defColours.setToolTipText(MessageManager
232             .getString("label.reset_min_max_colours_to_defaults"));
233     defColours.addActionListener(new ActionListener()
234     {
235
236       @Override
237       public void actionPerformed(ActionEvent arg0)
238       {
239         resetColours_actionPerformed();
240       }
241     });
242
243     annotations.addActionListener(new ActionListener()
244     {
245       @Override
246       public void actionPerformed(ActionEvent e)
247       {
248         annotations_actionPerformed();
249       }
250     });
251     getThreshold().addActionListener(new ActionListener()
252     {
253       @Override
254       public void actionPerformed(ActionEvent e)
255       {
256         threshold_actionPerformed();
257       }
258     });
259     thresholdValue.addActionListener(new ActionListener()
260     {
261       @Override
262       public void actionPerformed(ActionEvent e)
263       {
264         thresholdValue_actionPerformed();
265       }
266     });
267     slider.setPaintLabels(false);
268     slider.setPaintTicks(true);
269     slider.setBackground(Color.white);
270     slider.setEnabled(false);
271     slider.setOpaque(false);
272     slider.setPreferredSize(new Dimension(100, 32));
273     thresholdValue.setEnabled(false);
274     thresholdValue.setColumns(7);
275     currentColours.setFont(JvSwingUtils.getLabelFont());
276     currentColours.setOpaque(false);
277     currentColours.setText(MessageManager
278             .getString("label.use_original_colours"));
279     currentColours.addActionListener(new ActionListener()
280     {
281       @Override
282       public void actionPerformed(ActionEvent e)
283       {
284         currentColours_actionPerformed();
285       }
286     });
287     thresholdIsMin.setBackground(Color.white);
288     thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
289     thresholdIsMin.setText(MessageManager
290             .getString("label.threshold_minmax"));
291     thresholdIsMin.addActionListener(new ActionListener()
292     {
293       @Override
294       public void actionPerformed(ActionEvent actionEvent)
295       {
296         thresholdIsMin_actionPerformed();
297       }
298     });
299     seqAssociated.setBackground(Color.white);
300     seqAssociated.setFont(JvSwingUtils.getLabelFont());
301     seqAssociated.setText(MessageManager
302             .getString("label.per_sequence_only"));
303     seqAssociated.addActionListener(new ActionListener()
304     {
305
306       @Override
307       public void actionPerformed(ActionEvent arg0)
308       {
309         seqAssociated_actionPerformed(annotations);
310       }
311     });
312
313     this.setLayout(borderLayout1);
314     jPanel2.setLayout(new MigLayout("", "[left][center][right]", "[][][]"));
315     jPanel1.setBackground(Color.white);
316     jPanel2.setBackground(Color.white);
317
318     jPanel1.add(ok);
319     jPanel1.add(cancel);
320     jPanel2.add(annotations, "grow, wrap");
321     jPanel2.add(seqAssociated);
322     jPanel2.add(currentColours);
323     JPanel colpanel = new JPanel(new FlowLayout());
324     colpanel.setBackground(Color.white);
325     colpanel.add(minColour);
326     colpanel.add(maxColour);
327     jPanel2.add(colpanel, "wrap");
328     jPanel2.add(getThreshold());
329     jPanel2.add(defColours, "skip 1, wrap");
330     jPanel2.add(thresholdIsMin);
331     jPanel2.add(slider, "grow");
332     jPanel2.add(thresholdValue, "grow");
333     this.add(jPanel1, java.awt.BorderLayout.SOUTH);
334     this.add(jPanel2, java.awt.BorderLayout.CENTER);
335     this.validate();
336   }
337
338   protected void resetColours_actionPerformed()
339   {
340     setDefaultMinMax();
341     updateView();
342   }
343
344   private void setDefaultMinMax()
345   {
346     minColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN",
347             Color.orange));
348     maxColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX",
349             Color.red));
350   }
351
352   public void minColour_actionPerformed()
353   {
354     Color col = JColorChooser.showDialog(this,
355             MessageManager.getString("label.select_colour_minimum_value"),
356             minColour.getBackground());
357     if (col != null)
358     {
359       minColour.setBackground(col);
360     }
361     minColour.repaint();
362     updateView();
363   }
364
365   public void maxColour_actionPerformed()
366   {
367     Color col = JColorChooser.showDialog(this,
368             MessageManager.getString("label.select_colour_maximum_value"),
369             maxColour.getBackground());
370     if (col != null)
371     {
372       maxColour.setBackground(col);
373     }
374     maxColour.repaint();
375     updateView();
376   }
377
378   @Override
379   public void reset()
380   {
381     av.setGlobalColourScheme(oldcs);
382     if (av.getAlignment().getGroups() != null)
383     {
384
385       for (SequenceGroup sg : ap.av.getAlignment().getGroups())
386       {
387         sg.cs = oldgroupColours.get(sg);
388       }
389     }
390   }
391
392   @Override
393   public void valueChanged(boolean updateAllAnnotation)
394   {
395     if (slider.isEnabled())
396     {
397       if (currentColours.isSelected()
398               && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
399       {
400         updateView();
401       }
402       getCurrentAnnotation().threshold.value = slider.getValue() / 1000f;
403       propagateSeqAssociatedThreshold(updateAllAnnotation,
404               getCurrentAnnotation());
405       ap.paintAlignment(false);
406     }
407   }
408
409   public JComboBox<String> getThreshold()
410   {
411     return threshold;
412   }
413
414   public void setThreshold(JComboBox<String> threshold)
415   {
416     this.threshold = threshold;
417   }
418
419   public void currentColours_actionPerformed()
420   {
421     if (currentColours.isSelected())
422     {
423       reset();
424     }
425     maxColour.setEnabled(!currentColours.isSelected());
426     minColour.setEnabled(!currentColours.isSelected());
427     updateView();
428   }
429
430   @Override
431   public void updateView()
432   {
433     // Check if combobox is still adjusting
434     if (adjusting)
435     {
436       return;
437     }
438
439     setCurrentAnnotation(av.getAlignment().getAlignmentAnnotation()[annmap[annotations
440             .getSelectedIndex()]]);
441
442     int selectedThresholdItem = getSelectedThresholdItem(getThreshold()
443             .getSelectedIndex());
444
445     slider.setEnabled(true);
446     thresholdValue.setEnabled(true);
447     thresholdIsMin.setEnabled(true);
448
449     if (selectedThresholdItem == AnnotationColourGradient.NO_THRESHOLD)
450     {
451       slider.setEnabled(false);
452       thresholdValue.setEnabled(false);
453       thresholdValue.setText("");
454       thresholdIsMin.setEnabled(false);
455     }
456     else if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD
457             && getCurrentAnnotation().threshold == null)
458     {
459       getCurrentAnnotation()
460               .setThreshold(
461                       new jalview.datamodel.GraphLine(
462                               (getCurrentAnnotation().graphMax - getCurrentAnnotation().graphMin) / 2f,
463                               "Threshold", Color.black));
464     }
465
466     if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD)
467     {
468       adjusting = true;
469       float range = getCurrentAnnotation().graphMax * 1000
470               - getCurrentAnnotation().graphMin * 1000;
471
472       slider.setMinimum((int) (getCurrentAnnotation().graphMin * 1000));
473       slider.setMaximum((int) (getCurrentAnnotation().graphMax * 1000));
474       slider.setValue((int) (getCurrentAnnotation().threshold.value * 1000));
475       thresholdValue.setText(getCurrentAnnotation().threshold.value + "");
476       slider.setMajorTickSpacing((int) (range / 10f));
477       slider.setEnabled(true);
478       thresholdValue.setEnabled(true);
479       adjusting = false;
480     }
481     colorAlignmentContaining(getCurrentAnnotation(), selectedThresholdItem);
482
483     ap.alignmentChanged();
484     // ensure all associated views (overviews, structures, etc) are notified of
485     // updated colours.
486     ap.paintAlignment(true);
487   }
488
489 }