JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / jalview / appletgui / AnnotationColourChooser.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3  * Copyright (C) 2015 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.appletgui;
22
23 import jalview.datamodel.SequenceGroup;
24 import jalview.schemes.AnnotationColourGradient;
25 import jalview.schemes.ColourSchemeI;
26 import jalview.util.MessageManager;
27
28 import java.awt.BorderLayout;
29 import java.awt.Button;
30 import java.awt.Checkbox;
31 import java.awt.Choice;
32 import java.awt.Color;
33 import java.awt.Dimension;
34 import java.awt.FlowLayout;
35 import java.awt.Font;
36 import java.awt.Frame;
37 import java.awt.Panel;
38 import java.awt.Scrollbar;
39 import java.awt.TextField;
40 import java.awt.event.ActionEvent;
41 import java.awt.event.ActionListener;
42 import java.awt.event.AdjustmentEvent;
43 import java.awt.event.AdjustmentListener;
44 import java.awt.event.ItemEvent;
45 import java.awt.event.ItemListener;
46 import java.awt.event.MouseEvent;
47 import java.awt.event.MouseListener;
48 import java.util.Hashtable;
49 import java.util.Vector;
50
51 public class AnnotationColourChooser extends Panel implements
52         ActionListener, AdjustmentListener, ItemListener, MouseListener
53 {
54   Frame frame;
55
56   AlignViewport av;
57
58   AlignmentPanel ap;
59
60   ColourSchemeI oldcs;
61
62   Hashtable oldgroupColours;
63
64   jalview.datamodel.AlignmentAnnotation currentAnnotation;
65
66   boolean adjusting = false;
67
68   public AnnotationColourChooser(AlignViewport av, AlignmentPanel ap)
69   {
70     try
71     {
72       jbInit();
73     } catch (Exception ex)
74     {
75     }
76
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.cs != null)
84         {
85           oldgroupColours.put(sg, sg.cs);
86         }
87         else
88         {
89           oldgroupColours.put(sg, "null");
90         }
91       }
92     }
93     this.av = av;
94     this.ap = ap;
95
96     slider.addAdjustmentListener(this);
97     slider.addMouseListener(this);
98
99     if (av.getAlignment().getAlignmentAnnotation() == null)
100     {
101       return;
102     }
103
104     setDefaultMinMax();
105
106     adjusting = true;
107     if (oldcs instanceof AnnotationColourGradient)
108     {
109       AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
110       currentColours.setState(acg.isPredefinedColours()
111               || acg.getBaseColour() != null);
112       if (!acg.isPredefinedColours() && acg.getBaseColour() == null)
113       {
114         minColour.setBackground(acg.getMinColour());
115         maxColour.setBackground(acg.getMaxColour());
116       }
117       // seqAssociated.setState(acg.isSeqAssociated());
118     }
119
120     Vector list = new Vector();
121     int index = 1;
122     for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
123     {
124       String label = av.getAlignment().getAlignmentAnnotation()[i].label;
125       if (!list.contains(label))
126       {
127         list.addElement(label);
128       }
129       else
130       {
131         list.addElement(label + "_" + (index++));
132       }
133     }
134
135     for (int i = 0; i < list.size(); i++)
136     {
137       annotations.addItem(list.elementAt(i).toString());
138     }
139
140     threshold.addItem(MessageManager
141             .getString("label.threshold_feature_no_thereshold"));
142     threshold.addItem(MessageManager
143             .getString("label.threshold_feature_above_thereshold"));
144     threshold.addItem(MessageManager
145             .getString("label.threshold_feature_below_thereshold"));
146
147     if (oldcs instanceof AnnotationColourGradient)
148     {
149       AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
150       annotations.select(acg.getAnnotation());
151       switch (acg.getAboveThreshold())
152       {
153       case AnnotationColourGradient.NO_THRESHOLD:
154         threshold.select(0);
155         break;
156       case AnnotationColourGradient.ABOVE_THRESHOLD:
157         threshold.select(1);
158         break;
159       case AnnotationColourGradient.BELOW_THRESHOLD:
160         threshold.select(1);
161         break;
162       default:
163         throw new Error(
164                 MessageManager
165                         .getString("error.implementation_error_dont_know_thereshold_annotationcolourgradient"));
166       }
167       thresholdIsMin.setState(acg.thresholdIsMinMax);
168       thresholdValue.setText("" + acg.getAnnotationThreshold());
169     }
170
171     adjusting = false;
172
173     changeColour();
174
175     frame = new Frame();
176     frame.add(this);
177     jalview.bin.JalviewLite.addFrame(frame,
178             MessageManager.getString("label.colour_by_annotation"), 560,
179             175);
180     validate();
181   }
182
183   private void setDefaultMinMax()
184   {
185     minColour.setBackground(av.applet.getDefaultColourParameter(
186             "ANNOTATIONCOLOUR_MIN", Color.orange));
187     maxColour.setBackground(av.applet.getDefaultColourParameter(
188             "ANNOTATIONCOLOUR_MAX", Color.red));
189
190   }
191
192   public AnnotationColourChooser()
193   {
194     try
195     {
196       jbInit();
197     } catch (Exception ex)
198     {
199       ex.printStackTrace();
200     }
201   }
202
203   private void jbInit() throws Exception
204   {
205     minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
206     minColour.setLabel(MessageManager.getString("label.min_colour"));
207     minColour.addActionListener(this);
208
209     maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
210     maxColour.setLabel(MessageManager.getString("label.max_colour"));
211     maxColour.addActionListener(this);
212
213     thresholdIsMin.addItemListener(this);
214     ok.setLabel(MessageManager.getString("action.ok"));
215     ok.addActionListener(this);
216
217     cancel.setLabel(MessageManager.getString("action.cancel"));
218     cancel.addActionListener(this);
219
220     defColours.setLabel(MessageManager.getString("action.set_defaults"));
221     defColours.addActionListener(this);
222
223     annotations.addItemListener(this);
224
225     thresholdValue.addActionListener(this);
226     slider.setBackground(Color.white);
227     slider.setPreferredSize(new Dimension(193, 21));
228     slider.setEnabled(false);
229     thresholdValue.setPreferredSize(new Dimension(79, 22));
230     thresholdValue.setEnabled(false);
231     thresholdValue.setColumns(5);
232     currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
233     currentColours.setLabel(MessageManager
234             .getString("label.use_original_colours"));
235     currentColours.addItemListener(this);
236
237     thresholdIsMin.setBackground(Color.white);
238     thresholdIsMin.setLabel(MessageManager
239             .getString("label.threshold_minmax"));
240
241     this.setLayout(borderLayout1);
242
243     jPanel1.setBackground(Color.white);
244
245     jPanel2.setLayout(new FlowLayout());
246     jPanel2.setBackground(Color.white);
247     threshold.addItemListener(this);
248     jPanel3.setLayout(new FlowLayout());
249     jPanel3.setBackground(Color.white);
250     Panel jPanel4 = new Panel();
251     jPanel4.setLayout(new BorderLayout());
252     jPanel4.setBackground(Color.white);
253
254     jPanel1.add(ok);
255     jPanel1.add(cancel);
256
257     jPanel2.add(annotations);
258     jPanel2.add(currentColours);
259     jPanel2.add(minColour);
260     jPanel2.add(maxColour);
261
262     jPanel4.add(thresholdIsMin, BorderLayout.WEST);
263     jPanel4.add(slider, BorderLayout.CENTER);
264     jPanel4.add(thresholdValue, BorderLayout.EAST);
265
266     Panel jPanel34 = new Panel();
267     jPanel34.setLayout(new BorderLayout());
268     jPanel34.setBackground(Color.white);
269     jPanel34.add(jPanel2, BorderLayout.NORTH);
270     jPanel34.add(threshold, BorderLayout.WEST);
271     jPanel3.add(defColours);
272     jPanel34.add(jPanel3, BorderLayout.EAST);
273     jPanel34.add(jPanel4, BorderLayout.SOUTH);
274
275     this.add(jPanel34, java.awt.BorderLayout.CENTER);
276     this.add(jPanel1, java.awt.BorderLayout.SOUTH);
277
278   }
279
280   Choice annotations = new Choice();
281
282   Button minColour = new Button();
283
284   Button maxColour = new Button();
285
286   Button ok = new Button();
287
288   Button cancel = new Button();
289
290   Button defColours = new Button();
291
292   Panel jPanel1 = new Panel();
293
294   Panel jPanel2 = new Panel();
295
296   Choice threshold = new Choice();
297
298   FlowLayout flowLayout1 = new FlowLayout();
299
300   Panel jPanel3 = new Panel();
301
302   Scrollbar slider = new Scrollbar(Scrollbar.HORIZONTAL);
303
304   TextField thresholdValue = new TextField(20);
305
306   Checkbox currentColours = new Checkbox();
307
308   BorderLayout borderLayout1 = new BorderLayout();
309
310   Checkbox thresholdIsMin = new Checkbox();
311
312   public void actionPerformed(ActionEvent evt)
313   {
314     if (evt.getSource() == thresholdValue)
315     {
316       try
317       {
318         float f = new Float(thresholdValue.getText()).floatValue();
319         slider.setValue((int) (f * 1000));
320         adjustmentValueChanged(null);
321       } catch (NumberFormatException ex)
322       {
323       }
324     }
325     else if (evt.getSource() == minColour)
326     {
327       minColour_actionPerformed(null);
328     }
329     else if (evt.getSource() == maxColour)
330     {
331       maxColour_actionPerformed(null);
332     }
333     else if (evt.getSource() == defColours)
334     {
335       defColour_actionPerformed();
336     }
337     else if (evt.getSource() == ok)
338     {
339       frame.setVisible(false);
340     }
341     else if (evt.getSource() == cancel)
342     {
343       reset();
344       ap.paintAlignment(true);
345       frame.setVisible(false);
346     }
347
348     else
349     {
350       changeColour();
351     }
352   }
353
354   public void itemStateChanged(ItemEvent evt)
355   {
356     if (evt.getSource() == currentColours)
357     {
358       if (currentColours.getState())
359       {
360         reset();
361       }
362
363       maxColour.setEnabled(!currentColours.getState());
364       minColour.setEnabled(!currentColours.getState());
365
366     }
367
368     changeColour();
369   }
370
371   public void adjustmentValueChanged(AdjustmentEvent evt)
372   {
373     if (!adjusting)
374     {
375       thresholdValue.setText((slider.getValue() / 1000f) + "");
376       if (currentColours.getState()
377               && !(av.getGlobalColourScheme() instanceof AnnotationColourGradient))
378       {
379         changeColour();
380       }
381
382       currentAnnotation.threshold.value = slider.getValue() / 1000f;
383       ap.paintAlignment(false);
384     }
385   }
386
387   public void minColour_actionPerformed(Color newCol)
388   {
389     if (newCol != null)
390     {
391       minColour.setBackground(newCol);
392       minColour.repaint();
393       changeColour();
394     }
395     else
396     {
397       new UserDefinedColours(this, "Min Colour", minColour.getBackground());
398     }
399
400   }
401
402   public void maxColour_actionPerformed(Color newCol)
403   {
404     if (newCol != null)
405     {
406       maxColour.setBackground(newCol);
407       maxColour.repaint();
408       changeColour();
409     }
410     else
411     {
412       new UserDefinedColours(this, "Max Colour", maxColour.getBackground());
413     }
414   }
415
416   public void defColour_actionPerformed()
417   {
418     setDefaultMinMax();
419     minColour.repaint();
420     maxColour.repaint();
421     changeColour();
422   }
423
424   void changeColour()
425   {
426     // Check if combobox is still adjusting
427     if (adjusting)
428     {
429       return;
430     }
431
432     currentAnnotation = av.getAlignment().getAlignmentAnnotation()[annotations
433             .getSelectedIndex()];
434
435     int aboveThreshold = -1;
436     if (threshold.getSelectedIndex() == 1)
437     {
438       aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
439     }
440     else if (threshold.getSelectedIndex() == 2)
441     {
442       aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
443     }
444
445     slider.setEnabled(true);
446     thresholdValue.setEnabled(true);
447     thresholdIsMin.setEnabled(true);
448
449     if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
450     {
451       slider.setEnabled(false);
452       thresholdValue.setEnabled(false);
453       thresholdIsMin.setEnabled(false);
454       thresholdValue.setText("");
455     }
456     else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
457             && currentAnnotation.threshold == null)
458     {
459       currentAnnotation
460               .setThreshold(new jalview.datamodel.GraphLine(
461                       (currentAnnotation.graphMax - currentAnnotation.graphMin) / 2f,
462                       "Threshold", Color.black));
463     }
464
465     if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
466     {
467       adjusting = true;
468
469       slider.setMinimum((int) (currentAnnotation.graphMin * 1000));
470       slider.setMaximum((int) (currentAnnotation.graphMax * 1000));
471       slider.setValue((int) (currentAnnotation.threshold.value * 1000));
472       thresholdValue.setText(currentAnnotation.threshold.value + "");
473       slider.setEnabled(true);
474       thresholdValue.setEnabled(true);
475       adjusting = false;
476     }
477
478     AnnotationColourGradient acg = null;
479     if (currentColours.getState())
480     {
481       acg = new AnnotationColourGradient(currentAnnotation,
482               av.getGlobalColourScheme(), aboveThreshold);
483     }
484     else
485     {
486       acg = new AnnotationColourGradient(currentAnnotation,
487               minColour.getBackground(), maxColour.getBackground(),
488               aboveThreshold);
489     }
490
491     if (currentAnnotation.graphMin == 0f
492             && currentAnnotation.graphMax == 0f)
493     {
494       acg.setPredefinedColours(true);
495     }
496
497     acg.thresholdIsMinMax = thresholdIsMin.getState();
498
499     av.setGlobalColourScheme(acg);
500
501     // TODO: per group colour propagation not always desired
502     if (av.getAlignment().getGroups() != null)
503     {
504       for (SequenceGroup sg : ap.av.getAlignment().getGroups())
505       {
506
507         if (sg.cs == null)
508         {
509           continue;
510         }
511
512         if (currentColours.getState())
513         {
514           sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
515                   aboveThreshold);
516         }
517         else
518         {
519           sg.cs = new AnnotationColourGradient(currentAnnotation,
520                   minColour.getBackground(), maxColour.getBackground(),
521                   aboveThreshold);
522         }
523
524       }
525     }
526
527     // update colours in linked windows
528     ap.alignmentChanged();
529     ap.paintAlignment(true);
530   }
531
532   void reset()
533   {
534     av.setGlobalColourScheme(oldcs);
535     if (av.getAlignment().getGroups() != null)
536     {
537       for (SequenceGroup sg : ap.av.getAlignment().getGroups())
538       {
539         Object cs = oldgroupColours.get(sg);
540         if (cs instanceof ColourSchemeI)
541         {
542           sg.cs = (ColourSchemeI) cs;
543         }
544         else
545         {
546           // probably the "null" string we set it to if it was null originally.
547           sg.cs = null;
548         }
549       }
550     }
551     ap.paintAlignment(true);
552
553   }
554
555   public void mouseClicked(MouseEvent evt)
556   {
557   }
558
559   public void mousePressed(MouseEvent evt)
560   {
561   }
562
563   public void mouseReleased(MouseEvent evt)
564   {
565     ap.paintAlignment(true);
566   }
567
568   public void mouseEntered(MouseEvent evt)
569   {
570   }
571
572   public void mouseExited(MouseEvent evt)
573   {
574   }
575
576 }