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.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;
38 import java.awt.Color;
40 import java.awt.Insets;
41 import java.awt.event.MouseAdapter;
42 import java.awt.event.MouseEvent;
44 import java.io.FileOutputStream;
45 import java.io.OutputStreamWriter;
46 import java.io.PrintWriter;
47 import java.util.ArrayList;
48 import java.util.List;
50 import javax.swing.JButton;
51 import javax.swing.JInternalFrame;
52 import javax.swing.event.ChangeEvent;
53 import javax.swing.event.ChangeListener;
56 * This panel allows the user to assign colours to Amino Acid residue codes, and
57 * save the colour scheme.
59 * @author Andrew Waterhouse
60 * @author Mungo Carstairs
62 public class UserDefinedColours extends GUserDefinedColours implements
65 private static final Font VERDANA_BOLD_10 = new Font("Verdana",
68 public static final String USER_DEFINED_COLOURS = "USER_DEFINED_COLOURS";
70 private static final String LAST_DIRECTORY = "LAST_DIRECTORY";
72 private static final int MY_FRAME_HEIGHT = 440;
74 private static final int MY_FRAME_WIDTH = 810;
76 private static final int MY_FRAME_WIDTH_CASE_SENSITIVE = 970;
81 * the colour scheme when the dialog was opened, or
82 * the scheme last saved to file
84 ColourSchemeI oldColourScheme;
87 * flag is true if the colour scheme has been changed since the
88 * dialog was opened, or the changes last saved to file
94 List<JButton> upperCaseButtons;
96 List<JButton> lowerCaseButtons;
99 * Creates and displays a new UserDefinedColours panel
103 public UserDefinedColours(AlignmentPanel alignPanel)
107 lcaseColour.setEnabled(false);
109 this.ap = alignPanel;
111 oldColourScheme = alignPanel.av.getGlobalColourScheme();
113 if (oldColourScheme instanceof UserColourScheme)
115 schemeName.setText(oldColourScheme.getSchemeName());
116 if (((UserColourScheme) oldColourScheme).getLowerCaseColours() != null)
118 caseSensitive.setSelected(true);
119 lcaseColour.setEnabled(true);
120 resetButtonPanel(true);
124 resetButtonPanel(false);
129 resetButtonPanel(false);
138 selectedButtons = new ArrayList<JButton>();
143 colorChooser.getSelectionModel().addChangeListener(this);
144 frame = new JInternalFrame();
145 frame.setContentPane(this);
146 Desktop.addInternalFrame(frame,
147 MessageManager.getString("label.user_defined_colours"),
148 MY_FRAME_WIDTH, MY_FRAME_HEIGHT, true);
152 * Rebuilds the panel with coloured buttons for residues. If not case
153 * sensitive colours, show 3-letter amino acid code as button text. If case
154 * sensitive, just show the single letter code, in order to make space for the
155 * additional buttons.
157 * @param isCaseSensitive
159 void resetButtonPanel(boolean isCaseSensitive)
161 buttonPanel.removeAll();
163 if (upperCaseButtons == null)
165 upperCaseButtons = new ArrayList<JButton>();
168 for (int i = 0; i < 20; i++)
170 String label = isCaseSensitive ? ResidueProperties.aa[i]
171 : ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i])
173 JButton button = makeButton(label, ResidueProperties.aa[i],
174 upperCaseButtons, i);
175 buttonPanel.add(button);
178 buttonPanel.add(makeButton("B", "B", upperCaseButtons, 20));
179 buttonPanel.add(makeButton("Z", "Z", upperCaseButtons, 21));
180 buttonPanel.add(makeButton("X", "X", upperCaseButtons, 22));
181 buttonPanel.add(makeButton("Gap", "-", upperCaseButtons, 23));
183 if (!isCaseSensitive)
185 gridLayout.setRows(6);
186 gridLayout.setColumns(4);
190 gridLayout.setRows(7);
192 gridLayout.setColumns(cols + 1);
194 if (lowerCaseButtons == null)
196 lowerCaseButtons = new ArrayList<JButton>();
199 for (int i = 0; i < 20; i++)
201 int row = i / cols + 1;
202 int index = (row * cols) + i;
203 JButton button = makeButton(ResidueProperties.aa[i].toLowerCase(),
204 ResidueProperties.aa[i].toLowerCase(), lowerCaseButtons, i);
206 buttonPanel.add(button, index);
212 buttonPanel.add(makeButton("b", "b", lowerCaseButtons, 20));
213 buttonPanel.add(makeButton("z", "z", lowerCaseButtons, 21));
214 buttonPanel.add(makeButton("x", "x", lowerCaseButtons, 22));
217 // JAL-1360 widen the frame dynamically to accommodate case-sensitive AA
219 if (this.frame != null)
221 int newWidth = isCaseSensitive ? MY_FRAME_WIDTH_CASE_SENSITIVE
223 this.frame.setSize(newWidth, this.frame.getHeight());
226 buttonPanel.validate();
231 * ChangeListener handler for when a colour is picked in the colour chooser.
232 * The action is to apply the colour to all selected buttons as their
233 * background colour. Foreground colour (text) is set to a lighter shade in
234 * order to highlight which buttons are selected. If 'Lower Case Colour' is
235 * active, then the colour is applied to all lower case buttons (as well as
236 * the Lower Case Colour button itself).
241 public void stateChanged(ChangeEvent evt)
243 JButton button = null;
244 final Color newColour = colorChooser.getColor();
245 if (lcaseColour.isSelected())
247 selectedButtons.clear();
248 for (int i = 0; i < lowerCaseButtons.size(); i++)
250 button = lowerCaseButtons.get(i);
251 button.setBackground(newColour);
252 button.setForeground(ColorUtils.brighterThan(button.getBackground()));
255 for (int i = 0; i < selectedButtons.size(); i++)
257 button = selectedButtons.get(i);
258 button.setBackground(newColour);
259 button.setForeground(ColorUtils.brighterThan(newColour));
266 * Performs actions when a residue button is clicked. This manages the button
267 * selection set (highlighted by brighter foreground text).
269 * On select button(s) with Ctrl/click or Shift/click: set button foreground
270 * text to brighter than background.
272 * On unselect button(s) with Ctrl/click on selected, or click to release
273 * current selection: reset foreground text to darker than background.
275 * Simple click: clear selection (resetting foreground to darker); set clicked
276 * button foreground to brighter
278 * Finally, synchronize the colour chooser to the colour of the first button
279 * in the selected set.
283 public void colourButtonPressed(MouseEvent e)
285 JButton pressed = (JButton) e.getSource();
289 JButton start, end = (JButton) e.getSource();
290 if (selectedButtons.size() > 0)
292 start = selectedButtons.get(selectedButtons.size() - 1);
296 start = (JButton) e.getSource();
299 int startIndex = 0, endIndex = 0;
300 for (int b = 0; b < buttonPanel.getComponentCount(); b++)
302 if (buttonPanel.getComponent(b) == start)
306 if (buttonPanel.getComponent(b) == end)
312 if (startIndex > endIndex)
314 int temp = startIndex;
315 startIndex = endIndex;
319 for (int b = startIndex; b <= endIndex; b++)
321 JButton button = (JButton) buttonPanel.getComponent(b);
322 if (!selectedButtons.contains(button))
324 button.setForeground(ColorUtils.brighterThan(button
326 selectedButtons.add(button);
330 else if (!e.isControlDown())
332 for (int b = 0; b < selectedButtons.size(); b++)
334 JButton button = selectedButtons.get(b);
335 button.setForeground(ColorUtils.darkerThan(button.getBackground()));
337 selectedButtons.clear();
338 pressed.setForeground(ColorUtils.brighterThan(pressed.getBackground()));
339 selectedButtons.add(pressed);
342 else if (e.isControlDown())
344 if (selectedButtons.contains(pressed))
346 pressed.setForeground(ColorUtils.darkerThan(pressed.getBackground()));
347 selectedButtons.remove(pressed);
351 pressed.setForeground(ColorUtils.brighterThan(pressed
353 selectedButtons.add(pressed);
357 if (selectedButtons.size() > 0)
359 colorChooser.setColor((selectedButtons.get(0)).getBackground());
364 * A helper method to update or make a colour button, whose background colour
365 * is the associated colour, and text colour a darker shade of the same. If
366 * the button is already in the list, then its text and margins are updated,
367 * if not then it is created and added. This method supports toggling between
368 * case-sensitive and case-insensitive button panels. The case-sensitive
369 * version has abbreviated button text in order to fit in more buttons.
376 * the button's position in the list
378 JButton makeButton(String label, String residue, List<JButton> buttons,
381 final JButton button;
384 if (buttonIndex < buttons.size())
386 button = buttons.get(buttonIndex);
387 col = button.getBackground();
391 button = new JButton();
392 button.addMouseListener(new MouseAdapter()
395 public void mouseClicked(MouseEvent e)
397 colourButtonPressed(e);
404 * make initial button colour that of the current colour scheme,
405 * if it is a simple per-residue colouring, else white
408 if (oldColourScheme != null && oldColourScheme.isSimple())
410 col = oldColourScheme.findColour(residue.charAt(0), 0, null, null,
415 if (caseSensitive.isSelected())
417 button.setMargin(new Insets(2, 2, 2, 2));
421 button.setMargin(new Insets(2, 14, 2, 14));
424 button.setOpaque(true); // required for the next line to have effect
425 button.setBackground(col);
426 button.setText(label);
427 button.setForeground(ColorUtils.darkerThan(col));
428 button.setFont(VERDANA_BOLD_10);
434 * On 'OK', check that at least one colour has been assigned to a residue (and
435 * if not issue a warning), and apply the chosen colour scheme and close the
439 protected void okButton_actionPerformed()
441 if (isNoSelectionMade())
443 JvOptionPane.showMessageDialog(Desktop.desktop, MessageManager
444 .getString("label.no_colour_selection_in_scheme"),
445 MessageManager.getString("label.no_colour_selection_warn"),
446 JvOptionPane.WARNING_MESSAGE);
451 * OK is treated as 'apply colours and close'
453 applyButton_actionPerformed();
456 * If editing a named colour scheme, warn if changes
457 * have not been saved
459 warnIfUnsavedChanges();
463 frame.setClosed(true);
464 } catch (Exception ex)
471 * If we have made changes to an existing user defined colour scheme but not
472 * saved them, show a dialog with the option to save. If the user chooses to
473 * save, do so, else clear the colour scheme name to indicate a new colour
476 protected void warnIfUnsavedChanges()
483 String name = schemeName.getText().trim();
484 if (oldColourScheme != null && !"".equals(name)
485 && name.equals(oldColourScheme.getSchemeName()))
487 String message = MessageManager.formatMessage("label.scheme_changed",
489 String title = MessageManager.getString("label.save_changes");
490 String[] options = new String[] { title,
491 MessageManager.getString("label.dont_save_changes"), };
492 final String question = JvSwingUtils.wrapTooltip(true, message);
493 int response = JvOptionPane.showOptionDialog(Desktop.desktop,
494 question, title, JvOptionPane.DEFAULT_OPTION,
495 JvOptionPane.PLAIN_MESSAGE, null, options, options[0]);
497 boolean saved = false;
501 * prompt to save changes to file
503 saved = savebutton_actionPerformed();
507 * if user chooses not to save (either in this dialog or in the
508 * save as dialogs), treat this as a new user defined colour scheme
513 * clear scheme name and re-apply as an anonymous scheme
515 schemeName.setText("");
516 applyButton_actionPerformed();
522 * Returns true if the user has not made any colour selection (including if
523 * 'case-sensitive' selected and no lower-case colour chosen).
527 protected boolean isNoSelectionMade()
529 final boolean noUpperCaseSelected = upperCaseButtons == null
530 || upperCaseButtons.isEmpty();
531 final boolean noLowerCaseSelected = caseSensitive.isSelected()
532 && (lowerCaseButtons == null || lowerCaseButtons.isEmpty());
533 final boolean noSelectionMade = noUpperCaseSelected
534 || noLowerCaseSelected;
535 return noSelectionMade;
539 * Applies the current colour scheme to the alignment or sequence group
542 protected void applyButton_actionPerformed()
544 if (isNoSelectionMade())
546 JvOptionPane.showMessageDialog(Desktop.desktop, MessageManager
547 .getString("label.no_colour_selection_in_scheme"),
548 MessageManager.getString("label.no_colour_selection_warn"),
549 JvOptionPane.WARNING_MESSAGE);
552 UserColourScheme ucs = getSchemeFromButtons();
554 ap.alignFrame.changeColour(ucs);
558 * Constructs an instance of UserColourScheme with the residue colours
559 * currently set on the buttons on the panel
563 UserColourScheme getSchemeFromButtons()
566 Color[] newColours = new Color[24];
568 int length = upperCaseButtons.size();
572 for (JButton btn : upperCaseButtons)
574 newColours[i] = btn.getBackground();
580 for (int i = 0; i < 24; i++)
582 JButton button = upperCaseButtons.get(i);
583 newColours[i] = button.getBackground();
587 UserColourScheme ucs = new UserColourScheme(newColours);
588 ucs.setName(schemeName.getText());
590 if (caseSensitive.isSelected())
592 newColours = new Color[23];
593 length = lowerCaseButtons.size();
597 for (JButton btn : lowerCaseButtons)
599 newColours[i] = btn.getBackground();
605 for (int i = 0; i < 23; i++)
607 JButton button = lowerCaseButtons.get(i);
608 newColours[i] = button.getBackground();
611 ucs.setLowerCaseColours(newColours);
618 * Action on clicking Load scheme button.
620 * <li>Open a file chooser to browse for files with extension .jc</li>
621 * <li>Load in the colour scheme and transfer it to this panel's buttons</li>
622 * <li>Register the loaded colour scheme</li>
626 protected void loadbutton_actionPerformed()
628 upperCaseButtons = new ArrayList<JButton>();
629 lowerCaseButtons = new ArrayList<JButton>();
631 JalviewFileChooser chooser = new JalviewFileChooser("jc",
632 "Jalview User Colours");
633 chooser.setFileView(new JalviewFileView());
634 chooser.setDialogTitle(MessageManager
635 .getString("label.load_colour_scheme"));
636 chooser.setToolTipText(MessageManager.getString("action.load"));
638 int value = chooser.showOpenDialog(this);
640 if (value != JalviewFileChooser.APPROVE_OPTION)
644 File choice = chooser.getSelectedFile();
645 Cache.setProperty(LAST_DIRECTORY, choice.getParent());
647 UserColourScheme ucs = ColourSchemeLoader.loadColourScheme(choice
649 Color[] colors = ucs.getColours();
650 schemeName.setText(ucs.getSchemeName());
652 if (ucs.getLowerCaseColours() != null)
654 caseSensitive.setSelected(true);
655 lcaseColour.setEnabled(true);
656 resetButtonPanel(true);
657 for (int i = 0; i < lowerCaseButtons.size(); i++)
659 JButton button = lowerCaseButtons.get(i);
660 button.setBackground(ucs.getLowerCaseColours()[i]);
665 caseSensitive.setSelected(false);
666 lcaseColour.setEnabled(false);
667 resetButtonPanel(false);
670 for (int i = 0; i < upperCaseButtons.size(); i++)
672 JButton button = upperCaseButtons.get(i);
673 button.setBackground(colors[i]);
676 addNewColourScheme(choice.getPath());
680 * Loads the user-defined colour scheme from the first file listed in property
681 * "USER_DEFINED_COLOURS". If this fails, returns an all-white colour scheme.
685 public static UserColourScheme loadDefaultColours()
687 UserColourScheme ret = null;
689 String colours = Cache.getProperty(USER_DEFINED_COLOURS);
692 if (colours.indexOf("|") > -1)
694 colours = colours.substring(0, colours.indexOf("|"));
696 ret = ColourSchemeLoader.loadColourScheme(colours);
701 ret = new UserColourScheme("white");
708 * Action on pressing the Save button.
710 * <li>Check a name has been entered</li>
711 * <li>Warn if the name already exists, remove any existing scheme of the same
712 * name if overwriting</li>
713 * <li>Do the standard file chooser thing to write with extension .jc</li>
714 * <li>If saving changes (possibly not yet applied) to the currently selected
715 * colour scheme, then apply the changes, as it is too late to back out now</li>
716 * <li>Don't apply the changes if the currently selected scheme is different,
717 * to allow a new scheme to be configured and saved but not applied</li>
719 * Returns true if the scheme is saved to file, false if it is not
724 protected boolean savebutton_actionPerformed()
726 String name = schemeName.getText().trim();
727 if (name.length() < 1)
729 JvOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
730 .getString("label.user_colour_scheme_must_have_name"),
731 MessageManager.getString("label.no_name_colour_scheme"),
732 JvOptionPane.WARNING_MESSAGE);
736 if (ColourSchemes.getInstance().nameExists(name))
738 int reply = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
739 MessageManager.formatMessage(
740 "label.colour_scheme_exists_overwrite", new Object[] {
742 MessageManager.getString("label.duplicate_scheme_name"),
743 JvOptionPane.YES_NO_OPTION);
744 if (reply != JvOptionPane.YES_OPTION)
749 JalviewFileChooser chooser = new JalviewFileChooser("jc",
750 "Jalview User Colours");
752 JalviewFileView fileView = new JalviewFileView();
753 chooser.setFileView(fileView);
754 chooser.setDialogTitle(MessageManager
755 .getString("label.save_colour_scheme"));
756 chooser.setToolTipText(MessageManager.getString("action.save"));
758 int value = chooser.showSaveDialog(this);
760 if (value != JalviewFileChooser.APPROVE_OPTION)
765 File file = chooser.getSelectedFile();
766 UserColourScheme updatedScheme = addNewColourScheme(file.getPath());
771 * changes saved - apply to alignment if we are changing
772 * the currently selected colour scheme; also make the updated
773 * colours the 'backout' scheme on Cancel
775 if (oldColourScheme != null
776 && name.equals(oldColourScheme.getSchemeName()))
778 oldColourScheme = updatedScheme;
779 applyButton_actionPerformed();
785 * Adds the current colour scheme to the Jalview properties file so it is
786 * loaded on next startup, and updates the Colour menu in the parent
787 * AlignFrame (if there is one). Note this action does not including applying
793 protected UserColourScheme addNewColourScheme(String filePath)
796 * update the delimited list of user defined colour files in
797 * Jalview property USER_DEFINED_COLOURS
799 String defaultColours = Cache
800 .getDefault(USER_DEFINED_COLOURS, filePath);
801 if (defaultColours.indexOf(filePath) == -1)
803 if (defaultColours.length() > 0)
805 defaultColours = defaultColours.concat("|");
807 defaultColours = defaultColours.concat(filePath);
809 Cache.setProperty(USER_DEFINED_COLOURS, defaultColours);
812 * construct and register the colour scheme
814 UserColourScheme ucs = getSchemeFromButtons();
815 ColourSchemes.getInstance().registerColourScheme(ucs);
818 * update the Colour menu items
822 ap.alignFrame.buildColourMenu();
829 * Saves the colour scheme to file in XML format
833 protected void saveToFile(File toFile)
836 * build a Java model of colour scheme as XML, and
839 JalviewUserColours ucs = new JalviewUserColours();
840 String name = schemeName.getText();
841 ucs.setSchemeName(name);
844 PrintWriter out = new PrintWriter(new OutputStreamWriter(
845 new FileOutputStream(toFile), "UTF-8"));
847 for (int i = 0; i < buttonPanel.getComponentCount(); i++)
849 JButton button = (JButton) buttonPanel.getComponent(i);
850 Colour col = new Colour();
851 col.setName(button.getText());
852 col.setRGB(Format.getHexString(button.getBackground()));
857 } catch (Exception ex)
859 ex.printStackTrace();
864 * On cancel, restores the colour scheme that was selected before the dialogue
868 protected void cancelButton_actionPerformed()
870 ap.alignFrame.changeColour(oldColourScheme);
871 ap.paintAlignment(true);
875 frame.setClosed(true);
876 } catch (Exception ex)
882 * Action on selecting or deselecting the Case Sensitive option. When
883 * selected, separate buttons are shown for lower case residues, and the panel
884 * is resized to accommodate them. Also, the checkbox for 'apply colour to all
885 * lower case' is enabled.
888 public void caseSensitive_actionPerformed()
890 boolean selected = caseSensitive.isSelected();
891 resetButtonPanel(selected);
892 lcaseColour.setEnabled(selected);