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 java.util.Locale;
25 import jalview.bin.Cache;
26 import jalview.io.JalviewFileChooser;
27 import jalview.io.JalviewFileView;
28 import jalview.jbgui.GUserDefinedColours;
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.Platform;
38 import jalview.xml.binding.jalview.JalviewUserColours;
39 import jalview.xml.binding.jalview.JalviewUserColours.Colour;
40 import jalview.xml.binding.jalview.ObjectFactory;
42 import java.awt.Color;
44 import java.awt.Insets;
45 import java.awt.event.MouseAdapter;
46 import java.awt.event.MouseEvent;
48 import java.io.FileOutputStream;
49 import java.io.OutputStreamWriter;
50 import java.io.PrintWriter;
51 import java.util.ArrayList;
52 import java.util.List;
54 import javax.swing.JButton;
55 import javax.swing.JInternalFrame;
56 import javax.swing.event.ChangeEvent;
57 import javax.swing.event.ChangeListener;
58 import javax.xml.bind.JAXBContext;
59 import javax.xml.bind.Marshaller;
62 * This panel allows the user to assign colours to Amino Acid residue codes, and
63 * save the colour scheme.
65 * @author Andrew Waterhouse
66 * @author Mungo Carstairs
68 public class UserDefinedColours extends GUserDefinedColours
69 implements ChangeListener
71 private static final Font VERDANA_BOLD_10 = new Font("Verdana", Font.BOLD,
74 public static final String USER_DEFINED_COLOURS = "USER_DEFINED_COLOURS";
76 private static final String LAST_DIRECTORY = "LAST_DIRECTORY";
78 private static final int MY_FRAME_HEIGHT = 440;
80 private static final int MY_FRAME_WIDTH = 810;
82 private static final int MY_FRAME_WIDTH_CASE_SENSITIVE = 970;
87 * the colour scheme when the dialog was opened, or
88 * the scheme last saved to file
90 ColourSchemeI oldColourScheme;
93 * flag is true if the colour scheme has been changed since the
94 * dialog was opened, or the changes last saved to file
96 boolean changedButNotSaved;
100 List<JButton> upperCaseButtons;
102 List<JButton> lowerCaseButtons;
105 * Creates and displays a new UserDefinedColours panel
109 public UserDefinedColours(AlignmentPanel alignPanel)
113 lcaseColour.setEnabled(false);
115 this.ap = alignPanel;
117 oldColourScheme = alignPanel.av.getGlobalColourScheme();
119 if (oldColourScheme instanceof UserColourScheme)
121 schemeName.setText(oldColourScheme.getSchemeName());
122 if (((UserColourScheme) oldColourScheme)
123 .getLowerCaseColours() != null)
125 caseSensitive.setSelected(true);
126 lcaseColour.setEnabled(true);
127 resetButtonPanel(true);
131 resetButtonPanel(false);
136 resetButtonPanel(false);
145 selectedButtons = new ArrayList<>();
150 colorChooser.getSelectionModel().addChangeListener(this);
151 frame = new JInternalFrame();
152 frame.setContentPane(this);
153 Desktop.addInternalFrame(frame,
154 MessageManager.getString("label.user_defined_colours"), Desktop.FRAME_MAKE_VISIBLE,
155 MY_FRAME_WIDTH, MY_FRAME_HEIGHT, Desktop.FRAME_ALLOW_RESIZE, Desktop.FRAME_SET_MIN_SIZE_300);
159 * Rebuilds the panel with coloured buttons for residues. If not case
160 * sensitive colours, show 3-letter amino acid code as button text. If case
161 * sensitive, just show the single letter code, in order to make space for the
162 * additional buttons.
164 * @param isCaseSensitive
166 void resetButtonPanel(boolean isCaseSensitive)
168 buttonPanel.removeAll();
170 if (upperCaseButtons == null)
172 upperCaseButtons = new ArrayList<>();
175 for (int i = 0; i < 20; i++)
177 String label = isCaseSensitive ? ResidueProperties.aa[i]
178 : ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i])
180 JButton button = makeButton(label, ResidueProperties.aa[i],
181 upperCaseButtons, i);
182 buttonPanel.add(button);
185 buttonPanel.add(makeButton("B", "B", upperCaseButtons, 20));
186 buttonPanel.add(makeButton("Z", "Z", upperCaseButtons, 21));
187 buttonPanel.add(makeButton("X", "X", upperCaseButtons, 22));
188 buttonPanel.add(makeButton("Gap", "-", upperCaseButtons, 23));
190 if (!isCaseSensitive)
192 gridLayout.setRows(6);
193 gridLayout.setColumns(4);
197 gridLayout.setRows(7);
199 gridLayout.setColumns(cols + 1);
201 if (lowerCaseButtons == null)
203 lowerCaseButtons = new ArrayList<>();
206 for (int i = 0; i < 20; i++)
208 int row = i / cols + 1;
209 int index = (row * cols) + i;
210 JButton button = makeButton(
211 ResidueProperties.aa[i].toLowerCase(Locale.ROOT),
212 ResidueProperties.aa[i].toLowerCase(Locale.ROOT),
213 lowerCaseButtons, i);
215 buttonPanel.add(button, index);
221 buttonPanel.add(makeButton("b", "b", lowerCaseButtons, 20));
222 buttonPanel.add(makeButton("z", "z", lowerCaseButtons, 21));
223 buttonPanel.add(makeButton("x", "x", lowerCaseButtons, 22));
226 // JAL-1360 widen the frame dynamically to accommodate case-sensitive AA
228 if (this.frame != null)
230 int newWidth = isCaseSensitive ? MY_FRAME_WIDTH_CASE_SENSITIVE
232 this.frame.setSize(newWidth, this.frame.getHeight());
235 buttonPanel.validate();
240 * ChangeListener handler for when a colour is picked in the colour chooser.
241 * The action is to apply the colour to all selected buttons as their
242 * background colour. Foreground colour (text) is set to a lighter shade in
243 * order to highlight which buttons are selected. If 'Lower Case Colour' is
244 * active, then the colour is applied to all lower case buttons (as well as
245 * the Lower Case Colour button itself).
250 public void stateChanged(ChangeEvent evt)
252 JButton button = null;
253 final Color newColour = colorChooser.getColor();
254 if (lcaseColour.isSelected())
256 selectedButtons.clear();
257 for (int i = 0; i < lowerCaseButtons.size(); i++)
259 button = lowerCaseButtons.get(i);
260 button.setBackground(newColour);
261 button.setForeground(
262 ColorUtils.brighterThan(button.getBackground()));
265 for (int i = 0; i < selectedButtons.size(); i++)
267 button = selectedButtons.get(i);
268 button.setBackground(newColour);
269 button.setForeground(ColorUtils.brighterThan(newColour));
272 changedButNotSaved = true;
276 * Performs actions when a residue button is clicked. This manages the button
277 * selection set (highlighted by brighter foreground text).
279 * On select button(s) with Ctrl/click or Shift/click: set button foreground
280 * text to brighter than background.
282 * On unselect button(s) with Ctrl/click on selected, or click to release
283 * current selection: reset foreground text to darker than background.
285 * Simple click: clear selection (resetting foreground to darker); set clicked
286 * button foreground to brighter
288 * Finally, synchronize the colour chooser to the colour of the first button
289 * in the selected set.
293 public void colourButtonPressed(MouseEvent e)
295 JButton pressed = (JButton) e.getSource();
299 JButton start, end = (JButton) e.getSource();
300 if (selectedButtons.size() > 0)
302 start = selectedButtons.get(selectedButtons.size() - 1);
306 start = (JButton) e.getSource();
309 int startIndex = 0, endIndex = 0;
310 for (int b = 0; b < buttonPanel.getComponentCount(); b++)
312 if (buttonPanel.getComponent(b) == start)
316 if (buttonPanel.getComponent(b) == end)
322 if (startIndex > endIndex)
324 int temp = startIndex;
325 startIndex = endIndex;
329 for (int b = startIndex; b <= endIndex; b++)
331 JButton button = (JButton) buttonPanel.getComponent(b);
332 if (!selectedButtons.contains(button))
334 button.setForeground(
335 ColorUtils.brighterThan(button.getBackground()));
336 selectedButtons.add(button);
340 else if (!e.isControlDown())
342 for (int b = 0; b < selectedButtons.size(); b++)
344 JButton button = selectedButtons.get(b);
345 button.setForeground(ColorUtils.darkerThan(button.getBackground()));
347 selectedButtons.clear();
348 pressed.setForeground(
349 ColorUtils.brighterThan(pressed.getBackground()));
350 selectedButtons.add(pressed);
353 else if (e.isControlDown())
355 if (selectedButtons.contains(pressed))
357 pressed.setForeground(
358 ColorUtils.darkerThan(pressed.getBackground()));
359 selectedButtons.remove(pressed);
363 pressed.setForeground(
364 ColorUtils.brighterThan(pressed.getBackground()));
365 selectedButtons.add(pressed);
369 if (selectedButtons.size() > 0)
371 colorChooser.setColor((selectedButtons.get(0)).getBackground());
376 * A helper method to update or make a colour button, whose background colour
377 * is the associated colour, and text colour a darker shade of the same. If
378 * the button is already in the list, then its text and margins are updated,
379 * if not then it is created and added. This method supports toggling between
380 * case-sensitive and case-insensitive button panels. The case-sensitive
381 * version has abbreviated button text in order to fit in more buttons.
388 * the button's position in the list
390 JButton makeButton(String label, String residue, List<JButton> buttons,
393 final JButton button;
396 if (buttonIndex < buttons.size())
398 button = buttons.get(buttonIndex);
399 col = button.getBackground();
403 button = new JButton();
404 button.addMouseListener(new MouseAdapter()
407 public void mouseClicked(MouseEvent e)
409 colourButtonPressed(e);
416 * make initial button colour that of the current colour scheme,
417 * if it is a simple per-residue colouring, else white
420 if (oldColourScheme != null && oldColourScheme.isSimple())
422 col = oldColourScheme.findColour(residue.charAt(0), 0, null, null,
427 if (caseSensitive.isSelected())
429 button.setMargin(new Insets(2, 2, 2, 2));
433 button.setMargin(new Insets(2, 14, 2, 14));
436 button.setOpaque(true); // required for the next line to have effect
437 button.setBackground(col);
438 button.setText(label);
439 button.setForeground(ColorUtils.darkerThan(col));
440 button.setFont(VERDANA_BOLD_10);
446 * On 'OK', check that at least one colour has been assigned to a residue (and
447 * if not issue a warning), and apply the chosen colour scheme and close the
451 protected void okButton_actionPerformed()
453 if (isNoSelectionMade())
455 JvOptionPane.showMessageDialog(Desktop.getDesktopPane(),
457 .getString("label.no_colour_selection_in_scheme"),
458 MessageManager.getString("label.no_colour_selection_warn"),
459 JvOptionPane.WARNING_MESSAGE);
464 * OK is treated as 'apply colours and close'
466 applyButton_actionPerformed();
469 * If editing a named colour scheme, warn if changes
470 * have not been saved
472 warnIfUnsavedChanges();
476 frame.setClosed(true);
477 } catch (Exception ex)
484 * If we have made changes to an existing user defined colour scheme but not
485 * saved them, show a dialog with the option to save. If the user chooses to
486 * save, do so, else clear the colour scheme name to indicate a new colour
489 protected void warnIfUnsavedChanges()
491 // BH 2018 no warning in JavaScript TODO
493 if (!Platform.isJS() && changedButNotSaved)
500 String name = schemeName.getText().trim();
501 if (oldColourScheme != null && !"".equals(name)
502 && name.equals(oldColourScheme.getSchemeName()))
504 String message = MessageManager
505 .formatMessage("label.scheme_changed", name);
506 String title = MessageManager.getString("label.save_changes");
507 String[] options = new String[] { title,
508 MessageManager.getString("label.dont_save_changes"), };
509 final String question = JvSwingUtils.wrapTooltip(true, message);
510 int response = JvOptionPane.showOptionDialog(Desktop.getDesktopPane(),
511 question, title, JvOptionPane.DEFAULT_OPTION,
512 JvOptionPane.PLAIN_MESSAGE, null, options, options[0]);
517 * prompt to save changes to file; if done,
518 * resets 'changed' flag to false
520 savebutton_actionPerformed();
524 * if user chooses not to save (either in this dialog or in the
525 * save as dialogs), treat this as a new user defined colour scheme
527 if (changedButNotSaved)
530 * clear scheme name and re-apply as an anonymous scheme
532 schemeName.setText("");
533 applyButton_actionPerformed();
540 * Returns true if the user has not made any colour selection (including if
541 * 'case-sensitive' selected and no lower-case colour chosen).
545 protected boolean isNoSelectionMade()
547 final boolean noUpperCaseSelected = upperCaseButtons == null
548 || upperCaseButtons.isEmpty();
549 final boolean noLowerCaseSelected = caseSensitive.isSelected()
550 && (lowerCaseButtons == null || lowerCaseButtons.isEmpty());
551 final boolean noSelectionMade = noUpperCaseSelected
552 || noLowerCaseSelected;
553 return noSelectionMade;
557 * Applies the current colour scheme to the alignment or sequence group
560 protected void applyButton_actionPerformed()
562 if (isNoSelectionMade())
564 JvOptionPane.showMessageDialog(Desktop.getDesktopPane(),
566 .getString("label.no_colour_selection_in_scheme"),
567 MessageManager.getString("label.no_colour_selection_warn"),
568 JvOptionPane.WARNING_MESSAGE);
571 UserColourScheme ucs = getSchemeFromButtons();
573 ap.alignFrame.changeColour(ucs);
577 * Constructs an instance of UserColourScheme with the residue colours
578 * currently set on the buttons on the panel
582 UserColourScheme getSchemeFromButtons()
585 Color[] newColours = new Color[24];
587 int length = upperCaseButtons.size();
591 for (JButton btn : upperCaseButtons)
593 newColours[i] = btn.getBackground();
599 for (int i = 0; i < 24; i++)
601 JButton button = upperCaseButtons.get(i);
602 newColours[i] = button.getBackground();
606 UserColourScheme ucs = new UserColourScheme(newColours);
607 ucs.setName(schemeName.getText());
609 if (caseSensitive.isSelected())
611 newColours = new Color[23];
612 length = lowerCaseButtons.size();
616 for (JButton btn : lowerCaseButtons)
618 newColours[i] = btn.getBackground();
624 for (int i = 0; i < 23; i++)
626 JButton button = lowerCaseButtons.get(i);
627 newColours[i] = button.getBackground();
630 ucs.setLowerCaseColours(newColours);
637 * Action on clicking Load scheme button.
639 * <li>Open a file chooser to browse for files with extension .jc</li>
640 * <li>Load in the colour scheme and transfer it to this panel's buttons</li>
641 * <li>Register the loaded colour scheme</li>
645 protected void loadbutton_actionPerformed()
647 upperCaseButtons = new ArrayList<>();
648 lowerCaseButtons = new ArrayList<>();
649 JalviewFileChooser chooser = new JalviewFileChooser("jc",
650 "Jalview User Colours");
651 chooser.setFileView(new JalviewFileView());
652 chooser.setDialogTitle(
653 MessageManager.getString("label.load_colour_scheme"));
654 chooser.setToolTipText(MessageManager.getString("action.load"));
655 chooser.setResponseHandler(0, new Runnable()
660 File choice = chooser.getSelectedFile();
661 Cache.setProperty(LAST_DIRECTORY, choice.getParent());
663 UserColourScheme ucs = ColourSchemeLoader
664 .loadColourScheme(choice.getAbsolutePath());
665 Color[] colors = ucs.getColours();
666 schemeName.setText(ucs.getSchemeName());
668 if (ucs.getLowerCaseColours() != null)
670 caseSensitive.setSelected(true);
671 lcaseColour.setEnabled(true);
672 resetButtonPanel(true);
673 for (int i = 0; i < lowerCaseButtons.size(); i++)
675 JButton button = lowerCaseButtons.get(i);
676 button.setBackground(ucs.getLowerCaseColours()[i]);
681 caseSensitive.setSelected(false);
682 lcaseColour.setEnabled(false);
683 resetButtonPanel(false);
686 for (int i = 0; i < upperCaseButtons.size(); i++)
688 JButton button = upperCaseButtons.get(i);
689 button.setBackground(colors[i]);
692 addNewColourScheme(choice.getPath());
696 chooser.showOpenDialog(this);
700 * Loads the user-defined colour scheme from the first file listed in property
701 * "USER_DEFINED_COLOURS". If this fails, returns an all-white colour scheme.
705 public static UserColourScheme loadDefaultColours()
707 UserColourScheme ret = null;
709 String colours = Cache.getProperty(USER_DEFINED_COLOURS);
712 if (colours.indexOf("|") > -1)
714 colours = colours.substring(0, colours.indexOf("|"));
716 ret = ColourSchemeLoader.loadColourScheme(colours);
721 ret = new UserColourScheme("white");
728 * Action on pressing the Save button.
730 * <li>Check a name has been entered</li>
731 * <li>Warn if the name already exists, remove any existing scheme of the same
732 * name if overwriting</li>
733 * <li>Do the standard file chooser thing to write with extension .jc</li>
734 * <li>If saving changes (possibly not yet applied) to the currently selected
735 * colour scheme, then apply the changes, as it is too late to back out
737 * <li>Don't apply the changes if the currently selected scheme is different,
738 * to allow a new scheme to be configured and saved but not applied</li>
740 * If the scheme is saved to file, the 'changed' flag field is reset to false.
743 protected void savebutton_actionPerformed()
745 String name = schemeName.getText().trim();
746 if (name.length() < 1)
748 JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(),
750 .getString("label.user_colour_scheme_must_have_name"),
751 MessageManager.getString("label.no_name_colour_scheme"),
752 JvOptionPane.WARNING_MESSAGE);
755 if (!Platform.isJS() && ColourSchemes.getInstance().nameExists(name))
763 int reply = JvOptionPane.showInternalConfirmDialog(Desktop.getDesktopPane(),
764 MessageManager.formatMessage(
765 "label.colour_scheme_exists_overwrite", new Object[]
767 MessageManager.getString("label.duplicate_scheme_name"),
768 JvOptionPane.YES_NO_OPTION);
769 if (reply != JvOptionPane.YES_OPTION)
776 JalviewFileChooser chooser = new JalviewFileChooser("jc",
777 "Jalview User Colours");
779 JalviewFileView fileView = new JalviewFileView();
780 chooser.setFileView(fileView);
781 chooser.setDialogTitle(
782 MessageManager.getString("label.save_colour_scheme"));
783 chooser.setToolTipText(MessageManager.getString("action.save"));
784 int option = chooser.showSaveDialog(this);
785 if (option == JalviewFileChooser.APPROVE_OPTION)
787 File file = chooser.getSelectedFile();
788 UserColourScheme updatedScheme = addNewColourScheme(file.getPath());
790 changedButNotSaved = false;
793 * changes saved - apply to alignment if we are changing
794 * the currently selected colour scheme; also make the updated
795 * colours the 'backout' scheme on Cancel
797 if (oldColourScheme != null
798 && name.equals(oldColourScheme.getSchemeName()))
800 oldColourScheme = updatedScheme;
801 applyButton_actionPerformed();
807 * Adds the current colour scheme to the Jalview properties file so it is
808 * loaded on next startup, and updates the Colour menu in the parent
809 * AlignFrame (if there is one). Note this action does not including applying
815 protected UserColourScheme addNewColourScheme(String filePath)
818 * update the delimited list of user defined colour files in
819 * Jalview property USER_DEFINED_COLOURS
821 String defaultColours = Cache.getDefault(USER_DEFINED_COLOURS,
823 if (defaultColours.indexOf(filePath) == -1)
825 if (defaultColours.length() > 0)
827 defaultColours = defaultColours.concat("|");
829 defaultColours = defaultColours.concat(filePath);
831 Cache.setProperty(USER_DEFINED_COLOURS, defaultColours);
834 * construct and register the colour scheme
836 UserColourScheme ucs = getSchemeFromButtons();
837 ColourSchemes.getInstance().registerColourScheme(ucs);
840 * update the Colour menu items
844 ap.alignFrame.buildColourMenu();
851 * Saves the colour scheme to file in XML format
855 protected void saveToFile(File toFile)
858 * build a Java model of colour scheme as XML, and
861 JalviewUserColours ucs = new JalviewUserColours();
862 String name = schemeName.getText();
863 ucs.setSchemeName(name);
866 PrintWriter out = new PrintWriter(new OutputStreamWriter(
867 new FileOutputStream(toFile), "UTF-8"));
869 for (int i = 0; i < buttonPanel.getComponentCount(); i++)
871 JButton button = (JButton) buttonPanel.getComponent(i);
872 Colour col = new Colour();
873 col.setName(button.getText());
874 col.setRGB(Format.getHexString(button.getBackground()));
875 ucs.getColour().add(col);
877 JAXBContext jaxbContext = JAXBContext
878 .newInstance(JalviewUserColours.class);
879 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
880 jaxbMarshaller.marshal(
881 new ObjectFactory().createJalviewUserColours(ucs), out);
884 } catch (Exception ex)
886 ex.printStackTrace();
891 * On cancel, restores the colour scheme that was selected before the dialogue
895 protected void cancelButton_actionPerformed()
897 ap.alignFrame.changeColour(oldColourScheme);
898 ap.paintAlignment(true, true);
902 frame.setClosed(true);
903 } catch (Exception ex)
909 * Action on selecting or deselecting the Case Sensitive option. When
910 * selected, separate buttons are shown for lower case residues, and the panel
911 * is resized to accommodate them. Also, the checkbox for 'apply colour to all
912 * lower case' is enabled.
915 public void caseSensitive_actionPerformed()
917 boolean selected = caseSensitive.isSelected();
918 resetButtonPanel(selected);
919 lcaseColour.setEnabled(selected);