253a7ec91e03f95ded558a175cee5378f8d91227
[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<SequenceGroup, ColourSchemeI>();
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(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     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           minColour_actionPerformed();
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           maxColour_actionPerformed();
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(MessageManager
216             .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.setText(MessageManager
228             .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.setText(MessageManager
240             .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     this.setLayout(new BorderLayout());
252     JPanel jPanel1 = new JPanel();
253     JPanel jPanel2 = new JPanel();
254     jPanel2.setLayout(new MigLayout("", "[left][center][right]", "[][][]"));
255     jPanel1.setBackground(Color.white);
256     jPanel2.setBackground(Color.white);
257
258     jPanel1.add(ok);
259     jPanel1.add(cancel);
260     jPanel2.add(annotations, "grow, wrap");
261     jPanel2.add(seqAssociated);
262     jPanel2.add(useOriginalColours);
263     JPanel colpanel = new JPanel(new FlowLayout());
264     colpanel.setBackground(Color.white);
265     colpanel.add(minColour);
266     colpanel.add(maxColour);
267     jPanel2.add(colpanel, "wrap");
268     jPanel2.add(getThreshold());
269     jPanel2.add(defColours, "skip 1, wrap");
270     jPanel2.add(thresholdIsMin);
271     jPanel2.add(slider, "grow");
272     jPanel2.add(thresholdValue, "grow");
273     this.add(jPanel1, java.awt.BorderLayout.SOUTH);
274     this.add(jPanel2, java.awt.BorderLayout.CENTER);
275     this.validate();
276   }
277
278   protected void resetColours_actionPerformed()
279   {
280     setDefaultMinMax();
281     updateView();
282   }
283
284   private void setDefaultMinMax()
285   {
286     minColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN",
287             Color.orange));
288     maxColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX",
289             Color.red));
290   }
291
292   public void minColour_actionPerformed()
293   {
294     Color col = JColorChooser.showDialog(this,
295             MessageManager.getString("label.select_colour_minimum_value"),
296             minColour.getBackground());
297     if (col != null)
298     {
299       minColour.setBackground(col);
300     }
301     minColour.repaint();
302     updateView();
303   }
304
305   public void maxColour_actionPerformed()
306   {
307     Color col = JColorChooser.showDialog(this,
308             MessageManager.getString("label.select_colour_maximum_value"),
309             maxColour.getBackground());
310     if (col != null)
311     {
312       maxColour.setBackground(col);
313     }
314     maxColour.repaint();
315     updateView();
316   }
317
318   @Override
319   public void reset()
320   {
321     av.setGlobalColourScheme(oldcs);
322     if (av.getAlignment().getGroups() != null)
323     {
324
325       for (SequenceGroup sg : ap.av.getAlignment().getGroups())
326       {
327         sg.setColourScheme(oldgroupColours.get(sg));
328       }
329     }
330   }
331
332   @Override
333   public void valueChanged(boolean updateAllAnnotation)
334   {
335     if (slider.isEnabled())
336     {
337       if (useOriginalColours.isSelected()
338               && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
339       {
340         updateView();
341       }
342       getCurrentAnnotation().threshold.value = slider.getValue() / 1000f;
343       propagateSeqAssociatedThreshold(updateAllAnnotation,
344               getCurrentAnnotation());
345       ap.paintAlignment(false);
346     }
347   }
348
349   public void originalColours_actionPerformed()
350   {
351     boolean selected = useOriginalColours.isSelected();
352     if (selected)
353     {
354       reset();
355     }
356     maxColour.setEnabled(!selected);
357     minColour.setEnabled(!selected);
358     thresholdIsMin.setEnabled(!selected);
359     updateView();
360   }
361
362   @Override
363   public void updateView()
364   {
365     // Check if combobox is still adjusting
366     if (adjusting)
367     {
368       return;
369     }
370
371     setCurrentAnnotation(av.getAlignment().getAlignmentAnnotation()[annmap[annotations
372             .getSelectedIndex()]]);
373
374     int selectedThresholdItem = getSelectedThresholdItem(getThreshold()
375             .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()
392               .setThreshold(
393                       new GraphLine(
394                               (getCurrentAnnotation().graphMax - getCurrentAnnotation().graphMin) / 2f,
395                               "Threshold", Color.black));
396     }
397
398     if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD)
399     {
400       adjusting = true;
401       float range = getCurrentAnnotation().graphMax * ONETHOUSAND
402               - getCurrentAnnotation().graphMin * ONETHOUSAND;
403
404       slider.setMinimum((int) (getCurrentAnnotation().graphMin * ONETHOUSAND));
405       slider.setMaximum((int) (getCurrentAnnotation().graphMax * ONETHOUSAND));
406       slider.setValue((int) (getCurrentAnnotation().threshold.value * ONETHOUSAND));
407       thresholdValue.setText(getCurrentAnnotation().threshold.value + "");
408       slider.setMajorTickSpacing((int) (range / 10f));
409       slider.setEnabled(true);
410       thresholdValue.setEnabled(true);
411       adjusting = false;
412     }
413     colorAlignmentContaining(getCurrentAnnotation(), selectedThresholdItem);
414
415     ap.alignmentChanged();
416     // ensure all associated views (overviews, structures, etc) are notified of
417     // updated colours.
418     ap.paintAlignment(true);
419   }
420
421   protected boolean colorAlignmentContaining(AlignmentAnnotation currentAnn, 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   
457         if (useOriginalColours.isSelected())
458         {
459           sg.setColourScheme(new AnnotationColourGradient(currentAnn, sg
460                   .getColourScheme(), selectedThresholdOption));
461           ((AnnotationColourGradient) sg.cs).setSeqAssociated(seqAssociated
462                   .isSelected());
463         }
464         else
465         {
466           sg.setColourScheme(new AnnotationColourGradient(currentAnn,
467                   minColour.getBackground(), maxColour.getBackground(),
468                   selectedThresholdOption));
469           ((AnnotationColourGradient) sg.cs).setSeqAssociated(seqAssociated
470                   .isSelected());
471         }
472       }
473     }
474     return false;
475   }
476
477 }