Allow for non graph annotations in combobox
[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   }\r
73 \r
74   public AnnotationColourChooser()\r
75   {\r
76     try\r
77     {\r
78       jbInit();\r
79     }\r
80     catch (Exception ex)\r
81     {\r
82       ex.printStackTrace();\r
83     }\r
84   }\r
85 \r
86   private void jbInit()\r
87       throws Exception\r
88   {\r
89     minColour.setBounds(new Rectangle(145, 5, 85, 25));\r
90     minColour.setToolTipText("");\r
91     minColour.setMargin(new Insets(2, 2, 2, 2));\r
92     minColour.setText("Min Colour");\r
93     minColour.addActionListener(new ActionListener()\r
94     {\r
95       public void actionPerformed(ActionEvent e)\r
96       {\r
97         minColour_actionPerformed(e);\r
98       }\r
99     });\r
100     maxColour.setBounds(new Rectangle(235, 5, 89, 25));\r
101     maxColour.setMargin(new Insets(2, 2, 2, 2));\r
102     maxColour.setText("Max Colour");\r
103     maxColour.addActionListener(new ActionListener()\r
104     {\r
105       public void actionPerformed(ActionEvent e)\r
106       {\r
107         maxColour_actionPerformed(e);\r
108       }\r
109     });\r
110     thresholdCheck.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));\r
111     thresholdCheck.setOpaque(false);\r
112     thresholdCheck.setHorizontalTextPosition(SwingConstants.LEADING);\r
113     thresholdCheck.setSelectedIcon(null);\r
114     thresholdCheck.setText("Above Threshold");\r
115     thresholdCheck.setBounds(new Rectangle(329, 6, 126, 23));\r
116     thresholdCheck.addActionListener(new ActionListener()\r
117     {\r
118       public void actionPerformed(ActionEvent e)\r
119       {\r
120         thresholdCheck_actionPerformed(e);\r
121       }\r
122     });\r
123     ok.setOpaque(false);\r
124     ok.setText("OK");\r
125     ok.addActionListener(new ActionListener()\r
126     {\r
127       public void actionPerformed(ActionEvent e)\r
128       {\r
129         ok_actionPerformed(e);\r
130       }\r
131     });\r
132     cancel.setOpaque(false);\r
133     cancel.setText("Cancel");\r
134     cancel.addActionListener(new ActionListener()\r
135     {\r
136       public void actionPerformed(ActionEvent e)\r
137       {\r
138         cancel_actionPerformed(e);\r
139       }\r
140     });\r
141     this.setLayout(borderLayout1);\r
142     jPanel2.setLayout(null);\r
143     annotations.setBounds(new Rectangle(5, 7, 135, 21));\r
144     annotations.addActionListener(new ActionListener()\r
145     {\r
146       public void actionPerformed(ActionEvent e)\r
147       {\r
148         annotations_actionPerformed(e);\r
149       }\r
150     });\r
151     jPanel1.setBackground(Color.white);\r
152     jPanel2.setBackground(Color.white);\r
153     jPanel1.add(ok);\r
154     jPanel1.add(cancel);\r
155     jPanel2.add(annotations);\r
156     jPanel2.add(minColour);\r
157     jPanel2.add(maxColour);\r
158     jPanel2.add(thresholdCheck);\r
159     this.add(jPanel1, java.awt.BorderLayout.SOUTH);\r
160     this.add(jPanel2, java.awt.BorderLayout.CENTER);\r
161   }\r
162 \r
163   JComboBox annotations = new JComboBox();\r
164   JButton minColour = new JButton();\r
165   JButton maxColour = new JButton();\r
166   JCheckBox thresholdCheck = new JCheckBox();\r
167   JButton ok = new JButton();\r
168   JButton cancel = new JButton();\r
169   JPanel jPanel1 = new JPanel();\r
170   JPanel jPanel2 = new JPanel();\r
171   BorderLayout borderLayout1 = new BorderLayout();\r
172 \r
173 \r
174   public void minColour_actionPerformed(ActionEvent e)\r
175   {\r
176     Color col = JColorChooser.showDialog(this,\r
177                                          "Select Colour for Minimum Value",\r
178                                          minColour.getBackground());\r
179     if (col != null)\r
180       minColour.setBackground(col);\r
181     minColour.repaint();\r
182     changeColour();\r
183   }\r
184 \r
185   public void maxColour_actionPerformed(ActionEvent e)\r
186   {\r
187     Color col = JColorChooser.showDialog(this,\r
188                                          "Select Colour for Maximum Value",\r
189                                          maxColour.getBackground());\r
190     if (col != null)\r
191       maxColour.setBackground(col);\r
192     maxColour.repaint();\r
193     changeColour();\r
194   }\r
195 \r
196   void changeColour()\r
197   {\r
198     // We removed the non-graph annotations when filling the combobox\r
199     // so allow for them again here\r
200     int nograph = 0, graph = -1;\r
201     for (int i = 0; i < av.alignment.getAlignmentAnnotation().length; i++)\r
202     {\r
203       if (av.alignment.getAlignmentAnnotation()[i].graph == 0)\r
204         nograph ++;\r
205       else\r
206         graph ++;\r
207 \r
208       if(graph==annotations.getSelectedIndex())\r
209         break;\r
210     }\r
211 \r
212     jalview.datamodel.AlignmentAnnotation aa\r
213         = av.alignment.getAlignmentAnnotation()[graph+nograph];\r
214 \r
215     AnnotationColourGradient acg = new AnnotationColourGradient(aa,\r
216      minColour.getBackground(),\r
217      maxColour.getBackground(),\r
218      thresholdCheck.isSelected());\r
219 \r
220    if(thresholdCheck.isSelected() && aa.graphLines==null)\r
221    {\r
222      aa.addGraphLine(new jalview.datamodel.GraphLine((aa.graphMax-aa.graphMin)/2f, "Threshold", Color.black));\r
223    }\r
224 \r
225    av.setGlobalColourScheme(acg);\r
226    ap.repaint();\r
227 \r
228   }\r
229 \r
230   public void ok_actionPerformed(ActionEvent e)\r
231   {\r
232     changeColour();\r
233     try{\r
234       frame.setClosed(true);\r
235     }catch(Exception ex){}\r
236   }\r
237 \r
238   public void cancel_actionPerformed(ActionEvent e)\r
239   {\r
240     av.setGlobalColourScheme(oldcs);\r
241     try{\r
242       frame.setClosed(true);\r
243     }catch(Exception ex){}\r
244   }\r
245 \r
246   public void thresholdCheck_actionPerformed(ActionEvent e)\r
247   {\r
248     changeColour();\r
249   }\r
250 \r
251   public void annotations_actionPerformed(ActionEvent e)\r
252   {\r
253     changeColour();\r
254   }\r
255 \r
256 }\r