Use paintAlignment
[jalview.git] / src / jalview / appletgui / AnnotationColourChooser.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 package jalview.appletgui;\r
20 \r
21 import java.util.*;\r
22 \r
23 import java.awt.*;\r
24 import java.awt.event.*;\r
25 \r
26 import jalview.datamodel.*;\r
27 import jalview.schemes.*;\r
28 \r
29 public class AnnotationColourChooser\r
30     extends Panel implements ActionListener,\r
31     AdjustmentListener, ItemListener, MouseListener\r
32 {\r
33   Frame frame;\r
34   AlignViewport av;\r
35   AlignmentPanel ap;\r
36   ColourSchemeI oldcs;\r
37   Hashtable oldgroupColours;\r
38   jalview.datamodel.AlignmentAnnotation currentAnnotation;\r
39   boolean adjusting = false;\r
40 \r
41   public AnnotationColourChooser(AlignViewport av, AlignmentPanel ap)\r
42   {\r
43     try\r
44     {\r
45       jbInit();\r
46     }\r
47     catch (Exception ex)\r
48     {}\r
49 \r
50     oldcs = av.getGlobalColourScheme();\r
51     if (av.alignment.getGroups() != null)\r
52     {\r
53       oldgroupColours = new Hashtable();\r
54       Vector allGroups = ap.av.alignment.getGroups();\r
55       SequenceGroup sg;\r
56       for (int g = 0; g < allGroups.size(); g++)\r
57       {\r
58         sg = (SequenceGroup) allGroups.elementAt(g);\r
59         oldgroupColours.put(sg, sg.cs);\r
60       }\r
61     }\r
62     this.av = av;\r
63     this.ap = ap;\r
64 \r
65     slider.addAdjustmentListener(this);\r
66     slider.addMouseListener(this);\r
67 \r
68     if (av.alignment.getAlignmentAnnotation() == null)\r
69     {\r
70       return;\r
71     }\r
72 \r
73     if (oldcs instanceof AnnotationColourGradient)\r
74     {\r
75       AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;\r
76       minColour.setBackground(acg.getMinColour());\r
77       maxColour.setBackground(acg.getMaxColour());\r
78     }\r
79     else\r
80     {\r
81       minColour.setBackground(Color.orange);\r
82       maxColour.setBackground(Color.red);\r
83     }\r
84 \r
85     adjusting = true;\r
86     for (int i = 0; i < av.alignment.getAlignmentAnnotation().length; i++)\r
87     {\r
88       if (av.alignment.getAlignmentAnnotation()[i].graph > 0)\r
89       {\r
90         annotations.addItem(av.alignment.getAlignmentAnnotation()[i].label);\r
91       }\r
92     }\r
93 \r
94     threshold.addItem("No Threshold");\r
95     threshold.addItem("Above Threshold");\r
96     threshold.addItem("Below Threshold");\r
97 \r
98     adjusting = false;\r
99 \r
100     changeColour();\r
101 \r
102     frame = new Frame();\r
103     frame.add(this);\r
104     jalview.bin.JalviewLite.addFrame(frame, "Colour by Annotation", 480, 145);\r
105     validate();\r
106   }\r
107 \r
108   public AnnotationColourChooser()\r
109   {\r
110     try\r
111     {\r
112       jbInit();\r
113     }\r
114     catch (Exception ex)\r
115     {\r
116       ex.printStackTrace();\r
117     }\r
118   }\r
119 \r
120   private void jbInit()\r
121       throws Exception\r
122   {\r
123     minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));\r
124     minColour.setLabel("Min Colour");\r
125     minColour.addActionListener(this);\r
126 \r
127     maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));\r
128     maxColour.setLabel("Max Colour");\r
129     maxColour.addActionListener(this);\r
130 \r
131     ok.setLabel("OK");\r
132     ok.addActionListener(this);\r
133 \r
134     cancel.setLabel("Cancel");\r
135     cancel.addActionListener(this);\r
136 \r
137     this.setLayout(borderLayout1);\r
138     jPanel2.setLayout(flowLayout1);\r
139     annotations.addItemListener(this);\r
140 \r
141     jPanel1.setBackground(Color.white);\r
142     jPanel2.setBackground(Color.white);\r
143     threshold.addItemListener(this);\r
144     jPanel3.setLayout(null);\r
145     thresholdValue.addActionListener(this);\r
146 \r
147     slider.setBackground(Color.white);\r
148     slider.setEnabled(false);\r
149     slider.setBounds(new Rectangle(172, 7, 120, 16));\r
150     thresholdValue.setEnabled(false);\r
151     thresholdValue.setBounds(new Rectangle(295, 4, 83, 22));\r
152     thresholdValue.setColumns(10);\r
153     jPanel3.setBackground(Color.white);\r
154     currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));\r
155     currentColours.setLabel("Use Original Colours");\r
156     currentColours.addItemListener(this);\r
157 \r
158     threshold.setBounds(new Rectangle(19, 4, 141, 22));\r
159     jPanel1.add(ok);\r
160     jPanel1.add(cancel);\r
161     jPanel2.add(annotations);\r
162     jPanel2.add(currentColours);\r
163     jPanel2.add(minColour);\r
164     jPanel2.add(maxColour);\r
165     jPanel3.add(threshold);\r
166     jPanel3.add(slider);\r
167     jPanel3.add(thresholdValue);\r
168     this.add(jPanel2, java.awt.BorderLayout.NORTH);\r
169     this.add(jPanel3, java.awt.BorderLayout.CENTER);\r
170     this.add(jPanel1, java.awt.BorderLayout.SOUTH);\r
171   }\r
172 \r
173   Choice annotations = new Choice();\r
174   Button minColour = new Button();\r
175   Button maxColour = new Button();\r
176   Button ok = new Button();\r
177   Button cancel = new Button();\r
178   Panel jPanel1 = new Panel();\r
179   Panel jPanel2 = new Panel();\r
180   Choice threshold = new Choice();\r
181   FlowLayout flowLayout1 = new FlowLayout();\r
182   Panel jPanel3 = new Panel();\r
183   Scrollbar slider = new Scrollbar(Scrollbar.HORIZONTAL);\r
184   TextField thresholdValue = new TextField(20);\r
185   Checkbox currentColours = new Checkbox();\r
186   BorderLayout borderLayout1 = new BorderLayout();\r
187 \r
188   public void actionPerformed(ActionEvent evt)\r
189   {\r
190     if (evt.getSource() == thresholdValue)\r
191     {\r
192       try\r
193       {\r
194         float f = new Float(thresholdValue.getText()).floatValue();\r
195         slider.setValue( (int) (f * 1000));\r
196         adjustmentValueChanged(null);\r
197       }\r
198       catch (NumberFormatException ex)\r
199       {}\r
200     }\r
201     else if (evt.getSource() == minColour)\r
202     {\r
203       minColour_actionPerformed(null);\r
204     }\r
205     else if (evt.getSource() == maxColour)\r
206     {\r
207       maxColour_actionPerformed(null);\r
208     }\r
209 \r
210     else if (evt.getSource() == ok)\r
211     {\r
212       changeColour();\r
213       frame.setVisible(false);\r
214     }\r
215     else if (evt.getSource() == cancel)\r
216     {\r
217       reset();\r
218       ap.paintAlignment(true);\r
219       frame.setVisible(false);\r
220     }\r
221 \r
222     else\r
223     {\r
224       changeColour();\r
225     }\r
226   }\r
227 \r
228   public void itemStateChanged(ItemEvent evt)\r
229   {\r
230     if (evt.getSource() == currentColours)\r
231     {\r
232       if (currentColours.getState())\r
233       {\r
234         reset();\r
235       }\r
236 \r
237       maxColour.setEnabled(!currentColours.getState());\r
238       minColour.setEnabled(!currentColours.getState());\r
239 \r
240     }\r
241 \r
242     changeColour();\r
243   }\r
244 \r
245   public void adjustmentValueChanged(AdjustmentEvent evt)\r
246   {\r
247     if (!adjusting)\r
248     {\r
249       thresholdValue.setText( ( (float) slider.getValue() / 1000f) + "");\r
250       if (currentColours.getState()\r
251           && ! (av.getGlobalColourScheme() instanceof AnnotationColourGradient))\r
252       {\r
253         changeColour();\r
254       }\r
255 \r
256       currentAnnotation.threshold.value = (float) slider.getValue() / 1000f;\r
257       ap.paintAlignment(false);\r
258     }\r
259   }\r
260 \r
261   public void minColour_actionPerformed(Color newCol)\r
262   {\r
263     if (newCol != null)\r
264     {\r
265       minColour.setBackground(newCol);\r
266       minColour.repaint();\r
267       changeColour();\r
268     }\r
269     else\r
270     {\r
271       new UserDefinedColours(this, "Min Colour",\r
272                              minColour.getBackground());\r
273     }\r
274 \r
275   }\r
276 \r
277   public void maxColour_actionPerformed(Color newCol)\r
278   {\r
279     if (newCol != null)\r
280     {\r
281       maxColour.setBackground(newCol);\r
282       maxColour.repaint();\r
283       changeColour();\r
284     }\r
285     else\r
286     {\r
287       new UserDefinedColours(this, "Max Colour",\r
288                              maxColour.getBackground());\r
289     }\r
290   }\r
291 \r
292   void changeColour()\r
293   {\r
294     // Check if combobox is still adjusting\r
295     if (adjusting)\r
296     {\r
297       return;\r
298     }\r
299 \r
300     // We removed the non-graph annotations when filling the combobox\r
301     // so allow for them again here\r
302     int nograph = 0, graph = -1;\r
303     for (int i = 0; i < av.alignment.getAlignmentAnnotation().length; i++)\r
304     {\r
305       if (av.alignment.getAlignmentAnnotation()[i].graph == 0)\r
306       {\r
307         nograph++;\r
308       }\r
309       else\r
310       {\r
311         graph++;\r
312       }\r
313 \r
314       if (graph == annotations.getSelectedIndex())\r
315       {\r
316         break;\r
317       }\r
318     }\r
319 \r
320     currentAnnotation = av.alignment.getAlignmentAnnotation()[graph + nograph];\r
321 \r
322     int aboveThreshold = -1;\r
323     if (threshold.getSelectedItem().equals("Above Threshold"))\r
324     {\r
325       aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;\r
326     }\r
327     else if (threshold.getSelectedItem().equals("Below Threshold"))\r
328     {\r
329       aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;\r
330     }\r
331 \r
332     slider.setEnabled(true);\r
333     thresholdValue.setEnabled(true);\r
334 \r
335     if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)\r
336     {\r
337       slider.setEnabled(false);\r
338       thresholdValue.setEnabled(false);\r
339       thresholdValue.setText("");\r
340     }\r
341     else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD &&\r
342              currentAnnotation.threshold == null)\r
343     {\r
344       currentAnnotation.setThreshold(new jalview.datamodel.GraphLine\r
345                                      ( (currentAnnotation.graphMax -\r
346                                         currentAnnotation.graphMin) / 2f,\r
347                                       "Threshold",\r
348                                       Color.black));\r
349     }\r
350 \r
351     if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)\r
352     {\r
353       adjusting = true;\r
354 \r
355       slider.setMinimum( (int) (currentAnnotation.graphMin * 1000));\r
356       slider.setMaximum( (int) (currentAnnotation.graphMax * 1000));\r
357       slider.setValue( (int) (currentAnnotation.threshold.value * 1000));\r
358       thresholdValue.setText(currentAnnotation.threshold.value + "");\r
359       slider.setEnabled(true);\r
360       thresholdValue.setEnabled(true);\r
361       adjusting = false;\r
362     }\r
363 \r
364     AnnotationColourGradient acg = null;\r
365     if (currentColours.getState())\r
366     {\r
367       acg = new AnnotationColourGradient(\r
368           currentAnnotation,\r
369           av.getGlobalColourScheme(), aboveThreshold);\r
370     }\r
371     else\r
372     {\r
373       acg =\r
374           new AnnotationColourGradient(\r
375               currentAnnotation,\r
376               minColour.getBackground(),\r
377               maxColour.getBackground(),\r
378               aboveThreshold);\r
379     }\r
380 \r
381     av.setGlobalColourScheme(acg);\r
382 \r
383     if (av.alignment.getGroups() != null)\r
384     {\r
385       Vector allGroups = ap.av.alignment.getGroups();\r
386       SequenceGroup sg;\r
387       for (int g = 0; g < allGroups.size(); g++)\r
388       {\r
389         sg = (SequenceGroup) allGroups.elementAt(g);\r
390 \r
391         if (sg.cs == null)\r
392         {\r
393           continue;\r
394         }\r
395 \r
396         if (currentColours.getState())\r
397         {\r
398           sg.cs = new AnnotationColourGradient(\r
399               currentAnnotation,\r
400               sg.cs, aboveThreshold);\r
401         }\r
402         else\r
403         {\r
404           sg.cs = new AnnotationColourGradient(\r
405               currentAnnotation,\r
406               minColour.getBackground(),\r
407               maxColour.getBackground(),\r
408               aboveThreshold);\r
409         }\r
410 \r
411       }\r
412     }\r
413 \r
414     ap.paintAlignment(false);\r
415   }\r
416 \r
417   void reset()\r
418   {\r
419     av.setGlobalColourScheme(oldcs);\r
420     if (av.alignment.getGroups() != null)\r
421     {\r
422       Vector allGroups = ap.av.alignment.getGroups();\r
423       SequenceGroup sg;\r
424       for (int g = 0; g < allGroups.size(); g++)\r
425       {\r
426         sg = (SequenceGroup) allGroups.elementAt(g);\r
427         sg.cs = (ColourSchemeI) oldgroupColours.get(sg);\r
428       }\r
429     }\r
430     ap.paintAlignment(true);\r
431 \r
432   }\r
433 \r
434   public void mouseClicked(MouseEvent evt){}\r
435   public void mousePressed(MouseEvent evt){}\r
436   public void mouseReleased(MouseEvent evt){ ap.paintAlignment(true);}\r
437   public void mouseEntered(MouseEvent evt){}\r
438   public void mouseExited(MouseEvent evt){}\r
439 \r
440 \r
441 }\r