JAL-1066 basic gui and functionality for per-sequence colour by annotation
[jalview.git] / src / jalview / gui / AnnotationColourChooser.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.gui;
19
20 import java.util.*;
21
22 import java.awt.*;
23 import java.awt.event.*;
24
25 import javax.swing.*;
26 import javax.swing.event.*;
27
28 import net.miginfocom.swing.MigLayout;
29
30 import jalview.bin.Cache;
31 import jalview.datamodel.*;
32 import jalview.schemes.*;
33 import java.awt.Dimension;
34
35 public class AnnotationColourChooser extends JPanel
36 {
37   JInternalFrame frame;
38
39   AlignViewport av;
40
41   AlignmentPanel ap;
42
43   ColourSchemeI oldcs;
44
45   Hashtable oldgroupColours;
46
47   jalview.datamodel.AlignmentAnnotation currentAnnotation;
48
49   boolean adjusting = false;
50
51   public AnnotationColourChooser(AlignViewport av, final AlignmentPanel ap)
52   {
53     oldcs = av.getGlobalColourScheme();
54     if (av.getAlignment().getGroups() != null)
55     {
56       oldgroupColours = new Hashtable();
57       for (SequenceGroup sg:ap.av.getAlignment().getGroups())
58       {
59         if (sg.cs != null)
60         {
61           oldgroupColours.put(sg, sg.cs);
62         }
63       }
64     }
65     this.av = av;
66     this.ap = ap;
67     frame = new JInternalFrame();
68     frame.setContentPane(this);
69     frame.setLayer(JLayeredPane.PALETTE_LAYER);
70     Desktop.addInternalFrame(frame, "Colour by Annotation", 520, 215);
71
72     slider.addChangeListener(new ChangeListener()
73     {
74       public void stateChanged(ChangeEvent evt)
75       {
76         if (!adjusting)
77         {
78           thresholdValue.setText(((float) slider.getValue() / 1000f) + "");
79           valueChanged();
80         }
81       }
82     });
83     slider.addMouseListener(new MouseAdapter()
84     {
85       public void mouseReleased(MouseEvent evt)
86       {
87         ap.paintAlignment(true);
88       }
89     });
90
91     if (av.getAlignment().getAlignmentAnnotation() == null)
92     {
93       return;
94     }
95
96     // Always get default shading from preferences.
97     setDefaultMinMax();
98     
99     if (oldcs instanceof AnnotationColourGradient)
100     {
101       AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
102       currentColours.setSelected(acg.predefinedColours);
103       if (!acg.predefinedColours)
104       {
105         minColour.setBackground(acg.getMinColour());
106         maxColour.setBackground(acg.getMaxColour());
107       }
108       seqAssociated.setSelected(acg.isSeqAssociated());
109     }
110     adjusting = true;
111     Vector list = new Vector();
112     int index = 1;
113     for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
114     {
115       String label = av.getAlignment().getAlignmentAnnotation()[i].label;
116       if (!list.contains(label))
117         list.addElement(label);
118       else
119         list.addElement(label + "_" + (index++));
120     }
121
122     annotations = new JComboBox(list);
123
124     threshold.addItem("No Threshold");
125     threshold.addItem("Above Threshold");
126     threshold.addItem("Below Threshold");
127
128     if (oldcs instanceof AnnotationColourGradient)
129     {
130       AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
131       annotations.setSelectedItem(acg.getAnnotation());
132       switch (acg.getAboveThreshold()) {
133       case AnnotationColourGradient.NO_THRESHOLD:
134           threshold.setSelectedItem("No Threshold");
135         break;
136       case AnnotationColourGradient.ABOVE_THRESHOLD:
137           threshold.setSelectedItem("Above Threshold");
138         break;
139       case AnnotationColourGradient.BELOW_THRESHOLD:
140         threshold.setSelectedItem("Below Threshold");
141         break;
142         default:
143           throw new Error("Implementation error: don't know about threshold setting for current AnnotationColourGradient.");
144       }
145       thresholdIsMin.setSelected(acg.thresholdIsMinMax);
146       thresholdValue.setText(""+acg.getAnnotationThreshold());
147     }
148
149     try
150     {
151       jbInit();
152     } catch (Exception ex)
153     {
154     }
155
156     adjusting = false;
157
158     changeColour();
159     frame.invalidate();
160     frame.pack();
161
162   }
163
164   private void setDefaultMinMax()
165   {
166     minColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN", Color.orange));
167     maxColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX", Color.red));
168   }
169
170   public AnnotationColourChooser()
171   {
172     try
173     {
174       jbInit();
175     } catch (Exception ex)
176     {
177       ex.printStackTrace();
178     }
179   }
180
181   private void jbInit() throws Exception
182   {
183     minColour.setFont(JvSwingUtils.getLabelFont());
184     minColour.setBorder(BorderFactory.createEtchedBorder());
185     minColour.setPreferredSize(new Dimension(40, 20));
186     minColour.setToolTipText("Minimum Colour");
187     minColour.addMouseListener(new MouseAdapter()
188     {
189       public void mousePressed(MouseEvent e)
190       {
191         if (minColour.isEnabled())
192         {
193           minColour_actionPerformed();
194         }
195       }
196     });
197     maxColour.setFont(JvSwingUtils.getLabelFont());
198     maxColour.setBorder(BorderFactory.createEtchedBorder());
199     maxColour.setPreferredSize(new Dimension(40, 20));
200     maxColour.setToolTipText("Maximum Colour");
201     maxColour.addMouseListener(new MouseAdapter()
202     {
203       public void mousePressed(MouseEvent e)
204       {
205         if (maxColour.isEnabled())
206         {
207           maxColour_actionPerformed();
208         }
209       }
210     });
211     ok.setOpaque(false);
212     ok.setText("OK");
213     ok.addActionListener(new ActionListener()
214     {
215       public void actionPerformed(ActionEvent e)
216       {
217         ok_actionPerformed(e);
218       }
219     });
220     cancel.setOpaque(false);
221     cancel.setText("Cancel");
222     cancel.addActionListener(new ActionListener()
223     {
224       public void actionPerformed(ActionEvent e)
225       {
226         cancel_actionPerformed(e);
227       }
228     });
229     defColours.setOpaque(false);
230     defColours.setText("Defaults");
231     defColours.setToolTipText("Reset min and max colours to defaults from user preferences.");
232     defColours.addActionListener(new ActionListener()
233     {
234       
235       @Override
236       public void actionPerformed(ActionEvent arg0)
237       {
238         resetColours_actionPerformed(arg0);
239       }
240     });
241     
242     annotations.addActionListener(new ActionListener()
243     {
244       public void actionPerformed(ActionEvent e)
245       {
246         annotations_actionPerformed(e);
247       }
248     });
249     threshold.addActionListener(new ActionListener()
250     {
251       public void actionPerformed(ActionEvent e)
252       {
253         threshold_actionPerformed(e);
254       }
255     });
256     thresholdValue.addActionListener(new ActionListener()
257     {
258       public void actionPerformed(ActionEvent e)
259       {
260         thresholdValue_actionPerformed(e);
261       }
262     });
263     slider.setPaintLabels(false);
264     slider.setPaintTicks(true);
265     slider.setBackground(Color.white);
266     slider.setEnabled(false);
267     slider.setOpaque(false);
268     slider.setPreferredSize(new Dimension(100, 32));
269     thresholdValue.setEnabled(false);
270     thresholdValue.setColumns(7);
271     currentColours.setFont(JvSwingUtils.getLabelFont());
272     currentColours.setOpaque(false);
273     currentColours.setText("Use Original Colours");
274     currentColours.addActionListener(new ActionListener()
275     {
276       public void actionPerformed(ActionEvent e)
277       {
278         currentColours_actionPerformed(e);
279       }
280     });
281     thresholdIsMin.setBackground(Color.white);
282     thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
283     thresholdIsMin.setText("Threshold is Min/Max");
284     thresholdIsMin.addActionListener(new ActionListener()
285     {
286       public void actionPerformed(ActionEvent actionEvent)
287       {
288         thresholdIsMin_actionPerformed(actionEvent);
289       }
290     });
291     seqAssociated.setBackground(Color.white);
292     seqAssociated.setFont(JvSwingUtils.getLabelFont());
293     seqAssociated.setText("Per-sequence only");
294     seqAssociated.addActionListener(new ActionListener()
295     {
296       
297       @Override
298       public void actionPerformed(ActionEvent arg0)
299       {
300         seqAssociated_actionPerformed(arg0);
301       }
302     });
303     
304     this.setLayout(borderLayout1);
305     jPanel2.setLayout(new MigLayout("","[left][center][right]","[][][]"));
306     jPanel1.setBackground(Color.white);
307     jPanel2.setBackground(Color.white);
308
309     jPanel1.add(ok);
310     jPanel1.add(cancel);
311     jPanel2.add(annotations);
312     jPanel2.add(currentColours);
313     JPanel colpanel = new JPanel(new FlowLayout());
314     colpanel.setBackground(Color.white);
315     colpanel.add(minColour);
316     colpanel.add(maxColour);
317     jPanel2.add(colpanel, "wrap");
318     jPanel2.add(seqAssociated, "wrap");
319     jPanel2.add(threshold);
320     jPanel2.add(defColours,"skip 1, wrap");
321     jPanel2.add(thresholdIsMin);
322     jPanel2.add(slider, "grow");
323     jPanel2.add(thresholdValue, "grow");
324     this.add(jPanel1, java.awt.BorderLayout.SOUTH);
325     this.add(jPanel2, java.awt.BorderLayout.CENTER);
326     this.validate();
327   }
328
329   protected void seqAssociated_actionPerformed(ActionEvent arg0)
330   {
331     changeColour();
332   }
333
334   protected void resetColours_actionPerformed(ActionEvent arg0)
335   {
336     setDefaultMinMax();
337     changeColour();
338   }
339
340   JComboBox annotations;
341
342   JPanel minColour = new JPanel();
343
344   JPanel maxColour = new JPanel();
345   JButton defColours = new JButton();
346   JButton ok = new JButton();
347
348   JButton cancel = new JButton();
349
350   JPanel jPanel1 = new JPanel();
351   JPanel jPanel2 = new JPanel();
352   
353   BorderLayout borderLayout1 = new BorderLayout();
354
355   JComboBox threshold = new JComboBox();
356
357
358   JSlider slider = new JSlider();
359
360   JTextField thresholdValue = new JTextField(20);
361
362   JCheckBox currentColours = new JCheckBox();
363
364   JCheckBox thresholdIsMin = new JCheckBox();
365   
366   JCheckBox seqAssociated = new JCheckBox();
367
368   public void minColour_actionPerformed()
369   {
370     Color col = JColorChooser.showDialog(this,
371             "Select Colour for Minimum Value", minColour.getBackground());
372     if (col != null)
373     {
374       minColour.setBackground(col);
375     }
376     minColour.repaint();
377     changeColour();
378   }
379
380   public void maxColour_actionPerformed()
381   {
382     Color col = JColorChooser.showDialog(this,
383             "Select Colour for Maximum Value", maxColour.getBackground());
384     if (col != null)
385     {
386       maxColour.setBackground(col);
387     }
388     maxColour.repaint();
389     changeColour();
390   }
391
392   void changeColour()
393   {
394     // Check if combobox is still adjusting
395     if (adjusting)
396     {
397       return;
398     }
399
400     currentAnnotation = av.getAlignment().getAlignmentAnnotation()[annotations
401             .getSelectedIndex()];
402
403     int aboveThreshold = -1;
404     if (threshold.getSelectedItem().equals("Above Threshold"))
405     {
406       aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
407     }
408     else if (threshold.getSelectedItem().equals("Below Threshold"))
409     {
410       aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
411     }
412
413     slider.setEnabled(true);
414     thresholdValue.setEnabled(true);
415     thresholdIsMin.setEnabled(true);
416
417     if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
418     {
419       slider.setEnabled(false);
420       thresholdValue.setEnabled(false);
421       thresholdValue.setText("");
422       thresholdIsMin.setEnabled(false);
423     }
424     else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
425             && currentAnnotation.threshold == null)
426     {
427       currentAnnotation
428               .setThreshold(new jalview.datamodel.GraphLine(
429                       (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
430                       "Threshold", Color.black));
431     }
432
433     if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
434     {
435       adjusting = true;
436       float range = currentAnnotation.graphMax * 1000
437               - currentAnnotation.graphMin * 1000;
438
439       slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
440       slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
441       slider.setValue((int) (currentAnnotation.threshold.value * 1000));
442       thresholdValue.setText(currentAnnotation.threshold.value + "");
443       slider.setMajorTickSpacing((int) (range / 10f));
444       slider.setEnabled(true);
445       thresholdValue.setEnabled(true);
446       adjusting = false;
447     }
448
449     AnnotationColourGradient acg = null;
450     if (currentColours.isSelected())
451     {
452       acg = new AnnotationColourGradient(currentAnnotation,
453               av.getGlobalColourScheme(), aboveThreshold);
454     }
455     else
456     {
457       acg = new AnnotationColourGradient(currentAnnotation,
458               minColour.getBackground(), maxColour.getBackground(),
459               aboveThreshold);
460     }
461     acg.setSeqAssociated(seqAssociated.isSelected());
462
463     if (currentAnnotation.graphMin == 0f
464             && currentAnnotation.graphMax == 0f)
465     {
466       acg.predefinedColours = true;
467     }
468
469     acg.thresholdIsMinMax = thresholdIsMin.isSelected();
470
471     av.setGlobalColourScheme(acg);
472
473     if (av.getAlignment().getGroups() != null)
474     {
475       
476       for (SequenceGroup sg:ap.av.getAlignment().getGroups())
477       {
478         if (sg.cs == null)
479         {
480           continue;
481         }
482
483         if (currentColours.isSelected())
484         {
485           sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
486                   aboveThreshold);
487           ((AnnotationColourGradient)sg.cs).setSeqAssociated(seqAssociated.isSelected());
488
489         }
490         else
491         {
492           sg.cs = new AnnotationColourGradient(currentAnnotation,
493                   minColour.getBackground(), maxColour.getBackground(),
494                   aboveThreshold);
495           ((AnnotationColourGradient)sg.cs).setSeqAssociated(seqAssociated.isSelected());
496         }
497
498       }
499     }
500     ap.alignmentChanged();
501     // ensure all associated views (overviews, structures, etc) are notified of updated colours.
502     ap.paintAlignment(true);
503   }
504
505   public void ok_actionPerformed(ActionEvent e)
506   {
507     changeColour();
508     try
509     {
510       frame.setClosed(true);
511     } catch (Exception ex)
512     {
513     }
514   }
515
516   public void cancel_actionPerformed(ActionEvent e)
517   {
518     reset();
519     // ensure all original colouring is propagated to listeners. 
520     ap.paintAlignment(true);
521     try
522     {
523       frame.setClosed(true);
524     } catch (Exception ex)
525     {
526     }
527   }
528
529   void reset()
530   {
531     av.setGlobalColourScheme(oldcs);
532     if (av.getAlignment().getGroups() != null)
533     {
534       
535       for (SequenceGroup sg:ap.av.getAlignment().getGroups())
536       {
537         sg.cs = (ColourSchemeI) oldgroupColours.get(sg);
538       }
539     }
540   }
541
542   public void thresholdCheck_actionPerformed(ActionEvent e)
543   {
544     changeColour();
545   }
546
547   public void annotations_actionPerformed(ActionEvent e)
548   {
549     changeColour();
550   }
551
552   public void threshold_actionPerformed(ActionEvent e)
553   {
554     changeColour();
555   }
556
557   public void thresholdValue_actionPerformed(ActionEvent e)
558   {
559     try
560     {
561       float f = Float.parseFloat(thresholdValue.getText());
562       slider.setValue((int) (f * 1000));
563     } catch (NumberFormatException ex)
564     {
565     }
566   }
567
568   public void valueChanged()
569   {
570     if (currentColours.isSelected()
571             && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
572     {
573       changeColour();
574     }
575
576     currentAnnotation.threshold.value = (float) slider.getValue() / 1000f;
577     ap.paintAlignment(false);
578   }
579
580   public void currentColours_actionPerformed(ActionEvent e)
581   {
582     if (currentColours.isSelected())
583     {
584       reset();
585     }
586
587     maxColour.setEnabled(!currentColours.isSelected());
588     minColour.setEnabled(!currentColours.isSelected());
589
590     changeColour();
591   }
592
593   public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)
594   {
595     changeColour();
596   }
597
598 }