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