Refactored AnnotationColumnChoser and AnnotationColorChooser to apply the DRY principle
[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 (currentColours.isSelected()
391             && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
392     {
393       updateView();
394     }
395     getCurrentAnnotation().threshold.value = slider.getValue() / 1000f;
396     propagateSeqAssociatedThreshold(updateAllAnnotation,
397             getCurrentAnnotation());
398     ap.paintAlignment(false);
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     changeColour();
426   }
427   void changeColour()
428   {
429     // Check if combobox is still adjusting
430     if (adjusting)
431     {
432       return;
433     }
434
435     setCurrentAnnotation(av.getAlignment().getAlignmentAnnotation()[annmap[annotations
436             .getSelectedIndex()]]);
437
438     int selectedThresholdItem = getSelectedThresholdItem(getThreshold()
439             .getSelectedIndex());
440
441     slider.setEnabled(true);
442     thresholdValue.setEnabled(true);
443     thresholdIsMin.setEnabled(true);
444
445     if (selectedThresholdItem == AnnotationColourGradient.NO_THRESHOLD)
446     {
447       slider.setEnabled(false);
448       thresholdValue.setEnabled(false);
449       thresholdValue.setText("");
450       thresholdIsMin.setEnabled(false);
451     }
452     else if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD
453             && getCurrentAnnotation().threshold == null)
454     {
455       getCurrentAnnotation()
456               .setThreshold(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 }