No need for apply button, just update colours when events occur
[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", 470, 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       annotations.addItem(av.alignment.getAlignmentAnnotation()[i].label);\r
69     }\r
70 \r
71   }\r
72 \r
73   public AnnotationColourChooser()\r
74   {\r
75     try\r
76     {\r
77       jbInit();\r
78     }\r
79     catch (Exception ex)\r
80     {\r
81       ex.printStackTrace();\r
82     }\r
83   }\r
84 \r
85   private void jbInit()\r
86       throws Exception\r
87   {\r
88     minColour.setBounds(new Rectangle(145, 5, 85, 25));\r
89     minColour.setToolTipText("");\r
90     minColour.setMargin(new Insets(2, 2, 2, 2));\r
91     minColour.setText("Min Colour");\r
92     minColour.addActionListener(new ActionListener()\r
93     {\r
94       public void actionPerformed(ActionEvent e)\r
95       {\r
96         minColour_actionPerformed(e);\r
97       }\r
98     });\r
99     maxColour.setBounds(new Rectangle(235, 5, 89, 25));\r
100     maxColour.setMargin(new Insets(2, 2, 2, 2));\r
101     maxColour.setText("Max Colour");\r
102     maxColour.addActionListener(new ActionListener()\r
103     {\r
104       public void actionPerformed(ActionEvent e)\r
105       {\r
106         maxColour_actionPerformed(e);\r
107       }\r
108     });\r
109     thresholdCheck.setOpaque(false);\r
110     thresholdCheck.setHorizontalTextPosition(SwingConstants.LEADING);\r
111     thresholdCheck.setSelectedIcon(null);\r
112     thresholdCheck.setText("Above Threshold");\r
113     thresholdCheck.setBounds(new Rectangle(329, 6, 109, 23));\r
114     thresholdCheck.addActionListener(new ActionListener()\r
115     {\r
116       public void actionPerformed(ActionEvent e)\r
117       {\r
118         thresholdCheck_actionPerformed(e);\r
119       }\r
120     });\r
121     ok.setOpaque(false);\r
122     ok.setText("OK");\r
123     ok.addActionListener(new ActionListener()\r
124     {\r
125       public void actionPerformed(ActionEvent e)\r
126       {\r
127         ok_actionPerformed(e);\r
128       }\r
129     });\r
130     cancel.setOpaque(false);\r
131     cancel.setText("Cancel");\r
132     cancel.addActionListener(new ActionListener()\r
133     {\r
134       public void actionPerformed(ActionEvent e)\r
135       {\r
136         cancel_actionPerformed(e);\r
137       }\r
138     });\r
139     this.setLayout(borderLayout1);\r
140     jPanel2.setLayout(null);\r
141     annotations.setBounds(new Rectangle(5, 7, 135, 21));\r
142     annotations.addActionListener(new ActionListener()\r
143     {\r
144       public void actionPerformed(ActionEvent e)\r
145       {\r
146         annotations_actionPerformed(e);\r
147       }\r
148     });\r
149     jPanel1.setBackground(Color.white);\r
150     jPanel2.setBackground(Color.white);\r
151     jPanel1.add(ok);\r
152     jPanel1.add(cancel);\r
153     jPanel2.add(annotations);\r
154     jPanel2.add(minColour);\r
155     jPanel2.add(maxColour);\r
156     jPanel2.add(thresholdCheck);\r
157     this.add(jPanel1, java.awt.BorderLayout.SOUTH);\r
158     this.add(jPanel2, java.awt.BorderLayout.CENTER);\r
159   }\r
160 \r
161   JComboBox annotations = new JComboBox();\r
162   JButton minColour = new JButton();\r
163   JButton maxColour = new JButton();\r
164   JCheckBox thresholdCheck = new JCheckBox();\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 \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     jalview.datamodel.AlignmentAnnotation aa = av.alignment.getAlignmentAnnotation()[annotations.getSelectedIndex()];\r
197 \r
198     AnnotationColourGradient acg = new AnnotationColourGradient(aa,\r
199      minColour.getBackground(),\r
200      maxColour.getBackground(),\r
201      thresholdCheck.isSelected());\r
202 \r
203    if(thresholdCheck.isSelected() && aa.graphLines==null)\r
204    {\r
205      aa.addGraphLine(new jalview.datamodel.GraphLine((aa.graphMax-aa.graphMin)/2f, "Threshold", Color.black));\r
206    }\r
207 \r
208    av.setGlobalColourScheme(acg);\r
209    ap.repaint();\r
210 \r
211   }\r
212 \r
213   public void ok_actionPerformed(ActionEvent e)\r
214   {\r
215     changeColour();\r
216     try{\r
217       frame.setClosed(true);\r
218     }catch(Exception ex){}\r
219   }\r
220 \r
221   public void cancel_actionPerformed(ActionEvent e)\r
222   {\r
223     av.setGlobalColourScheme(oldcs);\r
224     try{\r
225       frame.setClosed(true);\r
226     }catch(Exception ex){}\r
227   }\r
228 \r
229   public void thresholdCheck_actionPerformed(ActionEvent e)\r
230   {\r
231     changeColour();\r
232   }\r
233 \r
234   public void annotations_actionPerformed(ActionEvent e)\r
235   {\r
236     changeColour();\r
237   }\r
238 \r
239 }\r