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