JAL-1553 enhancement of column selection by annotation row to include the query filte...
[jalview.git] / src / jalview / gui / AnnotationColourChooser.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 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   JButton ok = new JButton();
67
68   JButton cancel = new JButton();
69
70   JPanel jPanel1 = new JPanel();
71
72   JPanel jPanel2 = new JPanel();
73
74   BorderLayout borderLayout1 = new BorderLayout();
75
76   private JComboBox<String> threshold = new JComboBox<String>();
77
78   public AnnotationColourChooser(AlignViewport av, final AlignmentPanel ap)
79   {
80     super(av, ap);
81     oldcs = av.getGlobalColourScheme();
82     if (av.getAlignment().getGroups() != null)
83     {
84       oldgroupColours = new Hashtable<SequenceGroup, ColourSchemeI>();
85       for (SequenceGroup sg : ap.av.getAlignment().getGroups())
86       {
87         if (sg.cs != null)
88         {
89           oldgroupColours.put(sg, sg.cs);
90         }
91       }
92     }
93     frame = new JInternalFrame();
94     frame.setContentPane(this);
95     frame.setLayer(JLayeredPane.PALETTE_LAYER);
96     Desktop.addInternalFrame(frame,
97             MessageManager.getString("label.colour_by_annotation"), 520,
98             215);
99
100     addSliderChangeListener();
101     addSliderMouseListeners();
102
103     if (av.getAlignment().getAlignmentAnnotation() == null)
104     {
105       return;
106     }
107
108     // Always get default shading from preferences.
109     setDefaultMinMax();
110
111     adjusting = true;
112     if (oldcs instanceof AnnotationColourGradient)
113     {
114       AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
115       currentColours.setSelected(acg.isPredefinedColours()
116               || acg.getBaseColour() != null);
117       if (!acg.isPredefinedColours() && acg.getBaseColour() == null)
118       {
119         minColour.setBackground(acg.getMinColour());
120         maxColour.setBackground(acg.getMaxColour());
121       }
122       seqAssociated.setSelected(acg.isSeqAssociated());
123
124     }
125     annotations = new JComboBox<String>(
126             getAnnotationItems(seqAssociated.isSelected()));
127
128     populateThresholdComboBox(threshold);
129
130     if (oldcs instanceof AnnotationColourGradient)
131     {
132       AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
133       annotations.setSelectedItem(acg.getAnnotation());
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(MessageManager.getString("error.implementation_error_dont_know_about_thereshold_setting"));
147       }
148       thresholdIsMin.setSelected(acg.thresholdIsMinMax);
149       thresholdValue.setText("" + acg.getAnnotationThreshold());
150     }
151
152     try
153     {
154       jbInit();
155     } catch (Exception ex)
156     {
157     }
158     adjusting = false;
159
160     updateView();
161     frame.invalidate();
162     frame.pack();
163   }
164
165   public AnnotationColourChooser()
166   {
167     try
168     {
169       jbInit();
170     } catch (Exception ex)
171     {
172       ex.printStackTrace();
173     }
174   }
175
176   private void jbInit() throws Exception
177   {
178     minColour.setFont(JvSwingUtils.getLabelFont());
179     minColour.setBorder(BorderFactory.createEtchedBorder());
180     minColour.setPreferredSize(new Dimension(40, 20));
181     minColour.setToolTipText(MessageManager.getString("label.min_colour"));
182     minColour.addMouseListener(new MouseAdapter()
183     {
184       @Override
185       public void mousePressed(MouseEvent e)
186       {
187         if (minColour.isEnabled())
188         {
189           minColour_actionPerformed();
190         }
191       }
192     });
193     maxColour.setFont(JvSwingUtils.getLabelFont());
194     maxColour.setBorder(BorderFactory.createEtchedBorder());
195     maxColour.setPreferredSize(new Dimension(40, 20));
196     maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
197     maxColour.addMouseListener(new MouseAdapter()
198     {
199       @Override
200       public void mousePressed(MouseEvent e)
201       {
202         if (maxColour.isEnabled())
203         {
204           maxColour_actionPerformed();
205         }
206       }
207     });
208     ok.setOpaque(false);
209     ok.setText(MessageManager.getString("action.ok"));
210     ok.addActionListener(new ActionListener()
211     {
212       @Override
213       public void actionPerformed(ActionEvent e)
214       {
215         ok_actionPerformed(e);
216       }
217     });
218     cancel.setOpaque(false);
219     cancel.setText(MessageManager.getString("action.cancel"));
220     cancel.addActionListener(new ActionListener()
221     {
222       @Override
223       public void actionPerformed(ActionEvent e)
224       {
225         cancel_actionPerformed(e);
226       }
227     });
228     defColours.setOpaque(false);
229     defColours.setText(MessageManager.getString("action.set_defaults"));
230     defColours.setToolTipText(MessageManager
231             .getString("label.reset_min_max_colours_to_defaults"));
232     defColours.addActionListener(new ActionListener()
233     {
234
235       @Override
236       public void actionPerformed(ActionEvent arg0)
237       {
238         resetColours_actionPerformed(arg0);
239       }
240     });
241
242     annotations.addActionListener(new ActionListener()
243     {
244       @Override
245       public void actionPerformed(ActionEvent e)
246       {
247         annotations_actionPerformed(e);
248       }
249     });
250     getThreshold().addActionListener(new ActionListener()
251     {
252       @Override
253       public void actionPerformed(ActionEvent e)
254       {
255         threshold_actionPerformed(e);
256       }
257     });
258     thresholdValue.addActionListener(new ActionListener()
259     {
260       @Override
261       public void actionPerformed(ActionEvent e)
262       {
263         thresholdValue_actionPerformed(e);
264       }
265     });
266     slider.setPaintLabels(false);
267     slider.setPaintTicks(true);
268     slider.setBackground(Color.white);
269     slider.setEnabled(false);
270     slider.setOpaque(false);
271     slider.setPreferredSize(new Dimension(100, 32));
272     thresholdValue.setEnabled(false);
273     thresholdValue.setColumns(7);
274     currentColours.setFont(JvSwingUtils.getLabelFont());
275     currentColours.setOpaque(false);
276     currentColours.setText(MessageManager
277             .getString("label.use_original_colours"));
278     currentColours.addActionListener(new ActionListener()
279     {
280       @Override
281       public void actionPerformed(ActionEvent e)
282       {
283         currentColours_actionPerformed(e);
284       }
285     });
286     thresholdIsMin.setBackground(Color.white);
287     thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
288     thresholdIsMin.setText(MessageManager
289             .getString("label.threshold_minmax"));
290     thresholdIsMin.addActionListener(new ActionListener()
291     {
292       @Override
293       public void actionPerformed(ActionEvent actionEvent)
294       {
295         thresholdIsMin_actionPerformed(actionEvent);
296       }
297     });
298     seqAssociated.setBackground(Color.white);
299     seqAssociated.setFont(JvSwingUtils.getLabelFont());
300     seqAssociated.setText(MessageManager
301             .getString("label.per_sequence_only"));
302     seqAssociated.addActionListener(new ActionListener()
303     {
304
305       @Override
306       public void actionPerformed(ActionEvent arg0)
307       {
308         seqAssociated_actionPerformed(arg0, annotations, seqAssociated);
309       }
310     });
311
312     this.setLayout(borderLayout1);
313     jPanel2.setLayout(new MigLayout("", "[left][center][right]", "[][][]"));
314     jPanel1.setBackground(Color.white);
315     jPanel2.setBackground(Color.white);
316
317     jPanel1.add(ok);
318     jPanel1.add(cancel);
319     jPanel2.add(annotations, "grow, wrap");
320     jPanel2.add(seqAssociated);
321     jPanel2.add(currentColours);
322     JPanel colpanel = new JPanel(new FlowLayout());
323     colpanel.setBackground(Color.white);
324     colpanel.add(minColour);
325     colpanel.add(maxColour);
326     jPanel2.add(colpanel, "wrap");
327     jPanel2.add(getThreshold());
328     jPanel2.add(defColours, "skip 1, wrap");
329     jPanel2.add(thresholdIsMin);
330     jPanel2.add(slider, "grow");
331     jPanel2.add(thresholdValue, "grow");
332     this.add(jPanel1, java.awt.BorderLayout.SOUTH);
333     this.add(jPanel2, java.awt.BorderLayout.CENTER);
334     this.validate();
335   }
336
337   protected void resetColours_actionPerformed(ActionEvent arg0)
338   {
339     setDefaultMinMax();
340     updateView();
341   }
342
343   private void setDefaultMinMax()
344   {
345     minColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN",
346             Color.orange));
347     maxColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX",
348             Color.red));
349   }
350
351   public void minColour_actionPerformed()
352   {
353     Color col = JColorChooser.showDialog(this,
354             MessageManager.getString("label.select_colour_minimum_value"), minColour.getBackground());
355     if (col != null)
356     {
357       minColour.setBackground(col);
358     }
359     minColour.repaint();
360     updateView();
361   }
362
363   public void maxColour_actionPerformed()
364   {
365     Color col = JColorChooser.showDialog(this,
366             MessageManager.getString("label.select_colour_maximum_value"), 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(new jalview.datamodel.GraphLine(
456                       (getCurrentAnnotation().graphMax - getCurrentAnnotation().graphMin) / 2f,
457                       "Threshold", Color.black));
458     }
459
460     if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD)
461     {
462       adjusting = true;
463       float range = getCurrentAnnotation().graphMax * 1000
464               - getCurrentAnnotation().graphMin * 1000;
465
466       slider.setMinimum((int) (getCurrentAnnotation().graphMin * 1000));
467       slider.setMaximum((int) (getCurrentAnnotation().graphMax * 1000));
468       slider.setValue((int) (getCurrentAnnotation().threshold.value * 1000));
469       thresholdValue.setText(getCurrentAnnotation().threshold.value + "");
470       slider.setMajorTickSpacing((int) (range / 10f));
471       slider.setEnabled(true);
472       thresholdValue.setEnabled(true);
473       adjusting = false;
474     }
475     colorAlignmContaining(getCurrentAnnotation(), selectedThresholdItem);
476
477     ap.alignmentChanged();
478     // ensure all associated views (overviews, structures, etc) are notified of
479     // updated colours.
480     ap.paintAlignment(true);
481   }
482
483 }