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