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"),
155 MY_FRAME_WIDTH, MY_FRAME_HEIGHT, true);
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(ResidueProperties.aa[i].toLowerCase(Locale.ROOT),
211 ResidueProperties.aa[i].toLowerCase(Locale.ROOT), lowerCaseButtons, i);
213 buttonPanel.add(button, index);
219 buttonPanel.add(makeButton("b", "b", lowerCaseButtons, 20));
220 buttonPanel.add(makeButton("z", "z", lowerCaseButtons, 21));
221 buttonPanel.add(makeButton("x", "x", lowerCaseButtons, 22));
224 // JAL-1360 widen the frame dynamically to accommodate case-sensitive AA
226 if (this.frame != null)
228 int newWidth = isCaseSensitive ? MY_FRAME_WIDTH_CASE_SENSITIVE
230 this.frame.setSize(newWidth, this.frame.getHeight());
233 buttonPanel.validate();
238 * ChangeListener handler for when a colour is picked in the colour chooser.
239 * The action is to apply the colour to all selected buttons as their
240 * background colour. Foreground colour (text) is set to a lighter shade in
241 * order to highlight which buttons are selected. If 'Lower Case Colour' is
242 * active, then the colour is applied to all lower case buttons (as well as
243 * the Lower Case Colour button itself).
248 public void stateChanged(ChangeEvent evt)
250 JButton button = null;
251 final Color newColour = colorChooser.getColor();
252 if (lcaseColour.isSelected())
254 selectedButtons.clear();
255 for (int i = 0; i < lowerCaseButtons.size(); i++)
257 button = lowerCaseButtons.get(i);
258 button.setBackground(newColour);
259 button.setForeground(
260 ColorUtils.brighterThan(button.getBackground()));
263 for (int i = 0; i < selectedButtons.size(); i++)
265 button = selectedButtons.get(i);
266 button.setBackground(newColour);
267 button.setForeground(ColorUtils.brighterThan(newColour));
270 changedButNotSaved = true;
274 * Performs actions when a residue button is clicked. This manages the button
275 * selection set (highlighted by brighter foreground text).
277 * On select button(s) with Ctrl/click or Shift/click: set button foreground
278 * text to brighter than background.
280 * On unselect button(s) with Ctrl/click on selected, or click to release
281 * current selection: reset foreground text to darker than background.
283 * Simple click: clear selection (resetting foreground to darker); set clicked
284 * button foreground to brighter
286 * Finally, synchronize the colour chooser to the colour of the first button
287 * in the selected set.
291 public void colourButtonPressed(MouseEvent e)
293 JButton pressed = (JButton) e.getSource();
297 JButton start, end = (JButton) e.getSource();
298 if (selectedButtons.size() > 0)
300 start = selectedButtons.get(selectedButtons.size() - 1);
304 start = (JButton) e.getSource();
307 int startIndex = 0, endIndex = 0;
308 for (int b = 0; b < buttonPanel.getComponentCount(); b++)
310 if (buttonPanel.getComponent(b) == start)
314 if (buttonPanel.getComponent(b) == end)
320 if (startIndex > endIndex)
322 int temp = startIndex;
323 startIndex = endIndex;
327 for (int b = startIndex; b <= endIndex; b++)
329 JButton button = (JButton) buttonPanel.getComponent(b);
330 if (!selectedButtons.contains(button))
332 button.setForeground(
333 ColorUtils.brighterThan(button.getBackground()));
334 selectedButtons.add(button);
338 else if (!e.isControlDown())
340 for (int b = 0; b < selectedButtons.size(); b++)
342 JButton button = selectedButtons.get(b);
343 button.setForeground(ColorUtils.darkerThan(button.getBackground()));
345 selectedButtons.clear();
346 pressed.setForeground(
347 ColorUtils.brighterThan(pressed.getBackground()));
348 selectedButtons.add(pressed);
351 else if (e.isControlDown())
353 if (selectedButtons.contains(pressed))
355 pressed.setForeground(
356 ColorUtils.darkerThan(pressed.getBackground()));
357 selectedButtons.remove(pressed);
361 pressed.setForeground(
362 ColorUtils.brighterThan(pressed.getBackground()));
363 selectedButtons.add(pressed);
367 if (selectedButtons.size() > 0)
369 colorChooser.setColor((selectedButtons.get(0)).getBackground());
374 * A helper method to update or make a colour button, whose background colour
375 * is the associated colour, and text colour a darker shade of the same. If
376 * the button is already in the list, then its text and margins are updated,
377 * if not then it is created and added. This method supports toggling between
378 * case-sensitive and case-insensitive button panels. The case-sensitive
379 * version has abbreviated button text in order to fit in more buttons.
386 * the button's position in the list
388 JButton makeButton(String label, String residue, List<JButton> buttons,
391 final JButton button;
394 if (buttonIndex < buttons.size())
396 button = buttons.get(buttonIndex);
397 col = button.getBackground();
401 button = new JButton();
402 button.addMouseListener(new MouseAdapter()
405 public void mouseClicked(MouseEvent e)
407 colourButtonPressed(e);
414 * make initial button colour that of the current colour scheme,
415 * if it is a simple per-residue colouring, else white
418 if (oldColourScheme != null && oldColourScheme.isSimple())
420 col = oldColourScheme.findColour(residue.charAt(0), 0, null, null,
425 if (caseSensitive.isSelected())
427 button.setMargin(new Insets(2, 2, 2, 2));
431 button.setMargin(new Insets(2, 14, 2, 14));
434 button.setOpaque(true); // required for the next line to have effect
435 button.setBackground(col);
436 button.setText(label);
437 button.setForeground(ColorUtils.darkerThan(col));
438 button.setFont(VERDANA_BOLD_10);
444 * On 'OK', check that at least one colour has been assigned to a residue (and
445 * if not issue a warning), and apply the chosen colour scheme and close the
449 protected void okButton_actionPerformed()
451 if (isNoSelectionMade())
453 JvOptionPane.showMessageDialog(Desktop.desktop,
455 .getString("label.no_colour_selection_in_scheme"),
456 MessageManager.getString("label.no_colour_selection_warn"),
457 JvOptionPane.WARNING_MESSAGE);
462 * OK is treated as 'apply colours and close'
464 applyButton_actionPerformed();
467 * If editing a named colour scheme, warn if changes
468 * have not been saved
470 warnIfUnsavedChanges();
474 frame.setClosed(true);
475 } catch (Exception ex)
482 * If we have made changes to an existing user defined colour scheme but not
483 * saved them, show a dialog with the option to save. If the user chooses to
484 * save, do so, else clear the colour scheme name to indicate a new colour
487 protected void warnIfUnsavedChanges()
489 // BH 2018 no warning in JavaScript TODO
491 if (!Platform.isJS() && changedButNotSaved)
498 String name = schemeName.getText().trim();
499 if (oldColourScheme != null && !"".equals(name)
500 && name.equals(oldColourScheme.getSchemeName()))
502 String message = MessageManager
503 .formatMessage("label.scheme_changed", name);
504 String title = MessageManager.getString("label.save_changes");
505 String[] options = new String[] { title,
506 MessageManager.getString("label.dont_save_changes"), };
507 final String question = JvSwingUtils.wrapTooltip(true, message);
508 int response = JvOptionPane.showOptionDialog(Desktop.desktop,
509 question, title, JvOptionPane.DEFAULT_OPTION,
510 JvOptionPane.PLAIN_MESSAGE, null, options, options[0]);
515 * prompt to save changes to file; if done,
516 * resets 'changed' flag to false
518 savebutton_actionPerformed();
522 * if user chooses not to save (either in this dialog or in the
523 * save as dialogs), treat this as a new user defined colour scheme
525 if (changedButNotSaved)
528 * clear scheme name and re-apply as an anonymous scheme
530 schemeName.setText("");
531 applyButton_actionPerformed();
538 * Returns true if the user has not made any colour selection (including if
539 * 'case-sensitive' selected and no lower-case colour chosen).
543 protected boolean isNoSelectionMade()
545 final boolean noUpperCaseSelected = upperCaseButtons == null
546 || upperCaseButtons.isEmpty();
547 final boolean noLowerCaseSelected = caseSensitive.isSelected()
548 && (lowerCaseButtons == null || lowerCaseButtons.isEmpty());
549 final boolean noSelectionMade = noUpperCaseSelected
550 || noLowerCaseSelected;
551 return noSelectionMade;
555 * Applies the current colour scheme to the alignment or sequence group
558 protected void applyButton_actionPerformed()
560 if (isNoSelectionMade())
562 JvOptionPane.showMessageDialog(Desktop.desktop,
564 .getString("label.no_colour_selection_in_scheme"),
565 MessageManager.getString("label.no_colour_selection_warn"),
566 JvOptionPane.WARNING_MESSAGE);
569 UserColourScheme ucs = getSchemeFromButtons();
571 ap.alignFrame.changeColour(ucs);
575 * Constructs an instance of UserColourScheme with the residue colours
576 * currently set on the buttons on the panel
580 UserColourScheme getSchemeFromButtons()
583 Color[] newColours = new Color[24];
585 int length = upperCaseButtons.size();
589 for (JButton btn : upperCaseButtons)
591 newColours[i] = btn.getBackground();
597 for (int i = 0; i < 24; i++)
599 JButton button = upperCaseButtons.get(i);
600 newColours[i] = button.getBackground();
604 UserColourScheme ucs = new UserColourScheme(newColours);
605 ucs.setName(schemeName.getText());
607 if (caseSensitive.isSelected())
609 newColours = new Color[23];
610 length = lowerCaseButtons.size();
614 for (JButton btn : lowerCaseButtons)
616 newColours[i] = btn.getBackground();
622 for (int i = 0; i < 23; i++)
624 JButton button = lowerCaseButtons.get(i);
625 newColours[i] = button.getBackground();
628 ucs.setLowerCaseColours(newColours);
635 * Action on clicking Load scheme button.
637 * <li>Open a file chooser to browse for files with extension .jc</li>
638 * <li>Load in the colour scheme and transfer it to this panel's buttons</li>
639 * <li>Register the loaded colour scheme</li>
643 protected void loadbutton_actionPerformed()
645 upperCaseButtons = new ArrayList<>();
646 lowerCaseButtons = new ArrayList<>();
647 JalviewFileChooser chooser = new JalviewFileChooser("jc",
648 "Jalview User Colours");
649 chooser.setFileView(new JalviewFileView());
650 chooser.setDialogTitle(
651 MessageManager.getString("label.load_colour_scheme"));
652 chooser.setToolTipText(MessageManager.getString("action.load"));
653 chooser.setResponseHandler(0, new Runnable()
658 File choice = chooser.getSelectedFile();
659 Cache.setProperty(LAST_DIRECTORY, choice.getParent());
661 UserColourScheme ucs = ColourSchemeLoader
662 .loadColourScheme(choice.getAbsolutePath());
663 Color[] colors = ucs.getColours();
664 schemeName.setText(ucs.getSchemeName());
666 if (ucs.getLowerCaseColours() != null)
668 caseSensitive.setSelected(true);
669 lcaseColour.setEnabled(true);
670 resetButtonPanel(true);
671 for (int i = 0; i < lowerCaseButtons.size(); i++)
673 JButton button = lowerCaseButtons.get(i);
674 button.setBackground(ucs.getLowerCaseColours()[i]);
679 caseSensitive.setSelected(false);
680 lcaseColour.setEnabled(false);
681 resetButtonPanel(false);
684 for (int i = 0; i < upperCaseButtons.size(); i++)
686 JButton button = upperCaseButtons.get(i);
687 button.setBackground(colors[i]);
690 addNewColourScheme(choice.getPath());
694 chooser.showOpenDialog(this);
698 * Loads the user-defined colour scheme from the first file listed in property
699 * "USER_DEFINED_COLOURS". If this fails, returns an all-white colour scheme.
703 public static UserColourScheme loadDefaultColours()
705 UserColourScheme ret = null;
707 String colours = Cache.getProperty(USER_DEFINED_COLOURS);
710 if (colours.indexOf("|") > -1)
712 colours = colours.substring(0, colours.indexOf("|"));
714 ret = ColourSchemeLoader.loadColourScheme(colours);
719 ret = new UserColourScheme("white");
726 * Action on pressing the Save button.
728 * <li>Check a name has been entered</li>
729 * <li>Warn if the name already exists, remove any existing scheme of the same
730 * name if overwriting</li>
731 * <li>Do the standard file chooser thing to write with extension .jc</li>
732 * <li>If saving changes (possibly not yet applied) to the currently selected
733 * colour scheme, then apply the changes, as it is too late to back out
735 * <li>Don't apply the changes if the currently selected scheme is different,
736 * to allow a new scheme to be configured and saved but not applied</li>
738 * If the scheme is saved to file, the 'changed' flag field is reset to false.
741 protected void savebutton_actionPerformed()
743 String name = schemeName.getText().trim();
744 if (name.length() < 1)
746 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
748 .getString("label.user_colour_scheme_must_have_name"),
749 MessageManager.getString("label.no_name_colour_scheme"),
750 JvOptionPane.WARNING_MESSAGE);
753 if (!Platform.isJS() && ColourSchemes.getInstance().nameExists(name))
761 int reply = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
762 MessageManager.formatMessage(
763 "label.colour_scheme_exists_overwrite", new Object[]
765 MessageManager.getString("label.duplicate_scheme_name"),
766 JvOptionPane.YES_NO_OPTION);
767 if (reply != JvOptionPane.YES_OPTION)
774 JalviewFileChooser chooser = new JalviewFileChooser("jc",
775 "Jalview User Colours");
777 JalviewFileView fileView = new JalviewFileView();
778 chooser.setFileView(fileView);
779 chooser.setDialogTitle(
780 MessageManager.getString("label.save_colour_scheme"));
781 chooser.setToolTipText(MessageManager.getString("action.save"));
782 int option = chooser.showSaveDialog(this);
783 if (option == JalviewFileChooser.APPROVE_OPTION)
785 File file = chooser.getSelectedFile();
786 UserColourScheme updatedScheme = addNewColourScheme(file.getPath());
788 changedButNotSaved = false;
791 * changes saved - apply to alignment if we are changing
792 * the currently selected colour scheme; also make the updated
793 * colours the 'backout' scheme on Cancel
795 if (oldColourScheme != null
796 && name.equals(oldColourScheme.getSchemeName()))
798 oldColourScheme = updatedScheme;
799 applyButton_actionPerformed();
805 * Adds the current colour scheme to the Jalview properties file so it is
806 * loaded on next startup, and updates the Colour menu in the parent
807 * AlignFrame (if there is one). Note this action does not including applying
813 protected UserColourScheme addNewColourScheme(String filePath)
816 * update the delimited list of user defined colour files in
817 * Jalview property USER_DEFINED_COLOURS
819 String defaultColours = Cache.getDefault(USER_DEFINED_COLOURS,
821 if (defaultColours.indexOf(filePath) == -1)
823 if (defaultColours.length() > 0)
825 defaultColours = defaultColours.concat("|");
827 defaultColours = defaultColours.concat(filePath);
829 Cache.setProperty(USER_DEFINED_COLOURS, defaultColours);
832 * construct and register the colour scheme
834 UserColourScheme ucs = getSchemeFromButtons();
835 ColourSchemes.getInstance().registerColourScheme(ucs);
838 * update the Colour menu items
842 ap.alignFrame.buildColourMenu();
849 * Saves the colour scheme to file in XML format
853 protected void saveToFile(File toFile)
856 * build a Java model of colour scheme as XML, and
859 JalviewUserColours ucs = new JalviewUserColours();
860 String name = schemeName.getText();
861 ucs.setSchemeName(name);
864 PrintWriter out = new PrintWriter(new OutputStreamWriter(
865 new FileOutputStream(toFile), "UTF-8"));
867 for (int i = 0; i < buttonPanel.getComponentCount(); i++)
869 JButton button = (JButton) buttonPanel.getComponent(i);
870 Colour col = new Colour();
871 col.setName(button.getText());
872 col.setRGB(Format.getHexString(button.getBackground()));
873 ucs.getColour().add(col);
875 JAXBContext jaxbContext = JAXBContext
876 .newInstance(JalviewUserColours.class);
877 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
878 jaxbMarshaller.marshal(
879 new ObjectFactory().createJalviewUserColours(ucs), out);
882 } catch (Exception ex)
884 ex.printStackTrace();
889 * On cancel, restores the colour scheme that was selected before the dialogue
893 protected void cancelButton_actionPerformed()
895 ap.alignFrame.changeColour(oldColourScheme);
896 ap.paintAlignment(true, true);
900 frame.setClosed(true);
901 } catch (Exception ex)
907 * Action on selecting or deselecting the Case Sensitive option. When
908 * selected, separate buttons are shown for lower case residues, and the panel
909 * is resized to accommodate them. Also, the checkbox for 'apply colour to all
910 * lower case' is enabled.
913 public void caseSensitive_actionPerformed()
915 boolean selected = caseSensitive.isSelected();
916 resetButtonPanel(selected);
917 lcaseColour.setEnabled(selected);