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