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.api.structures.JalviewStructureDisplayI;
24 import jalview.bin.Cache;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.io.JalviewFileChooser;
27 import jalview.io.JalviewFileView;
28 import jalview.jbgui.GUserDefinedColours;
29 import jalview.schemabinding.version2.Colour;
30 import jalview.schemabinding.version2.JalviewUserColours;
31 import jalview.schemes.ColourSchemeI;
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.ActionEvent;
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;
58 * This panel allows the user to assign colours to Amino Acid residue codes, and
59 * save the colour scheme.
61 * @author Andrew Waterhouse
62 * @author Mungo Carstairs
64 public class UserDefinedColours extends GUserDefinedColours implements
67 private static final Font VERDANA_BOLD_10 = new Font("Verdana",
70 public static final String USER_DEFINED_COLOURS = "USER_DEFINED_COLOURS";
72 private static final String LAST_DIRECTORY = "LAST_DIRECTORY";
74 private static final int MY_FRAME_HEIGHT = 420;
76 private static final int MY_FRAME_WIDTH = 810;
78 private static final int MY_FRAME_WIDTH_CASE_SENSITIVE = 970;
82 SequenceGroup seqGroup;
84 List<JButton> selectedButtons;
86 ColourSchemeI oldColourScheme;
90 JalviewStructureDisplayI structureViewer;
92 List<JButton> upperCaseButtons;
94 List<JButton> lowerCaseButtons;
97 * Creates a new UserDefinedColours object.
102 public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)
106 lcaseColour.setEnabled(false);
111 if (seqGroup != null)
113 oldColourScheme = seqGroup.cs;
117 oldColourScheme = ap.av.getGlobalColourScheme();
120 if (oldColourScheme instanceof UserColourScheme)
122 schemeName.setText(oldColourScheme.getSchemeName());
123 if (((UserColourScheme) oldColourScheme).getLowerCaseColours() != null)
125 caseSensitive.setSelected(true);
126 lcaseColour.setEnabled(true);
127 resetButtonPanel(true);
131 resetButtonPanel(false);
136 resetButtonPanel(false);
142 public UserDefinedColours(JalviewStructureDisplayI viewer,
146 this.structureViewer = viewer;
148 colorChooser.getSelectionModel().addChangeListener(this);
150 oldColourScheme = oldcs;
152 if (oldColourScheme instanceof UserColourScheme)
154 schemeName.setText(((UserColourScheme) oldColourScheme)
158 resetButtonPanel(false);
166 colorChooser.getSelectionModel().addChangeListener(this);
167 frame = new JInternalFrame();
168 frame.setContentPane(this);
169 Desktop.addInternalFrame(frame,
170 MessageManager.getString("label.user_defined_colours"),
171 MY_FRAME_WIDTH, MY_FRAME_HEIGHT, true);
173 if (seqGroup != null)
175 frame.setTitle(frame.getTitle() + " (" + seqGroup.getName() + ")");
180 * Rebuilds the panel with coloured buttons for residues. If not case
181 * sensitive colours, show 3-letter amino acid code as button text. If case
182 * sensitive, just show the single letter code, in order to make space for the
183 * additional buttons.
185 * @param isCaseSensitive
187 void resetButtonPanel(boolean isCaseSensitive)
189 buttonPanel.removeAll();
191 if (upperCaseButtons == null)
193 upperCaseButtons = new ArrayList<JButton>();
196 for (int i = 0; i < 20; i++)
198 String label = isCaseSensitive ? ResidueProperties.aa[i]
199 : ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i])
201 JButton button = makeButton(label, ResidueProperties.aa[i],
202 upperCaseButtons, i);
203 buttonPanel.add(button);
206 buttonPanel.add(makeButton("B", "B", upperCaseButtons, 20));
207 buttonPanel.add(makeButton("Z", "Z", upperCaseButtons, 21));
208 buttonPanel.add(makeButton("X", "X", upperCaseButtons, 22));
209 buttonPanel.add(makeButton("Gap", "-", upperCaseButtons, 23));
211 if (!isCaseSensitive)
213 gridLayout.setRows(6);
214 gridLayout.setColumns(4);
218 gridLayout.setRows(7);
220 gridLayout.setColumns(cols + 1);
222 if (lowerCaseButtons == null)
224 lowerCaseButtons = new ArrayList<JButton>();
227 for (int i = 0; i < 20; i++)
229 int row = i / cols + 1;
230 int index = (row * cols) + i;
231 JButton button = makeButton(ResidueProperties.aa[i].toLowerCase(),
232 ResidueProperties.aa[i].toLowerCase(), lowerCaseButtons, i);
234 buttonPanel.add(button, index);
240 buttonPanel.add(makeButton("b", "b", lowerCaseButtons, 20));
241 buttonPanel.add(makeButton("z", "z", lowerCaseButtons, 21));
242 buttonPanel.add(makeButton("x", "x", lowerCaseButtons, 22));
245 // JAL-1360 widen the frame dynamically to accommodate case-sensitive AA
247 if (this.frame != null)
249 int newWidth = isCaseSensitive ? MY_FRAME_WIDTH_CASE_SENSITIVE
251 this.frame.setSize(newWidth, this.frame.getHeight());
254 buttonPanel.validate();
265 public void stateChanged(ChangeEvent evt)
267 if (selectedButtons != null)
269 JButton button = null;
270 final Color newColour = colorChooser.getColor();
271 for (int i = 0; i < selectedButtons.size(); i++)
273 button = selectedButtons.get(i);
274 button.setBackground(newColour);
275 button.setForeground(ColorUtils.brighterThan(newColour));
277 if (button == lcaseColour)
279 for (int i = 0; i < lowerCaseButtons.size(); i++)
281 button = lowerCaseButtons.get(i);
282 button.setBackground(newColour);
283 button.setForeground(ColorUtils.brighterThan(button
291 * Performs actions when a residue button is clicked. This manages the button
292 * selection set (highlighted by brighter foreground text).
294 * On select button(s) with Ctrl/click or Shift/click: set button foreground
295 * text to brighter than background.
297 * On unselect button(s) with Ctrl/click on selected, or click to release
298 * current selection: reset foreground text to darker than background.
300 * Simple click: clear selection (resetting foreground to darker); set clicked
301 * button foreground to brighter
303 * Finally, synchronize the colour chooser to the colour of the first button
304 * in the selected set.
308 public void colourButtonPressed(MouseEvent e)
310 if (selectedButtons == null)
312 selectedButtons = new ArrayList<JButton>();
315 JButton pressed = (JButton) e.getSource();
319 JButton start, end = (JButton) e.getSource();
320 if (selectedButtons.size() > 0)
322 start = selectedButtons.get(selectedButtons.size() - 1);
326 start = (JButton) e.getSource();
329 int startIndex = 0, endIndex = 0;
330 for (int b = 0; b < buttonPanel.getComponentCount(); b++)
332 if (buttonPanel.getComponent(b) == start)
336 if (buttonPanel.getComponent(b) == end)
342 if (startIndex > endIndex)
344 int temp = startIndex;
345 startIndex = endIndex;
349 for (int b = startIndex; b <= endIndex; b++)
351 JButton button = (JButton) buttonPanel.getComponent(b);
352 if (!selectedButtons.contains(button))
354 button.setForeground(ColorUtils.brighterThan(button
356 selectedButtons.add(button);
360 else if (!e.isControlDown())
362 for (int b = 0; b < selectedButtons.size(); b++)
364 JButton button = selectedButtons.get(b);
365 button.setForeground(ColorUtils.darkerThan(button.getBackground()));
367 selectedButtons.clear();
368 pressed.setForeground(ColorUtils.brighterThan(pressed.getBackground()));
369 selectedButtons.add(pressed);
372 else if (e.isControlDown())
374 if (selectedButtons.contains(pressed))
376 pressed.setForeground(ColorUtils.darkerThan(pressed.getBackground()));
377 selectedButtons.remove(pressed);
381 pressed.setForeground(ColorUtils.brighterThan(pressed
383 selectedButtons.add(pressed);
387 if (selectedButtons.size() > 0)
389 colorChooser.setColor((selectedButtons.get(0)).getBackground());
394 * A helper method to update or make a colour button, whose background colour
395 * is the associated colour, and text colour a darker shade of the same. If
396 * the button is already in the list, then its text and margins are updated,
397 * if not then it is created and added. This method supports toggling between
398 * case-sensitive and case-insensitive button panels. The case-sensitive
399 * version has abbreviated button text in order to fit in more buttons.
406 * the button's position in the list
408 JButton makeButton(String label, String residue, List<JButton> buttons,
411 final JButton button;
414 if (buttonIndex < buttons.size())
416 button = buttons.get(buttonIndex);
417 col = button.getBackground();
421 button = new JButton();
422 button.addMouseListener(new MouseAdapter()
425 public void mouseClicked(MouseEvent e)
427 colourButtonPressed(e);
434 * make initial button colour that of the current colour scheme,
435 * if it is a simple per-residue colouring, else white
438 if (oldColourScheme != null && oldColourScheme.isSimple())
440 col = oldColourScheme.findColour(residue.charAt(0));
444 if (caseSensitive.isSelected())
446 button.setMargin(new Insets(2, 2, 2, 2));
450 button.setMargin(new Insets(2, 14, 2, 14));
453 button.setOpaque(true); // required for the next line to have effect
454 button.setBackground(col);
455 button.setText(label);
456 button.setForeground(ColorUtils.darkerThan(col));
457 button.setFont(VERDANA_BOLD_10);
463 * On 'OK', check that at least one colour has been assigned to a residue (and
464 * if not issue a warning), and apply the chosen colour scheme and close the
468 protected void okButton_actionPerformed()
470 if (isNoSelectionMade())
472 JvOptionPane.showMessageDialog(Desktop.desktop, MessageManager
473 .getString("label.no_colour_selection_in_scheme"),
474 MessageManager.getString("label.no_colour_selection_warn"),
475 JvOptionPane.WARNING_MESSAGE);
479 applyButton_actionPerformed();
483 frame.setClosed(true);
484 } catch (Exception ex)
491 * Returns true if the user has not made any colour selection (including if
492 * 'case-sensitive' selected and no lower-case colour chosen).
496 protected boolean isNoSelectionMade()
498 final boolean noUpperCaseSelected = upperCaseButtons == null
499 || upperCaseButtons.isEmpty();
500 final boolean noLowerCaseSelected = caseSensitive.isSelected()
501 && (lowerCaseButtons == null || lowerCaseButtons.isEmpty());
502 final boolean noSelectionMade = noUpperCaseSelected
503 || noLowerCaseSelected;
504 return noSelectionMade;
508 * Applies the current colour scheme to the alignment, sequence group or
512 protected void applyButton_actionPerformed()
514 if (isNoSelectionMade())
516 JvOptionPane.showMessageDialog(Desktop.desktop, MessageManager
517 .getString("label.no_colour_selection_in_scheme"),
518 MessageManager.getString("label.no_colour_selection_warn"),
519 JvOptionPane.WARNING_MESSAGE);
522 UserColourScheme ucs = getSchemeFromButtons();
524 if (seqGroup != null)
527 ap.paintAlignment(true);
531 ap.alignFrame.changeColour(ucs);
533 else if (structureViewer != null)
535 structureViewer.setJalviewColourScheme(ucs);
539 UserColourScheme getSchemeFromButtons()
542 Color[] newColours = new Color[24];
544 int length = upperCaseButtons.size();
548 for (JButton btn : upperCaseButtons)
550 newColours[i] = btn.getBackground();
556 for (int i = 0; i < 24; i++)
558 JButton button = upperCaseButtons.get(i);
559 newColours[i] = button.getBackground();
563 UserColourScheme ucs = new UserColourScheme(newColours);
564 ucs.setName(schemeName.getText());
566 if (caseSensitive.isSelected())
568 newColours = new Color[23];
569 length = lowerCaseButtons.size();
573 for (JButton btn : lowerCaseButtons)
575 newColours[i] = btn.getBackground();
581 for (int i = 0; i < 23; i++)
583 JButton button = lowerCaseButtons.get(i);
584 newColours[i] = button.getBackground();
587 ucs.setLowerCaseColours(newColours);
592 ucs.setThreshold(0, ap.av.isIgnoreGapsConsensus());
605 protected void loadbutton_actionPerformed(ActionEvent e)
607 upperCaseButtons = new ArrayList<JButton>();
608 lowerCaseButtons = new ArrayList<JButton>();
610 JalviewFileChooser chooser = new JalviewFileChooser("jc",
611 "Jalview User Colours");
612 chooser.setFileView(new JalviewFileView());
613 chooser.setDialogTitle(MessageManager
614 .getString("label.load_colour_scheme"));
615 chooser.setToolTipText(MessageManager.getString("action.load"));
617 int value = chooser.showOpenDialog(this);
619 if (value != JalviewFileChooser.APPROVE_OPTION)
623 File choice = chooser.getSelectedFile();
624 Cache.setProperty(LAST_DIRECTORY, choice.getParent());
626 UserColourScheme ucs = ColourSchemes.loadColourScheme(choice
628 Color[] colors = ucs.getColours();
629 schemeName.setText(ucs.getSchemeName());
631 if (ucs.getLowerCaseColours() != null)
633 caseSensitive.setSelected(true);
634 lcaseColour.setEnabled(true);
635 resetButtonPanel(true);
636 for (int i = 0; i < lowerCaseButtons.size(); i++)
638 JButton button = lowerCaseButtons.get(i);
639 button.setBackground(ucs.getLowerCaseColours()[i]);
644 caseSensitive.setSelected(false);
645 lcaseColour.setEnabled(false);
646 resetButtonPanel(false);
649 for (int i = 0; i < upperCaseButtons.size(); i++)
651 JButton button = upperCaseButtons.get(i);
652 button.setBackground(colors[i]);
655 addNewColourScheme(choice.getPath());
659 * Loads the user-defined colour scheme from the first file listed in property
660 * "USER_DEFINED_COLOURS". If this fails, returns an all-white colour scheme.
664 public static UserColourScheme loadDefaultColours()
666 UserColourScheme ret = null;
668 String colours = Cache.getProperty(USER_DEFINED_COLOURS);
671 if (colours.indexOf("|") > -1)
673 colours = colours.substring(0, colours.indexOf("|"));
675 ret = ColourSchemes.loadColourScheme(colours);
680 ret = new UserColourScheme("white");
693 protected void savebutton_actionPerformed(ActionEvent e)
695 String name = schemeName.getText().trim();
696 if (name.length() < 1)
698 JvOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
699 .getString("label.user_colour_scheme_must_have_name"),
700 MessageManager.getString("label.no_name_colour_scheme"),
701 JvOptionPane.WARNING_MESSAGE);
705 if (ColourSchemes.getInstance().nameExists(name))
707 int reply = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
708 MessageManager.formatMessage(
709 "label.colour_scheme_exists_overwrite", new Object[] {
711 MessageManager.getString("label.duplicate_scheme_name"),
712 JvOptionPane.YES_NO_OPTION);
713 if (reply != JvOptionPane.YES_OPTION)
717 ColourSchemes.getInstance().removeColourScheme(name);
719 JalviewFileChooser chooser = new JalviewFileChooser("jc",
720 "Jalview User Colours");
722 JalviewFileView fileView = new JalviewFileView();
723 chooser.setFileView(fileView);
724 chooser.setDialogTitle(MessageManager
725 .getString("label.save_colour_scheme"));
726 chooser.setToolTipText(MessageManager.getString("action.save"));
728 int value = chooser.showSaveDialog(this);
730 if (value == JalviewFileChooser.APPROVE_OPTION)
732 File file = chooser.getSelectedFile();
733 addNewColourScheme(file.getPath());
739 * Adds the current colour scheme to the Jalview properties file so it is
740 * loaded on next startup, and updates the Colour menu in the parent
741 * AlignFrame (if there is one). Note this action does not including applying
746 protected void addNewColourScheme(String filePath)
749 * update the delimited list of user defined colour files in
750 * Jalview property USER_DEFINED_COLOURS
752 String defaultColours = Cache
753 .getDefault(USER_DEFINED_COLOURS, filePath);
754 if (defaultColours.indexOf(filePath) == -1)
756 if (defaultColours.length() > 0)
758 defaultColours = defaultColours.concat("|");
760 defaultColours = defaultColours.concat(filePath);
762 Cache.setProperty(USER_DEFINED_COLOURS, defaultColours);
765 * construct and register the colour scheme
767 UserColourScheme ucs = getSchemeFromButtons();
768 ColourSchemes.getInstance().registerColourScheme(ucs);
771 * update the Colour menu items
775 ap.alignFrame.buildColourMenu();
780 * Saves the colour scheme to file in XML format
784 protected void saveToFile(File toFile)
787 * build a Java model of colour scheme as XML, and
790 JalviewUserColours ucs = new JalviewUserColours();
791 ucs.setSchemeName(schemeName.getText());
794 PrintWriter out = new PrintWriter(new OutputStreamWriter(
795 new FileOutputStream(toFile), "UTF-8"));
797 for (int i = 0; i < buttonPanel.getComponentCount(); i++)
799 JButton button = (JButton) buttonPanel.getComponent(i);
800 Colour col = new Colour();
801 col.setName(button.getText());
802 col.setRGB(Format.getHexString(button.getBackground()));
807 } catch (Exception ex)
809 ex.printStackTrace();
814 * On cancel, restores the colour scheme before the dialogue was opened
819 protected void cancelButton_actionPerformed(ActionEvent e)
823 if (seqGroup != null)
825 seqGroup.cs = oldColourScheme;
829 ap.alignFrame.changeColour(oldColourScheme);
831 ap.paintAlignment(true);
834 if (structureViewer != null)
836 structureViewer.setJalviewColourScheme(oldColourScheme);
841 frame.setClosed(true);
842 } catch (Exception ex)
848 public void caseSensitive_actionPerformed(ActionEvent e)
850 resetButtonPanel(caseSensitive.isSelected());
851 lcaseColour.setEnabled(caseSensitive.isSelected());
855 public void lcaseColour_actionPerformed(ActionEvent e)
857 if (selectedButtons == null)
859 selectedButtons = new ArrayList<JButton>();
863 selectedButtons.clear();
865 selectedButtons.add(lcaseColour);