2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
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;
40 import java.awt.Color;
42 import java.awt.Insets;
43 import java.awt.event.MouseAdapter;
44 import java.awt.event.MouseEvent;
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;
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;
60 * This panel allows the user to assign colours to Amino Acid residue codes, and
61 * save the colour scheme.
63 * @author Andrew Waterhouse
64 * @author Mungo Carstairs
66 public class UserDefinedColours extends GUserDefinedColours
67 implements ChangeListener
69 private static final Font VERDANA_BOLD_10 = new Font("Verdana", Font.BOLD,
72 public static final String USER_DEFINED_COLOURS = "USER_DEFINED_COLOURS";
74 private static final String LAST_DIRECTORY = "LAST_DIRECTORY";
76 private static final int MY_FRAME_HEIGHT = 440;
78 private static final int MY_FRAME_WIDTH = 810;
80 private static final int MY_FRAME_WIDTH_CASE_SENSITIVE = 970;
85 * the colour scheme when the dialog was opened, or
86 * the scheme last saved to file
88 ColourSchemeI oldColourScheme;
91 * flag is true if the colour scheme has been changed since the
92 * dialog was opened, or the changes last saved to file
94 boolean changedButNotSaved;
98 List<JButton> upperCaseButtons;
100 List<JButton> lowerCaseButtons;
103 * Creates and displays a new UserDefinedColours panel
107 public UserDefinedColours(AlignmentPanel alignPanel)
111 lcaseColour.setEnabled(false);
113 this.ap = alignPanel;
115 oldColourScheme = alignPanel.av.getGlobalColourScheme();
117 if (oldColourScheme instanceof UserColourScheme)
119 schemeName.setText(oldColourScheme.getSchemeName());
120 if (((UserColourScheme) oldColourScheme)
121 .getLowerCaseColours() != null)
123 caseSensitive.setSelected(true);
124 lcaseColour.setEnabled(true);
125 resetButtonPanel(true);
129 resetButtonPanel(false);
134 resetButtonPanel(false);
143 selectedButtons = new ArrayList<>();
148 colorChooser.getSelectionModel().addChangeListener(this);
149 frame = new JInternalFrame();
150 frame.setContentPane(this);
151 Desktop.addInternalFrame(frame,
152 MessageManager.getString("label.user_defined_colours"), Desktop.FRAME_MAKE_VISIBLE,
153 MY_FRAME_WIDTH, MY_FRAME_HEIGHT, Desktop.FRAME_ALLOW_RESIZE, Desktop.FRAME_SET_MIN_SIZE_300);
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.
162 * @param isCaseSensitive
164 void resetButtonPanel(boolean isCaseSensitive)
166 buttonPanel.removeAll();
168 if (upperCaseButtons == null)
170 upperCaseButtons = new ArrayList<>();
173 for (int i = 0; i < 20; i++)
175 String label = isCaseSensitive ? ResidueProperties.aa[i]
176 : ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i])
178 JButton button = makeButton(label, ResidueProperties.aa[i],
179 upperCaseButtons, i);
180 buttonPanel.add(button);
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));
188 if (!isCaseSensitive)
190 gridLayout.setRows(6);
191 gridLayout.setColumns(4);
195 gridLayout.setRows(7);
197 gridLayout.setColumns(cols + 1);
199 if (lowerCaseButtons == null)
201 lowerCaseButtons = new ArrayList<>();
204 for (int i = 0; i < 20; i++)
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);
211 buttonPanel.add(button, index);
217 buttonPanel.add(makeButton("b", "b", lowerCaseButtons, 20));
218 buttonPanel.add(makeButton("z", "z", lowerCaseButtons, 21));
219 buttonPanel.add(makeButton("x", "x", lowerCaseButtons, 22));
222 // JAL-1360 widen the frame dynamically to accommodate case-sensitive AA
224 if (this.frame != null)
226 int newWidth = isCaseSensitive ? MY_FRAME_WIDTH_CASE_SENSITIVE
228 this.frame.setSize(newWidth, this.frame.getHeight());
231 buttonPanel.validate();
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).
246 public void stateChanged(ChangeEvent evt)
248 JButton button = null;
249 final Color newColour = colorChooser.getColor();
250 if (lcaseColour.isSelected())
252 selectedButtons.clear();
253 for (int i = 0; i < lowerCaseButtons.size(); i++)
255 button = lowerCaseButtons.get(i);
256 button.setBackground(newColour);
257 button.setForeground(
258 ColorUtils.brighterThan(button.getBackground()));
261 for (int i = 0; i < selectedButtons.size(); i++)
263 button = selectedButtons.get(i);
264 button.setBackground(newColour);
265 button.setForeground(ColorUtils.brighterThan(newColour));
268 changedButNotSaved = true;
272 * Performs actions when a residue button is clicked. This manages the button
273 * selection set (highlighted by brighter foreground text).
275 * On select button(s) with Ctrl/click or Shift/click: set button foreground
276 * text to brighter than background.
278 * On unselect button(s) with Ctrl/click on selected, or click to release
279 * current selection: reset foreground text to darker than background.
281 * Simple click: clear selection (resetting foreground to darker); set clicked
282 * button foreground to brighter
284 * Finally, synchronize the colour chooser to the colour of the first button
285 * in the selected set.
289 public void colourButtonPressed(MouseEvent e)
291 JButton pressed = (JButton) e.getSource();
295 JButton start, end = (JButton) e.getSource();
296 if (selectedButtons.size() > 0)
298 start = selectedButtons.get(selectedButtons.size() - 1);
302 start = (JButton) e.getSource();
305 int startIndex = 0, endIndex = 0;
306 for (int b = 0; b < buttonPanel.getComponentCount(); b++)
308 if (buttonPanel.getComponent(b) == start)
312 if (buttonPanel.getComponent(b) == end)
318 if (startIndex > endIndex)
320 int temp = startIndex;
321 startIndex = endIndex;
325 for (int b = startIndex; b <= endIndex; b++)
327 JButton button = (JButton) buttonPanel.getComponent(b);
328 if (!selectedButtons.contains(button))
330 button.setForeground(
331 ColorUtils.brighterThan(button.getBackground()));
332 selectedButtons.add(button);
336 else if (!e.isControlDown())
338 for (int b = 0; b < selectedButtons.size(); b++)
340 JButton button = selectedButtons.get(b);
341 button.setForeground(ColorUtils.darkerThan(button.getBackground()));
343 selectedButtons.clear();
344 pressed.setForeground(
345 ColorUtils.brighterThan(pressed.getBackground()));
346 selectedButtons.add(pressed);
349 else if (e.isControlDown())
351 if (selectedButtons.contains(pressed))
353 pressed.setForeground(
354 ColorUtils.darkerThan(pressed.getBackground()));
355 selectedButtons.remove(pressed);
359 pressed.setForeground(
360 ColorUtils.brighterThan(pressed.getBackground()));
361 selectedButtons.add(pressed);
365 if (selectedButtons.size() > 0)
367 colorChooser.setColor((selectedButtons.get(0)).getBackground());
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.
384 * the button's position in the list
386 JButton makeButton(String label, String residue, List<JButton> buttons,
389 final JButton button;
392 if (buttonIndex < buttons.size())
394 button = buttons.get(buttonIndex);
395 col = button.getBackground();
399 button = new JButton();
400 button.addMouseListener(new MouseAdapter()
403 public void mouseClicked(MouseEvent e)
405 colourButtonPressed(e);
412 * make initial button colour that of the current colour scheme,
413 * if it is a simple per-residue colouring, else white
416 if (oldColourScheme != null && oldColourScheme.isSimple())
418 col = oldColourScheme.findColour(residue.charAt(0), 0, null, null,
423 if (caseSensitive.isSelected())
425 button.setMargin(new Insets(2, 2, 2, 2));
429 button.setMargin(new Insets(2, 14, 2, 14));
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);
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
447 protected void okButton_actionPerformed()
449 if (isNoSelectionMade())
451 JvOptionPane.showMessageDialog(Desktop.getDesktopPane(),
453 .getString("label.no_colour_selection_in_scheme"),
454 MessageManager.getString("label.no_colour_selection_warn"),
455 JvOptionPane.WARNING_MESSAGE);
460 * OK is treated as 'apply colours and close'
462 applyButton_actionPerformed();
465 * If editing a named colour scheme, warn if changes
466 * have not been saved
468 warnIfUnsavedChanges();
472 frame.setClosed(true);
473 } catch (Exception ex)
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
485 protected void warnIfUnsavedChanges()
487 // BH 2018 no warning in JavaScript TODO
489 if (!Platform.isJS() && changedButNotSaved)
496 String name = schemeName.getText().trim();
497 if (oldColourScheme != null && !"".equals(name)
498 && name.equals(oldColourScheme.getSchemeName()))
500 String message = MessageManager
501 .formatMessage("label.scheme_changed", name);
502 String title = MessageManager.getString("label.save_changes");
503 String[] options = new String[] { title,
504 MessageManager.getString("label.dont_save_changes"), };
505 final String question = JvSwingUtils.wrapTooltip(true, message);
506 int response = JvOptionPane.showOptionDialog(Desktop.getDesktopPane(),
507 question, title, JvOptionPane.DEFAULT_OPTION,
508 JvOptionPane.PLAIN_MESSAGE, null, options, options[0]);
513 * prompt to save changes to file; if done,
514 * resets 'changed' flag to false
516 savebutton_actionPerformed();
520 * if user chooses not to save (either in this dialog or in the
521 * save as dialogs), treat this as a new user defined colour scheme
523 if (changedButNotSaved)
526 * clear scheme name and re-apply as an anonymous scheme
528 schemeName.setText("");
529 applyButton_actionPerformed();
536 * Returns true if the user has not made any colour selection (including if
537 * 'case-sensitive' selected and no lower-case colour chosen).
541 protected boolean isNoSelectionMade()
543 final boolean noUpperCaseSelected = upperCaseButtons == null
544 || upperCaseButtons.isEmpty();
545 final boolean noLowerCaseSelected = caseSensitive.isSelected()
546 && (lowerCaseButtons == null || lowerCaseButtons.isEmpty());
547 final boolean noSelectionMade = noUpperCaseSelected
548 || noLowerCaseSelected;
549 return noSelectionMade;
553 * Applies the current colour scheme to the alignment or sequence group
556 protected void applyButton_actionPerformed()
558 if (isNoSelectionMade())
560 JvOptionPane.showMessageDialog(Desktop.getDesktopPane(),
562 .getString("label.no_colour_selection_in_scheme"),
563 MessageManager.getString("label.no_colour_selection_warn"),
564 JvOptionPane.WARNING_MESSAGE);
567 UserColourScheme ucs = getSchemeFromButtons();
569 ap.alignFrame.changeColour(ucs);
573 * Constructs an instance of UserColourScheme with the residue colours
574 * currently set on the buttons on the panel
578 UserColourScheme getSchemeFromButtons()
581 Color[] newColours = new Color[24];
583 int length = upperCaseButtons.size();
587 for (JButton btn : upperCaseButtons)
589 newColours[i] = btn.getBackground();
595 for (int i = 0; i < 24; i++)
597 JButton button = upperCaseButtons.get(i);
598 newColours[i] = button.getBackground();
602 UserColourScheme ucs = new UserColourScheme(newColours);
603 ucs.setName(schemeName.getText());
605 if (caseSensitive.isSelected())
607 newColours = new Color[23];
608 length = lowerCaseButtons.size();
612 for (JButton btn : lowerCaseButtons)
614 newColours[i] = btn.getBackground();
620 for (int i = 0; i < 23; i++)
622 JButton button = lowerCaseButtons.get(i);
623 newColours[i] = button.getBackground();
626 ucs.setLowerCaseColours(newColours);
633 * Action on clicking Load scheme button.
635 * <li>Open a file chooser to browse for files with extension .jc</li>
636 * <li>Load in the colour scheme and transfer it to this panel's buttons</li>
637 * <li>Register the loaded colour scheme</li>
641 protected void loadbutton_actionPerformed()
643 upperCaseButtons = new ArrayList<>();
644 lowerCaseButtons = new ArrayList<>();
645 JalviewFileChooser chooser = new JalviewFileChooser("jc",
646 "Jalview User Colours");
647 chooser.setFileView(new JalviewFileView());
648 chooser.setDialogTitle(
649 MessageManager.getString("label.load_colour_scheme"));
650 chooser.setToolTipText(MessageManager.getString("action.load"));
651 chooser.setResponseHandler(0, new Runnable()
656 File choice = chooser.getSelectedFile();
657 Cache.setProperty(LAST_DIRECTORY, choice.getParent());
659 UserColourScheme ucs = ColourSchemeLoader
660 .loadColourScheme(choice.getAbsolutePath());
661 Color[] colors = ucs.getColours();
662 schemeName.setText(ucs.getSchemeName());
664 if (ucs.getLowerCaseColours() != null)
666 caseSensitive.setSelected(true);
667 lcaseColour.setEnabled(true);
668 resetButtonPanel(true);
669 for (int i = 0; i < lowerCaseButtons.size(); i++)
671 JButton button = lowerCaseButtons.get(i);
672 button.setBackground(ucs.getLowerCaseColours()[i]);
677 caseSensitive.setSelected(false);
678 lcaseColour.setEnabled(false);
679 resetButtonPanel(false);
682 for (int i = 0; i < upperCaseButtons.size(); i++)
684 JButton button = upperCaseButtons.get(i);
685 button.setBackground(colors[i]);
688 addNewColourScheme(choice.getPath());
692 chooser.showOpenDialog(this);
696 * Loads the user-defined colour scheme from the first file listed in property
697 * "USER_DEFINED_COLOURS". If this fails, returns an all-white colour scheme.
701 public static UserColourScheme loadDefaultColours()
703 UserColourScheme ret = null;
705 String colours = Cache.getProperty(USER_DEFINED_COLOURS);
708 if (colours.indexOf("|") > -1)
710 colours = colours.substring(0, colours.indexOf("|"));
712 ret = ColourSchemeLoader.loadColourScheme(colours);
717 ret = new UserColourScheme("white");
724 * Action on pressing the Save button.
726 * <li>Check a name has been entered</li>
727 * <li>Warn if the name already exists, remove any existing scheme of the same
728 * name if overwriting</li>
729 * <li>Do the standard file chooser thing to write with extension .jc</li>
730 * <li>If saving changes (possibly not yet applied) to the currently selected
731 * colour scheme, then apply the changes, as it is too late to back out
733 * <li>Don't apply the changes if the currently selected scheme is different,
734 * to allow a new scheme to be configured and saved but not applied</li>
736 * If the scheme is saved to file, the 'changed' flag field is reset to false.
739 protected void savebutton_actionPerformed()
741 String name = schemeName.getText().trim();
742 if (name.length() < 1)
744 JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(),
746 .getString("label.user_colour_scheme_must_have_name"),
747 MessageManager.getString("label.no_name_colour_scheme"),
748 JvOptionPane.WARNING_MESSAGE);
751 if (!Platform.isJS() && ColourSchemes.getInstance().nameExists(name))
759 int reply = JvOptionPane.showInternalConfirmDialog(Desktop.getDesktopPane(),
760 MessageManager.formatMessage(
761 "label.colour_scheme_exists_overwrite", new Object[]
763 MessageManager.getString("label.duplicate_scheme_name"),
764 JvOptionPane.YES_NO_OPTION);
765 if (reply != JvOptionPane.YES_OPTION)
772 JalviewFileChooser chooser = new JalviewFileChooser("jc",
773 "Jalview User Colours");
775 JalviewFileView fileView = new JalviewFileView();
776 chooser.setFileView(fileView);
777 chooser.setDialogTitle(
778 MessageManager.getString("label.save_colour_scheme"));
779 chooser.setToolTipText(MessageManager.getString("action.save"));
780 int option = chooser.showSaveDialog(this);
781 if (option == JalviewFileChooser.APPROVE_OPTION)
783 File file = chooser.getSelectedFile();
784 UserColourScheme updatedScheme = addNewColourScheme(file.getPath());
786 changedButNotSaved = false;
789 * changes saved - apply to alignment if we are changing
790 * the currently selected colour scheme; also make the updated
791 * colours the 'backout' scheme on Cancel
793 if (oldColourScheme != null
794 && name.equals(oldColourScheme.getSchemeName()))
796 oldColourScheme = updatedScheme;
797 applyButton_actionPerformed();
803 * Adds the current colour scheme to the Jalview properties file so it is
804 * loaded on next startup, and updates the Colour menu in the parent
805 * AlignFrame (if there is one). Note this action does not including applying
811 protected UserColourScheme addNewColourScheme(String filePath)
814 * update the delimited list of user defined colour files in
815 * Jalview property USER_DEFINED_COLOURS
817 String defaultColours = Cache.getDefault(USER_DEFINED_COLOURS,
819 if (defaultColours.indexOf(filePath) == -1)
821 if (defaultColours.length() > 0)
823 defaultColours = defaultColours.concat("|");
825 defaultColours = defaultColours.concat(filePath);
827 Cache.setProperty(USER_DEFINED_COLOURS, defaultColours);
830 * construct and register the colour scheme
832 UserColourScheme ucs = getSchemeFromButtons();
833 ColourSchemes.getInstance().registerColourScheme(ucs);
836 * update the Colour menu items
840 ap.alignFrame.buildColourMenu();
847 * Saves the colour scheme to file in XML format
851 protected void saveToFile(File toFile)
854 * build a Java model of colour scheme as XML, and
857 JalviewUserColours ucs = new JalviewUserColours();
858 String name = schemeName.getText();
859 ucs.setSchemeName(name);
862 PrintWriter out = new PrintWriter(new OutputStreamWriter(
863 new FileOutputStream(toFile), "UTF-8"));
865 for (int i = 0; i < buttonPanel.getComponentCount(); i++)
867 JButton button = (JButton) buttonPanel.getComponent(i);
868 Colour col = new Colour();
869 col.setName(button.getText());
870 col.setRGB(Format.getHexString(button.getBackground()));
871 ucs.getColour().add(col);
873 JAXBContext jaxbContext = JAXBContext
874 .newInstance(JalviewUserColours.class);
875 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
876 jaxbMarshaller.marshal(
877 new ObjectFactory().createJalviewUserColours(ucs), out);
880 } catch (Exception ex)
882 ex.printStackTrace();
887 * On cancel, restores the colour scheme that was selected before the dialogue
891 protected void cancelButton_actionPerformed()
893 ap.alignFrame.changeColour(oldColourScheme);
894 ap.paintAlignment(true, true);
898 frame.setClosed(true);
899 } catch (Exception ex)
905 * Action on selecting or deselecting the Case Sensitive option. When
906 * selected, separate buttons are shown for lower case residues, and the panel
907 * is resized to accommodate them. Also, the checkbox for 'apply colour to all
908 * lower case' is enabled.
911 public void caseSensitive_actionPerformed()
913 boolean selected = caseSensitive.isSelected();
914 resetButtonPanel(selected);
915 lcaseColour.setEnabled(selected);