JAL-2446 merged to spike branch
[jalview.git] / src / jalview / gui / UserDefinedColours.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.gui;
22
23 import jalview.bin.Cache;
24 import jalview.io.JalviewFileChooser;
25 import jalview.io.JalviewFileView;
26 import jalview.jbgui.GUserDefinedColours;
27 import jalview.schemabinding.version2.Colour;
28 import jalview.schemabinding.version2.JalviewUserColours;
29 import jalview.schemes.ColourSchemeI;
30 import jalview.schemes.ColourSchemeLoader;
31 import jalview.schemes.ColourSchemes;
32 import jalview.schemes.ResidueProperties;
33 import jalview.schemes.UserColourScheme;
34 import jalview.util.ColorUtils;
35 import jalview.util.Format;
36 import jalview.util.MessageManager;
37
38 import java.awt.Color;
39 import java.awt.Font;
40 import java.awt.Insets;
41 import java.awt.event.MouseAdapter;
42 import java.awt.event.MouseEvent;
43 import java.io.File;
44 import java.io.FileOutputStream;
45 import java.io.OutputStreamWriter;
46 import java.io.PrintWriter;
47 import java.util.ArrayList;
48 import java.util.List;
49
50 import javax.swing.JButton;
51 import javax.swing.JInternalFrame;
52 import javax.swing.event.ChangeEvent;
53 import javax.swing.event.ChangeListener;
54
55 /**
56  * This panel allows the user to assign colours to Amino Acid residue codes, and
57  * save the colour scheme.
58  * 
59  * @author Andrew Waterhouse
60  * @author Mungo Carstairs
61  */
62 public class UserDefinedColours extends GUserDefinedColours implements
63         ChangeListener
64 {
65   private static final Font VERDANA_BOLD_10 = new Font("Verdana",
66           Font.BOLD, 10);
67
68   public static final String USER_DEFINED_COLOURS = "USER_DEFINED_COLOURS";
69
70   private static final String LAST_DIRECTORY = "LAST_DIRECTORY";
71
72   private static final int MY_FRAME_HEIGHT = 440;
73
74   private static final int MY_FRAME_WIDTH = 810;
75
76   private static final int MY_FRAME_WIDTH_CASE_SENSITIVE = 970;
77
78   AlignmentPanel ap;
79
80   /*
81    * the colour scheme when the dialog was opened, or
82    * the scheme last saved to file
83    */
84   ColourSchemeI oldColourScheme;
85
86   /*
87    * flag is true if the colour scheme has been changed since the
88    * dialog was opened, or the changes last saved to file
89    */
90   boolean changed;
91
92   JInternalFrame frame;
93
94   List<JButton> upperCaseButtons;
95
96   List<JButton> lowerCaseButtons;
97
98   /**
99    * Creates and displays a new UserDefinedColours panel
100    * 
101    * @param alignPanel
102    */
103   public UserDefinedColours(AlignmentPanel alignPanel)
104   {
105     this();
106
107     lcaseColour.setEnabled(false);
108
109     this.ap = alignPanel;
110
111     oldColourScheme = alignPanel.av.getGlobalColourScheme();
112
113     if (oldColourScheme instanceof UserColourScheme)
114     {
115       schemeName.setText(oldColourScheme.getSchemeName());
116       if (((UserColourScheme) oldColourScheme).getLowerCaseColours() != null)
117       {
118         caseSensitive.setSelected(true);
119         lcaseColour.setEnabled(true);
120         resetButtonPanel(true);
121       }
122       else
123       {
124         resetButtonPanel(false);
125       }
126     }
127     else
128     {
129       resetButtonPanel(false);
130     }
131
132     showFrame();
133   }
134
135   UserDefinedColours()
136   {
137     super();
138     selectedButtons = new ArrayList<JButton>();
139   }
140
141   void showFrame()
142   {
143     colorChooser.getSelectionModel().addChangeListener(this);
144     frame = new JInternalFrame();
145     frame.setContentPane(this);
146     Desktop.addInternalFrame(frame,
147             MessageManager.getString("label.user_defined_colours"),
148             MY_FRAME_WIDTH, MY_FRAME_HEIGHT, true);
149   }
150
151   /**
152    * Rebuilds the panel with coloured buttons for residues. If not case
153    * sensitive colours, show 3-letter amino acid code as button text. If case
154    * sensitive, just show the single letter code, in order to make space for the
155    * additional buttons.
156    * 
157    * @param isCaseSensitive
158    */
159   void resetButtonPanel(boolean isCaseSensitive)
160   {
161     buttonPanel.removeAll();
162
163     if (upperCaseButtons == null)
164     {
165       upperCaseButtons = new ArrayList<JButton>();
166     }
167
168     for (int i = 0; i < 20; i++)
169     {
170       String label = isCaseSensitive ? ResidueProperties.aa[i]
171               : ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i])
172                       .toString();
173       JButton button = makeButton(label, ResidueProperties.aa[i],
174               upperCaseButtons, i);
175       buttonPanel.add(button);
176     }
177
178     buttonPanel.add(makeButton("B", "B", upperCaseButtons, 20));
179     buttonPanel.add(makeButton("Z", "Z", upperCaseButtons, 21));
180     buttonPanel.add(makeButton("X", "X", upperCaseButtons, 22));
181     buttonPanel.add(makeButton("Gap", "-", upperCaseButtons, 23));
182
183     if (!isCaseSensitive)
184     {
185       gridLayout.setRows(6);
186       gridLayout.setColumns(4);
187     }
188     else
189     {
190       gridLayout.setRows(7);
191       int cols = 7;
192       gridLayout.setColumns(cols + 1);
193
194       if (lowerCaseButtons == null)
195       {
196         lowerCaseButtons = new ArrayList<JButton>();
197       }
198
199       for (int i = 0; i < 20; i++)
200       {
201         int row = i / cols + 1;
202         int index = (row * cols) + i;
203         JButton button = makeButton(ResidueProperties.aa[i].toLowerCase(),
204                 ResidueProperties.aa[i].toLowerCase(), lowerCaseButtons, i);
205
206         buttonPanel.add(button, index);
207       }
208     }
209
210     if (isCaseSensitive)
211     {
212       buttonPanel.add(makeButton("b", "b", lowerCaseButtons, 20));
213       buttonPanel.add(makeButton("z", "z", lowerCaseButtons, 21));
214       buttonPanel.add(makeButton("x", "x", lowerCaseButtons, 22));
215     }
216
217     // JAL-1360 widen the frame dynamically to accommodate case-sensitive AA
218     // codes
219     if (this.frame != null)
220     {
221       int newWidth = isCaseSensitive ? MY_FRAME_WIDTH_CASE_SENSITIVE
222               : MY_FRAME_WIDTH;
223       this.frame.setSize(newWidth, this.frame.getHeight());
224     }
225
226     buttonPanel.validate();
227     validate();
228   }
229
230   /**
231    * ChangeListener handler for when a colour is picked in the colour chooser.
232    * The action is to apply the colour to all selected buttons as their
233    * background colour. Foreground colour (text) is set to a lighter shade in
234    * order to highlight which buttons are selected. If 'Lower Case Colour' is
235    * active, then the colour is applied to all lower case buttons (as well as
236    * the Lower Case Colour button itself).
237    * 
238    * @param evt
239    */
240   @Override
241   public void stateChanged(ChangeEvent evt)
242   {
243     JButton button = null;
244     final Color newColour = colorChooser.getColor();
245     if (lcaseColour.isSelected())
246     {
247       selectedButtons.clear();
248       for (int i = 0; i < lowerCaseButtons.size(); i++)
249       {
250         button = lowerCaseButtons.get(i);
251         button.setBackground(newColour);
252         button.setForeground(ColorUtils.brighterThan(button.getBackground()));
253       }
254     }
255     for (int i = 0; i < selectedButtons.size(); i++)
256     {
257       button = selectedButtons.get(i);
258       button.setBackground(newColour);
259       button.setForeground(ColorUtils.brighterThan(newColour));
260     }
261
262     changed = true;
263   }
264
265   /**
266    * Performs actions when a residue button is clicked. This manages the button
267    * selection set (highlighted by brighter foreground text).
268    * <p>
269    * On select button(s) with Ctrl/click or Shift/click: set button foreground
270    * text to brighter than background.
271    * <p>
272    * On unselect button(s) with Ctrl/click on selected, or click to release
273    * current selection: reset foreground text to darker than background.
274    * <p>
275    * Simple click: clear selection (resetting foreground to darker); set clicked
276    * button foreground to brighter
277    * <p>
278    * Finally, synchronize the colour chooser to the colour of the first button
279    * in the selected set.
280    * 
281    * @param e
282    */
283   public void colourButtonPressed(MouseEvent e)
284   {
285     JButton pressed = (JButton) e.getSource();
286
287     if (e.isShiftDown())
288     {
289       JButton start, end = (JButton) e.getSource();
290       if (selectedButtons.size() > 0)
291       {
292         start = selectedButtons.get(selectedButtons.size() - 1);
293       }
294       else
295       {
296         start = (JButton) e.getSource();
297       }
298
299       int startIndex = 0, endIndex = 0;
300       for (int b = 0; b < buttonPanel.getComponentCount(); b++)
301       {
302         if (buttonPanel.getComponent(b) == start)
303         {
304           startIndex = b;
305         }
306         if (buttonPanel.getComponent(b) == end)
307         {
308           endIndex = b;
309         }
310       }
311
312       if (startIndex > endIndex)
313       {
314         int temp = startIndex;
315         startIndex = endIndex;
316         endIndex = temp;
317       }
318
319       for (int b = startIndex; b <= endIndex; b++)
320       {
321         JButton button = (JButton) buttonPanel.getComponent(b);
322         if (!selectedButtons.contains(button))
323         {
324           button.setForeground(ColorUtils.brighterThan(button
325                   .getBackground()));
326           selectedButtons.add(button);
327         }
328       }
329     }
330     else if (!e.isControlDown())
331     {
332       for (int b = 0; b < selectedButtons.size(); b++)
333       {
334         JButton button = selectedButtons.get(b);
335         button.setForeground(ColorUtils.darkerThan(button.getBackground()));
336       }
337       selectedButtons.clear();
338       pressed.setForeground(ColorUtils.brighterThan(pressed.getBackground()));
339       selectedButtons.add(pressed);
340
341     }
342     else if (e.isControlDown())
343     {
344       if (selectedButtons.contains(pressed))
345       {
346         pressed.setForeground(ColorUtils.darkerThan(pressed.getBackground()));
347         selectedButtons.remove(pressed);
348       }
349       else
350       {
351         pressed.setForeground(ColorUtils.brighterThan(pressed
352                 .getBackground()));
353         selectedButtons.add(pressed);
354       }
355     }
356
357     if (selectedButtons.size() > 0)
358     {
359       colorChooser.setColor((selectedButtons.get(0)).getBackground());
360     }
361   }
362
363   /**
364    * A helper method to update or make a colour button, whose background colour
365    * is the associated colour, and text colour a darker shade of the same. If
366    * the button is already in the list, then its text and margins are updated,
367    * if not then it is created and added. This method supports toggling between
368    * case-sensitive and case-insensitive button panels. The case-sensitive
369    * version has abbreviated button text in order to fit in more buttons.
370    * 
371    * @param label
372    * @param residue
373    * @param the
374    *          list of buttons
375    * @param buttonIndex
376    *          the button's position in the list
377    */
378   JButton makeButton(String label, String residue, List<JButton> buttons,
379           int buttonIndex)
380   {
381     final JButton button;
382     Color col;
383
384     if (buttonIndex < buttons.size())
385     {
386       button = buttons.get(buttonIndex);
387       col = button.getBackground();
388     }
389     else
390     {
391       button = new JButton();
392       button.addMouseListener(new MouseAdapter()
393       {
394         @Override
395         public void mouseClicked(MouseEvent e)
396         {
397           colourButtonPressed(e);
398         }
399       });
400
401       buttons.add(button);
402
403       /*
404        * make initial button colour that of the current colour scheme,
405        * if it is a simple per-residue colouring, else white
406        */
407       col = Color.white;
408       if (oldColourScheme != null && oldColourScheme.isSimple())
409       {
410         col = oldColourScheme.findColour(residue.charAt(0), 0, null, null,
411                 0f);
412       }
413     }
414
415     if (caseSensitive.isSelected())
416     {
417       button.setMargin(new Insets(2, 2, 2, 2));
418     }
419     else
420     {
421       button.setMargin(new Insets(2, 14, 2, 14));
422     }
423
424     button.setOpaque(true); // required for the next line to have effect
425     button.setBackground(col);
426     button.setText(label);
427     button.setForeground(ColorUtils.darkerThan(col));
428     button.setFont(VERDANA_BOLD_10);
429
430     return button;
431   }
432
433   /**
434    * On 'OK', check that at least one colour has been assigned to a residue (and
435    * if not issue a warning), and apply the chosen colour scheme and close the
436    * panel.
437    */
438   @Override
439   protected void okButton_actionPerformed()
440   {
441     if (isNoSelectionMade())
442     {
443       JvOptionPane.showMessageDialog(Desktop.desktop, MessageManager
444               .getString("label.no_colour_selection_in_scheme"),
445               MessageManager.getString("label.no_colour_selection_warn"),
446               JvOptionPane.WARNING_MESSAGE);
447     }
448     else
449     {
450       /*
451        * OK is treated as 'apply colours and close'
452        */
453       applyButton_actionPerformed();
454
455       /*
456        * If editing a named colour scheme, warn if changes
457        * have not been saved
458        */
459       warnIfUnsavedChanges();
460
461       try
462       {
463         frame.setClosed(true);
464       } catch (Exception ex)
465       {
466       }
467     }
468   }
469
470   /**
471    * If we have made changes to an existing user defined colour scheme but not
472    * saved them, show a dialog with the option to save. If the user chooses to
473    * save, do so, else clear the colour scheme name to indicate a new colour
474    * scheme.
475    */
476   protected void warnIfUnsavedChanges()
477   {
478     if (!changed)
479     {
480       return;
481     }
482
483     String name = schemeName.getText().trim();
484     if (oldColourScheme != null && !"".equals(name)
485             && name.equals(oldColourScheme.getSchemeName()))
486     {
487       String message = MessageManager.formatMessage("label.scheme_changed",
488               name);
489       String title = MessageManager.getString("label.save_changes");
490       String[] options = new String[] { title,
491           MessageManager.getString("label.dont_save_changes"), };
492       final String question = JvSwingUtils.wrapTooltip(true, message);
493       int response = JvOptionPane.showOptionDialog(Desktop.desktop,
494               question, title, JvOptionPane.DEFAULT_OPTION,
495               JvOptionPane.PLAIN_MESSAGE, null, options, options[0]);
496
497       boolean saved = false;
498       if (response == 0)
499       {
500         /*
501          * prompt to save changes to file
502          */
503         saved = savebutton_actionPerformed();
504       }
505
506       /*
507        * if user chooses not to save (either in this dialog or in the
508        * save as dialogs), treat this as a new user defined colour scheme
509        */
510       if (!saved)
511       {
512         /*
513          * clear scheme name and re-apply as an anonymous scheme
514          */
515         schemeName.setText("");
516         applyButton_actionPerformed();
517       }
518     }
519   }
520
521   /**
522    * Returns true if the user has not made any colour selection (including if
523    * 'case-sensitive' selected and no lower-case colour chosen).
524    * 
525    * @return
526    */
527   protected boolean isNoSelectionMade()
528   {
529     final boolean noUpperCaseSelected = upperCaseButtons == null
530             || upperCaseButtons.isEmpty();
531     final boolean noLowerCaseSelected = caseSensitive.isSelected()
532             && (lowerCaseButtons == null || lowerCaseButtons.isEmpty());
533     final boolean noSelectionMade = noUpperCaseSelected
534             || noLowerCaseSelected;
535     return noSelectionMade;
536   }
537
538   /**
539    * Applies the current colour scheme to the alignment or sequence group
540    */
541   @Override
542   protected void applyButton_actionPerformed()
543   {
544     if (isNoSelectionMade())
545     {
546       JvOptionPane.showMessageDialog(Desktop.desktop, MessageManager
547               .getString("label.no_colour_selection_in_scheme"),
548               MessageManager.getString("label.no_colour_selection_warn"),
549               JvOptionPane.WARNING_MESSAGE);
550
551     }
552     UserColourScheme ucs = getSchemeFromButtons();
553
554     ap.alignFrame.changeColour(ucs);
555   }
556
557   /**
558    * Constructs an instance of UserColourScheme with the residue colours
559    * currently set on the buttons on the panel
560    * 
561    * @return
562    */
563   UserColourScheme getSchemeFromButtons()
564   {
565
566     Color[] newColours = new Color[24];
567
568     int length = upperCaseButtons.size();
569     if (length < 24)
570     {
571       int i = 0;
572       for (JButton btn : upperCaseButtons)
573       {
574         newColours[i] = btn.getBackground();
575         i++;
576       }
577     }
578     else
579     {
580       for (int i = 0; i < 24; i++)
581       {
582         JButton button = upperCaseButtons.get(i);
583         newColours[i] = button.getBackground();
584       }
585     }
586
587     UserColourScheme ucs = new UserColourScheme(newColours);
588     ucs.setName(schemeName.getText());
589
590     if (caseSensitive.isSelected())
591     {
592       newColours = new Color[23];
593       length = lowerCaseButtons.size();
594       if (length < 23)
595       {
596         int i = 0;
597         for (JButton btn : lowerCaseButtons)
598         {
599           newColours[i] = btn.getBackground();
600           i++;
601         }
602       }
603       else
604       {
605         for (int i = 0; i < 23; i++)
606         {
607           JButton button = lowerCaseButtons.get(i);
608           newColours[i] = button.getBackground();
609         }
610       }
611       ucs.setLowerCaseColours(newColours);
612     }
613
614     return ucs;
615   }
616
617   /**
618    * Action on clicking Load scheme button.
619    * <ul>
620    * <li>Open a file chooser to browse for files with extension .jc</li>
621    * <li>Load in the colour scheme and transfer it to this panel's buttons</li>
622    * <li>Register the loaded colour scheme</li>
623    * </ul>
624    */
625   @Override
626   protected void loadbutton_actionPerformed()
627   {
628     upperCaseButtons = new ArrayList<JButton>();
629     lowerCaseButtons = new ArrayList<JButton>();
630
631     JalviewFileChooser chooser = new JalviewFileChooser("jc",
632             "Jalview User Colours");
633     chooser.setFileView(new JalviewFileView());
634     chooser.setDialogTitle(MessageManager
635             .getString("label.load_colour_scheme"));
636     chooser.setToolTipText(MessageManager.getString("action.load"));
637
638     int value = chooser.showOpenDialog(this);
639
640     if (value != JalviewFileChooser.APPROVE_OPTION)
641     {
642       return;
643     }
644     File choice = chooser.getSelectedFile();
645     Cache.setProperty(LAST_DIRECTORY, choice.getParent());
646
647     UserColourScheme ucs = ColourSchemeLoader.loadColourScheme(choice
648             .getAbsolutePath());
649     Color[] colors = ucs.getColours();
650     schemeName.setText(ucs.getSchemeName());
651
652     if (ucs.getLowerCaseColours() != null)
653     {
654       caseSensitive.setSelected(true);
655       lcaseColour.setEnabled(true);
656       resetButtonPanel(true);
657       for (int i = 0; i < lowerCaseButtons.size(); i++)
658       {
659         JButton button = lowerCaseButtons.get(i);
660         button.setBackground(ucs.getLowerCaseColours()[i]);
661       }
662     }
663     else
664     {
665       caseSensitive.setSelected(false);
666       lcaseColour.setEnabled(false);
667       resetButtonPanel(false);
668     }
669
670     for (int i = 0; i < upperCaseButtons.size(); i++)
671     {
672       JButton button = upperCaseButtons.get(i);
673       button.setBackground(colors[i]);
674     }
675
676     addNewColourScheme(choice.getPath());
677   }
678
679   /**
680    * Loads the user-defined colour scheme from the first file listed in property
681    * "USER_DEFINED_COLOURS". If this fails, returns an all-white colour scheme.
682    * 
683    * @return
684    */
685   public static UserColourScheme loadDefaultColours()
686   {
687     UserColourScheme ret = null;
688
689     String colours = Cache.getProperty(USER_DEFINED_COLOURS);
690     if (colours != null)
691     {
692       if (colours.indexOf("|") > -1)
693       {
694         colours = colours.substring(0, colours.indexOf("|"));
695       }
696       ret = ColourSchemeLoader.loadColourScheme(colours);
697     }
698
699     if (ret == null)
700     {
701       ret = new UserColourScheme("white");
702     }
703
704     return ret;
705   }
706
707   /**
708    * Action on pressing the Save button.
709    * <ul>
710    * <li>Check a name has been entered</li>
711    * <li>Warn if the name already exists, remove any existing scheme of the same
712    * name if overwriting</li>
713    * <li>Do the standard file chooser thing to write with extension .jc</li>
714    * <li>If saving changes (possibly not yet applied) to the currently selected
715    * colour scheme, then apply the changes, as it is too late to back out now</li>
716    * <li>Don't apply the changes if the currently selected scheme is different,
717    * to allow a new scheme to be configured and saved but not applied</li>
718    * </ul>
719    * Returns true if the scheme is saved to file, false if it is not
720    * 
721    * @return
722    */
723   @Override
724   protected boolean savebutton_actionPerformed()
725   {
726     String name = schemeName.getText().trim();
727     if (name.length() < 1)
728     {
729       JvOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
730               .getString("label.user_colour_scheme_must_have_name"),
731               MessageManager.getString("label.no_name_colour_scheme"),
732               JvOptionPane.WARNING_MESSAGE);
733       return false;
734     }
735
736     if (ColourSchemes.getInstance().nameExists(name))
737     {
738       int reply = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
739               MessageManager.formatMessage(
740                       "label.colour_scheme_exists_overwrite", new Object[] {
741                           name, name }),
742               MessageManager.getString("label.duplicate_scheme_name"),
743               JvOptionPane.YES_NO_OPTION);
744       if (reply != JvOptionPane.YES_OPTION)
745       {
746         return false;
747       }
748     }
749     JalviewFileChooser chooser = new JalviewFileChooser("jc",
750             "Jalview User Colours");
751
752     JalviewFileView fileView = new JalviewFileView();
753     chooser.setFileView(fileView);
754     chooser.setDialogTitle(MessageManager
755             .getString("label.save_colour_scheme"));
756     chooser.setToolTipText(MessageManager.getString("action.save"));
757
758     int value = chooser.showSaveDialog(this);
759
760     if (value != JalviewFileChooser.APPROVE_OPTION)
761     {
762       return false;
763     }
764
765     File file = chooser.getSelectedFile();
766     UserColourScheme updatedScheme = addNewColourScheme(file.getPath());
767     saveToFile(file);
768     changed = false;
769
770     /*
771      * changes saved - apply to alignment if we are changing 
772      * the currently selected colour scheme; also make the updated
773      * colours the 'backout' scheme on Cancel
774      */
775     if (oldColourScheme != null
776             && name.equals(oldColourScheme.getSchemeName()))
777     {
778       oldColourScheme = updatedScheme;
779       applyButton_actionPerformed();
780     }
781     return true;
782   }
783
784   /**
785    * Adds the current colour scheme to the Jalview properties file so it is
786    * loaded on next startup, and updates the Colour menu in the parent
787    * AlignFrame (if there is one). Note this action does not including applying
788    * the colour scheme.
789    * 
790    * @param filePath
791    * @return
792    */
793   protected UserColourScheme addNewColourScheme(String filePath)
794   {
795     /*
796      * update the delimited list of user defined colour files in
797      * Jalview property USER_DEFINED_COLOURS
798      */
799     String defaultColours = Cache
800             .getDefault(USER_DEFINED_COLOURS, filePath);
801     if (defaultColours.indexOf(filePath) == -1)
802     {
803       if (defaultColours.length() > 0)
804       {
805         defaultColours = defaultColours.concat("|");
806       }
807       defaultColours = defaultColours.concat(filePath);
808     }
809     Cache.setProperty(USER_DEFINED_COLOURS, defaultColours);
810
811     /*
812      * construct and register the colour scheme
813      */
814     UserColourScheme ucs = getSchemeFromButtons();
815     ColourSchemes.getInstance().registerColourScheme(ucs);
816
817     /*
818      * update the Colour menu items
819      */
820     if (ap != null)
821     {
822       ap.alignFrame.buildColourMenu();
823     }
824
825     return ucs;
826   }
827
828   /**
829    * Saves the colour scheme to file in XML format
830    * 
831    * @param path
832    */
833   protected void saveToFile(File toFile)
834   {
835     /*
836      * build a Java model of colour scheme as XML, and 
837      * marshal to file
838      */
839     JalviewUserColours ucs = new JalviewUserColours();
840     String name = schemeName.getText();
841     ucs.setSchemeName(name);
842     try
843     {
844       PrintWriter out = new PrintWriter(new OutputStreamWriter(
845               new FileOutputStream(toFile), "UTF-8"));
846
847       for (int i = 0; i < buttonPanel.getComponentCount(); i++)
848       {
849         JButton button = (JButton) buttonPanel.getComponent(i);
850         Colour col = new Colour();
851         col.setName(button.getText());
852         col.setRGB(Format.getHexString(button.getBackground()));
853         ucs.addColour(col);
854       }
855       ucs.marshal(out);
856       out.close();
857     } catch (Exception ex)
858     {
859       ex.printStackTrace();
860     }
861   }
862
863   /**
864    * On cancel, restores the colour scheme that was selected before the dialogue
865    * was opened
866    */
867   @Override
868   protected void cancelButton_actionPerformed()
869   {
870     ap.alignFrame.changeColour(oldColourScheme);
871     ap.paintAlignment(true);
872
873     try
874     {
875       frame.setClosed(true);
876     } catch (Exception ex)
877     {
878     }
879   }
880
881   /**
882    * Action on selecting or deselecting the Case Sensitive option. When
883    * selected, separate buttons are shown for lower case residues, and the panel
884    * is resized to accommodate them. Also, the checkbox for 'apply colour to all
885    * lower case' is enabled.
886    */
887   @Override
888   public void caseSensitive_actionPerformed()
889   {
890     boolean selected = caseSensitive.isSelected();
891     resetButtonPanel(selected);
892     lcaseColour.setEnabled(selected);
893   }
894 }