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