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