Check hiddenSequence before placing in tmp
[jalview.git] / src / jalview / gui / UserDefinedColours.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2006 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 /**\r
42  * DOCUMENT ME!\r
43  *\r
44  * @author $author$\r
45  * @version $Revision$\r
46  */\r
47 public class UserDefinedColours extends GUserDefinedColours\r
48     implements ChangeListener\r
49 {\r
50     AlignmentPanel ap;\r
51     SequenceGroup seqGroup;\r
52     Vector selectedButtons;\r
53     ColourSchemeI oldColourScheme;\r
54     JInternalFrame frame;\r
55     MCview.PDBCanvas pdbcanvas;\r
56     Vector upperCaseButtons;\r
57     Vector lowerCaseButtons;\r
58 \r
59 \r
60     /**\r
61      * Creates a new UserDefinedColours object.\r
62      *\r
63      * @param ap DOCUMENT ME!\r
64      * @param sg DOCUMENT ME!\r
65      */\r
66     public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)\r
67     {\r
68         super();\r
69 \r
70         //AW - LOWER CASE DISABLED IN 2.1.01 bug fix release\r
71         casePanel.setVisible(false);\r
72 \r
73 \r
74         lcaseColour.setEnabled(false);\r
75 \r
76         this.ap = ap;\r
77         seqGroup = sg;\r
78 \r
79         if (seqGroup != null)\r
80             oldColourScheme = seqGroup.cs;\r
81         else\r
82             oldColourScheme = ap.av.getGlobalColourScheme();\r
83 \r
84         if (oldColourScheme instanceof UserColourScheme)\r
85         {\r
86           schemeName.setText( ( (UserColourScheme) oldColourScheme).getName());\r
87           if(( (UserColourScheme) oldColourScheme).getLowerCaseColours()!=null)\r
88           {\r
89             caseSensitive.setSelected(true);\r
90             lcaseColour.setEnabled(true);\r
91             resetButtonPanel(true);\r
92           }\r
93           else\r
94             resetButtonPanel(false);\r
95         }\r
96         else\r
97            resetButtonPanel(false);\r
98 \r
99         showFrame();\r
100     }\r
101 \r
102     public UserDefinedColours(MCview.PDBCanvas pdb, ColourSchemeI oldcs)\r
103     {\r
104         super();\r
105         pdbcanvas = pdb;\r
106 \r
107         colorChooser.getSelectionModel().addChangeListener(this);\r
108 \r
109         oldColourScheme = oldcs;\r
110 \r
111         if (oldColourScheme instanceof UserColourScheme)\r
112         {\r
113               schemeName.setText( ( (UserColourScheme) oldColourScheme).getName());\r
114         }\r
115 \r
116         resetButtonPanel(false);\r
117 \r
118         showFrame();\r
119 \r
120     }\r
121 \r
122     void showFrame()\r
123     {\r
124       colorChooser.getSelectionModel().addChangeListener(this);\r
125       frame = new JInternalFrame();\r
126       frame.setContentPane(this);\r
127       Desktop.addInternalFrame(frame, "User Defined Colours", 720, 370, true);\r
128 \r
129       if (seqGroup != null)\r
130             frame.setTitle(frame.getTitle() + " (" + seqGroup.getName() + ")");\r
131 \r
132       if (System.getProperty("os.name").startsWith("Mac"))\r
133       {\r
134         frame.setSize(760, 370);\r
135       }\r
136     }\r
137 \r
138     void resetButtonPanel(boolean caseSensitive)\r
139     {\r
140       //AW - LOWER CASE DISABLED IN 2.1.01 bug fix release\r
141       caseSensitive = false;\r
142 \r
143       buttonPanel.removeAll();\r
144 \r
145 \r
146       if(upperCaseButtons==null)\r
147         upperCaseButtons = new Vector();\r
148 \r
149       JButton button;\r
150       String label;\r
151       for (int i = 0; i < 20; i++)\r
152       {\r
153         if(caseSensitive)\r
154           label = ResidueProperties.aa[i];\r
155         else\r
156           label = ResidueProperties.aa2Triplet.get\r
157               (ResidueProperties.aa[i]).toString();\r
158 \r
159         button = makeButton(label,\r
160                               ResidueProperties.aa[i],\r
161                               upperCaseButtons, i);\r
162 \r
163         buttonPanel.add(button);\r
164       }\r
165 \r
166         buttonPanel.add(makeButton("B", "B", upperCaseButtons, 20));\r
167         buttonPanel.add(makeButton("Z", "Z", upperCaseButtons, 21));\r
168         buttonPanel.add(makeButton("X", "X", upperCaseButtons, 22));\r
169         buttonPanel.add(makeButton("Gap", "-", upperCaseButtons, 23));\r
170 \r
171       if(!caseSensitive)\r
172       {\r
173         gridLayout.setRows(6);\r
174         gridLayout.setColumns(4);\r
175       }\r
176       else\r
177       {\r
178         gridLayout.setRows(7);\r
179         int cols = 7;\r
180         gridLayout.setColumns(cols+1);\r
181 \r
182         if(lowerCaseButtons==null)\r
183           lowerCaseButtons = new Vector();\r
184 \r
185 \r
186           for (int i = 0; i < 20; i++)\r
187           {\r
188             int row = i / cols + 1;\r
189             int index = (row * cols) + i;\r
190             button = makeButton(\r
191                 ResidueProperties.aa[i].toLowerCase(),\r
192                 ResidueProperties.aa[i].toLowerCase(),\r
193                 lowerCaseButtons,\r
194                 i);\r
195 \r
196             buttonPanel.add(button, index);\r
197           }\r
198       }\r
199 \r
200 \r
201       if(caseSensitive)\r
202       {\r
203         buttonPanel.add(makeButton("b", "b", lowerCaseButtons, 20));\r
204         buttonPanel.add(makeButton("z", "z", lowerCaseButtons, 21));\r
205         buttonPanel.add(makeButton("x", "x", lowerCaseButtons, 22));\r
206       }\r
207 \r
208       buttonPanel.validate();\r
209       validate();\r
210     }\r
211 \r
212 \r
213     /**\r
214      * DOCUMENT ME!\r
215      *\r
216      * @param evt DOCUMENT ME!\r
217      */\r
218     public void stateChanged(ChangeEvent evt)\r
219     {\r
220         if (selectedButtons != null)\r
221         {\r
222           JButton button=null;\r
223           for(int i=0; i<selectedButtons.size(); i++)\r
224           {\r
225             button = (JButton)selectedButtons.elementAt(i);\r
226             button.setBackground(colorChooser.getColor());\r
227             button.setForeground( button.getBackground().brighter().brighter().brighter());\r
228           }\r
229           if(button==lcaseColour)\r
230           {\r
231             for(int i=0; i<lowerCaseButtons.size(); i++)\r
232             {\r
233               button = (JButton)lowerCaseButtons.elementAt(i);\r
234               button.setBackground(colorChooser.getColor());\r
235               button.setForeground( button.getBackground().brighter().brighter().brighter());\r
236             }\r
237           }\r
238         }\r
239     }\r
240 \r
241     /**\r
242      * DOCUMENT ME!\r
243      *\r
244      * @param e DOCUMENT ME!\r
245      */\r
246     public void colourButtonPressed(MouseEvent e)\r
247     {\r
248       if(selectedButtons == null)\r
249         selectedButtons = new Vector();\r
250 \r
251       JButton pressed = (JButton) e.getSource();\r
252 \r
253       if(e.isShiftDown())\r
254       {\r
255         JButton start, end = (JButton) e.getSource();\r
256         if(selectedButtons.size()>0)\r
257           start = (JButton)selectedButtons.elementAt(selectedButtons.size()-1);\r
258         else\r
259           start = (JButton) e.getSource();\r
260 \r
261         int startIndex=0, endIndex=0;\r
262         for(int b=0; b<buttonPanel.getComponentCount(); b++)\r
263         {\r
264           if(buttonPanel.getComponent(b)==start)\r
265             startIndex = b;\r
266           if(buttonPanel.getComponent(b)==end)\r
267             endIndex = b;\r
268         }\r
269 \r
270         if(startIndex > endIndex)\r
271         {\r
272           int temp = startIndex;\r
273           startIndex = endIndex;\r
274           endIndex = temp;\r
275         }\r
276 \r
277         for(int b=startIndex; b<=endIndex; b++)\r
278         {\r
279           JButton button = (JButton)buttonPanel.getComponent(b);\r
280           if(!selectedButtons.contains(button))\r
281           {\r
282             button.setForeground(button.getBackground().brighter().brighter());\r
283             selectedButtons.add(button);\r
284           }\r
285         }\r
286       }\r
287       else if(!e.isControlDown())\r
288       {\r
289         for(int b=0; b<selectedButtons.size(); b++)\r
290         {\r
291           JButton button = (JButton)selectedButtons.elementAt(b);\r
292           button.setForeground(button.getBackground().darker().darker());\r
293         }\r
294         selectedButtons.clear();\r
295         pressed.setForeground( pressed.getBackground().brighter().brighter());\r
296         selectedButtons.addElement(pressed);\r
297 \r
298       }\r
299       else if(e.isControlDown())\r
300       {\r
301         if(selectedButtons.contains(pressed))\r
302         {\r
303           pressed.setForeground(pressed.getBackground().darker().darker());\r
304           selectedButtons.remove(pressed);\r
305         }\r
306         else\r
307         {\r
308           pressed.setForeground( pressed.getBackground().brighter().brighter());\r
309           selectedButtons.addElement(pressed);\r
310         }\r
311       }\r
312 \r
313       if(selectedButtons.size()>0)\r
314       colorChooser.setColor( ((JButton)selectedButtons.elementAt(0)).getBackground());\r
315     }\r
316 \r
317     /**\r
318      * DOCUMENT ME!\r
319      *\r
320      * @param label DOCUMENT ME!\r
321      * @param aa DOCUMENT ME!\r
322      */\r
323     JButton makeButton(String label,\r
324                        String aa,\r
325                        Vector caseSensitiveButtons,\r
326                        int buttonIndex)\r
327     {\r
328         final JButton button;\r
329         Color col;\r
330 \r
331         if(buttonIndex<caseSensitiveButtons.size())\r
332         {\r
333           button = (JButton) caseSensitiveButtons.elementAt(buttonIndex);\r
334           col = button.getBackground();\r
335         }\r
336         else\r
337         {\r
338           button = new JButton();\r
339           button.addMouseListener(new java.awt.event.MouseAdapter()\r
340           {\r
341             public void mouseClicked(MouseEvent e)\r
342             {\r
343               colourButtonPressed(e);\r
344             }\r
345           });\r
346 \r
347           caseSensitiveButtons.addElement(button);\r
348 \r
349           col = Color.white;\r
350 \r
351           try\r
352           {\r
353             col = oldColourScheme.findColour(aa, -1);\r
354           }\r
355           catch (Exception ex)\r
356           {    }\r
357         }\r
358 \r
359       /* //AW - LOWER CASE DISABLED IN 2.1.01 bug fix release\r
360         if(caseSensitive.isSelected())\r
361           button.setMargin(new java.awt.Insets(2,2,2,2));\r
362         else\r
363          button.setMargin(new java.awt.Insets(2,14,2,14));\r
364       */\r
365 \r
366         button.setBackground(col);\r
367         button.setText(label);\r
368         button.setForeground(col.darker().darker().darker());\r
369         button.setFont(new java.awt.Font("Verdana", Font.BOLD, 10));\r
370 \r
371         return button;\r
372     }\r
373 \r
374     /**\r
375      * DOCUMENT ME!\r
376      *\r
377      * @param e DOCUMENT ME!\r
378      */\r
379     protected void okButton_actionPerformed(ActionEvent e)\r
380     {\r
381         applyButton_actionPerformed(null);\r
382 \r
383         try\r
384         {\r
385             frame.setClosed(true);\r
386         }\r
387         catch (Exception ex)\r
388         {\r
389         }\r
390     }\r
391 \r
392     /**\r
393      * DOCUMENT ME!\r
394      *\r
395      * @param e DOCUMENT ME!\r
396      */\r
397     protected void applyButton_actionPerformed(ActionEvent e)\r
398     {\r
399         UserColourScheme ucs = getSchemeFromButtons();\r
400         ucs.setName(schemeName.getText());\r
401 \r
402         if (seqGroup != null)\r
403         {\r
404             seqGroup.cs = ucs;\r
405             ap.repaint();\r
406         }\r
407         else if(ap!=null)\r
408         {\r
409             ap.alignFrame.changeColour(ucs);\r
410         }\r
411         else if(pdbcanvas!=null)\r
412         {\r
413           pdbcanvas.pdb.setColours(ucs);\r
414           pdbcanvas.updateSeqColours();\r
415         }\r
416     }\r
417 \r
418     UserColourScheme getSchemeFromButtons()\r
419     {\r
420 \r
421       Color[] newColours = new Color[24];\r
422 \r
423       for (int i = 0; i < 24; i++)\r
424       {\r
425         JButton button = (JButton) upperCaseButtons.elementAt(i);\r
426         newColours[i] = button.getBackground();\r
427       }\r
428 \r
429       UserColourScheme ucs = new UserColourScheme(newColours);\r
430 \r
431 \r
432       if(caseSensitive.isSelected())\r
433       {\r
434         newColours = new Color[23];\r
435         for (int i = 0; i < 23; i++)\r
436         {\r
437           JButton button = (JButton) lowerCaseButtons.elementAt(i);\r
438           newColours[i] = button.getBackground();\r
439         }\r
440         ucs.setLowerCaseColours(newColours);\r
441       }\r
442 \r
443       if(ap!=null)\r
444         ucs.setThreshold(0, ap.av.getIgnoreGapsConsensus());\r
445 \r
446 \r
447       return ucs;\r
448     }\r
449 \r
450 \r
451 \r
452     /**\r
453      * DOCUMENT ME!\r
454      *\r
455      * @param e DOCUMENT ME!\r
456      */\r
457     protected void loadbutton_actionPerformed(ActionEvent e)\r
458     {\r
459       upperCaseButtons = new Vector();\r
460       lowerCaseButtons = new Vector();\r
461 \r
462         JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(\r
463                     "LAST_DIRECTORY"), new String[] { "jc" },\r
464                 new String[] { "Jalview User Colours" }, "Jalview User Colours");\r
465         chooser.setFileView(new jalview.io.JalviewFileView());\r
466         chooser.setDialogTitle("Load colour scheme");\r
467         chooser.setToolTipText("Load");\r
468 \r
469         int value = chooser.showOpenDialog(this);\r
470 \r
471         if (value == JalviewFileChooser.APPROVE_OPTION)\r
472         {\r
473             File choice = chooser.getSelectedFile();\r
474             jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());\r
475             String defaultColours = jalview.bin.Cache.getDefault("USER_DEFINED_COLOURS",\r
476                 choice.getPath());\r
477             if (defaultColours.indexOf(choice.getPath()) == -1)\r
478               defaultColours = defaultColours.concat("|").concat(choice.getPath());\r
479 \r
480             jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);\r
481 \r
482 \r
483             UserColourScheme ucs = loadColours(choice.getAbsolutePath());\r
484             Color[] colors = ucs.getColours();\r
485             schemeName.setText(ucs.getName());\r
486 \r
487             if(ucs.getLowerCaseColours()!=null)\r
488             {\r
489               caseSensitive.setSelected(true);\r
490               lcaseColour.setEnabled(true);\r
491               resetButtonPanel(true);\r
492               for (int i = 0; i < lowerCaseButtons.size(); i++)\r
493               {\r
494                 JButton button = (JButton) lowerCaseButtons.elementAt(i);\r
495                 button.setBackground(ucs.getLowerCaseColours()[i]);\r
496               }\r
497 \r
498             }\r
499             else\r
500             {\r
501               caseSensitive.setSelected(false);\r
502               lcaseColour.setEnabled(false);\r
503               resetButtonPanel(false);\r
504             }\r
505 \r
506             for (int i = 0; i < upperCaseButtons.size(); i++)\r
507             {\r
508                 JButton button = (JButton) upperCaseButtons.elementAt(i);\r
509                 button.setBackground(colors[i]);\r
510             }\r
511 \r
512         }\r
513     }\r
514 \r
515     /**\r
516      * DOCUMENT ME!\r
517      *\r
518      * @return DOCUMENT ME!\r
519      */\r
520     public static UserColourScheme loadDefaultColours()\r
521     {\r
522         UserColourScheme ret = null;\r
523 \r
524         String colours = jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS");\r
525         if ( colours != null )\r
526         {\r
527           if(colours.indexOf("|")>-1)\r
528             colours = colours.substring(0, colours.indexOf("|"));\r
529 \r
530           ret = loadColours(colours);\r
531         }\r
532 \r
533         if(ret == null)\r
534         {\r
535           Color[] newColours = new Color[24];\r
536           for (int i = 0; i < 24; i++)\r
537           {\r
538             newColours[i] = Color.white;\r
539           }\r
540           ret =  new UserColourScheme(newColours);\r
541         }\r
542 \r
543         return ret;\r
544     }\r
545 \r
546     /**\r
547      * DOCUMENT ME!\r
548      *\r
549      * @param file DOCUMENT ME!\r
550      *\r
551      * @return DOCUMENT ME!\r
552      */\r
553     static UserColourScheme loadColours(String file)\r
554     {\r
555          UserColourScheme ucs = null;\r
556          Color[]  newColours = null;\r
557         try\r
558         {\r
559             InputStreamReader in = new InputStreamReader(new FileInputStream(\r
560                         file), "UTF-8");\r
561 \r
562               jalview.schemabinding.version2.JalviewUserColours jucs\r
563                   = new jalview.schemabinding.version2.JalviewUserColours();\r
564 \r
565               org.exolab.castor.xml.Unmarshaller unmar\r
566                   = new org.exolab.castor.xml.Unmarshaller(jucs);\r
567               jucs = (jalview.schemabinding.version2.JalviewUserColours) unmar.unmarshal( in );\r
568 \r
569               newColours = new Color[24];\r
570 \r
571               Color [] lowerCase = null;\r
572               boolean caseSensitive = false;\r
573 \r
574 \r
575             String name;\r
576             int index;\r
577             for (int i = 0; i < jucs.getColourCount(); i++)\r
578             {\r
579               name =  jucs.getColour(i).getName();\r
580               if(ResidueProperties.aa3Hash.containsKey(name))\r
581               {\r
582                 index = ((Integer)ResidueProperties.aa3Hash.get(name)).intValue();\r
583               }\r
584               else\r
585               {\r
586                 index = ResidueProperties.aaIndex[name.charAt(0)];\r
587               }\r
588               if(index == -1)\r
589                 continue;\r
590 \r
591               if(name.toLowerCase().equals(name))\r
592               {\r
593                 if(lowerCase==null)\r
594                    lowerCase = new Color[23];\r
595                 caseSensitive = true;\r
596                 lowerCase[index] = new Color(Integer.parseInt(\r
597                   jucs.getColour(i).getRGB(), 16));\r
598               }\r
599               else\r
600               {\r
601                 newColours[index] = new Color(Integer.parseInt(\r
602                     jucs.getColour(i).getRGB(), 16));\r
603               }\r
604             }\r
605 \r
606             if (newColours != null)\r
607             {\r
608               ucs = new UserColourScheme(newColours);\r
609               ucs.setName( jucs.getSchemeName() );\r
610               if(caseSensitive)\r
611               {\r
612                 ucs.setLowerCaseColours(lowerCase);\r
613               }\r
614             }\r
615 \r
616         }\r
617         catch (Exception ex)\r
618         {\r
619             //Could be Archive Jalview format\r
620             try{\r
621               InputStreamReader in = new InputStreamReader(new FileInputStream(\r
622                   file), "UTF-8");\r
623 \r
624               jalview.binding.JalviewUserColours jucs\r
625                   = new jalview.binding.JalviewUserColours();\r
626 \r
627               jucs = (jalview.binding.JalviewUserColours) jucs.unmarshal(in);\r
628 \r
629               newColours = new Color[jucs.getColourCount()];\r
630 \r
631               for (int i = 0; i < 24; i++)\r
632               {\r
633                 newColours[i] = new Color(Integer.parseInt(\r
634                     jucs.getColour(i).getRGB(), 16));\r
635               }\r
636               if (newColours != null)\r
637               {\r
638                 ucs = new UserColourScheme(newColours);\r
639                 ucs.setName(jucs.getSchemeName());\r
640               }\r
641             }catch(Exception ex2)\r
642             { ex2.printStackTrace(); }\r
643 \r
644             if(newColours==null)\r
645               System.out.println("Error loading User ColourFile\n"+ex);\r
646         }\r
647 \r
648        return ucs;\r
649     }\r
650 \r
651     /**\r
652      * DOCUMENT ME!\r
653      *\r
654      * @param e DOCUMENT ME!\r
655      */\r
656     protected void savebutton_actionPerformed(ActionEvent e)\r
657     {\r
658       if(schemeName.getText().trim().length()<1)\r
659       {\r
660         JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
661                                               "User colour scheme must have a name!",\r
662                                               "No name for colour scheme",\r
663                                               JOptionPane.WARNING_MESSAGE);\r
664         return;\r
665       }\r
666 \r
667       if(userColourSchemes!=null && userColourSchemes.containsKey(schemeName.getText()) )\r
668       {\r
669         int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop, "Colour scheme "+schemeName.getText()+ " exists."\r
670             +"\nContinue saving colour scheme as "+schemeName.getText()+"?",\r
671            "Duplicate scheme name", JOptionPane.YES_NO_OPTION);\r
672         if(reply != JOptionPane.YES_OPTION)\r
673           return;\r
674 \r
675          userColourSchemes.remove(schemeName.getText());\r
676       }\r
677         JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(\r
678                     "LAST_DIRECTORY"), new String[] { "jc" },\r
679                 new String[] { "Jalview User Colours" }, "Jalview User Colours");\r
680 \r
681         chooser.setFileView(new jalview.io.JalviewFileView());\r
682         chooser.setDialogTitle("Save colour scheme");\r
683         chooser.setToolTipText("Save");\r
684 \r
685         int value = chooser.showSaveDialog(this);\r
686 \r
687         if (value == JalviewFileChooser.APPROVE_OPTION)\r
688         {\r
689             String choice = chooser.getSelectedFile().getPath();\r
690             String defaultColours = jalview.bin.Cache.getDefault("USER_DEFINED_COLOURS", choice);\r
691             if(defaultColours.indexOf(choice)==-1)\r
692             {\r
693               if(defaultColours.length()>0)\r
694                 defaultColours = defaultColours.concat("|");\r
695               defaultColours = defaultColours.concat(choice);\r
696             }\r
697 \r
698             userColourSchemes.put(schemeName.getText(), getSchemeFromButtons());\r
699 \r
700             ap.alignFrame.updateUserColourMenu();\r
701 \r
702             jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);\r
703 \r
704             jalview.schemabinding.version2.JalviewUserColours ucs\r
705                 = new jalview.schemabinding.version2.JalviewUserColours();\r
706 \r
707             ucs.setSchemeName(schemeName.getText());\r
708             try\r
709             {\r
710                 PrintWriter out = new PrintWriter(new OutputStreamWriter(\r
711                             new FileOutputStream(choice), "UTF-8"));\r
712 \r
713                 for (int i = 0; i < buttonPanel.getComponentCount(); i++)\r
714                 {\r
715                     JButton button = (JButton) buttonPanel.getComponent(i);\r
716                     jalview.schemabinding.version2.Colour col\r
717                         = new jalview.schemabinding.version2.Colour();\r
718                     col.setName(button.getText());\r
719                     col.setRGB(jalview.util.Format.getHexString(\r
720                             button.getBackground()));\r
721                     ucs.addColour(col);\r
722                 }\r
723 \r
724                 ucs.marshal(out);\r
725                 out.close();\r
726             }\r
727             catch (Exception ex)\r
728             {\r
729                 ex.printStackTrace();\r
730             }\r
731         }\r
732     }\r
733 \r
734     /**\r
735      * DOCUMENT ME!\r
736      *\r
737      * @param e DOCUMENT ME!\r
738      */\r
739     protected void cancelButton_actionPerformed(ActionEvent e)\r
740     {\r
741       if (ap != null)\r
742       {\r
743         if (seqGroup != null)\r
744         {\r
745           seqGroup.cs = oldColourScheme;\r
746         }\r
747         else if (ap != null)\r
748         {\r
749           ap.av.setGlobalColourScheme(oldColourScheme);\r
750         }\r
751         ap.repaint();\r
752       }\r
753 \r
754       if(pdbcanvas!=null)\r
755       {\r
756         pdbcanvas.pdb.setColours(oldColourScheme);\r
757       }\r
758 \r
759         try\r
760         {\r
761             frame.setClosed(true);\r
762         }\r
763         catch (Exception ex)\r
764         {\r
765         }\r
766     }\r
767 \r
768 \r
769     static Hashtable userColourSchemes;\r
770 \r
771     public static Hashtable getUserColourSchemes()\r
772     {\r
773       return userColourSchemes;\r
774     }\r
775 \r
776     public static void initUserColourSchemes(String files)\r
777     {\r
778       userColourSchemes = new Hashtable();\r
779 \r
780       if(files==null || files.length()==0)\r
781         return;\r
782 \r
783 \r
784       // In case colours can't be loaded, we'll remove them\r
785       // from the default list here.\r
786       StringBuffer coloursFound = new StringBuffer();\r
787       StringTokenizer st = new StringTokenizer(files, "|");\r
788       while (st.hasMoreElements())\r
789       {\r
790         String file = st.nextToken();\r
791         try\r
792         {\r
793           UserColourScheme ucs = loadColours(file);\r
794           if (ucs != null)\r
795           {\r
796             if (coloursFound.length() > 0)\r
797               coloursFound.append("|");\r
798             coloursFound.append(file);\r
799             userColourSchemes.put(ucs.getName(), ucs);\r
800           }\r
801         }\r
802         catch (Exception ex)\r
803         {\r
804           System.out.println("Error loading User ColourFile\n" + ex);\r
805         }\r
806       }\r
807       if (!files.equals(coloursFound.toString()))\r
808       {\r
809         if (coloursFound.toString().length() > 1)\r
810           jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS",\r
811                                         coloursFound.toString());\r
812         else\r
813           jalview.bin.Cache.applicationProperties.remove("USER_DEFINED_COLOURS");\r
814       }\r
815     }\r
816 \r
817     public static void removeColourFromDefaults(String target)\r
818     {\r
819       // The only way to find colours by name is to load them in\r
820       // In case colours can't be loaded, we'll remove them\r
821       // from the default list here.\r
822 \r
823       userColourSchemes = new Hashtable();\r
824 \r
825       StringBuffer coloursFound = new StringBuffer();\r
826       StringTokenizer st = new StringTokenizer(\r
827            jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS"), "|");\r
828 \r
829       while (st.hasMoreElements())\r
830       {\r
831         String file = st.nextToken();\r
832         try\r
833         {\r
834           UserColourScheme ucs = loadColours(file);\r
835           if (ucs != null && !ucs.getName().equals(target))\r
836           {\r
837             if (coloursFound.length() > 0)\r
838               coloursFound.append("|");\r
839             coloursFound.append(file);\r
840             userColourSchemes.put(ucs.getName(), ucs);\r
841           }\r
842         }\r
843         catch (Exception ex)\r
844         {\r
845           System.out.println("Error loading User ColourFile\n" + ex);\r
846         }\r
847       }\r
848 \r
849       if (coloursFound.toString().length() > 1)\r
850         jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", coloursFound.toString());\r
851       else\r
852         jalview.bin.Cache.applicationProperties.remove("USER_DEFINED_COLOURS");\r
853 \r
854     }\r
855 \r
856     public void caseSensitive_actionPerformed(ActionEvent e)\r
857     {\r
858       resetButtonPanel(caseSensitive.isSelected());\r
859       lcaseColour.setEnabled(caseSensitive.isSelected());\r
860     }\r
861 \r
862     public void lcaseColour_actionPerformed(ActionEvent e)\r
863     {\r
864       if(selectedButtons==null)\r
865         selectedButtons = new Vector();\r
866       else\r
867         selectedButtons.clear();\r
868       selectedButtons.add(lcaseColour);\r
869     }\r
870 \r
871 }\r