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