IgnoreGapsConsensus
[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 jalview.datamodel.*;\r
22 \r
23 import jalview.io.*;\r
24 \r
25 import jalview.jbgui.*;\r
26 \r
27 import jalview.schemes.*;\r
28 \r
29 import java.awt.*;\r
30 import java.awt.event.*;\r
31 \r
32 import java.io.*;\r
33 \r
34 import java.util.*;\r
35 \r
36 import javax.swing.*;\r
37 import javax.swing.event.*;\r
38 \r
39 \r
40 /**\r
41  * DOCUMENT ME!\r
42  *\r
43  * @author $author$\r
44  * @version $Revision$\r
45  */\r
46 public class UserDefinedColours extends GUserDefinedColours\r
47     implements ChangeListener\r
48 {\r
49     AlignmentPanel ap;\r
50     SequenceGroup seqGroup;\r
51     Vector selectedButtons;\r
52     Vector oldColours = new Vector();\r
53     ColourSchemeI oldColourScheme;\r
54     JInternalFrame frame;\r
55 \r
56     /**\r
57      * Creates a new UserDefinedColours object.\r
58      *\r
59      * @param ap DOCUMENT ME!\r
60      * @param sg DOCUMENT ME!\r
61      */\r
62     public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)\r
63     {\r
64         super();\r
65         frame = new JInternalFrame();\r
66         frame.setContentPane(this);\r
67         Desktop.addInternalFrame(frame, "User Defined Colours", 450, 530, false);\r
68 \r
69         if (System.getProperty("os.name").startsWith("Mac"))\r
70         {\r
71             frame.setSize(450, 560);\r
72         }\r
73 \r
74         if (sg != null)\r
75         {\r
76             frame.setTitle(frame.getTitle() + " (" + sg.getName() + ")");\r
77         }\r
78 \r
79         colorChooser.getSelectionModel().addChangeListener(this);\r
80 \r
81         this.ap = ap;\r
82         seqGroup = sg;\r
83 \r
84         if (seqGroup != null)\r
85         {\r
86             oldColourScheme = seqGroup.cs;\r
87         }\r
88         else\r
89         {\r
90             oldColourScheme = ap.av.getGlobalColourScheme();\r
91         }\r
92 \r
93         for (int i = 0; i < 20; i++)\r
94         {\r
95             makeButton(ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i]) +\r
96                 "", ResidueProperties.aa[i]);\r
97         }\r
98 \r
99         makeButton("B", "B");\r
100         makeButton("Z", "Z");\r
101         makeButton("X", "X");\r
102         makeButton("Gap", "'.','-',' '");\r
103 \r
104         if (jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR") != null)\r
105         {\r
106             loadColours(jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR"));\r
107         }\r
108     }\r
109 \r
110     /**\r
111      * DOCUMENT ME!\r
112      *\r
113      * @param evt DOCUMENT ME!\r
114      */\r
115     public void stateChanged(ChangeEvent evt)\r
116     {\r
117         if (selectedButtons != null)\r
118         {\r
119           JButton button;\r
120           for(int i=0; i<selectedButtons.size(); i++)\r
121           {\r
122             button = (JButton)selectedButtons.elementAt(i);\r
123             button.setBackground(colorChooser.getColor());\r
124             button.setForeground( button.getBackground().brighter().brighter().brighter());\r
125           }\r
126         }\r
127     }\r
128 \r
129     /**\r
130      * DOCUMENT ME!\r
131      *\r
132      * @param e DOCUMENT ME!\r
133      */\r
134     public void colourButtonPressed(MouseEvent e)\r
135     {\r
136       if(selectedButtons == null)\r
137         selectedButtons = new Vector();\r
138 \r
139       JButton pressed = (JButton) e.getSource();\r
140 \r
141       if(e.isShiftDown())\r
142       {\r
143         JButton start = (JButton)selectedButtons.elementAt(selectedButtons.size()-1);\r
144         JButton end = (JButton) e.getSource();\r
145         int startIndex=0, endIndex=0;\r
146         for(int b=0; b<buttonPanel.getComponentCount(); b++)\r
147         {\r
148           if(buttonPanel.getComponent(b)==start)\r
149             startIndex = b;\r
150           if(buttonPanel.getComponent(b)==end)\r
151             endIndex = b;\r
152         }\r
153 \r
154         if(startIndex > endIndex)\r
155         {\r
156           int temp = startIndex;\r
157           startIndex = endIndex;\r
158           endIndex = temp;\r
159         }\r
160 \r
161         for(int b=startIndex; b<=endIndex; b++)\r
162         {\r
163           JButton button = (JButton)buttonPanel.getComponent(b);\r
164           if(!selectedButtons.contains(button))\r
165           {\r
166             button.setForeground(button.getBackground().brighter().brighter());\r
167             selectedButtons.add(button);\r
168           }\r
169         }\r
170       }\r
171       else if(!e.isControlDown())\r
172       {\r
173         for(int b=0; b<selectedButtons.size(); b++)\r
174         {\r
175           JButton button = (JButton)selectedButtons.elementAt(b);\r
176           button.setForeground(button.getBackground().darker().darker());\r
177         }\r
178         selectedButtons.clear();\r
179         pressed.setForeground( pressed.getBackground().brighter().brighter());\r
180         selectedButtons.addElement(pressed);\r
181 \r
182       }\r
183       else if(e.isControlDown())\r
184       {\r
185         if(selectedButtons.contains(pressed))\r
186         {\r
187           pressed.setForeground(pressed.getBackground().darker().darker());\r
188           selectedButtons.remove(pressed);\r
189         }\r
190         else\r
191         {\r
192           pressed.setForeground( pressed.getBackground().brighter().brighter());\r
193           selectedButtons.addElement(pressed);\r
194         }\r
195       }\r
196 \r
197       if(selectedButtons.size()>0)\r
198       colorChooser.setColor( ((JButton)selectedButtons.elementAt(0)).getBackground());\r
199     }\r
200 \r
201     /**\r
202      * DOCUMENT ME!\r
203      *\r
204      * @param label DOCUMENT ME!\r
205      * @param aa DOCUMENT ME!\r
206      */\r
207     void makeButton(String label, String aa)\r
208     {\r
209         final JButton button = new JButton();\r
210         Color col = Color.white;\r
211 \r
212         try\r
213         {\r
214             col = oldColourScheme.findColour(aa, -1);\r
215         }\r
216         catch (Exception ex)\r
217         {\r
218         }\r
219 \r
220         button.setBackground(col);\r
221         oldColours.addElement(col);\r
222         button.setText(label);\r
223         button.setForeground(col.darker().darker().darker());\r
224         button.setFont(new java.awt.Font("Verdana", 1, 10));\r
225         button.addMouseListener(new java.awt.event.MouseAdapter()\r
226             {\r
227                 public void mouseClicked(MouseEvent e)\r
228                 {\r
229                     colourButtonPressed(e);\r
230                 }\r
231             });\r
232 \r
233         buttonPanel.add(button, null);\r
234     }\r
235 \r
236     /**\r
237      * DOCUMENT ME!\r
238      *\r
239      * @param e DOCUMENT ME!\r
240      */\r
241     protected void okButton_actionPerformed(ActionEvent e)\r
242     {\r
243         applyButton_actionPerformed(null);\r
244 \r
245         try\r
246         {\r
247             frame.setClosed(true);\r
248         }\r
249         catch (Exception ex)\r
250         {\r
251         }\r
252     }\r
253 \r
254     /**\r
255      * DOCUMENT ME!\r
256      *\r
257      * @param e DOCUMENT ME!\r
258      */\r
259     protected void applyButton_actionPerformed(ActionEvent e)\r
260     {\r
261         Color[] newColours = new Color[24];\r
262 \r
263         for (int i = 0; i < 24; i++)\r
264         {\r
265             JButton button = (JButton) buttonPanel.getComponent(i);\r
266             newColours[i] = button.getBackground();\r
267         }\r
268 \r
269         UserColourScheme ucs = new UserColourScheme(newColours);\r
270         ucs.setThreshold(0, ap.av.getIgnoreGapsConsensus());\r
271 \r
272         if (seqGroup != null)\r
273         {\r
274             seqGroup.cs = ucs;\r
275             ap.repaint();\r
276         }\r
277         else\r
278         {\r
279             ap.alignFrame.changeColour(ucs);\r
280         }\r
281     }\r
282 \r
283     /**\r
284      * DOCUMENT ME!\r
285      *\r
286      * @param e DOCUMENT ME!\r
287      */\r
288     protected void loadbutton_actionPerformed(ActionEvent e)\r
289     {\r
290         JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(\r
291                     "LAST_DIRECTORY"), new String[] { "jc" },\r
292                 new String[] { "Jalview User Colours" }, "Jalview User Colours");\r
293         chooser.setFileView(new jalview.io.JalviewFileView());\r
294         chooser.setDialogTitle("Load colour scheme");\r
295         chooser.setToolTipText("Load");\r
296 \r
297         int value = chooser.showOpenDialog(this);\r
298 \r
299         if (value == JalviewFileChooser.APPROVE_OPTION)\r
300         {\r
301             File choice = chooser.getSelectedFile();\r
302             jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());\r
303             jalview.bin.Cache.setProperty("USER_DEFINED_COLOUR",\r
304                 choice.getPath());\r
305 \r
306             Color[] colors = loadColours(choice.getAbsolutePath());\r
307 \r
308             for (int i = 0; i < colors.length; i++)\r
309             {\r
310                 JButton button = (JButton) buttonPanel.getComponent(i);\r
311                 button.setBackground(colors[i]);\r
312             }\r
313         }\r
314     }\r
315 \r
316     /**\r
317      * DOCUMENT ME!\r
318      *\r
319      * @return DOCUMENT ME!\r
320      */\r
321     public static UserColourScheme loadDefaultColours()\r
322     {\r
323         if (jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR") != null)\r
324         {\r
325             return loadDefaultColours(jalview.bin.Cache.getProperty(\r
326                     "USER_DEFINED_COLOUR"));\r
327         }\r
328         else\r
329         {\r
330             return null;\r
331         }\r
332     }\r
333 \r
334     /**\r
335      * DOCUMENT ME!\r
336      *\r
337      * @param file DOCUMENT ME!\r
338      *\r
339      * @return DOCUMENT ME!\r
340      */\r
341     public static UserColourScheme loadDefaultColours(String file)\r
342     {\r
343         UserColourScheme ucs = null;\r
344         Color[] cols = loadColours(file);\r
345 \r
346         if (cols != null)\r
347         {\r
348             ucs = new UserColourScheme(cols);\r
349         }\r
350 \r
351         return ucs;\r
352     }\r
353 \r
354     static Color[] loadColours(String file)\r
355     {\r
356         Color[] newColours = null;\r
357 \r
358         try\r
359         {\r
360             InputStreamReader in = new InputStreamReader(new FileInputStream(\r
361                         file), "UTF-8");\r
362 \r
363             jalview.binding.JalviewUserColours ucs = new jalview.binding.JalviewUserColours();\r
364             ucs = (jalview.binding.JalviewUserColours) ucs.unmarshal(in);\r
365 \r
366             newColours = new Color[ucs.getColourCount()];\r
367 \r
368             for (int i = 0; i < 24; i++)\r
369             {\r
370                 newColours[i] = new Color(Integer.parseInt(\r
371                             ucs.getColour(i).getRGB(), 16));\r
372             }\r
373         }\r
374         catch (Exception ex)\r
375         {\r
376             System.out.println("Error loading UserColourFile " + file);\r
377         }\r
378 \r
379         return newColours;\r
380     }\r
381 \r
382     /**\r
383      * DOCUMENT ME!\r
384      *\r
385      * @param e DOCUMENT ME!\r
386      */\r
387     protected void savebutton_actionPerformed(ActionEvent e)\r
388     {\r
389         JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(\r
390                     "LAST_DIRECTORY"), new String[] { "jc" },\r
391                 new String[] { "Jalview User Colours" }, "Jalview User Colours");\r
392 \r
393         chooser.setFileView(new jalview.io.JalviewFileView());\r
394         chooser.setDialogTitle("Save colour scheme");\r
395         chooser.setToolTipText("Save");\r
396 \r
397         int value = chooser.showSaveDialog(this);\r
398 \r
399         if (value == JalviewFileChooser.APPROVE_OPTION)\r
400         {\r
401             String choice = chooser.getSelectedFile().getPath();\r
402             jalview.bin.Cache.setProperty("USER_DEFINED_COLOUR", choice);\r
403 \r
404             jalview.binding.JalviewUserColours ucs = new jalview.binding.JalviewUserColours();\r
405 \r
406             try\r
407             {\r
408                 PrintWriter out = new PrintWriter(new OutputStreamWriter(\r
409                             new FileOutputStream(choice), "UTF-8"));\r
410 \r
411                 for (int i = 0; i < 24; i++)\r
412                 {\r
413                     JButton button = (JButton) buttonPanel.getComponent(i);\r
414                     jalview.binding.Colour col = new jalview.binding.Colour();\r
415                     col.setName(button.getText());\r
416                     col.setRGB(jalview.util.Format.getHexString(\r
417                             button.getBackground()));\r
418                     ucs.addColour(col);\r
419                 }\r
420 \r
421                 ucs.marshal(out);\r
422                 out.close();\r
423             }\r
424             catch (Exception ex)\r
425             {\r
426                 ex.printStackTrace();\r
427             }\r
428         }\r
429     }\r
430 \r
431     /**\r
432      * DOCUMENT ME!\r
433      *\r
434      * @param e DOCUMENT ME!\r
435      */\r
436     protected void cancelButton_actionPerformed(ActionEvent e)\r
437     {\r
438         Color[] newColours = new Color[24];\r
439 \r
440         for (int i = 0; i < 24; i++)\r
441         {\r
442             newColours[i] = (Color) oldColours.elementAt(i);\r
443             buttonPanel.getComponent(i).setBackground(newColours[i]);\r
444         }\r
445 \r
446         UserColourScheme ucs = new UserColourScheme(newColours);\r
447 \r
448         if (seqGroup != null)\r
449         {\r
450             seqGroup.cs = ucs;\r
451         }\r
452         else\r
453         {\r
454             ap.av.setGlobalColourScheme(ucs);\r
455         }\r
456 \r
457         ap.repaint();\r
458 \r
459         try\r
460         {\r
461             frame.setClosed(true);\r
462         }\r
463         catch (Exception ex)\r
464         {\r
465         }\r
466     }\r
467 }\r