JAL-1556 fix bad import
[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 java.awt.BorderLayout;
24 import java.awt.Color;
25 import java.awt.Dimension;
26 import java.awt.FlowLayout;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
29 import java.awt.event.MouseAdapter;
30 import java.awt.event.MouseEvent;
31 import java.util.Hashtable;
32 import java.util.Vector;
33
34 import javax.swing.BorderFactory;
35 import javax.swing.JButton;
36 import javax.swing.JCheckBox;
37 import javax.swing.JComboBox;
38 import javax.swing.JInternalFrame;
39 import javax.swing.JLayeredPane;
40 import javax.swing.JPanel;
41
42 import jalview.bin.Cache;
43 import jalview.datamodel.AlignmentAnnotation;
44 import jalview.datamodel.Annotation;
45 import jalview.datamodel.GraphLine;
46 import jalview.datamodel.SequenceGroup;
47 import jalview.gui.JalviewColourChooser.ColourChooserListener;
48 import jalview.schemes.AnnotationColourGradient;
49 import jalview.schemes.ColourSchemeI;
50 import jalview.util.MessageManager;
51 import net.miginfocom.swing.MigLayout;
52
53 @SuppressWarnings("serial")
54 public class AnnotationColourChooser extends AnnotationRowFilter
55 {
56   private ColourSchemeI oldcs;
57
58   private JButton defColours;
59
60   private Hashtable<SequenceGroup, ColourSchemeI> oldgroupColours;
61
62   private JCheckBox useOriginalColours = new JCheckBox();
63
64   JPanel minColour = new JPanel();
65
66   JPanel maxColour = new JPanel();
67
68   private JCheckBox thresholdIsMin = new JCheckBox();
69
70   protected static final int MIN_WIDTH = 500;
71
72   protected static final int MIN_HEIGHT = 240;
73
74   public AnnotationColourChooser(AlignViewport av, final AlignmentPanel ap)
75   {
76     this(av,ap,null);
77   }
78   public AnnotationColourChooser(AlignViewport av, final AlignmentPanel ap,AnnotationColourGradient initSettings)
79   {
80     super(av, ap);
81     oldcs = av.getGlobalColourScheme();
82     if (av.getAlignment().getGroups() != null)
83     {
84       oldgroupColours = new Hashtable<>();
85       for (SequenceGroup sg : ap.av.getAlignment().getGroups())
86       {
87         if (sg.getColourScheme() != null)
88         {
89           oldgroupColours.put(sg, sg.getColourScheme());
90         }
91       }
92     }
93     frame = new JInternalFrame();
94     frame.setFrameIcon(null);
95     frame.setContentPane(this);
96     frame.setLayer(JLayeredPane.PALETTE_LAYER);
97     Desktop.addInternalFrame(frame,
98             MessageManager.getString("label.colour_by_annotation"), 520,
99             215);
100     frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
101     addSliderChangeListener();
102     addSliderMouseListeners();
103
104     if (av.getAlignment().getAlignmentAnnotation() == null)
105     {
106       return;
107     }
108
109     // Always get default shading from preferences.
110     setDefaultMinMax();
111
112     adjusting = true;
113     if (oldcs instanceof AnnotationColourGradient && initSettings==null)
114     {
115       // init from oldcs
116       initialiseFrom((AnnotationColourGradient) oldcs);
117     } else {
118       // use initial colour gradient - if any..
119       initialiseFrom(initSettings);
120     }
121     
122     jbInit();
123     adjusting = false;
124
125     updateView();
126     frame.invalidate();
127     frame.pack();
128   }
129   private void initialiseFrom(AnnotationColourGradient acg)
130   {
131     if (acg!=null)
132     {
133       useOriginalColours.setSelected(
134             acg.isPredefinedColours() || acg.getBaseColour() != null);
135       if (!acg.isPredefinedColours() && acg.getBaseColour() == null)
136       {
137         minColour.setBackground(acg.getMinColour());
138         maxColour.setBackground(acg.getMaxColour());
139       }
140       seqAssociated.setSelected(acg.isSeqAssociated());
141
142     }
143     Vector<String> annotItems = getAnnotationItems(
144             seqAssociated.isSelected());
145     annotations = new JComboBox<>(annotItems);
146
147     populateThresholdComboBox(threshold);
148
149     if (acg!=null)
150     {
151       String label = getAnnotationMenuLabel(acg.getAnnotation());
152       // TODO: workaround below shouldn't be necessary - there's a bug in getAnnotationMenuLabel!
153       if (acg.isSeqAssociated())
154       {
155         label = acg.getAnnotation().label;
156       }
157       annotations.setSelectedItem(label);
158       switch (acg.getAboveThreshold())
159       {
160       case AnnotationColourGradient.NO_THRESHOLD:
161         getThreshold().setSelectedIndex(0);
162         break;
163       case AnnotationColourGradient.ABOVE_THRESHOLD:
164         getThreshold().setSelectedIndex(1);
165         break;
166       case AnnotationColourGradient.BELOW_THRESHOLD:
167         getThreshold().setSelectedIndex(2);
168         break;
169       default:
170         throw new Error(MessageManager.getString(
171                 "error.implementation_error_dont_know_about_threshold_setting"));
172       }
173       thresholdIsMin.setSelected(acg.isThresholdIsMinMax());
174       thresholdValue.setText(String.valueOf(acg.getAnnotationThreshold()));
175     }
176   }
177
178   @Override
179   protected void jbInit()
180   {
181     super.jbInit();
182
183     minColour.setFont(JvSwingUtils.getLabelFont());
184     minColour.setBorder(BorderFactory.createEtchedBorder());
185     minColour.setPreferredSize(new Dimension(40, 20));
186     minColour.setToolTipText(MessageManager.getString("label.min_colour"));
187     minColour.addMouseListener(new MouseAdapter()
188     {
189       @Override
190       public void mousePressed(MouseEvent e)
191       {
192         if (minColour.isEnabled())
193         {
194           showColourChooser(minColour, "label.select_colour_minimum_value");
195         }
196       }
197     });
198     maxColour.setFont(JvSwingUtils.getLabelFont());
199     maxColour.setBorder(BorderFactory.createEtchedBorder());
200     maxColour.setPreferredSize(new Dimension(40, 20));
201     maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
202     maxColour.addMouseListener(new MouseAdapter()
203     {
204       @Override
205       public void mousePressed(MouseEvent e)
206       {
207         if (maxColour.isEnabled())
208         {
209           showColourChooser(maxColour, "label.select_colour_maximum_value");
210         }
211       }
212     });
213
214     defColours = new JButton();
215     defColours.setOpaque(false);
216     defColours.setText(MessageManager.getString("action.set_defaults"));
217     defColours.setToolTipText(MessageManager
218             .getString("label.reset_min_max_colours_to_defaults"));
219     defColours.addActionListener(new ActionListener()
220     {
221
222       @Override
223       public void actionPerformed(ActionEvent arg0)
224       {
225         resetColours_actionPerformed();
226       }
227     });
228
229     useOriginalColours.setFont(JvSwingUtils.getLabelFont());
230     useOriginalColours.setOpaque(false);
231     useOriginalColours.setText(
232             MessageManager.getString("label.use_original_colours"));
233     useOriginalColours.addActionListener(new ActionListener()
234     {
235       @Override
236       public void actionPerformed(ActionEvent e)
237       {
238         originalColours_actionPerformed();
239       }
240     });
241     thresholdIsMin.setBackground(Color.white);
242     thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
243     thresholdIsMin
244             .setText(MessageManager.getString("label.threshold_minmax"));
245     thresholdIsMin.addActionListener(new ActionListener()
246     {
247       @Override
248       public void actionPerformed(ActionEvent actionEvent)
249       {
250         thresholdIsMin_actionPerformed();
251       }
252     });
253     seqAssociated.setBackground(Color.white);
254     seqAssociated.setFont(JvSwingUtils.getLabelFont());
255     seqAssociated
256             .setText(MessageManager.getString("label.per_sequence_only"));
257     seqAssociated.addActionListener(new ActionListener()
258     {
259
260       @Override
261       public void actionPerformed(ActionEvent arg0)
262       {
263         seqAssociated_actionPerformed(annotations);
264       }
265     });
266
267     this.setLayout(new BorderLayout());
268     JPanel jPanel1 = new JPanel();
269     JPanel jPanel2 = new JPanel();
270     jPanel2.setLayout(new MigLayout("", "[left][center][right]", "[][][]"));
271     jPanel1.setBackground(Color.white);
272     jPanel2.setBackground(Color.white);
273
274     jPanel1.add(ok);
275     jPanel1.add(cancel);
276     jPanel2.add(annotations, "grow, wrap");
277     jPanel2.add(seqAssociated);
278     jPanel2.add(useOriginalColours);
279     JPanel colpanel = new JPanel(new FlowLayout());
280     colpanel.setBackground(Color.white);
281     colpanel.add(minColour);
282     colpanel.add(maxColour);
283     jPanel2.add(colpanel, "wrap");
284     jPanel2.add(getThreshold());
285     jPanel2.add(defColours, "skip 1, wrap");
286     jPanel2.add(thresholdIsMin);
287     jPanel2.add(slider, "grow");
288     jPanel2.add(thresholdValue, "grow");
289     this.add(jPanel1, java.awt.BorderLayout.SOUTH);
290     this.add(jPanel2, java.awt.BorderLayout.CENTER);
291     this.validate();
292   }
293
294   protected void resetColours_actionPerformed()
295   {
296     setDefaultMinMax();
297     updateView();
298   }
299
300   private void setDefaultMinMax()
301   {
302     minColour.setBackground(
303             Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN", Color.orange));
304     maxColour.setBackground(
305             Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX", Color.red));
306   }
307
308   protected void showColourChooser(JPanel colourPanel, String titleKey)
309   {
310     String ttl = MessageManager.getString(titleKey);
311     ColourChooserListener listener = new ColourChooserListener()
312     {
313       @Override
314       public void colourSelected(Color c)
315       {
316         colourPanel.setBackground(c);
317         colourPanel.repaint();
318         updateView();
319       }
320     };
321     JalviewColourChooser.showColourChooser(Desktop.getDesktop(), ttl,
322             colourPanel.getBackground(), listener);
323   }
324
325   @Override
326   public void reset()
327   {
328     this.ap.alignFrame.changeColour(oldcs);
329     if (av.getAlignment().getGroups() != null)
330     {
331
332       for (SequenceGroup sg : ap.av.getAlignment().getGroups())
333       {
334         sg.setColourScheme(oldgroupColours.get(sg));
335       }
336     }
337   }
338
339   @Override
340   public void valueChanged(boolean updateAllAnnotation)
341   {
342     if (slider.isEnabled())
343     {
344       if (useOriginalColours.isSelected() && !(av
345               .getGlobalColourScheme() instanceof AnnotationColourGradient))
346       {
347         updateView();
348       }
349       getCurrentAnnotation().threshold.value = getSliderValue();
350       propagateSeqAssociatedThreshold(updateAllAnnotation,
351               getCurrentAnnotation());
352       ap.paintAlignment(false, false);
353     }
354   }
355
356   public void originalColours_actionPerformed()
357   {
358     boolean selected = useOriginalColours.isSelected();
359     if (selected)
360     {
361       reset();
362     }
363     maxColour.setEnabled(!selected);
364     minColour.setEnabled(!selected);
365     thresholdIsMin.setEnabled(!selected);
366     updateView();
367   }
368
369   @Override
370   public void updateView()
371   {
372     // Check if combobox is still adjusting
373     if (adjusting)
374     {
375       return;
376     }
377
378     int selIndex = annotations
379             .getSelectedIndex();
380     int annIndex = annmap[selIndex];
381     setCurrentAnnotation(
382             av.getAlignment().getAlignmentAnnotation()[annIndex]);
383
384     int selectedThresholdItem = getSelectedThresholdItem(
385             getThreshold().getSelectedIndex());
386
387     slider.setEnabled(true);
388     thresholdValue.setEnabled(true);
389     thresholdIsMin.setEnabled(!useOriginalColours.isSelected());
390
391     final AlignmentAnnotation currentAnnotation = getCurrentAnnotation();
392     if (selectedThresholdItem == AnnotationColourGradient.NO_THRESHOLD)
393     {
394       slider.setEnabled(false);
395       thresholdValue.setEnabled(false);
396       thresholdValue.setText("");
397       thresholdIsMin.setEnabled(false);
398     }
399     else if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD
400             && currentAnnotation.threshold == null)
401     {
402       currentAnnotation.setThreshold(new GraphLine(
403               (currentAnnotation.graphMax - currentAnnotation.graphMin)
404                       / 2f,
405               "Threshold", Color.black));
406     }
407
408     if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD)
409     {
410       adjusting = true;
411       setSliderModel(currentAnnotation.graphMin, currentAnnotation.graphMax,
412               currentAnnotation.threshold.value);
413       slider.setEnabled(true);
414
415       setThresholdValueText();
416       thresholdValue.setEnabled(true);
417       adjusting = false;
418     }
419     colorAlignmentContaining(currentAnnotation, selectedThresholdItem);
420
421     ap.alignmentChanged();
422   }
423
424   protected void colorAlignmentContaining(AlignmentAnnotation currentAnn,
425           int selectedThresholdOption)
426   {
427
428     AnnotationColourGradient acg = null;
429     if (useOriginalColours.isSelected())
430     {
431       acg = new AnnotationColourGradient(currentAnn,
432               av.getGlobalColourScheme(), selectedThresholdOption);
433     }
434     else
435     {
436       acg = new AnnotationColourGradient(currentAnn,
437               minColour.getBackground(), maxColour.getBackground(),
438               selectedThresholdOption);
439     }
440     acg.setSeqAssociated(seqAssociated.isSelected());
441
442     if (currentAnn.graphMin == 0f && currentAnn.graphMax == 0f)
443     {
444       acg.setPredefinedColours(true);
445     }
446
447     acg.setThresholdIsMinMax(thresholdIsMin.isSelected());
448
449     this.ap.alignFrame.changeColour(acg);
450
451     if (av.getAlignment().getGroups() != null)
452     {
453
454       for (SequenceGroup sg : ap.av.getAlignment().getGroups())
455       {
456         if (sg.cs == null)
457         {
458           continue;
459         }
460         sg.setColourScheme(acg.getInstance(av, sg));
461       }
462     }
463   }
464
465   @Override
466   protected void sliderDragReleased()
467   {
468     super.sliderDragReleased();
469     ap.paintAlignment(true, true);
470   }
471
472   /**
473    * construct and display a colourchooser for a given annotation row
474    * 
475    * @param av
476    * @param ap
477    * @param alignmentAnnotation
478    * @param perseq - when true, enable per-sequence if alignment annotation is per sequence 
479    */
480   public static void displayFor(AlignViewport av, AlignmentPanel ap,
481           AlignmentAnnotation alignmentAnnotation, boolean perSeq)
482   {
483     ColourSchemeI global = av.getGlobalColourScheme();
484     AnnotationColourGradient newCS = new AnnotationColourGradient(alignmentAnnotation, global, alignmentAnnotation.threshold!=null ? AnnotationColourGradient.ABOVE_THRESHOLD:AnnotationColourGradient.NO_THRESHOLD);
485     if (alignmentAnnotation.sequenceRef!=null)
486     {
487       newCS.setSeqAssociated(perSeq);
488     }
489     for (int i=0;i<alignmentAnnotation.annotations.length;i++)
490     {
491       Annotation ann = alignmentAnnotation.annotations[i];
492       if (ann!=null && ann.colour!=null && !ann.colour.equals(Color.white))
493       {
494         newCS.setPredefinedColours(true);
495         break;
496       }
497     }
498     AnnotationColourChooser achooser = new AnnotationColourChooser(av,ap,newCS);
499   }
500 }