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