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