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