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