JAL-1432 updated copyright notices
[jalview.git] / src / jalview / gui / UserDefinedColours.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3  * Copyright (C) 2014 The Jalview Authors
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  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.gui;
20
21 import jalview.datamodel.SequenceGroup;
22 import jalview.io.JalviewFileChooser;
23 import jalview.jbgui.GUserDefinedColours;
24 import jalview.schemes.ColourSchemeI;
25 import jalview.schemes.ResidueProperties;
26 import jalview.schemes.UserColourScheme;
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               "User colour scheme must have a name!",
715               "No name for 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               "Colour scheme " + schemeName.getText() + " exists."
725                       + "\nContinue saving colour scheme as "
726                       + schemeName.getText() + "?",
727               "Duplicate scheme name", JOptionPane.YES_NO_OPTION);
728       if (reply != JOptionPane.YES_OPTION)
729       {
730         return;
731       }
732
733       userColourSchemes.remove(schemeName.getText());
734     }
735     JalviewFileChooser chooser = new JalviewFileChooser(
736             jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
737             { "jc" }, new String[]
738             { "Jalview User Colours" }, "Jalview User Colours");
739
740     chooser.setFileView(new jalview.io.JalviewFileView());
741     chooser.setDialogTitle("Save colour scheme");
742     chooser.setToolTipText("Save");
743
744     int value = chooser.showSaveDialog(this);
745
746     if (value == JalviewFileChooser.APPROVE_OPTION)
747     {
748       String choice = chooser.getSelectedFile().getPath();
749       String defaultColours = jalview.bin.Cache.getDefault(
750               "USER_DEFINED_COLOURS", choice);
751       if (defaultColours.indexOf(choice) == -1)
752       {
753         if (defaultColours.length() > 0)
754         {
755           defaultColours = defaultColours.concat("|");
756         }
757         defaultColours = defaultColours.concat(choice);
758       }
759
760       userColourSchemes.put(schemeName.getText(), getSchemeFromButtons());
761
762       ap.alignFrame.updateUserColourMenu();
763
764       jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
765
766       jalview.schemabinding.version2.JalviewUserColours ucs = new jalview.schemabinding.version2.JalviewUserColours();
767
768       ucs.setSchemeName(schemeName.getText());
769       try
770       {
771         PrintWriter out = new PrintWriter(new OutputStreamWriter(
772                 new FileOutputStream(choice), "UTF-8"));
773
774         for (int i = 0; i < buttonPanel.getComponentCount(); i++)
775         {
776           JButton button = (JButton) buttonPanel.getComponent(i);
777           jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
778           col.setName(button.getText());
779           col.setRGB(jalview.util.Format.getHexString(button
780                   .getBackground()));
781           ucs.addColour(col);
782         }
783
784         ucs.marshal(out);
785         out.close();
786       } catch (Exception ex)
787       {
788         ex.printStackTrace();
789       }
790     }
791   }
792
793   /**
794    * DOCUMENT ME!
795    * 
796    * @param e
797    *          DOCUMENT ME!
798    */
799   protected void cancelButton_actionPerformed(ActionEvent e)
800   {
801     if (ap != null)
802     {
803       if (seqGroup != null)
804       {
805         seqGroup.cs = oldColourScheme;
806       }
807       else if (ap != null)
808       {
809         ap.av.setGlobalColourScheme(oldColourScheme);
810       }
811       ap.paintAlignment(true);
812     }
813
814     if (jmol != null)
815     {
816       jmol.setJalviewColourScheme(oldColourScheme);
817     }
818
819     try
820     {
821       frame.setClosed(true);
822     } catch (Exception ex)
823     {
824     }
825   }
826
827   static Hashtable userColourSchemes;
828
829   public static Hashtable getUserColourSchemes()
830   {
831     return userColourSchemes;
832   }
833
834   public static void initUserColourSchemes(String files)
835   {
836     userColourSchemes = new Hashtable();
837
838     if (files == null || files.length() == 0)
839     {
840       return;
841     }
842
843     // In case colours can't be loaded, we'll remove them
844     // from the default list here.
845     StringBuffer coloursFound = new StringBuffer();
846     StringTokenizer st = new StringTokenizer(files, "|");
847     while (st.hasMoreElements())
848     {
849       String file = st.nextToken();
850       try
851       {
852         UserColourScheme ucs = loadColours(file);
853         if (ucs != null)
854         {
855           if (coloursFound.length() > 0)
856           {
857             coloursFound.append("|");
858           }
859           coloursFound.append(file);
860           userColourSchemes.put(ucs.getName(), ucs);
861         }
862       } catch (Exception ex)
863       {
864         System.out.println("Error loading User ColourFile\n" + ex);
865       }
866     }
867     if (!files.equals(coloursFound.toString()))
868     {
869       if (coloursFound.toString().length() > 1)
870       {
871         jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS",
872                 coloursFound.toString());
873       }
874       else
875       {
876         jalview.bin.Cache.applicationProperties
877                 .remove("USER_DEFINED_COLOURS");
878       }
879     }
880   }
881
882   public static void removeColourFromDefaults(String target)
883   {
884     // The only way to find colours by name is to load them in
885     // In case colours can't be loaded, we'll remove them
886     // from the default list here.
887
888     userColourSchemes = new Hashtable();
889
890     StringBuffer coloursFound = new StringBuffer();
891     StringTokenizer st = new StringTokenizer(
892             jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS"), "|");
893
894     while (st.hasMoreElements())
895     {
896       String file = st.nextToken();
897       try
898       {
899         UserColourScheme ucs = loadColours(file);
900         if (ucs != null && !ucs.getName().equals(target))
901         {
902           if (coloursFound.length() > 0)
903           {
904             coloursFound.append("|");
905           }
906           coloursFound.append(file);
907           userColourSchemes.put(ucs.getName(), ucs);
908         }
909       } catch (Exception ex)
910       {
911         System.out.println("Error loading User ColourFile\n" + ex);
912       }
913     }
914
915     if (coloursFound.toString().length() > 1)
916     {
917       jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS",
918               coloursFound.toString());
919     }
920     else
921     {
922       jalview.bin.Cache.applicationProperties
923               .remove("USER_DEFINED_COLOURS");
924     }
925
926   }
927
928   public void caseSensitive_actionPerformed(ActionEvent e)
929   {
930     resetButtonPanel(caseSensitive.isSelected());
931     lcaseColour.setEnabled(caseSensitive.isSelected());
932   }
933
934   public void lcaseColour_actionPerformed(ActionEvent e)
935   {
936     if (selectedButtons == null)
937     {
938       selectedButtons = new Vector();
939     }
940     else
941     {
942       selectedButtons.clear();
943     }
944     selectedButtons.add(lcaseColour);
945   }
946
947 }