JAL-1807 explicit imports (jalview.ws.*)
[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.GraphLine;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.schemes.AnnotationColourGradient;
27 import jalview.schemes.ColourSchemeI;
28 import jalview.util.MessageManager;
29
30 import java.awt.BorderLayout;
31 import java.awt.Color;
32 import java.awt.Dimension;
33 import java.awt.FlowLayout;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
36 import java.awt.event.MouseAdapter;
37 import java.awt.event.MouseEvent;
38 import java.util.Hashtable;
39
40 import javax.swing.BorderFactory;
41 import javax.swing.JButton;
42 import javax.swing.JColorChooser;
43 import javax.swing.JComboBox;
44 import javax.swing.JInternalFrame;
45 import javax.swing.JLayeredPane;
46 import javax.swing.JPanel;
47
48 import net.miginfocom.swing.MigLayout;
49
50 @SuppressWarnings("serial")
51 public class AnnotationColourChooser extends AnnotationRowFilter
52 {
53
54   ColourSchemeI oldcs;
55
56   Hashtable<SequenceGroup, ColourSchemeI> oldgroupColours;
57
58   /**
59    * enabled if the user is dragging the slider - try to keep updates to a
60    * minimun
61    */
62
63   JComboBox<String> annotations;
64
65   JButton defColours = new JButton();
66
67
68   JPanel jPanel1 = new JPanel();
69
70   JPanel jPanel2 = new JPanel();
71
72   BorderLayout borderLayout1 = new BorderLayout();
73
74   private JComboBox<String> threshold = new JComboBox<String>();
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.cs != null)
86         {
87           oldgroupColours.put(sg, sg.cs);
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
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       currentColours.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     annotations = new JComboBox<String>(
124             getAnnotationItems(seqAssociated.isSelected()));
125
126     populateThresholdComboBox(threshold);
127
128     if (oldcs instanceof AnnotationColourGradient)
129     {
130       AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
131       annotations.setSelectedItem(acg.getAnnotation());
132       switch (acg.getAboveThreshold())
133       {
134       case AnnotationColourGradient.NO_THRESHOLD:
135         getThreshold().setSelectedIndex(0);
136         break;
137       case AnnotationColourGradient.ABOVE_THRESHOLD:
138         getThreshold().setSelectedIndex(1);
139         break;
140       case AnnotationColourGradient.BELOW_THRESHOLD:
141         getThreshold().setSelectedIndex(2);
142         break;
143       default:
144         throw new Error(MessageManager.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"), minColour.getBackground());
353     if (col != null)
354     {
355       minColour.setBackground(col);
356     }
357     minColour.repaint();
358     updateView();
359   }
360
361   public void maxColour_actionPerformed()
362   {
363     Color col = JColorChooser.showDialog(this,
364             MessageManager.getString("label.select_colour_maximum_value"), maxColour.getBackground());
365     if (col != null)
366     {
367       maxColour.setBackground(col);
368     }
369     maxColour.repaint();
370     updateView();
371   }
372
373   public void reset()
374   {
375     av.setGlobalColourScheme(oldcs);
376     if (av.getAlignment().getGroups() != null)
377     {
378
379       for (SequenceGroup sg : ap.av.getAlignment().getGroups())
380       {
381         sg.cs = oldgroupColours.get(sg);
382       }
383     }
384   }
385
386   public void valueChanged(boolean updateAllAnnotation)
387   {
388     if (slider.isEnabled())
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
402   public JComboBox<String> getThreshold()
403   {
404     return threshold;
405   }
406
407   public void setThreshold(JComboBox<String> threshold)
408   {
409     this.threshold = threshold;
410   }
411
412   public void currentColours_actionPerformed(ActionEvent e)
413   {
414     if (currentColours.isSelected())
415     {
416       reset();
417     }
418     maxColour.setEnabled(!currentColours.isSelected());
419     minColour.setEnabled(!currentColours.isSelected());
420     updateView();
421   }
422
423   @Override
424   public void updateView()
425   {
426     // Check if combobox is still adjusting
427     if (adjusting)
428     {
429       return;
430     }
431
432     setCurrentAnnotation(av.getAlignment().getAlignmentAnnotation()[annmap[annotations
433             .getSelectedIndex()]]);
434
435     int selectedThresholdItem = getSelectedThresholdItem(getThreshold()
436             .getSelectedIndex());
437
438     slider.setEnabled(true);
439     thresholdValue.setEnabled(true);
440     thresholdIsMin.setEnabled(true);
441
442     if (selectedThresholdItem == AnnotationColourGradient.NO_THRESHOLD)
443     {
444       slider.setEnabled(false);
445       thresholdValue.setEnabled(false);
446       thresholdValue.setText("");
447       thresholdIsMin.setEnabled(false);
448     }
449     else if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD
450             && getCurrentAnnotation().threshold == null)
451     {
452       getCurrentAnnotation()
453               .setThreshold(
454                       new GraphLine(
455                       (getCurrentAnnotation().graphMax - getCurrentAnnotation().graphMin) / 2f,
456                       "Threshold", Color.black));
457     }
458
459     if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD)
460     {
461       adjusting = true;
462       float range = getCurrentAnnotation().graphMax * 1000
463               - getCurrentAnnotation().graphMin * 1000;
464
465       slider.setMinimum((int) (getCurrentAnnotation().graphMin * 1000));
466       slider.setMaximum((int) (getCurrentAnnotation().graphMax * 1000));
467       slider.setValue((int) (getCurrentAnnotation().threshold.value * 1000));
468       thresholdValue.setText(getCurrentAnnotation().threshold.value + "");
469       slider.setMajorTickSpacing((int) (range / 10f));
470       slider.setEnabled(true);
471       thresholdValue.setEnabled(true);
472       adjusting = false;
473     }
474     colorAlignmContaining(getCurrentAnnotation(), selectedThresholdItem);
475
476     ap.alignmentChanged();
477     // ensure all associated views (overviews, structures, etc) are notified of
478     // updated colours.
479     ap.paintAlignment(true);
480   }
481
482 }