Threshold single element
[jalview.git] / src / jalview / gui / AnnotationColourChooser.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2005 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.gui;\r
20 \r
21 import javax.swing.*;\r
22 import java.awt.event.ActionListener;\r
23 import java.awt.event.ActionEvent;\r
24 import java.awt.BorderLayout;\r
25 import java.awt.*;\r
26 \r
27 import jalview.schemes.*;\r
28 \r
29 public class AnnotationColourChooser\r
30     extends JPanel\r
31 {\r
32   JInternalFrame frame;\r
33   AlignViewport av;\r
34   AlignmentPanel ap;\r
35   ColourSchemeI oldcs;\r
36 \r
37   public AnnotationColourChooser(AlignViewport av, AlignmentPanel ap)\r
38   {\r
39     oldcs = av.getGlobalColourScheme();\r
40     this.av = av;\r
41     this.ap = ap;\r
42     frame = new JInternalFrame();\r
43     frame.setContentPane(this);\r
44     frame.setLayer(JLayeredPane.PALETTE_LAYER);\r
45     Desktop.addInternalFrame(frame, "Colour by Annotation", 480, 110, false);\r
46 \r
47     try{\r
48       jbInit();\r
49     }catch(Exception ex){}\r
50 \r
51     if(av.alignment.getAlignmentAnnotation()==null)\r
52       return;\r
53 \r
54     if(oldcs instanceof AnnotationColourGradient)\r
55     {\r
56       AnnotationColourGradient acg = (AnnotationColourGradient)oldcs;\r
57       minColour.setBackground( acg.getMinColour() );\r
58       maxColour.setBackground( acg.getMaxColour() );\r
59     }\r
60     else\r
61     {\r
62       minColour.setBackground(Color.orange);\r
63       maxColour.setBackground(Color.red);\r
64     }\r
65 \r
66     for (int i = 0; i < av.alignment.getAlignmentAnnotation().length; i++)\r
67     {\r
68       if(av.alignment.getAlignmentAnnotation()[i].graph>0)\r
69         annotations.addItem(av.alignment.getAlignmentAnnotation()[i].label);\r
70     }\r
71 \r
72     threshold.addItem("No Threshold");\r
73     threshold.addItem("Above Threshold");\r
74     threshold.addItem("Below Threshold");\r
75 \r
76   }\r
77 \r
78   public AnnotationColourChooser()\r
79   {\r
80     try\r
81     {\r
82       jbInit();\r
83     }\r
84     catch (Exception ex)\r
85     {\r
86       ex.printStackTrace();\r
87     }\r
88   }\r
89 \r
90   private void jbInit()\r
91       throws Exception\r
92   {\r
93     minColour.setBounds(new Rectangle(145, 5, 85, 25));\r
94     minColour.setToolTipText("");\r
95     minColour.setMargin(new Insets(2, 2, 2, 2));\r
96     minColour.setText("Min Colour");\r
97     minColour.addActionListener(new ActionListener()\r
98     {\r
99       public void actionPerformed(ActionEvent e)\r
100       {\r
101         minColour_actionPerformed(e);\r
102       }\r
103     });\r
104     maxColour.setBounds(new Rectangle(235, 5, 89, 25));\r
105     maxColour.setMargin(new Insets(2, 2, 2, 2));\r
106     maxColour.setText("Max Colour");\r
107     maxColour.addActionListener(new ActionListener()\r
108     {\r
109       public void actionPerformed(ActionEvent e)\r
110       {\r
111         maxColour_actionPerformed(e);\r
112       }\r
113     });\r
114     ok.setOpaque(false);\r
115     ok.setText("OK");\r
116     ok.addActionListener(new ActionListener()\r
117     {\r
118       public void actionPerformed(ActionEvent e)\r
119       {\r
120         ok_actionPerformed(e);\r
121       }\r
122     });\r
123     cancel.setOpaque(false);\r
124     cancel.setText("Cancel");\r
125     cancel.addActionListener(new ActionListener()\r
126     {\r
127       public void actionPerformed(ActionEvent e)\r
128       {\r
129         cancel_actionPerformed(e);\r
130       }\r
131     });\r
132     this.setLayout(borderLayout1);\r
133     jPanel2.setLayout(null);\r
134     annotations.setBounds(new Rectangle(5, 7, 135, 21));\r
135     annotations.addActionListener(new ActionListener()\r
136     {\r
137       public void actionPerformed(ActionEvent e)\r
138       {\r
139         annotations_actionPerformed(e);\r
140       }\r
141     });\r
142     jPanel1.setBackground(Color.white);\r
143     jPanel2.setBackground(Color.white);\r
144     threshold.setBounds(new Rectangle(328, 6, 125, 22));\r
145     threshold.addActionListener(new ActionListener()\r
146     {\r
147       public void actionPerformed(ActionEvent e)\r
148       {\r
149         threshold_actionPerformed(e);\r
150       }\r
151     });\r
152     jPanel1.add(ok);\r
153     jPanel1.add(cancel);\r
154     jPanel2.add(annotations);\r
155     jPanel2.add(minColour);\r
156     jPanel2.add(maxColour);\r
157     jPanel2.add(threshold);\r
158     this.add(jPanel1, java.awt.BorderLayout.SOUTH);\r
159     this.add(jPanel2, java.awt.BorderLayout.CENTER);\r
160   }\r
161 \r
162   JComboBox annotations = new JComboBox();\r
163   JButton minColour = new JButton();\r
164   JButton maxColour = new JButton();\r
165   JButton ok = new JButton();\r
166   JButton cancel = new JButton();\r
167   JPanel jPanel1 = new JPanel();\r
168   JPanel jPanel2 = new JPanel();\r
169   BorderLayout borderLayout1 = new BorderLayout();\r
170   JComboBox threshold = new JComboBox();\r
171 \r
172   public void minColour_actionPerformed(ActionEvent e)\r
173   {\r
174     Color col = JColorChooser.showDialog(this,\r
175                                          "Select Colour for Minimum Value",\r
176                                          minColour.getBackground());\r
177     if (col != null)\r
178       minColour.setBackground(col);\r
179     minColour.repaint();\r
180     changeColour();\r
181   }\r
182 \r
183   public void maxColour_actionPerformed(ActionEvent e)\r
184   {\r
185     Color col = JColorChooser.showDialog(this,\r
186                                          "Select Colour for Maximum Value",\r
187                                          maxColour.getBackground());\r
188     if (col != null)\r
189       maxColour.setBackground(col);\r
190     maxColour.repaint();\r
191     changeColour();\r
192   }\r
193 \r
194   void changeColour()\r
195   {\r
196     // Check if combobox is still adjusting\r
197     if(threshold.getSelectedIndex()==-1)\r
198           return;\r
199 \r
200     // We removed the non-graph annotations when filling the combobox\r
201     // so allow for them again here\r
202     int nograph = 0, graph = -1;\r
203     for (int i = 0; i < av.alignment.getAlignmentAnnotation().length; i++)\r
204     {\r
205       if (av.alignment.getAlignmentAnnotation()[i].graph == 0)\r
206         nograph ++;\r
207       else\r
208         graph ++;\r
209 \r
210       if(graph==annotations.getSelectedIndex())\r
211         break;\r
212     }\r
213 \r
214     jalview.datamodel.AlignmentAnnotation aa\r
215         = av.alignment.getAlignmentAnnotation()[graph+nograph];\r
216 \r
217 \r
218     int aboveThreshold = -1;\r
219     if(threshold.getSelectedItem().equals("Above Threshold"))\r
220       aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;\r
221     else if(threshold.getSelectedItem().equals("Below Threshold"))\r
222       aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;\r
223 \r
224     if(aboveThreshold!=AnnotationColourGradient.NO_THRESHOLD && aa.threshold==null)\r
225    {\r
226      aa.setThreshold(new jalview.datamodel.GraphLine((aa.graphMax-aa.graphMin)/2f, "Threshold", Color.black));\r
227    }\r
228 \r
229     AnnotationColourGradient acg = new AnnotationColourGradient(aa,\r
230      minColour.getBackground(),\r
231      maxColour.getBackground(),\r
232      aboveThreshold );\r
233 \r
234 \r
235 \r
236 \r
237    av.setGlobalColourScheme(acg);\r
238    ap.repaint();\r
239 \r
240   }\r
241 \r
242   public void ok_actionPerformed(ActionEvent e)\r
243   {\r
244     changeColour();\r
245     try{\r
246       frame.setClosed(true);\r
247     }catch(Exception ex){}\r
248   }\r
249 \r
250   public void cancel_actionPerformed(ActionEvent e)\r
251   {\r
252     av.setGlobalColourScheme(oldcs);\r
253     try{\r
254       frame.setClosed(true);\r
255     }catch(Exception ex){}\r
256   }\r
257 \r
258   public void thresholdCheck_actionPerformed(ActionEvent e)\r
259   {\r
260     changeColour();\r
261   }\r
262 \r
263   public void annotations_actionPerformed(ActionEvent e)\r
264   {\r
265     changeColour();\r
266   }\r
267 \r
268   public void threshold_actionPerformed(ActionEvent e)\r
269   {\r
270     changeColour();\r
271   }\r
272 \r
273 }\r