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