JAL-3814 NONEWS property
[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.AlignmentAnnotation;
25 import jalview.datamodel.GraphLine;
26 import jalview.datamodel.SequenceGroup;
27 import jalview.schemes.AnnotationColourGradient;
28 import jalview.schemes.ColourSchemeI;
29 import jalview.util.MessageManager;
30
31 import java.awt.BorderLayout;
32 import java.awt.Color;
33 import java.awt.Dimension;
34 import java.awt.FlowLayout;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.ActionListener;
37 import java.awt.event.MouseAdapter;
38 import java.awt.event.MouseEvent;
39 import java.util.Hashtable;
40 import java.util.Vector;
41
42 import javax.swing.BorderFactory;
43 import javax.swing.JButton;
44 import javax.swing.JCheckBox;
45 import javax.swing.JColorChooser;
46 import javax.swing.JComboBox;
47 import javax.swing.JInternalFrame;
48 import javax.swing.JLayeredPane;
49 import javax.swing.JPanel;
50
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   private JPanel minColour = new JPanel();
65
66   private 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     super(av, ap);
77     oldcs = av.getGlobalColourScheme();
78     if (av.getAlignment().getGroups() != null)
79     {
80       oldgroupColours = new Hashtable<>();
81       for (SequenceGroup sg : ap.av.getAlignment().getGroups())
82       {
83         if (sg.getColourScheme() != null)
84         {
85           oldgroupColours.put(sg, sg.getColourScheme());
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     frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
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       useOriginalColours.setSelected(
112               acg.isPredefinedColours() || 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     Vector<String> annotItems = getAnnotationItems(
122             seqAssociated.isSelected());
123     annotations = new JComboBox<>(annotItems);
124
125     populateThresholdComboBox(threshold);
126
127     if (oldcs instanceof AnnotationColourGradient)
128     {
129       AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
130       String label = getAnnotationMenuLabel(acg.getAnnotation());
131       annotations.setSelectedItem(label);
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(
145                 "error.implementation_error_dont_know_about_threshold_setting"));
146       }
147       thresholdIsMin.setSelected(acg.isThresholdIsMinMax());
148       thresholdValue
149               .setText(String.valueOf(acg.getAnnotationThreshold()));
150     }
151
152     jbInit();
153     adjusting = false;
154
155     updateView();
156     frame.invalidate();
157     frame.pack();
158   }
159
160   @Override
161   protected void jbInit()
162   {
163     super.jbInit();
164
165     minColour.setFont(JvSwingUtils.getLabelFont());
166     minColour.setBorder(BorderFactory.createEtchedBorder());
167     minColour.setPreferredSize(new Dimension(40, 20));
168     minColour.setToolTipText(MessageManager.getString("label.min_colour"));
169     minColour.addMouseListener(new MouseAdapter()
170     {
171       @Override
172       public void mousePressed(MouseEvent e)
173       {
174         if (minColour.isEnabled())
175         {
176           minColour_actionPerformed();
177         }
178       }
179     });
180     maxColour.setFont(JvSwingUtils.getLabelFont());
181     maxColour.setBorder(BorderFactory.createEtchedBorder());
182     maxColour.setPreferredSize(new Dimension(40, 20));
183     maxColour.setToolTipText(MessageManager.getString("label.max_colour"));
184     maxColour.addMouseListener(new MouseAdapter()
185     {
186       @Override
187       public void mousePressed(MouseEvent e)
188       {
189         if (maxColour.isEnabled())
190         {
191           maxColour_actionPerformed();
192         }
193       }
194     });
195
196     defColours = new JButton();
197     defColours.setOpaque(false);
198     defColours.setText(MessageManager.getString("action.set_defaults"));
199     defColours.setToolTipText(MessageManager
200             .getString("label.reset_min_max_colours_to_defaults"));
201     defColours.addActionListener(new ActionListener()
202     {
203
204       @Override
205       public void actionPerformed(ActionEvent arg0)
206       {
207         resetColours_actionPerformed();
208       }
209     });
210
211     useOriginalColours.setFont(JvSwingUtils.getLabelFont());
212     useOriginalColours.setOpaque(false);
213     useOriginalColours.setText(
214             MessageManager.getString("label.use_original_colours"));
215     useOriginalColours.addActionListener(new ActionListener()
216     {
217       @Override
218       public void actionPerformed(ActionEvent e)
219       {
220         originalColours_actionPerformed();
221       }
222     });
223     thresholdIsMin.setBackground(Color.white);
224     thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
225     thresholdIsMin
226             .setText(MessageManager.getString("label.threshold_minmax"));
227     thresholdIsMin.addActionListener(new ActionListener()
228     {
229       @Override
230       public void actionPerformed(ActionEvent actionEvent)
231       {
232         thresholdIsMin_actionPerformed();
233       }
234     });
235     seqAssociated.setBackground(Color.white);
236     seqAssociated.setFont(JvSwingUtils.getLabelFont());
237     seqAssociated
238             .setText(MessageManager.getString("label.per_sequence_only"));
239     seqAssociated.addActionListener(new ActionListener()
240     {
241
242       @Override
243       public void actionPerformed(ActionEvent arg0)
244       {
245         seqAssociated_actionPerformed(annotations);
246       }
247     });
248
249     this.setLayout(new BorderLayout());
250     JPanel jPanel1 = new JPanel();
251     JPanel jPanel2 = new JPanel();
252     jPanel2.setLayout(new MigLayout("", "[left][center][right]", "[][][]"));
253     jPanel1.setBackground(Color.white);
254     jPanel2.setBackground(Color.white);
255
256     jPanel1.add(ok);
257     jPanel1.add(cancel);
258     jPanel2.add(annotations, "grow, wrap");
259     jPanel2.add(seqAssociated);
260     jPanel2.add(useOriginalColours);
261     JPanel colpanel = new JPanel(new FlowLayout());
262     colpanel.setBackground(Color.white);
263     colpanel.add(minColour);
264     colpanel.add(maxColour);
265     jPanel2.add(colpanel, "wrap");
266     jPanel2.add(getThreshold());
267     jPanel2.add(defColours, "skip 1, wrap");
268     jPanel2.add(thresholdIsMin);
269     jPanel2.add(slider, "grow");
270     jPanel2.add(thresholdValue, "grow");
271     this.add(jPanel1, java.awt.BorderLayout.SOUTH);
272     this.add(jPanel2, java.awt.BorderLayout.CENTER);
273     this.validate();
274   }
275
276   protected void resetColours_actionPerformed()
277   {
278     setDefaultMinMax();
279     updateView();
280   }
281
282   private void setDefaultMinMax()
283   {
284     minColour.setBackground(
285             Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN", Color.orange));
286     maxColour.setBackground(
287             Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX", Color.red));
288   }
289
290   public void minColour_actionPerformed()
291   {
292     Color col = JColorChooser.showDialog(this,
293             MessageManager.getString("label.select_colour_minimum_value"),
294             minColour.getBackground());
295     if (col != null)
296     {
297       minColour.setBackground(col);
298     }
299     minColour.repaint();
300     updateView();
301   }
302
303   public void maxColour_actionPerformed()
304   {
305     Color col = JColorChooser.showDialog(this,
306             MessageManager.getString("label.select_colour_maximum_value"),
307             maxColour.getBackground());
308     if (col != null)
309     {
310       maxColour.setBackground(col);
311     }
312     maxColour.repaint();
313     updateView();
314   }
315
316   @Override
317   public void reset()
318   {
319     this.ap.alignFrame.changeColour(oldcs);
320     if (av.getAlignment().getGroups() != null)
321     {
322
323       for (SequenceGroup sg : ap.av.getAlignment().getGroups())
324       {
325         sg.setColourScheme(oldgroupColours.get(sg));
326       }
327     }
328   }
329
330   @Override
331   public void valueChanged(boolean updateAllAnnotation)
332   {
333     if (slider.isEnabled())
334     {
335       if (useOriginalColours.isSelected() && !(av
336               .getGlobalColourScheme() instanceof AnnotationColourGradient))
337       {
338         updateView();
339       }
340       getCurrentAnnotation().threshold.value = getSliderValue();
341       propagateSeqAssociatedThreshold(updateAllAnnotation,
342               getCurrentAnnotation());
343       ap.paintAlignment(false, false);
344     }
345   }
346
347   public void originalColours_actionPerformed()
348   {
349     boolean selected = useOriginalColours.isSelected();
350     if (selected)
351     {
352       reset();
353     }
354     maxColour.setEnabled(!selected);
355     minColour.setEnabled(!selected);
356     thresholdIsMin.setEnabled(!selected);
357     updateView();
358   }
359
360   @Override
361   public void updateView()
362   {
363     // Check if combobox is still adjusting
364     if (adjusting)
365     {
366       return;
367     }
368
369     setCurrentAnnotation(
370             av.getAlignment().getAlignmentAnnotation()[annmap[annotations
371                     .getSelectedIndex()]]);
372
373     int selectedThresholdItem = getSelectedThresholdItem(
374             getThreshold().getSelectedIndex());
375
376     slider.setEnabled(true);
377     thresholdValue.setEnabled(true);
378     thresholdIsMin.setEnabled(!useOriginalColours.isSelected());
379
380     final AlignmentAnnotation currentAnnotation = getCurrentAnnotation();
381     if (selectedThresholdItem == AnnotationColourGradient.NO_THRESHOLD)
382     {
383       slider.setEnabled(false);
384       thresholdValue.setEnabled(false);
385       thresholdValue.setText("");
386       thresholdIsMin.setEnabled(false);
387     }
388     else if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD
389             && currentAnnotation.threshold == null)
390     {
391       currentAnnotation.setThreshold(new GraphLine(
392               (currentAnnotation.graphMax - currentAnnotation.graphMin)
393                       / 2f,
394               "Threshold", Color.black));
395     }
396
397     if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD)
398     {
399       adjusting = true;
400       setSliderModel(currentAnnotation.graphMin, currentAnnotation.graphMax,
401               currentAnnotation.threshold.value);
402       slider.setEnabled(true);
403
404       setThresholdValueText();
405       thresholdValue.setEnabled(true);
406       adjusting = false;
407     }
408     colorAlignmentContaining(currentAnnotation, selectedThresholdItem);
409
410     ap.alignmentChanged();
411   }
412
413   protected void colorAlignmentContaining(AlignmentAnnotation currentAnn,
414           int selectedThresholdOption)
415   {
416
417     AnnotationColourGradient acg = null;
418     if (useOriginalColours.isSelected())
419     {
420       acg = new AnnotationColourGradient(currentAnn,
421               av.getGlobalColourScheme(), selectedThresholdOption);
422     }
423     else
424     {
425       acg = new AnnotationColourGradient(currentAnn,
426               minColour.getBackground(), maxColour.getBackground(),
427               selectedThresholdOption);
428     }
429     acg.setSeqAssociated(seqAssociated.isSelected());
430
431     if (currentAnn.graphMin == 0f && currentAnn.graphMax == 0f)
432     {
433       acg.setPredefinedColours(true);
434     }
435
436     acg.setThresholdIsMinMax(thresholdIsMin.isSelected());
437
438     this.ap.alignFrame.changeColour(acg);
439
440     if (av.getAlignment().getGroups() != null)
441     {
442
443       for (SequenceGroup sg : ap.av.getAlignment().getGroups())
444       {
445         if (sg.cs == null)
446         {
447           continue;
448         }
449         sg.setColourScheme(
450                 acg.getInstance(av, sg));
451       }
452     }
453   }
454
455   @Override
456   protected void sliderDragReleased()
457   {
458     super.sliderDragReleased();
459     ap.paintAlignment(true, true);
460   }
461 }