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.datamodel.SequenceGroup;
25 import jalview.io.JalviewFileChooser;
26 import jalview.io.JalviewFileView;
27 import jalview.jbgui.GUserDefinedColours;
28 import jalview.schemabinding.version2.Colour;
29 import jalview.schemabinding.version2.JalviewUserColours;
30 import jalview.schemes.ColourSchemeI;
31 import jalview.schemes.ColourSchemeLoader;
32 import jalview.schemes.ColourSchemes;
33 import jalview.schemes.ResidueProperties;
34 import jalview.schemes.UserColourScheme;
35 import jalview.util.ColorUtils;
36 import jalview.util.Format;
37 import jalview.util.MessageManager;
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;
57 * This panel allows the user to assign colours to Amino Acid residue codes, and
58 * save the colour scheme.
60 * @author Andrew Waterhouse
61 * @author Mungo Carstairs
63 public class UserDefinedColours extends GUserDefinedColours implements
66 private static final Font VERDANA_BOLD_10 = new Font("Verdana",
69 public static final String USER_DEFINED_COLOURS = "USER_DEFINED_COLOURS";
71 private static final String LAST_DIRECTORY = "LAST_DIRECTORY";
73 private static final int MY_FRAME_HEIGHT = 440;
75 private static final int MY_FRAME_WIDTH = 810;
77 private static final int MY_FRAME_WIDTH_CASE_SENSITIVE = 970;
81 SequenceGroup seqGroup;
83 ColourSchemeI oldColourScheme;
87 List<JButton> upperCaseButtons;
89 List<JButton> lowerCaseButtons;
92 * Creates a new UserDefinedColours object.
97 public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)
101 lcaseColour.setEnabled(false);
106 if (seqGroup != null)
108 oldColourScheme = seqGroup.getColourScheme();
112 oldColourScheme = ap.av.getGlobalColourScheme();
115 if (oldColourScheme instanceof UserColourScheme)
117 schemeName.setText(oldColourScheme.getSchemeName());
118 if (((UserColourScheme) oldColourScheme).getLowerCaseColours() != null)
120 caseSensitive.setSelected(true);
121 lcaseColour.setEnabled(true);
122 resetButtonPanel(true);
126 resetButtonPanel(false);
131 resetButtonPanel(false);
140 selectedButtons = new ArrayList<JButton>();
145 colorChooser.getSelectionModel().addChangeListener(this);
146 frame = new JInternalFrame();
147 frame.setContentPane(this);
148 Desktop.addInternalFrame(frame,
149 MessageManager.getString("label.user_defined_colours"),
150 MY_FRAME_WIDTH, MY_FRAME_HEIGHT, true);
152 if (seqGroup != null)
154 frame.setTitle(frame.getTitle() + " (" + seqGroup.getName() + ")");
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<JButton>();
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<JButton>();
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(),
211 ResidueProperties.aa[i].toLowerCase(), 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(ColorUtils.brighterThan(button.getBackground()));
262 for (int i = 0; i < selectedButtons.size(); i++)
264 button = selectedButtons.get(i);
265 button.setBackground(newColour);
266 button.setForeground(ColorUtils.brighterThan(newColour));
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(ColorUtils.brighterThan(button
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(ColorUtils.brighterThan(pressed.getBackground()));
344 selectedButtons.add(pressed);
347 else if (e.isControlDown())
349 if (selectedButtons.contains(pressed))
351 pressed.setForeground(ColorUtils.darkerThan(pressed.getBackground()));
352 selectedButtons.remove(pressed);
356 pressed.setForeground(ColorUtils.brighterThan(pressed
358 selectedButtons.add(pressed);
362 if (selectedButtons.size() > 0)
364 colorChooser.setColor((selectedButtons.get(0)).getBackground());
369 * A helper method to update or make a colour button, whose background colour
370 * is the associated colour, and text colour a darker shade of the same. If
371 * the button is already in the list, then its text and margins are updated,
372 * if not then it is created and added. This method supports toggling between
373 * case-sensitive and case-insensitive button panels. The case-sensitive
374 * version has abbreviated button text in order to fit in more buttons.
381 * the button's position in the list
383 JButton makeButton(String label, String residue, List<JButton> buttons,
386 final JButton button;
389 if (buttonIndex < buttons.size())
391 button = buttons.get(buttonIndex);
392 col = button.getBackground();
396 button = new JButton();
397 button.addMouseListener(new MouseAdapter()
400 public void mouseClicked(MouseEvent e)
402 colourButtonPressed(e);
409 * make initial button colour that of the current colour scheme,
410 * if it is a simple per-residue colouring, else white
413 if (oldColourScheme != null && oldColourScheme.isSimple())
415 col = oldColourScheme.findColour(residue.charAt(0), 0, null, null,
420 if (caseSensitive.isSelected())
422 button.setMargin(new Insets(2, 2, 2, 2));
426 button.setMargin(new Insets(2, 14, 2, 14));
429 button.setOpaque(true); // required for the next line to have effect
430 button.setBackground(col);
431 button.setText(label);
432 button.setForeground(ColorUtils.darkerThan(col));
433 button.setFont(VERDANA_BOLD_10);
439 * On 'OK', check that at least one colour has been assigned to a residue (and
440 * if not issue a warning), and apply the chosen colour scheme and close the
444 protected void okButton_actionPerformed()
446 if (isNoSelectionMade())
448 JvOptionPane.showMessageDialog(Desktop.desktop, MessageManager
449 .getString("label.no_colour_selection_in_scheme"),
450 MessageManager.getString("label.no_colour_selection_warn"),
451 JvOptionPane.WARNING_MESSAGE);
455 applyButton_actionPerformed();
459 frame.setClosed(true);
460 } catch (Exception ex)
467 * Returns true if the user has not made any colour selection (including if
468 * 'case-sensitive' selected and no lower-case colour chosen).
472 protected boolean isNoSelectionMade()
474 final boolean noUpperCaseSelected = upperCaseButtons == null
475 || upperCaseButtons.isEmpty();
476 final boolean noLowerCaseSelected = caseSensitive.isSelected()
477 && (lowerCaseButtons == null || lowerCaseButtons.isEmpty());
478 final boolean noSelectionMade = noUpperCaseSelected
479 || noLowerCaseSelected;
480 return noSelectionMade;
484 * Applies the current colour scheme to the alignment or sequence group
487 protected void applyButton_actionPerformed()
489 if (isNoSelectionMade())
491 JvOptionPane.showMessageDialog(Desktop.desktop, MessageManager
492 .getString("label.no_colour_selection_in_scheme"),
493 MessageManager.getString("label.no_colour_selection_warn"),
494 JvOptionPane.WARNING_MESSAGE);
497 UserColourScheme ucs = getSchemeFromButtons();
499 if (seqGroup != null)
501 seqGroup.setColourScheme(ucs);
502 ap.paintAlignment(true);
506 ap.alignFrame.changeColour(ucs);
511 * Constructs an instance of UserColourScheme with the residue colours
512 * currently set on the buttons on the panel
516 UserColourScheme getSchemeFromButtons()
519 Color[] newColours = new Color[24];
521 int length = upperCaseButtons.size();
525 for (JButton btn : upperCaseButtons)
527 newColours[i] = btn.getBackground();
533 for (int i = 0; i < 24; i++)
535 JButton button = upperCaseButtons.get(i);
536 newColours[i] = button.getBackground();
540 UserColourScheme ucs = new UserColourScheme(newColours);
541 ucs.setName(schemeName.getText());
543 if (caseSensitive.isSelected())
545 newColours = new Color[23];
546 length = lowerCaseButtons.size();
550 for (JButton btn : lowerCaseButtons)
552 newColours[i] = btn.getBackground();
558 for (int i = 0; i < 23; i++)
560 JButton button = lowerCaseButtons.get(i);
561 newColours[i] = button.getBackground();
564 ucs.setLowerCaseColours(newColours);
571 * Action on clicking Load scheme button.
573 * <li>Open a file chooser to browse for files with extension .jc</li>
574 * <li>Load in the colour scheme and transfer it to this panel's buttons</li>
575 * <li>Register the loaded colour scheme</li>
579 protected void loadbutton_actionPerformed()
581 upperCaseButtons = new ArrayList<JButton>();
582 lowerCaseButtons = new ArrayList<JButton>();
584 JalviewFileChooser chooser = new JalviewFileChooser("jc",
585 "Jalview User Colours");
586 chooser.setFileView(new JalviewFileView());
587 chooser.setDialogTitle(MessageManager
588 .getString("label.load_colour_scheme"));
589 chooser.setToolTipText(MessageManager.getString("action.load"));
591 int value = chooser.showOpenDialog(this);
593 if (value != JalviewFileChooser.APPROVE_OPTION)
597 File choice = chooser.getSelectedFile();
598 Cache.setProperty(LAST_DIRECTORY, choice.getParent());
600 UserColourScheme ucs = ColourSchemeLoader.loadColourScheme(choice
602 Color[] colors = ucs.getColours();
603 schemeName.setText(ucs.getSchemeName());
605 if (ucs.getLowerCaseColours() != null)
607 caseSensitive.setSelected(true);
608 lcaseColour.setEnabled(true);
609 resetButtonPanel(true);
610 for (int i = 0; i < lowerCaseButtons.size(); i++)
612 JButton button = lowerCaseButtons.get(i);
613 button.setBackground(ucs.getLowerCaseColours()[i]);
618 caseSensitive.setSelected(false);
619 lcaseColour.setEnabled(false);
620 resetButtonPanel(false);
623 for (int i = 0; i < upperCaseButtons.size(); i++)
625 JButton button = upperCaseButtons.get(i);
626 button.setBackground(colors[i]);
629 addNewColourScheme(choice.getPath());
633 * Loads the user-defined colour scheme from the first file listed in property
634 * "USER_DEFINED_COLOURS". If this fails, returns an all-white colour scheme.
638 public static UserColourScheme loadDefaultColours()
640 UserColourScheme ret = null;
642 String colours = Cache.getProperty(USER_DEFINED_COLOURS);
645 if (colours.indexOf("|") > -1)
647 colours = colours.substring(0, colours.indexOf("|"));
649 ret = ColourSchemeLoader.loadColourScheme(colours);
654 ret = new UserColourScheme("white");
661 * Action on pressing the Save button.
663 * <li>Check a name has been entered</li>
664 * <li>Warn if the name already exists, remove any existing scheme of the same
665 * name if overwriting</li>
666 * <li>Do the standard file chooser thing to write with extension .jc</li>
667 * <li>If saving changes (possibly not yet applied) to the currently selected
668 * colour scheme, then apply the changes, as it is too late to back out now</li>
669 * <li>Don't apply the changes if the currently selected scheme is different,
670 * to allow a new scheme to be configured and saved but not applied</li>
674 protected void savebutton_actionPerformed()
676 String name = schemeName.getText().trim();
677 if (name.length() < 1)
679 JvOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
680 .getString("label.user_colour_scheme_must_have_name"),
681 MessageManager.getString("label.no_name_colour_scheme"),
682 JvOptionPane.WARNING_MESSAGE);
686 if (ColourSchemes.getInstance().nameExists(name))
688 int reply = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
689 MessageManager.formatMessage(
690 "label.colour_scheme_exists_overwrite", new Object[] {
692 MessageManager.getString("label.duplicate_scheme_name"),
693 JvOptionPane.YES_NO_OPTION);
694 if (reply != JvOptionPane.YES_OPTION)
698 ColourSchemes.getInstance().removeColourScheme(name);
700 JalviewFileChooser chooser = new JalviewFileChooser("jc",
701 "Jalview User Colours");
703 JalviewFileView fileView = new JalviewFileView();
704 chooser.setFileView(fileView);
705 chooser.setDialogTitle(MessageManager
706 .getString("label.save_colour_scheme"));
707 chooser.setToolTipText(MessageManager.getString("action.save"));
709 int value = chooser.showSaveDialog(this);
711 if (value == JalviewFileChooser.APPROVE_OPTION)
713 File file = chooser.getSelectedFile();
714 addNewColourScheme(file.getPath());
718 * changes saved - apply to alignment if we are changing
719 * the currently selected colour scheme
721 if (oldColourScheme != null
722 && name.equals(oldColourScheme.getSchemeName()))
724 applyButton_actionPerformed();
730 * Adds the current colour scheme to the Jalview properties file so it is
731 * loaded on next startup, and updates the Colour menu in the parent
732 * AlignFrame (if there is one). Note this action does not including applying
737 protected void addNewColourScheme(String filePath)
740 * update the delimited list of user defined colour files in
741 * Jalview property USER_DEFINED_COLOURS
743 String defaultColours = Cache
744 .getDefault(USER_DEFINED_COLOURS, filePath);
745 if (defaultColours.indexOf(filePath) == -1)
747 if (defaultColours.length() > 0)
749 defaultColours = defaultColours.concat("|");
751 defaultColours = defaultColours.concat(filePath);
753 Cache.setProperty(USER_DEFINED_COLOURS, defaultColours);
756 * construct and register the colour scheme
758 UserColourScheme ucs = getSchemeFromButtons();
759 ColourSchemes.getInstance().registerColourScheme(ucs);
762 * update the Colour menu items
766 ap.alignFrame.buildColourMenu();
771 * Saves the colour scheme to file in XML format
775 protected void saveToFile(File toFile)
778 * build a Java model of colour scheme as XML, and
781 JalviewUserColours ucs = new JalviewUserColours();
782 String name = schemeName.getText();
783 ucs.setSchemeName(name);
786 PrintWriter out = new PrintWriter(new OutputStreamWriter(
787 new FileOutputStream(toFile), "UTF-8"));
789 for (int i = 0; i < buttonPanel.getComponentCount(); i++)
791 JButton button = (JButton) buttonPanel.getComponent(i);
792 Colour col = new Colour();
793 col.setName(button.getText());
794 col.setRGB(Format.getHexString(button.getBackground()));
799 } catch (Exception ex)
801 ex.printStackTrace();
806 * On cancel, restores the colour scheme that was selected before the dialogue
810 protected void cancelButton_actionPerformed()
814 if (seqGroup != null)
816 seqGroup.setColourScheme(oldColourScheme);
820 ap.alignFrame.changeColour(oldColourScheme);
822 ap.paintAlignment(true);
827 frame.setClosed(true);
828 } catch (Exception ex)
834 * Action on selecting or deselecting the Case Sensitive option. When
835 * selected, separate buttons are shown for lower case residues, and the panel
836 * is resized to accommodate them. Also, the checkbox for 'apply colour to all
837 * lower case' is enabled.
840 public void caseSensitive_actionPerformed()
842 boolean selected = caseSensitive.isSelected();
843 resetButtonPanel(selected);
844 lcaseColour.setEnabled(selected);