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