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