18382fbe4d9e9a96d257cc59fbfaac882bbf7bf6
[jalview.git] / src / jalview / gui / 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 package jalview.gui;\r
20 \r
21 import java.io.*;\r
22 import java.util.*;\r
23 \r
24 import java.awt.*;\r
25 import java.awt.event.*;\r
26 import javax.swing.*;\r
27 import javax.swing.event.*;\r
28 \r
29 import jalview.datamodel.*;\r
30 import jalview.io.*;\r
31 import jalview.jbgui.*;\r
32 import jalview.schemes.*;\r
33 \r
34 public class UserDefinedColours\r
35     extends GUserDefinedColours implements ChangeListener\r
36 {\r
37   AlignmentPanel ap;\r
38   SequenceGroup seqGroup;\r
39   JButton selectedButton;\r
40   Vector oldColours = new Vector();\r
41   ColourSchemeI oldColourScheme;\r
42   JInternalFrame frame;\r
43 \r
44   public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)\r
45   {\r
46     super();\r
47     frame = new JInternalFrame();\r
48     frame.setContentPane(this);\r
49     Desktop.addInternalFrame(frame, "User Defined Colours", 450, 530, false);\r
50 \r
51     if (System.getProperty("os.name").startsWith("Mac"))\r
52     {\r
53       frame.setSize(450, 560);\r
54     }\r
55 \r
56     if (sg != null)\r
57     {\r
58       frame.setTitle(frame.getTitle() + " (" + sg.getName() + ")");\r
59     }\r
60 \r
61     colorChooser.getSelectionModel().addChangeListener(this);\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     if (jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR") != null)\r
87     {\r
88       loadColours(jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR"));\r
89     }\r
90   }\r
91 \r
92   public void stateChanged(ChangeEvent evt)\r
93   {\r
94     if (selectedButton != null)\r
95     {\r
96       selectedButton.setBackground(colorChooser.getColor());\r
97     }\r
98   }\r
99 \r
100   public void colourButtonPressed(MouseEvent e)\r
101   {\r
102     selectedButton = (JButton) e.getSource();\r
103     colorChooser.setColor(selectedButton.getBackground());\r
104   }\r
105 \r
106   void makeButton(String label, String aa)\r
107   {\r
108     final JButton button = new JButton();\r
109     Color col = Color.white;\r
110 \r
111     try\r
112     {\r
113       col = oldColourScheme.findColour(aa, -1);\r
114     }\r
115     catch (Exception ex)\r
116     {\r
117     }\r
118 \r
119     button.setBackground(col);\r
120     oldColours.addElement(col);\r
121     button.setText(label);\r
122     button.setForeground(col.darker().darker().darker());\r
123     button.setFont(new java.awt.Font("Verdana", 1, 10));\r
124     button.addMouseListener(new java.awt.event.MouseAdapter()\r
125     {\r
126       public void mouseClicked(MouseEvent e)\r
127       {\r
128         colourButtonPressed(e);\r
129       }\r
130     });\r
131 \r
132     buttonPanel.add(button, null);\r
133   }\r
134 \r
135   protected void okButton_actionPerformed(ActionEvent e)\r
136   {\r
137     applyButton_actionPerformed(null);\r
138 \r
139     try\r
140     {\r
141       frame.setClosed(true);\r
142     }\r
143     catch (Exception ex)\r
144     {\r
145     }\r
146   }\r
147 \r
148   protected void applyButton_actionPerformed(ActionEvent e)\r
149   {\r
150     Color[] newColours = new Color[24];\r
151 \r
152     for (int i = 0; i < 24; i++)\r
153     {\r
154       JButton button = (JButton) buttonPanel.getComponent(i);\r
155       newColours[i] = button.getBackground();\r
156     }\r
157 \r
158     UserColourScheme ucs = new UserColourScheme(newColours);\r
159     ucs.setThreshold(0);\r
160 \r
161     if (seqGroup != null)\r
162     {\r
163       seqGroup.cs = ucs;\r
164       ap.repaint();\r
165     }\r
166     else\r
167     {\r
168       ap.alignFrame.changeColour(ucs);\r
169     }\r
170   }\r
171 \r
172   protected void loadbutton_actionPerformed(ActionEvent e)\r
173   {\r
174     JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.\r
175         getProperty(\r
176             "LAST_DIRECTORY"), new String[]\r
177         {"jc"},\r
178         new String[]\r
179         {"Jalview User Colours"}, "Jalview User Colours");\r
180     chooser.setFileView(new jalview.io.JalviewFileView());\r
181     chooser.setDialogTitle("Load colour scheme");\r
182     chooser.setToolTipText("Load");\r
183 \r
184     int value = chooser.showOpenDialog(this);\r
185 \r
186     if (value == JalviewFileChooser.APPROVE_OPTION)\r
187     {\r
188       File choice = chooser.getSelectedFile();\r
189       jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());\r
190       jalview.bin.Cache.setProperty("USER_DEFINED_COLOUR",\r
191                                     choice.getPath());\r
192 \r
193       Color[] colors = loadColours(choice.getAbsolutePath());\r
194 \r
195       for (int i = 0; i < colors.length; i++)\r
196       {\r
197         JButton button = (JButton) buttonPanel.getComponent(i);\r
198         button.setBackground(colors[i]);\r
199       }\r
200     }\r
201   }\r
202 \r
203   public static UserColourScheme loadDefaultColours()\r
204   {\r
205     if (jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR") != null)\r
206     {\r
207       return loadDefaultColours(jalview.bin.Cache.getProperty(\r
208           "USER_DEFINED_COLOUR"));\r
209     }\r
210     else\r
211     {\r
212       return null;\r
213     }\r
214   }\r
215 \r
216   public static UserColourScheme loadDefaultColours(String file)\r
217   {\r
218     UserColourScheme ucs = null;\r
219     Color[] cols = loadColours(file);\r
220 \r
221     if (cols != null)\r
222     {\r
223       ucs = new UserColourScheme(cols);\r
224       ucs.setThreshold(0);\r
225     }\r
226 \r
227     return ucs;\r
228   }\r
229 \r
230   static Color[] loadColours(String file)\r
231   {\r
232     Color[] newColours = null;\r
233 \r
234     try\r
235     {\r
236       InputStreamReader in = new InputStreamReader(new FileInputStream(\r
237           file), "UTF-8");\r
238 \r
239       jalview.binding.JalviewUserColours ucs = new jalview.binding.\r
240           JalviewUserColours();\r
241       ucs = (jalview.binding.JalviewUserColours) ucs.unmarshal(in);\r
242 \r
243       newColours = new Color[ucs.getColourCount()];\r
244 \r
245       for (int i = 0; i < 24; i++)\r
246       {\r
247         newColours[i] = new Color(Integer.parseInt(\r
248             ucs.getColour(i).getRGB(), 16));\r
249       }\r
250     }\r
251     catch (Exception ex)\r
252     {\r
253       System.out.println("Error loading UserColourFile " + file);\r
254     }\r
255 \r
256     return newColours;\r
257   }\r
258 \r
259   protected void savebutton_actionPerformed(ActionEvent e)\r
260   {\r
261     JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.\r
262         getProperty(\r
263             "LAST_DIRECTORY"), new String[]\r
264         {"jc"},\r
265         new String[]\r
266         {"Jalview User Colours"}, "Jalview User Colours");\r
267 \r
268     chooser.setFileView(new jalview.io.JalviewFileView());\r
269     chooser.setDialogTitle("Save colour scheme");\r
270     chooser.setToolTipText("Save");\r
271 \r
272     int value = chooser.showSaveDialog(this);\r
273 \r
274     if (value == JalviewFileChooser.APPROVE_OPTION)\r
275     {\r
276       String choice = chooser.getSelectedFile().getPath();\r
277       jalview.bin.Cache.setProperty("USER_DEFINED_COLOUR", choice);\r
278 \r
279       jalview.binding.JalviewUserColours ucs = new jalview.binding.\r
280           JalviewUserColours();\r
281 \r
282       try\r
283       {\r
284         PrintWriter out = new PrintWriter(new OutputStreamWriter(\r
285             new FileOutputStream(choice), "UTF-8"));\r
286 \r
287         for (int i = 0; i < 24; i++)\r
288         {\r
289           JButton button = (JButton) buttonPanel.getComponent(i);\r
290           jalview.binding.Colour col = new jalview.binding.Colour();\r
291           col.setName(button.getText());\r
292           col.setRGB(jalview.util.Format.getHexString(\r
293               button.getBackground()));\r
294           ucs.addColour(col);\r
295         }\r
296 \r
297         ucs.marshal(out);\r
298         out.close();\r
299       }\r
300       catch (Exception ex)\r
301       {\r
302         ex.printStackTrace();\r
303       }\r
304     }\r
305   }\r
306 \r
307   protected void cancelButton_actionPerformed(ActionEvent e)\r
308   {\r
309     Color[] newColours = new Color[24];\r
310 \r
311     for (int i = 0; i < 24; i++)\r
312     {\r
313       newColours[i] = (Color) oldColours.elementAt(i);\r
314       buttonPanel.getComponent(i).setBackground(newColours[i]);\r
315     }\r
316 \r
317     UserColourScheme ucs = new UserColourScheme(newColours);\r
318 \r
319     if (seqGroup != null)\r
320     {\r
321       seqGroup.cs = ucs;\r
322     }\r
323     else\r
324     {\r
325       ap.av.setGlobalColourScheme(ucs);\r
326     }\r
327 \r
328     ap.repaint();\r
329 \r
330     try\r
331     {\r
332       frame.setClosed(true);\r
333     }\r
334     catch (Exception ex)\r
335     {\r
336     }\r
337   }\r
338 }\r