Formatting
[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\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 \r
67     if (av.alignment.getAlignmentAnnotation() == null)\r
68     {\r
69       return;\r
70     }\r
71 \r
72     if (oldcs instanceof AnnotationColourGradient)\r
73     {\r
74       AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;\r
75       minColour.setBackground(acg.getMinColour());\r
76       maxColour.setBackground(acg.getMaxColour());\r
77     }\r
78     else\r
79     {\r
80       minColour.setBackground(Color.orange);\r
81       maxColour.setBackground(Color.red);\r
82     }\r
83 \r
84     adjusting = true;\r
85     for (int i = 0; i < av.alignment.getAlignmentAnnotation().length; i++)\r
86     {\r
87       if (av.alignment.getAlignmentAnnotation()[i].graph > 0)\r
88       {\r
89         annotations.addItem(av.alignment.getAlignmentAnnotation()[i].label);\r
90       }\r
91     }\r
92 \r
93     threshold.addItem("No Threshold");\r
94     threshold.addItem("Above Threshold");\r
95     threshold.addItem("Below Threshold");\r
96 \r
97     adjusting = false;\r
98 \r
99     changeColour();\r
100 \r
101     frame = new Frame();\r
102     frame.add(this);\r
103     jalview.bin.JalviewLite.addFrame(frame, "Colour by Annotation", 480, 145);\r
104     validate();\r
105   }\r
106 \r
107   public AnnotationColourChooser()\r
108   {\r
109     try\r
110     {\r
111       jbInit();\r
112     }\r
113     catch (Exception ex)\r
114     {\r
115       ex.printStackTrace();\r
116     }\r
117   }\r
118 \r
119   private void jbInit()\r
120       throws Exception\r
121   {\r
122     minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));\r
123     minColour.setLabel("Min Colour");\r
124     minColour.addActionListener(this);\r
125 \r
126     maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));\r
127     maxColour.setLabel("Max Colour");\r
128     maxColour.addActionListener(this);\r
129 \r
130     ok.setLabel("OK");\r
131     ok.addActionListener(this);\r
132 \r
133     cancel.setLabel("Cancel");\r
134     cancel.addActionListener(this);\r
135 \r
136     this.setLayout(borderLayout1);\r
137     jPanel2.setLayout(flowLayout1);\r
138     annotations.addItemListener(this);\r
139 \r
140     jPanel1.setBackground(Color.white);\r
141     jPanel2.setBackground(Color.white);\r
142     threshold.addItemListener(this);\r
143     jPanel3.setLayout(null);\r
144     thresholdValue.addActionListener(this);\r
145 \r
146     slider.setBackground(Color.white);\r
147     slider.setEnabled(false);\r
148     slider.setBounds(new Rectangle(172, 7, 120, 16));\r
149     thresholdValue.setEnabled(false);\r
150     thresholdValue.setBounds(new Rectangle(295, 4, 83, 22));\r
151     thresholdValue.setColumns(10);\r
152     jPanel3.setBackground(Color.white);\r
153     currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));\r
154     currentColours.setLabel("Use Original Colours");\r
155     currentColours.addItemListener(this);\r
156 \r
157     threshold.setBounds(new Rectangle(19, 4, 141, 22));\r
158     jPanel1.add(ok);\r
159     jPanel1.add(cancel);\r
160     jPanel2.add(annotations);\r
161     jPanel2.add(currentColours);\r
162     jPanel2.add(minColour);\r
163     jPanel2.add(maxColour);\r
164     jPanel3.add(threshold);\r
165     jPanel3.add(slider);\r
166     jPanel3.add(thresholdValue);\r
167     this.add(jPanel2, java.awt.BorderLayout.NORTH);\r
168     this.add(jPanel3, java.awt.BorderLayout.CENTER);\r
169     this.add(jPanel1, java.awt.BorderLayout.SOUTH);\r
170   }\r
171 \r
172   Choice annotations = new Choice();\r
173   Button minColour = new Button();\r
174   Button maxColour = new Button();\r
175   Button ok = new Button();\r
176   Button cancel = new Button();\r
177   Panel jPanel1 = new Panel();\r
178   Panel jPanel2 = new Panel();\r
179   Choice threshold = new Choice();\r
180   FlowLayout flowLayout1 = new FlowLayout();\r
181   Panel jPanel3 = new Panel();\r
182   Scrollbar slider = new Scrollbar(Scrollbar.HORIZONTAL);\r
183   TextField thresholdValue = new TextField(20);\r
184   Checkbox currentColours = new Checkbox();\r
185   BorderLayout borderLayout1 = new BorderLayout();\r
186 \r
187   public void actionPerformed(ActionEvent evt)\r
188   {\r
189     if (evt.getSource() == thresholdValue)\r
190     {\r
191       try\r
192       {\r
193         float f = new Float(thresholdValue.getText()).floatValue();\r
194         slider.setValue( (int) (f * 1000));\r
195         adjustmentValueChanged(null);\r
196       }\r
197       catch (NumberFormatException ex)\r
198       {}\r
199     }\r
200     else if (evt.getSource() == minColour)\r
201     {\r
202       minColour_actionPerformed(null);\r
203     }\r
204     else if (evt.getSource() == maxColour)\r
205     {\r
206       maxColour_actionPerformed(null);\r
207     }\r
208 \r
209     else if (evt.getSource() == ok)\r
210     {\r
211       changeColour();\r
212       frame.setVisible(false);\r
213     }\r
214     else if (evt.getSource() == cancel)\r
215     {\r
216       reset();\r
217       ap.repaint();\r
218       frame.setVisible(false);\r
219     }\r
220 \r
221     else\r
222     {\r
223       changeColour();\r
224     }\r
225   }\r
226 \r
227   public void itemStateChanged(ItemEvent evt)\r
228   {\r
229     if (evt.getSource() == currentColours)\r
230     {\r
231       if (currentColours.getState())\r
232       {\r
233         reset();\r
234       }\r
235 \r
236       maxColour.setEnabled(!currentColours.getState());\r
237       minColour.setEnabled(!currentColours.getState());\r
238 \r
239     }\r
240 \r
241     changeColour();\r
242   }\r
243 \r
244   public void adjustmentValueChanged(AdjustmentEvent evt)\r
245   {\r
246     if (!adjusting)\r
247     {\r
248       thresholdValue.setText( ( (float) slider.getValue() / 1000f) + "");\r
249       if (currentColours.getState()\r
250           && ! (av.getGlobalColourScheme() instanceof AnnotationColourGradient))\r
251       {\r
252         changeColour();\r
253       }\r
254 \r
255       currentAnnotation.threshold.value = (float) slider.getValue() / 1000f;\r
256       ap.repaint();\r
257     }\r
258   }\r
259 \r
260   public void minColour_actionPerformed(Color newCol)\r
261   {\r
262     if (newCol != null)\r
263     {\r
264       minColour.setBackground(newCol);\r
265       minColour.repaint();\r
266       changeColour();\r
267     }\r
268     else\r
269     {\r
270       new UserDefinedColours(this, "Min Colour",\r
271                              minColour.getBackground());\r
272     }\r
273 \r
274   }\r
275 \r
276   public void maxColour_actionPerformed(Color newCol)\r
277   {\r
278     if (newCol != null)\r
279     {\r
280       maxColour.setBackground(newCol);\r
281       maxColour.repaint();\r
282       changeColour();\r
283     }\r
284     else\r
285     {\r
286       new UserDefinedColours(this, "Max Colour",\r
287                              maxColour.getBackground());\r
288     }\r
289   }\r
290 \r
291   void changeColour()\r
292   {\r
293     // Check if combobox is still adjusting\r
294     if (adjusting)\r
295     {\r
296       return;\r
297     }\r
298 \r
299     // We removed the non-graph annotations when filling the combobox\r
300     // so allow for them again here\r
301     int nograph = 0, graph = -1;\r
302     for (int i = 0; i < av.alignment.getAlignmentAnnotation().length; i++)\r
303     {\r
304       if (av.alignment.getAlignmentAnnotation()[i].graph == 0)\r
305       {\r
306         nograph++;\r
307       }\r
308       else\r
309       {\r
310         graph++;\r
311       }\r
312 \r
313       if (graph == annotations.getSelectedIndex())\r
314       {\r
315         break;\r
316       }\r
317     }\r
318 \r
319     currentAnnotation = av.alignment.getAlignmentAnnotation()[graph + nograph];\r
320 \r
321     int aboveThreshold = -1;\r
322     if (threshold.getSelectedItem().equals("Above Threshold"))\r
323     {\r
324       aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;\r
325     }\r
326     else if (threshold.getSelectedItem().equals("Below Threshold"))\r
327     {\r
328       aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;\r
329     }\r
330 \r
331     slider.setEnabled(true);\r
332     thresholdValue.setEnabled(true);\r
333 \r
334     if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)\r
335     {\r
336       slider.setEnabled(false);\r
337       thresholdValue.setEnabled(false);\r
338       thresholdValue.setText("");\r
339     }\r
340     else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD &&\r
341              currentAnnotation.threshold == null)\r
342     {\r
343       currentAnnotation.setThreshold(new jalview.datamodel.GraphLine\r
344                                      ( (currentAnnotation.graphMax -\r
345                                         currentAnnotation.graphMin) / 2f,\r
346                                       "Threshold",\r
347                                       Color.black));\r
348     }\r
349 \r
350     if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)\r
351     {\r
352       adjusting = true;\r
353 \r
354       slider.setMinimum( (int) (currentAnnotation.graphMin * 1000));\r
355       slider.setMaximum( (int) (currentAnnotation.graphMax * 1000));\r
356       slider.setValue( (int) (currentAnnotation.threshold.value * 1000));\r
357       thresholdValue.setText(currentAnnotation.threshold.value + "");\r
358       slider.setEnabled(true);\r
359       thresholdValue.setEnabled(true);\r
360       adjusting = false;\r
361     }\r
362 \r
363     AnnotationColourGradient acg = null;\r
364     if (currentColours.getState())\r
365     {\r
366       acg = new AnnotationColourGradient(\r
367           currentAnnotation,\r
368           av.getGlobalColourScheme(), aboveThreshold);\r
369     }\r
370     else\r
371     {\r
372       acg =\r
373           new AnnotationColourGradient(\r
374               currentAnnotation,\r
375               minColour.getBackground(),\r
376               maxColour.getBackground(),\r
377               aboveThreshold);\r
378     }\r
379 \r
380     av.setGlobalColourScheme(acg);\r
381 \r
382     if (av.alignment.getGroups() != null)\r
383     {\r
384       Vector allGroups = ap.av.alignment.getGroups();\r
385       SequenceGroup sg;\r
386       for (int g = 0; g < allGroups.size(); g++)\r
387       {\r
388         sg = (SequenceGroup) allGroups.elementAt(g);\r
389 \r
390         if (sg.cs == null)\r
391         {\r
392           continue;\r
393         }\r
394 \r
395         if (currentColours.getState())\r
396         {\r
397           sg.cs = new AnnotationColourGradient(\r
398               currentAnnotation,\r
399               sg.cs, aboveThreshold);\r
400         }\r
401         else\r
402         {\r
403           sg.cs = new AnnotationColourGradient(\r
404               currentAnnotation,\r
405               minColour.getBackground(),\r
406               maxColour.getBackground(),\r
407               aboveThreshold);\r
408         }\r
409 \r
410       }\r
411     }\r
412 \r
413     ap.repaint();\r
414   }\r
415 \r
416   void reset()\r
417   {\r
418     av.setGlobalColourScheme(oldcs);\r
419     if (av.alignment.getGroups() != null)\r
420     {\r
421       Vector allGroups = ap.av.alignment.getGroups();\r
422       SequenceGroup sg;\r
423       for (int g = 0; g < allGroups.size(); g++)\r
424       {\r
425         sg = (SequenceGroup) allGroups.elementAt(g);\r
426         sg.cs = (ColourSchemeI) oldgroupColours.get(sg);\r
427       }\r
428     }\r
429     ap.repaint();\r
430 \r
431   }\r
432 \r
433 }\r