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.xml.binding.jalview.JalviewUserColours;
36 import jalview.xml.binding.jalview.JalviewUserColours.Colour;
37 import jalview.xml.binding.jalview.ObjectFactory;
39 import java.awt.Color;
41 import java.awt.Insets;
42 import java.awt.event.MouseAdapter;
43 import java.awt.event.MouseEvent;
45 import java.io.FileOutputStream;
46 import java.io.OutputStreamWriter;
47 import java.io.PrintWriter;
48 import java.util.ArrayList;
49 import java.util.List;
51 import javax.swing.JButton;
52 import javax.swing.JInternalFrame;
53 import javax.swing.event.ChangeEvent;
54 import javax.swing.event.ChangeListener;
55 import javax.xml.bind.JAXBContext;
56 import javax.xml.bind.Marshaller;
59 * This panel allows the user to assign colours to Amino Acid residue codes, and
60 * save the colour scheme.
62 * @author Andrew Waterhouse
63 * @author Mungo Carstairs
65 public class UserDefinedColours extends GUserDefinedColours
66 implements ChangeListener
68 private static final Font VERDANA_BOLD_10 = new Font("Verdana", Font.BOLD,
71 public static final String USER_DEFINED_COLOURS = "USER_DEFINED_COLOURS";
73 private static final String LAST_DIRECTORY = "LAST_DIRECTORY";
75 private static final int MY_FRAME_HEIGHT = 440;
77 private static final int MY_FRAME_WIDTH = 810;
79 private static final int MY_FRAME_WIDTH_CASE_SENSITIVE = 970;
84 * the colour scheme when the dialog was opened, or
85 * the scheme last saved to file
87 ColourSchemeI oldColourScheme;
90 * flag is true if the colour scheme has been changed since the
91 * dialog was opened, or the changes last saved to file
93 boolean changedButNotSaved;
97 List<JButton> upperCaseButtons;
99 List<JButton> lowerCaseButtons;
102 * Creates and displays a new UserDefinedColours panel
106 public UserDefinedColours(AlignmentPanel alignPanel)
110 lcaseColour.setEnabled(false);
112 this.ap = alignPanel;
114 oldColourScheme = alignPanel.av.getGlobalColourScheme();
116 if (oldColourScheme instanceof UserColourScheme)
118 schemeName.setText(oldColourScheme.getSchemeName());
119 if (((UserColourScheme) oldColourScheme)
120 .getLowerCaseColours() != null)
122 caseSensitive.setSelected(true);
123 lcaseColour.setEnabled(true);
124 resetButtonPanel(true);
128 resetButtonPanel(false);
133 resetButtonPanel(false);
142 selectedButtons = new ArrayList<>();
147 colorChooser.getSelectionModel().addChangeListener(this);
148 frame = new JInternalFrame();
149 frame.setContentPane(this);
150 Desktop.addInternalFrame(frame,
151 MessageManager.getString("label.user_defined_colours"),
152 MY_FRAME_WIDTH, MY_FRAME_HEIGHT, true);
156 * Rebuilds the panel with coloured buttons for residues. If not case
157 * sensitive colours, show 3-letter amino acid code as button text. If case
158 * sensitive, just show the single letter code, in order to make space for the
159 * additional buttons.
161 * @param isCaseSensitive
163 void resetButtonPanel(boolean isCaseSensitive)
165 buttonPanel.removeAll();
167 if (upperCaseButtons == null)
169 upperCaseButtons = new ArrayList<>();
172 for (int i = 0; i < 20; i++)
174 String label = isCaseSensitive ? ResidueProperties.aa[i]
175 : ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i])
177 JButton button = makeButton(label, ResidueProperties.aa[i],
178 upperCaseButtons, i);
179 buttonPanel.add(button);
182 buttonPanel.add(makeButton("B", "B", upperCaseButtons, 20));
183 buttonPanel.add(makeButton("Z", "Z", upperCaseButtons, 21));
184 buttonPanel.add(makeButton("X", "X", upperCaseButtons, 22));
185 buttonPanel.add(makeButton("Gap", "-", upperCaseButtons, 23));
187 if (!isCaseSensitive)
189 gridLayout.setRows(6);
190 gridLayout.setColumns(4);
194 gridLayout.setRows(7);
196 gridLayout.setColumns(cols + 1);
198 if (lowerCaseButtons == null)
200 lowerCaseButtons = new ArrayList<>();
203 for (int i = 0; i < 20; i++)
205 int row = i / cols + 1;
206 int index = (row * cols) + i;
207 JButton button = makeButton(ResidueProperties.aa[i].toLowerCase(),
208 ResidueProperties.aa[i].toLowerCase(), lowerCaseButtons, i);
210 buttonPanel.add(button, index);
216 buttonPanel.add(makeButton("b", "b", lowerCaseButtons, 20));
217 buttonPanel.add(makeButton("z", "z", lowerCaseButtons, 21));
218 buttonPanel.add(makeButton("x", "x", lowerCaseButtons, 22));
221 // JAL-1360 widen the frame dynamically to accommodate case-sensitive AA
223 if (this.frame != null)
225 int newWidth = isCaseSensitive ? MY_FRAME_WIDTH_CASE_SENSITIVE
227 this.frame.setSize(newWidth, this.frame.getHeight());
230 buttonPanel.validate();
235 * ChangeListener handler for when a colour is picked in the colour chooser.
236 * The action is to apply the colour to all selected buttons as their
237 * background colour. Foreground colour (text) is set to a lighter shade in
238 * order to highlight which buttons are selected. If 'Lower Case Colour' is
239 * active, then the colour is applied to all lower case buttons (as well as
240 * the Lower Case Colour button itself).
245 public void stateChanged(ChangeEvent evt)
247 JButton button = null;
248 final Color newColour = colorChooser.getColor();
249 if (lcaseColour.isSelected())
251 selectedButtons.clear();
252 for (int i = 0; i < lowerCaseButtons.size(); i++)
254 button = lowerCaseButtons.get(i);
255 button.setBackground(newColour);
256 button.setForeground(
257 ColorUtils.brighterThan(button.getBackground()));
260 for (int i = 0; i < selectedButtons.size(); i++)
262 button = selectedButtons.get(i);
263 button.setBackground(newColour);
264 button.setForeground(ColorUtils.brighterThan(newColour));
267 changedButNotSaved = true;
271 * Performs actions when a residue button is clicked. This manages the button
272 * selection set (highlighted by brighter foreground text).
274 * On select button(s) with Ctrl/click or Shift/click: set button foreground
275 * text to brighter than background.
277 * On unselect button(s) with Ctrl/click on selected, or click to release
278 * current selection: reset foreground text to darker than background.
280 * Simple click: clear selection (resetting foreground to darker); set clicked
281 * button foreground to brighter
283 * Finally, synchronize the colour chooser to the colour of the first button
284 * in the selected set.
288 public void colourButtonPressed(MouseEvent e)
290 JButton pressed = (JButton) e.getSource();
294 JButton start, end = (JButton) e.getSource();
295 if (selectedButtons.size() > 0)
297 start = selectedButtons.get(selectedButtons.size() - 1);
301 start = (JButton) e.getSource();
304 int startIndex = 0, endIndex = 0;
305 for (int b = 0; b < buttonPanel.getComponentCount(); b++)
307 if (buttonPanel.getComponent(b) == start)
311 if (buttonPanel.getComponent(b) == end)
317 if (startIndex > endIndex)
319 int temp = startIndex;
320 startIndex = endIndex;
324 for (int b = startIndex; b <= endIndex; b++)
326 JButton button = (JButton) buttonPanel.getComponent(b);
327 if (!selectedButtons.contains(button))
329 button.setForeground(
330 ColorUtils.brighterThan(button.getBackground()));
331 selectedButtons.add(button);
335 else if (!e.isControlDown())
337 for (int b = 0; b < selectedButtons.size(); b++)
339 JButton button = selectedButtons.get(b);
340 button.setForeground(ColorUtils.darkerThan(button.getBackground()));
342 selectedButtons.clear();
343 pressed.setForeground(
344 ColorUtils.brighterThan(pressed.getBackground()));
345 selectedButtons.add(pressed);
348 else if (e.isControlDown())
350 if (selectedButtons.contains(pressed))
352 pressed.setForeground(
353 ColorUtils.darkerThan(pressed.getBackground()));
354 selectedButtons.remove(pressed);
358 pressed.setForeground(
359 ColorUtils.brighterThan(pressed.getBackground()));
360 selectedButtons.add(pressed);
364 if (selectedButtons.size() > 0)
366 colorChooser.setColor((selectedButtons.get(0)).getBackground());
371 * A helper method to update or make a colour button, whose background colour
372 * is the associated colour, and text colour a darker shade of the same. If
373 * the button is already in the list, then its text and margins are updated,
374 * if not then it is created and added. This method supports toggling between
375 * case-sensitive and case-insensitive button panels. The case-sensitive
376 * version has abbreviated button text in order to fit in more buttons.
383 * the button's position in the list
385 JButton makeButton(String label, String residue, List<JButton> buttons,
388 final JButton button;
391 if (buttonIndex < buttons.size())
393 button = buttons.get(buttonIndex);
394 col = button.getBackground();
398 button = new JButton();
399 button.addMouseListener(new MouseAdapter()
402 public void mouseClicked(MouseEvent e)
404 colourButtonPressed(e);
411 * make initial button colour that of the current colour scheme,
412 * if it is a simple per-residue colouring, else white
415 if (oldColourScheme != null && oldColourScheme.isSimple())
417 col = oldColourScheme.findColour(residue.charAt(0), 0, null, null,
422 if (caseSensitive.isSelected())
424 button.setMargin(new Insets(2, 2, 2, 2));
428 button.setMargin(new Insets(2, 14, 2, 14));
431 button.setOpaque(true); // required for the next line to have effect
432 button.setBackground(col);
433 button.setText(label);
434 button.setForeground(ColorUtils.darkerThan(col));
435 button.setFont(VERDANA_BOLD_10);
441 * On 'OK', check that at least one colour has been assigned to a residue (and
442 * if not issue a warning), and apply the chosen colour scheme and close the
446 protected void okButton_actionPerformed()
448 if (isNoSelectionMade())
450 JvOptionPane.showMessageDialog(Desktop.desktop,
452 .getString("label.no_colour_selection_in_scheme"),
453 MessageManager.getString("label.no_colour_selection_warn"),
454 JvOptionPane.WARNING_MESSAGE);
459 * OK is treated as 'apply colours and close'
461 applyButton_actionPerformed();
464 * If editing a named colour scheme, warn if changes
465 * have not been saved
467 warnIfUnsavedChanges();
471 frame.setClosed(true);
472 } catch (Exception ex)
479 * If we have made changes to an existing user defined colour scheme but not
480 * saved them, show a dialog with the option to save. If the user chooses to
481 * save, do so, else clear the colour scheme name to indicate a new colour
484 protected void warnIfUnsavedChanges()
486 // BH 2018 no warning in JavaScript TODO
488 if (/** @j2sNative true || */ !changedButNotSaved)
493 String name = schemeName.getText().trim();
494 if (oldColourScheme != null && !"".equals(name)
495 && name.equals(oldColourScheme.getSchemeName()))
497 String message = MessageManager.formatMessage("label.scheme_changed",
499 String title = MessageManager.getString("label.save_changes");
500 String[] options = new String[] { title,
501 MessageManager.getString("label.dont_save_changes"), };
502 final String question = JvSwingUtils.wrapTooltip(true, message);
503 int response = JvOptionPane.showOptionDialog(Desktop.desktop,
504 question, title, JvOptionPane.DEFAULT_OPTION,
505 JvOptionPane.PLAIN_MESSAGE, null, options, options[0]);
510 * prompt to save changes to file; if done,
511 * resets 'changed' flag to false
513 savebutton_actionPerformed();
517 * if user chooses not to save (either in this dialog or in the
518 * save as dialogs), treat this as a new user defined colour scheme
520 if (changedButNotSaved)
523 * clear scheme name and re-apply as an anonymous scheme
525 schemeName.setText("");
526 applyButton_actionPerformed();
532 * Returns true if the user has not made any colour selection (including if
533 * 'case-sensitive' selected and no lower-case colour chosen).
537 protected boolean isNoSelectionMade()
539 final boolean noUpperCaseSelected = upperCaseButtons == null
540 || upperCaseButtons.isEmpty();
541 final boolean noLowerCaseSelected = caseSensitive.isSelected()
542 && (lowerCaseButtons == null || lowerCaseButtons.isEmpty());
543 final boolean noSelectionMade = noUpperCaseSelected
544 || noLowerCaseSelected;
545 return noSelectionMade;
549 * Applies the current colour scheme to the alignment or sequence group
552 protected void applyButton_actionPerformed()
554 if (isNoSelectionMade())
556 JvOptionPane.showMessageDialog(Desktop.desktop,
558 .getString("label.no_colour_selection_in_scheme"),
559 MessageManager.getString("label.no_colour_selection_warn"),
560 JvOptionPane.WARNING_MESSAGE);
563 UserColourScheme ucs = getSchemeFromButtons();
565 ap.alignFrame.changeColour(ucs);
569 * Constructs an instance of UserColourScheme with the residue colours
570 * currently set on the buttons on the panel
574 UserColourScheme getSchemeFromButtons()
577 Color[] newColours = new Color[24];
579 int length = upperCaseButtons.size();
583 for (JButton btn : upperCaseButtons)
585 newColours[i] = btn.getBackground();
591 for (int i = 0; i < 24; i++)
593 JButton button = upperCaseButtons.get(i);
594 newColours[i] = button.getBackground();
598 UserColourScheme ucs = new UserColourScheme(newColours);
599 ucs.setName(schemeName.getText());
601 if (caseSensitive.isSelected())
603 newColours = new Color[23];
604 length = lowerCaseButtons.size();
608 for (JButton btn : lowerCaseButtons)
610 newColours[i] = btn.getBackground();
616 for (int i = 0; i < 23; i++)
618 JButton button = lowerCaseButtons.get(i);
619 newColours[i] = button.getBackground();
622 ucs.setLowerCaseColours(newColours);
629 * Action on clicking Load scheme button.
631 * <li>Open a file chooser to browse for files with extension .jc</li>
632 * <li>Load in the colour scheme and transfer it to this panel's buttons</li>
633 * <li>Register the loaded colour scheme</li>
637 protected void loadbutton_actionPerformed()
639 upperCaseButtons = new ArrayList<>();
640 lowerCaseButtons = new ArrayList<>();
641 JalviewFileChooser chooser = new JalviewFileChooser("jc",
642 "Jalview User Colours");
643 chooser.setFileView(new JalviewFileView());
644 chooser.setDialogTitle(
645 MessageManager.getString("label.load_colour_scheme"));
646 chooser.setToolTipText(MessageManager.getString("action.load"));
647 chooser.setResponseHandler(0, new Runnable()
652 File choice = chooser.getSelectedFile();
653 Cache.setProperty(LAST_DIRECTORY, choice.getParent());
655 UserColourScheme ucs = ColourSchemeLoader
656 .loadColourScheme(choice.getAbsolutePath());
657 Color[] colors = ucs.getColours();
658 schemeName.setText(ucs.getSchemeName());
660 if (ucs.getLowerCaseColours() != null)
662 caseSensitive.setSelected(true);
663 lcaseColour.setEnabled(true);
664 resetButtonPanel(true);
665 for (int i = 0; i < lowerCaseButtons.size(); i++)
667 JButton button = lowerCaseButtons.get(i);
668 button.setBackground(ucs.getLowerCaseColours()[i]);
673 caseSensitive.setSelected(false);
674 lcaseColour.setEnabled(false);
675 resetButtonPanel(false);
678 for (int i = 0; i < upperCaseButtons.size(); i++)
680 JButton button = upperCaseButtons.get(i);
681 button.setBackground(colors[i]);
684 addNewColourScheme(choice.getPath());
688 chooser.showOpenDialog(this);
692 * Loads the user-defined colour scheme from the first file listed in property
693 * "USER_DEFINED_COLOURS". If this fails, returns an all-white colour scheme.
697 public static UserColourScheme loadDefaultColours()
699 UserColourScheme ret = null;
701 String colours = Cache.getProperty(USER_DEFINED_COLOURS);
704 if (colours.indexOf("|") > -1)
706 colours = colours.substring(0, colours.indexOf("|"));
708 ret = ColourSchemeLoader.loadColourScheme(colours);
713 ret = new UserColourScheme("white");
720 * Action on pressing the Save button.
722 * <li>Check a name has been entered</li>
723 * <li>Warn if the name already exists, remove any existing scheme of the same
724 * name if overwriting</li>
725 * <li>Do the standard file chooser thing to write with extension .jc</li>
726 * <li>If saving changes (possibly not yet applied) to the currently selected
727 * colour scheme, then apply the changes, as it is too late to back out
729 * <li>Don't apply the changes if the currently selected scheme is different,
730 * to allow a new scheme to be configured and saved but not applied</li>
732 * If the scheme is saved to file, the 'changed' flag field is reset to false.
735 protected void savebutton_actionPerformed()
737 String name = schemeName.getText().trim();
738 if (name.length() < 1)
740 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
742 .getString("label.user_colour_scheme_must_have_name"),
743 MessageManager.getString("label.no_name_colour_scheme"),
744 JvOptionPane.WARNING_MESSAGE);
747 if (ColourSchemes.getInstance().nameExists(name))
749 // BH 2018 SwingJS bypasses this question with YES_OPTION
750 int reply = /** @j2sNative 0 && */ JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
751 MessageManager.formatMessage(
752 "label.colour_scheme_exists_overwrite", new Object[]
754 MessageManager.getString("label.duplicate_scheme_name"),
755 JvOptionPane.YES_NO_OPTION);
756 if (reply != JvOptionPane.YES_OPTION)
761 JalviewFileChooser chooser = new JalviewFileChooser("jc",
762 "Jalview User Colours");
764 JalviewFileView fileView = new JalviewFileView();
765 chooser.setFileView(fileView);
766 chooser.setDialogTitle(
767 MessageManager.getString("label.save_colour_scheme"));
768 chooser.setToolTipText(MessageManager.getString("action.save"));
769 int option = chooser.showSaveDialog(this);
770 if (option == JalviewFileChooser.APPROVE_OPTION)
772 File file = chooser.getSelectedFile();
773 UserColourScheme updatedScheme = addNewColourScheme(file.getPath());
775 changedButNotSaved = false;
778 * changes saved - apply to alignment if we are changing
779 * the currently selected colour scheme; also make the updated
780 * colours the 'backout' scheme on Cancel
782 if (oldColourScheme != null
783 && name.equals(oldColourScheme.getSchemeName()))
785 oldColourScheme = updatedScheme;
786 applyButton_actionPerformed();
792 * Adds the current colour scheme to the Jalview properties file so it is
793 * loaded on next startup, and updates the Colour menu in the parent
794 * AlignFrame (if there is one). Note this action does not including applying
800 protected UserColourScheme addNewColourScheme(String filePath)
803 * update the delimited list of user defined colour files in
804 * Jalview property USER_DEFINED_COLOURS
806 String defaultColours = Cache.getDefault(USER_DEFINED_COLOURS,
808 if (defaultColours.indexOf(filePath) == -1)
810 if (defaultColours.length() > 0)
812 defaultColours = defaultColours.concat("|");
814 defaultColours = defaultColours.concat(filePath);
816 Cache.setProperty(USER_DEFINED_COLOURS, defaultColours);
819 * construct and register the colour scheme
821 UserColourScheme ucs = getSchemeFromButtons();
822 ColourSchemes.getInstance().registerColourScheme(ucs);
825 * update the Colour menu items
829 ap.alignFrame.buildColourMenu();
836 * Saves the colour scheme to file in XML format
840 protected void saveToFile(File toFile)
843 * build a Java model of colour scheme as XML, and
846 JalviewUserColours ucs = new JalviewUserColours();
847 String name = schemeName.getText();
848 ucs.setSchemeName(name);
851 PrintWriter out = new PrintWriter(new OutputStreamWriter(
852 new FileOutputStream(toFile), "UTF-8"));
854 for (int i = 0; i < buttonPanel.getComponentCount(); i++)
856 JButton button = (JButton) buttonPanel.getComponent(i);
857 Colour col = new Colour();
858 col.setName(button.getText());
859 col.setRGB(Format.getHexString(button.getBackground()));
860 ucs.getColour().add(col);
862 JAXBContext jaxbContext = JAXBContext
863 .newInstance(JalviewUserColours.class);
864 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
865 jaxbMarshaller.marshal(
866 new ObjectFactory().createJalviewUserColours(ucs), out);
869 } catch (Exception ex)
871 ex.printStackTrace();
876 * On cancel, restores the colour scheme that was selected before the dialogue
880 protected void cancelButton_actionPerformed()
882 ap.alignFrame.changeColour(oldColourScheme);
883 ap.paintAlignment(true, true);
887 frame.setClosed(true);
888 } catch (Exception ex)
894 * Action on selecting or deselecting the Case Sensitive option. When
895 * selected, separate buttons are shown for lower case residues, and the panel
896 * is resized to accommodate them. Also, the checkbox for 'apply colour to all
897 * lower case' is enabled.
900 public void caseSensitive_actionPerformed()
902 boolean selected = caseSensitive.isSelected();
903 resetButtonPanel(selected);
904 lcaseColour.setEnabled(selected);