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