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