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