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