UserColour gui modified
[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       rScroller_adjustmentValueChanged(null);\r
96     }\r
97     catch (NumberFormatException ex)\r
98     {}\r
99   }\r
100 \r
101   protected void gText_actionPerformed(ActionEvent e)\r
102   {\r
103     try\r
104     {\r
105       int i = Integer.parseInt(gText.getText());\r
106       gScroller.setValue(i);\r
107       gScroller_adjustmentValueChanged(null);\r
108     }\r
109     catch (NumberFormatException ex)\r
110     {}\r
111 \r
112   }\r
113 \r
114   protected void bText_actionPerformed(ActionEvent e)\r
115   {\r
116     try\r
117     {\r
118       int i = Integer.parseInt(bText.getText());\r
119       bScroller.setValue(i);\r
120       bScroller_adjustmentValueChanged(null);\r
121     }\r
122     catch (NumberFormatException ex)\r
123     {}\r
124 \r
125   }\r
126 \r
127   protected void rScroller_adjustmentValueChanged(AdjustmentEvent e)\r
128   {\r
129     R = rScroller.getValue();\r
130     rText.setText(R + "");\r
131     colourChanged();\r
132   }\r
133 \r
134   protected void gScroller_adjustmentValueChanged(AdjustmentEvent e)\r
135   {\r
136     G = gScroller.getValue();\r
137     gText.setText(G + "");\r
138     colourChanged();\r
139   }\r
140 \r
141   protected void bScroller_adjustmentValueChanged(AdjustmentEvent e)\r
142   {\r
143     B = bScroller.getValue();\r
144     bText.setText(B + "");\r
145     colourChanged();\r
146   }\r
147 \r
148   public void colourChanged()\r
149   {\r
150     Color col = new Color(R, G, B);\r
151     target.setBackground(col);\r
152     target.repaint();\r
153 \r
154     if (selectedButton != null)\r
155     {\r
156       selectedButton.setBackground(col);\r
157       selectedButton.repaint();\r
158     }\r
159   }\r
160 \r
161   public void colourButtonPressed(MouseEvent e)\r
162   {\r
163     selectedButton = (Button) e.getSource();\r
164     Color col = selectedButton.getBackground();\r
165     R = col.getRed();\r
166     G = col.getGreen();\r
167     B = col.getBlue();\r
168     rScroller.setValue(R);\r
169     gScroller.setValue(G);\r
170     bScroller.setValue(B);\r
171     rText.setText(R + "");\r
172     gText.setText(G + "");\r
173     bText.setText(B + "");\r
174 \r
175     colourChanged();\r
176   }\r
177 \r
178   void makeButton(String label, String aa)\r
179   {\r
180     final Button button = new Button();\r
181     Color col = Color.white;\r
182 \r
183     try\r
184     {\r
185       col = oldColourScheme.findColour(aa, -1);\r
186     }\r
187     catch (Exception ex)\r
188     {}\r
189 \r
190     button.setBackground(col);\r
191     oldColours.addElement(col);\r
192     button.setLabel(label);\r
193     button.setForeground(col.darker().darker().darker());\r
194     button.setFont(new java.awt.Font("Verdana", 1, 10));\r
195     button.addMouseListener(new java.awt.event.MouseAdapter()\r
196     {\r
197       public void mousePressed(MouseEvent e)\r
198       {\r
199         colourButtonPressed(e);\r
200       }\r
201     });\r
202 \r
203     buttonPanel.add(button, null);\r
204   }\r
205 \r
206   protected void okButton_actionPerformed(ActionEvent e)\r
207   {\r
208     applyButton_actionPerformed(null);\r
209     frame.setVisible(false);\r
210   }\r
211 \r
212   protected void applyButton_actionPerformed(ActionEvent e)\r
213   {\r
214 \r
215     Color[] newColours = new Color[24];\r
216     for (int i = 0; i < 24; i++)\r
217     {\r
218       Button button = (Button) buttonPanel.getComponent(i);\r
219       newColours[i] = button.getBackground();\r
220     }\r
221 \r
222     UserColourScheme ucs = new UserColourScheme(newColours);\r
223     ucs.setThreshold(0, ap.av.getIgnoreGapsConsensus());\r
224 \r
225     if (seqGroup != null)\r
226     {\r
227       seqGroup.cs = ucs;\r
228     }\r
229     else\r
230     {\r
231       ap.av.setGlobalColourScheme(ucs);\r
232     }\r
233 \r
234     ap.seqPanel.seqCanvas.img = null;\r
235     ap.repaint();\r
236   }\r
237 \r
238 \r
239   protected void cancelButton_actionPerformed(ActionEvent e)\r
240   {\r
241 \r
242     Color[] newColours = new Color[24];\r
243     for (int i = 0; i < 24; i++)\r
244     {\r
245       newColours[i] = (Color) oldColours.elementAt(i);\r
246       buttonPanel.getComponent(i).setBackground(newColours[i]);\r
247     }\r
248 \r
249     UserColourScheme ucs = new UserColourScheme(newColours);\r
250 \r
251     if (seqGroup != null)\r
252     {\r
253       seqGroup.cs = ucs;\r
254     }\r
255     else\r
256     {\r
257       ap.av.setGlobalColourScheme(ucs);\r
258     }\r
259 \r
260     ap.repaint();\r
261 \r
262     frame.setVisible(false);\r
263   }\r
264 \r
265 }\r