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