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