Validate buttons and fire on mouse press, not mouse click
[jalview.git] / src / jalview / appletgui / UserDefinedColours.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 \r
20 package jalview.appletgui;\r
21 \r
22 import java.util.*;\r
23 \r
24 import java.awt.*;\r
25 import java.awt.event.*;\r
26 \r
27 import jalview.datamodel.*;\r
28 import jalview.jbappletgui.*;\r
29 import jalview.schemes.*;\r
30 \r
31 public class UserDefinedColours\r
32     extends GUserDefinedColours\r
33 {\r
34 \r
35   AlignmentPanel ap;\r
36   SequenceGroup seqGroup;\r
37   Button selectedButton;\r
38   Vector oldColours = new Vector();\r
39   ColourSchemeI oldColourScheme;\r
40   Frame frame;\r
41 \r
42   int R = 0, G = 0, B = 0;\r
43 \r
44   public ColourSchemeI loadDefaultColours()\r
45   {\r
46     // NOT IMPLEMENTED YET IN APPLET VERSION\r
47     return null;\r
48   }\r
49 \r
50   public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)\r
51   {\r
52     super();\r
53     frame = new Frame();\r
54     frame.add(this);\r
55     jalview.bin.JalviewLite.addFrame(frame, "User defined colours", 420, 345);\r
56 \r
57     if (sg != null)\r
58     {\r
59       frame.setTitle(frame.getTitle() + " (" + sg.getName() + ")");\r
60     }\r
61 \r
62     this.ap = ap;\r
63     seqGroup = sg;\r
64 \r
65     if (seqGroup != null)\r
66     {\r
67       oldColourScheme = seqGroup.cs;\r
68     }\r
69     else\r
70     {\r
71       oldColourScheme = ap.av.getGlobalColourScheme();\r
72     }\r
73 \r
74     for (int i = 0; i < 20; i++)\r
75     {\r
76       makeButton(ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i]) +\r
77                  "", ResidueProperties.aa[i]);\r
78     }\r
79 \r
80     makeButton("B", "B");\r
81     makeButton("Z", "Z");\r
82     makeButton("X", "X");\r
83     makeButton("Gap", "'.','-',' '");\r
84 \r
85     validate();\r
86 \r
87   }\r
88 \r
89   protected void rText_actionPerformed(ActionEvent e)\r
90   {\r
91     try\r
92     {\r
93       int i = Integer.parseInt(rText.getText());\r
94       rScroller.setValue(i);\r
95     }\r
96     catch (NumberFormatException ex)\r
97     {}\r
98   }\r
99 \r
100   protected void gText_actionPerformed(ActionEvent e)\r
101   {\r
102     try\r
103     {\r
104       int i = Integer.parseInt(gText.getText());\r
105       gScroller.setValue(i);\r
106     }\r
107     catch (NumberFormatException ex)\r
108     {}\r
109 \r
110   }\r
111 \r
112   protected void bText_actionPerformed(ActionEvent e)\r
113   {\r
114     try\r
115     {\r
116       int i = Integer.parseInt(bText.getText());\r
117       bScroller.setValue(i);\r
118     }\r
119     catch (NumberFormatException ex)\r
120     {}\r
121 \r
122   }\r
123 \r
124   protected void rScroller_adjustmentValueChanged(AdjustmentEvent e)\r
125   {\r
126     R = rScroller.getValue();\r
127     rText.setText(R + "");\r
128     colourChanged();\r
129   }\r
130 \r
131   protected void gScroller_adjustmentValueChanged(AdjustmentEvent e)\r
132   {\r
133     G = gScroller.getValue();\r
134     gText.setText(G + "");\r
135     colourChanged();\r
136   }\r
137 \r
138   protected void bScroller_adjustmentValueChanged(AdjustmentEvent e)\r
139   {\r
140     B = bScroller.getValue();\r
141     bText.setText(B + "");\r
142     colourChanged();\r
143   }\r
144 \r
145   public void colourChanged()\r
146   {\r
147     Color col = new Color(R, G, B);\r
148     target.setBackground(col);\r
149 \r
150     if (selectedButton != null)\r
151     {\r
152       selectedButton.setBackground(col);\r
153     }\r
154   }\r
155 \r
156   public void colourButtonPressed(MouseEvent e)\r
157   {\r
158     selectedButton = (Button) e.getSource();\r
159     Color col = selectedButton.getBackground();\r
160     R = col.getRed();\r
161     G = col.getGreen();\r
162     B = col.getBlue();\r
163     rScroller.setValue(R);\r
164     gScroller.setValue(G);\r
165     bScroller.setValue(B);\r
166     rText.setText(R + "");\r
167     gText.setText(G + "");\r
168     bText.setText(B + "");\r
169 \r
170     colourChanged();\r
171   }\r
172 \r
173   void makeButton(String label, String aa)\r
174   {\r
175     final Button button = new Button();\r
176     Color col = Color.white;\r
177 \r
178     try\r
179     {\r
180       col = oldColourScheme.findColour(aa, -1);\r
181     }\r
182     catch (Exception ex)\r
183     {}\r
184 \r
185     button.setBackground(col);\r
186     oldColours.addElement(col);\r
187     button.setLabel(label);\r
188     button.setForeground(col.darker().darker().darker());\r
189     button.setFont(new java.awt.Font("Verdana", 1, 10));\r
190     button.addMouseListener(new java.awt.event.MouseAdapter()\r
191     {\r
192       public void mousePressed(MouseEvent e)\r
193       {\r
194         colourButtonPressed(e);\r
195       }\r
196     });\r
197 \r
198     buttonPanel.add(button, null);\r
199   }\r
200 \r
201   protected void okButton_actionPerformed(ActionEvent e)\r
202   {\r
203     applyButton_actionPerformed(null);\r
204     frame.setVisible(false);\r
205   }\r
206 \r
207   protected void applyButton_actionPerformed(ActionEvent e)\r
208   {\r
209 \r
210     Color[] newColours = new Color[24];\r
211     for (int i = 0; i < 24; i++)\r
212     {\r
213       Button button = (Button) buttonPanel.getComponent(i);\r
214       newColours[i] = button.getBackground();\r
215     }\r
216 \r
217     UserColourScheme ucs = new UserColourScheme(newColours);\r
218     ucs.setThreshold(0, ap.av.getIgnoreGapsConsensus());\r
219 \r
220     if (seqGroup != null)\r
221     {\r
222       seqGroup.cs = ucs;\r
223     }\r
224     else\r
225     {\r
226       ap.av.setGlobalColourScheme(ucs);\r
227     }\r
228 \r
229     ap.seqPanel.seqCanvas.img = null;\r
230     ap.repaint();\r
231   }\r
232 \r
233 \r
234   protected void cancelButton_actionPerformed(ActionEvent e)\r
235   {\r
236 \r
237     Color[] newColours = new Color[24];\r
238     for (int i = 0; i < 24; i++)\r
239     {\r
240       newColours[i] = (Color) oldColours.elementAt(i);\r
241       buttonPanel.getComponent(i).setBackground(newColours[i]);\r
242     }\r
243 \r
244     UserColourScheme ucs = new UserColourScheme(newColours);\r
245 \r
246     if (seqGroup != null)\r
247     {\r
248       seqGroup.cs = ucs;\r
249     }\r
250     else\r
251     {\r
252       ap.av.setGlobalColourScheme(ucs);\r
253     }\r
254 \r
255     ap.repaint();\r
256 \r
257     frame.setVisible(false);\r
258   }\r
259 \r
260 }\r