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 private static final String LAST_DIRECTORY = "LAST_DIRECTORY";
74 private static final int MY_FRAME_HEIGHT = 440;
76 private static final int MY_FRAME_WIDTH = 810;
78 private static final int MY_FRAME_WIDTH_CASE_SENSITIVE = 970;
83 * the colour scheme when the dialog was opened, or
84 * the scheme last saved to file
86 ColourSchemeI oldColourScheme;
89 * flag is true if the colour scheme has been changed since the
90 * dialog was opened, or the changes last saved to file
92 boolean changedButNotSaved;
96 List<JButton> upperCaseButtons;
98 List<JButton> lowerCaseButtons;
101 * Creates and displays a new UserDefinedColours panel
105 public UserDefinedColours(AlignmentPanel alignPanel)
109 lcaseColour.setEnabled(false);
111 this.ap = alignPanel;
113 oldColourScheme = alignPanel.av.getGlobalColourScheme();
115 if (oldColourScheme instanceof UserColourScheme)
117 schemeName.setText(oldColourScheme.getSchemeName());
118 if (((UserColourScheme) oldColourScheme)
119 .getLowerCaseColours() != null)
121 caseSensitive.setSelected(true);
122 lcaseColour.setEnabled(true);
123 resetButtonPanel(true);
127 resetButtonPanel(false);
132 resetButtonPanel(false);
141 selectedButtons = new ArrayList<>();
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);
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.
160 * @param isCaseSensitive
162 void resetButtonPanel(boolean isCaseSensitive)
164 buttonPanel.removeAll();
166 if (upperCaseButtons == null)
168 upperCaseButtons = new ArrayList<>();
171 for (int i = 0; i < 20; i++)
173 String label = isCaseSensitive ? ResidueProperties.aa[i]
174 : ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i])
176 JButton button = makeButton(label, ResidueProperties.aa[i],
177 upperCaseButtons, i);
178 buttonPanel.add(button);
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));
186 if (!isCaseSensitive)
188 gridLayout.setRows(6);
189 gridLayout.setColumns(4);
193 gridLayout.setRows(7);
195 gridLayout.setColumns(cols + 1);
197 if (lowerCaseButtons == null)
199 lowerCaseButtons = new ArrayList<>();
202 for (int i = 0; i < 20; i++)
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);
209 buttonPanel.add(button, index);
215 buttonPanel.add(makeButton("b", "b", lowerCaseButtons, 20));
216 buttonPanel.add(makeButton("z", "z", lowerCaseButtons, 21));
217 buttonPanel.add(makeButton("x", "x", lowerCaseButtons, 22));
220 // JAL-1360 widen the frame dynamically to accommodate case-sensitive AA
222 if (this.frame != null)
224 int newWidth = isCaseSensitive ? MY_FRAME_WIDTH_CASE_SENSITIVE
226 this.frame.setSize(newWidth, this.frame.getHeight());
229 buttonPanel.validate();
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).
244 public void stateChanged(ChangeEvent evt)
246 JButton button = null;
247 final Color newColour = colorChooser.getColor();
248 if (lcaseColour.isSelected())
250 selectedButtons.clear();
251 for (int i = 0; i < lowerCaseButtons.size(); i++)
253 button = lowerCaseButtons.get(i);
254 button.setBackground(newColour);
255 button.setForeground(
256 ColorUtils.brighterThan(button.getBackground()));
259 for (int i = 0; i < selectedButtons.size(); i++)
261 button = selectedButtons.get(i);
262 button.setBackground(newColour);
263 button.setForeground(ColorUtils.brighterThan(newColour));
266 changedButNotSaved = true;
270 * Performs actions when a residue button is clicked. This manages the button
271 * selection set (highlighted by brighter foreground text).
273 * On select button(s) with Ctrl/click or Shift/click: set button foreground
274 * text to brighter than background.
276 * On unselect button(s) with Ctrl/click on selected, or click to release
277 * current selection: reset foreground text to darker than background.
279 * Simple click: clear selection (resetting foreground to darker); set clicked
280 * button foreground to brighter
282 * Finally, synchronize the colour chooser to the colour of the first button
283 * in the selected set.
287 public void colourButtonPressed(MouseEvent e)
289 JButton pressed = (JButton) e.getSource();
293 JButton start, end = (JButton) e.getSource();
294 if (selectedButtons.size() > 0)
296 start = selectedButtons.get(selectedButtons.size() - 1);
300 start = (JButton) e.getSource();
303 int startIndex = 0, endIndex = 0;
304 for (int b = 0; b < buttonPanel.getComponentCount(); b++)
306 if (buttonPanel.getComponent(b) == start)
310 if (buttonPanel.getComponent(b) == end)
316 if (startIndex > endIndex)
318 int temp = startIndex;
319 startIndex = endIndex;
323 for (int b = startIndex; b <= endIndex; b++)
325 JButton button = (JButton) buttonPanel.getComponent(b);
326 if (!selectedButtons.contains(button))
328 button.setForeground(
329 ColorUtils.brighterThan(button.getBackground()));
330 selectedButtons.add(button);
334 else if (!e.isControlDown())
336 for (int b = 0; b < selectedButtons.size(); b++)
338 JButton button = selectedButtons.get(b);
339 button.setForeground(ColorUtils.darkerThan(button.getBackground()));
341 selectedButtons.clear();
342 pressed.setForeground(
343 ColorUtils.brighterThan(pressed.getBackground()));
344 selectedButtons.add(pressed);
347 else if (e.isControlDown())
349 if (selectedButtons.contains(pressed))
351 pressed.setForeground(
352 ColorUtils.darkerThan(pressed.getBackground()));
353 selectedButtons.remove(pressed);
357 pressed.setForeground(
358 ColorUtils.brighterThan(pressed.getBackground()));
359 selectedButtons.add(pressed);
363 if (selectedButtons.size() > 0)
365 colorChooser.setColor((selectedButtons.get(0)).getBackground());
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.
382 * the button's position in the list
384 JButton makeButton(String label, String residue, List<JButton> buttons,
387 final JButton button;
390 if (buttonIndex < buttons.size())
392 button = buttons.get(buttonIndex);
393 col = button.getBackground();
397 button = new JButton();
398 button.addMouseListener(new MouseAdapter()
401 public void mouseClicked(MouseEvent e)
403 colourButtonPressed(e);
410 * make initial button colour that of the current colour scheme,
411 * if it is a simple per-residue colouring, else white
414 if (oldColourScheme != null && oldColourScheme.isSimple())
416 col = oldColourScheme.findColour(residue.charAt(0), 0, null, null,
421 if (caseSensitive.isSelected())
423 button.setMargin(new Insets(2, 2, 2, 2));
427 button.setMargin(new Insets(2, 14, 2, 14));
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);
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
445 protected void okButton_actionPerformed()
447 if (isNoSelectionMade())
449 JvOptionPane.showMessageDialog(Desktop.getDesktopPane(),
451 .getString("label.no_colour_selection_in_scheme"),
452 MessageManager.getString("label.no_colour_selection_warn"),
453 JvOptionPane.WARNING_MESSAGE);
458 * OK is treated as 'apply colours and close'
460 applyButton_actionPerformed();
463 * If editing a named colour scheme, warn if changes
464 * have not been saved
466 warnIfUnsavedChanges();
470 frame.setClosed(true);
471 } catch (Exception ex)
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
483 protected void warnIfUnsavedChanges()
485 // BH 2018 no warning in JavaScript TODO
487 if (!Platform.isJS() && changedButNotSaved)
494 String name = schemeName.getText().trim();
495 if (oldColourScheme != null && !"".equals(name)
496 && name.equals(oldColourScheme.getSchemeName()))
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]);
511 * prompt to save changes to file; if done,
512 * resets 'changed' flag to false
514 savebutton_actionPerformed();
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
521 if (changedButNotSaved)
524 * clear scheme name and re-apply as an anonymous scheme
526 schemeName.setText("");
527 applyButton_actionPerformed();
534 * Returns true if the user has not made any colour selection (including if
535 * 'case-sensitive' selected and no lower-case colour chosen).
539 protected boolean isNoSelectionMade()
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;
551 * Applies the current colour scheme to the alignment or sequence group
554 protected void applyButton_actionPerformed()
556 if (isNoSelectionMade())
558 JvOptionPane.showMessageDialog(Desktop.getDesktopPane(),
560 .getString("label.no_colour_selection_in_scheme"),
561 MessageManager.getString("label.no_colour_selection_warn"),
562 JvOptionPane.WARNING_MESSAGE);
565 UserColourScheme ucs = getSchemeFromButtons();
567 ap.alignFrame.changeColour(ucs);
571 * Constructs an instance of UserColourScheme with the residue colours
572 * currently set on the buttons on the panel
576 UserColourScheme getSchemeFromButtons()
579 Color[] newColours = new Color[24];
581 int length = upperCaseButtons.size();
585 for (JButton btn : upperCaseButtons)
587 newColours[i] = btn.getBackground();
593 for (int i = 0; i < 24; i++)
595 JButton button = upperCaseButtons.get(i);
596 newColours[i] = button.getBackground();
600 UserColourScheme ucs = new UserColourScheme(newColours);
601 ucs.setName(schemeName.getText());
603 if (caseSensitive.isSelected())
605 newColours = new Color[23];
606 length = lowerCaseButtons.size();
610 for (JButton btn : lowerCaseButtons)
612 newColours[i] = btn.getBackground();
618 for (int i = 0; i < 23; i++)
620 JButton button = lowerCaseButtons.get(i);
621 newColours[i] = button.getBackground();
624 ucs.setLowerCaseColours(newColours);
631 * Action on clicking Load scheme button.
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>
639 protected void loadbutton_actionPerformed()
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()
654 File choice = chooser.getSelectedFile();
655 Cache.setProperty(LAST_DIRECTORY, choice.getParent());
657 UserColourScheme ucs = ColourSchemeLoader
658 .loadColourScheme(choice.getAbsolutePath());
659 Color[] colors = ucs.getColours();
660 schemeName.setText(ucs.getSchemeName());
662 if (ucs.getLowerCaseColours() != null)
664 caseSensitive.setSelected(true);
665 lcaseColour.setEnabled(true);
666 resetButtonPanel(true);
667 for (int i = 0; i < lowerCaseButtons.size(); i++)
669 JButton button = lowerCaseButtons.get(i);
670 button.setBackground(ucs.getLowerCaseColours()[i]);
675 caseSensitive.setSelected(false);
676 lcaseColour.setEnabled(false);
677 resetButtonPanel(false);
680 for (int i = 0; i < upperCaseButtons.size(); i++)
682 JButton button = upperCaseButtons.get(i);
683 button.setBackground(colors[i]);
686 addNewColourScheme(choice.getPath());
690 chooser.showOpenDialog(this);
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.
699 public static UserColourScheme loadDefaultColours()
701 UserColourScheme ret = null;
703 String colours = Cache.getProperty(Preferences.USER_DEFINED_COLOURS);
706 if (colours.indexOf("|") > -1)
708 colours = colours.substring(0, colours.indexOf("|"));
710 ret = ColourSchemeLoader.loadColourScheme(colours);
715 ret = new UserColourScheme("white");
722 * Action on pressing the Save button.
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
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>
734 * If the scheme is saved to file, the 'changed' flag field is reset to false.
737 protected void savebutton_actionPerformed()
739 String name = schemeName.getText().trim();
740 if (name.length() < 1)
742 JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(),
744 .getString("label.user_colour_scheme_must_have_name"),
745 MessageManager.getString("label.no_name_colour_scheme"),
746 JvOptionPane.WARNING_MESSAGE);
749 if (!Platform.isJS() && ColourSchemes.getInstance().nameExists(name))
757 int reply = JvOptionPane.showInternalConfirmDialog(Desktop.getDesktopPane(),
758 MessageManager.formatMessage(
759 "label.colour_scheme_exists_overwrite", new Object[]
761 MessageManager.getString("label.duplicate_scheme_name"),
762 JvOptionPane.YES_NO_OPTION);
763 if (reply != JvOptionPane.YES_OPTION)
770 JalviewFileChooser chooser = new JalviewFileChooser("jc",
771 "Jalview User Colours");
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)
781 File file = chooser.getSelectedFile();
782 UserColourScheme updatedScheme = addNewColourScheme(file.getPath());
784 changedButNotSaved = false;
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
791 if (oldColourScheme != null
792 && name.equals(oldColourScheme.getSchemeName()))
794 oldColourScheme = updatedScheme;
795 applyButton_actionPerformed();
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
809 protected UserColourScheme addNewColourScheme(String filePath)
812 * update the delimited list of user defined colour files in
813 * Jalview property USER_DEFINED_COLOURS
815 String defaultColours = Cache.getDefault(
816 Preferences.USER_DEFINED_COLOURS,
818 if (defaultColours.indexOf(filePath) == -1)
820 if (defaultColours.length() > 0)
822 defaultColours = defaultColours.concat("|");
824 defaultColours = defaultColours.concat(filePath);
826 Cache.setProperty(Preferences.USER_DEFINED_COLOURS, defaultColours);
829 * construct and register the colour scheme
831 UserColourScheme ucs = getSchemeFromButtons();
832 ColourSchemes.getInstance().registerColourScheme(ucs);
835 * update the Colour menu items
839 ap.alignFrame.buildColourMenu();
846 * Saves the colour scheme to file in XML format
850 protected void saveToFile(File toFile)
853 * build a Java model of colour scheme as XML, and
856 JalviewUserColours ucs = new JalviewUserColours();
857 String name = schemeName.getText();
858 ucs.setSchemeName(name);
861 PrintWriter out = new PrintWriter(new OutputStreamWriter(
862 new FileOutputStream(toFile), "UTF-8"));
864 for (int i = 0; i < buttonPanel.getComponentCount(); i++)
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);
872 JAXBContext jaxbContext = JAXBContext
873 .newInstance(JalviewUserColours.class);
874 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
875 jaxbMarshaller.marshal(
876 new ObjectFactory().createJalviewUserColours(ucs), out);
879 } catch (Exception ex)
881 ex.printStackTrace();
886 * On cancel, restores the colour scheme that was selected before the dialogue
890 protected void cancelButton_actionPerformed()
892 ap.alignFrame.changeColour(oldColourScheme);
893 ap.paintAlignment(true, true);
897 frame.setClosed(true);
898 } catch (Exception ex)
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.
910 public void caseSensitive_actionPerformed()
912 boolean selected = caseSensitive.isSelected();
913 resetButtonPanel(selected);
914 lcaseColour.setEnabled(selected);