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", Font.BOLD, 10);
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 = 420;
75 private static final int MY_FRAME_WIDTH = 810;
77 private static final int MY_FRAME_WIDTH_CASE_SENSITIVE = 970;
81 SequenceGroup seqGroup;
83 List<JButton> selectedButtons;
85 ColourSchemeI oldColourScheme;
89 JalviewStructureDisplayI structureViewer;
91 List<JButton> upperCaseButtons;
93 List<JButton> lowerCaseButtons;
96 * Creates a new UserDefinedColours object.
101 public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)
105 lcaseColour.setEnabled(false);
110 if (seqGroup != null)
112 oldColourScheme = seqGroup.cs;
116 oldColourScheme = ap.av.getGlobalColourScheme();
119 if (oldColourScheme instanceof UserColourScheme)
121 schemeName.setText(oldColourScheme.getSchemeName());
122 if (((UserColourScheme) oldColourScheme).getLowerCaseColours() != null)
124 caseSensitive.setSelected(true);
125 lcaseColour.setEnabled(true);
126 resetButtonPanel(true);
130 resetButtonPanel(false);
135 resetButtonPanel(false);
141 public UserDefinedColours(JalviewStructureDisplayI viewer,
145 this.structureViewer = viewer;
147 colorChooser.getSelectionModel().addChangeListener(this);
149 oldColourScheme = oldcs;
151 if (oldColourScheme instanceof UserColourScheme)
153 schemeName.setText(((UserColourScheme) oldColourScheme).getSchemeName());
156 resetButtonPanel(false);
164 colorChooser.getSelectionModel().addChangeListener(this);
165 frame = new JInternalFrame();
166 frame.setContentPane(this);
167 Desktop.addInternalFrame(frame,
168 MessageManager.getString("label.user_defined_colours"),
169 MY_FRAME_WIDTH, MY_FRAME_HEIGHT, true);
171 if (seqGroup != null)
173 frame.setTitle(frame.getTitle() + " (" + seqGroup.getName() + ")");
178 * Rebuilds the panel with coloured buttons for residues. If not case
179 * sensitive colours, show 3-letter amino acid code as button text. If case
180 * sensitive, just show the single letter code, in order to make space for the
181 * additional buttons.
183 * @param isCaseSensitive
185 void resetButtonPanel(boolean isCaseSensitive)
187 buttonPanel.removeAll();
189 if (upperCaseButtons == null)
191 upperCaseButtons = new ArrayList<JButton>();
194 for (int i = 0; i < 20; i++)
196 String label = isCaseSensitive ? ResidueProperties.aa[i]
197 : ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i])
199 JButton button = makeButton(label, ResidueProperties.aa[i],
200 upperCaseButtons, i);
201 buttonPanel.add(button);
204 buttonPanel.add(makeButton("B", "B", upperCaseButtons, 20));
205 buttonPanel.add(makeButton("Z", "Z", upperCaseButtons, 21));
206 buttonPanel.add(makeButton("X", "X", upperCaseButtons, 22));
207 buttonPanel.add(makeButton("Gap", "-", upperCaseButtons, 23));
209 if (!isCaseSensitive)
211 gridLayout.setRows(6);
212 gridLayout.setColumns(4);
216 gridLayout.setRows(7);
218 gridLayout.setColumns(cols + 1);
220 if (lowerCaseButtons == null)
222 lowerCaseButtons = new ArrayList<JButton>();
225 for (int i = 0; i < 20; i++)
227 int row = i / cols + 1;
228 int index = (row * cols) + i;
229 JButton button = makeButton(ResidueProperties.aa[i].toLowerCase(),
230 ResidueProperties.aa[i].toLowerCase(), lowerCaseButtons, i);
232 buttonPanel.add(button, index);
238 buttonPanel.add(makeButton("b", "b", lowerCaseButtons, 20));
239 buttonPanel.add(makeButton("z", "z", lowerCaseButtons, 21));
240 buttonPanel.add(makeButton("x", "x", lowerCaseButtons, 22));
243 // JAL-1360 widen the frame dynamically to accommodate case-sensitive AA
245 if (this.frame != null)
247 int newWidth = isCaseSensitive ? MY_FRAME_WIDTH_CASE_SENSITIVE
249 this.frame.setSize(newWidth, this.frame.getHeight());
252 buttonPanel.validate();
263 public void stateChanged(ChangeEvent evt)
265 if (selectedButtons != null)
267 JButton button = null;
268 final Color newColour = colorChooser.getColor();
269 for (int i = 0; i < selectedButtons.size(); i++)
271 button = selectedButtons.get(i);
272 button.setBackground(newColour);
273 button.setForeground(ColorUtils.brighterThan(newColour));
275 if (button == lcaseColour)
277 for (int i = 0; i < lowerCaseButtons.size(); i++)
279 button = lowerCaseButtons.get(i);
280 button.setBackground(newColour);
281 button.setForeground(ColorUtils.brighterThan(button
289 * Performs actions when a residue button is clicked. This manages the button
290 * selection set (highlighted by brighter foreground text).
292 * On select button(s) with Ctrl/click or Shift/click: set button foreground
293 * text to brighter than background.
295 * On unselect button(s) with Ctrl/click on selected, or click to release
296 * current selection: reset foreground text to darker than background.
298 * Simple click: clear selection (resetting foreground to darker); set clicked
299 * button foreground to brighter
301 * Finally, synchronize the colour chooser to the colour of the first button
302 * in the selected set.
306 public void colourButtonPressed(MouseEvent e)
308 if (selectedButtons == null)
310 selectedButtons = new ArrayList<JButton>();
313 JButton pressed = (JButton) e.getSource();
317 JButton start, end = (JButton) e.getSource();
318 if (selectedButtons.size() > 0)
320 start = selectedButtons.get(selectedButtons.size() - 1);
324 start = (JButton) e.getSource();
327 int startIndex = 0, endIndex = 0;
328 for (int b = 0; b < buttonPanel.getComponentCount(); b++)
330 if (buttonPanel.getComponent(b) == start)
334 if (buttonPanel.getComponent(b) == end)
340 if (startIndex > endIndex)
342 int temp = startIndex;
343 startIndex = endIndex;
347 for (int b = startIndex; b <= endIndex; b++)
349 JButton button = (JButton) buttonPanel.getComponent(b);
350 if (!selectedButtons.contains(button))
352 button.setForeground(ColorUtils.brighterThan(button
354 selectedButtons.add(button);
358 else if (!e.isControlDown())
360 for (int b = 0; b < selectedButtons.size(); b++)
362 JButton button = selectedButtons.get(b);
363 button.setForeground(ColorUtils.darkerThan(button.getBackground()));
365 selectedButtons.clear();
366 pressed.setForeground(ColorUtils.brighterThan(pressed.getBackground()));
367 selectedButtons.add(pressed);
370 else if (e.isControlDown())
372 if (selectedButtons.contains(pressed))
374 pressed.setForeground(ColorUtils.darkerThan(pressed.getBackground()));
375 selectedButtons.remove(pressed);
379 pressed.setForeground(ColorUtils.brighterThan(pressed
381 selectedButtons.add(pressed);
385 if (selectedButtons.size() > 0)
387 colorChooser.setColor((selectedButtons.get(0)).getBackground());
392 * A helper method to update or make a colour button, whose background colour
393 * is the associated colour, and text colour a darker shade of the same. If
394 * the button is already in the list, then its text and margins are updated,
395 * if not then it is created and added. This method supports toggling between
396 * case-sensitive and case-insensitive button panels. The case-sensitive
397 * version has abbreviated button text in order to fit in more buttons.
404 * the button's position in the list
406 JButton makeButton(String label, String residue,
407 List<JButton> buttons, int buttonIndex)
409 final JButton button;
412 if (buttonIndex < buttons.size())
414 button = buttons.get(buttonIndex);
415 col = button.getBackground();
419 button = new JButton();
420 button.addMouseListener(new MouseAdapter()
423 public void mouseClicked(MouseEvent e)
425 colourButtonPressed(e);
432 * make initial button colour that of the current colour scheme,
433 * if it is a simple per-residue colouring, else white
436 if (oldColourScheme != null && oldColourScheme.isSimple())
438 col = oldColourScheme.findColour(residue.charAt(0));
442 if (caseSensitive.isSelected())
444 button.setMargin(new Insets(2, 2, 2, 2));
448 button.setMargin(new Insets(2, 14, 2, 14));
451 button.setOpaque(true); // required for the next line to have effect
452 button.setBackground(col);
453 button.setText(label);
454 button.setForeground(ColorUtils.darkerThan(col));
455 button.setFont(VERDANA_BOLD_10);
461 * On 'OK', check that at least one colour has been assigned to a residue (and
462 * if not issue a warning), and apply the chosen colour scheme and close the
466 protected void okButton_actionPerformed()
468 if (isNoSelectionMade())
470 JvOptionPane.showMessageDialog(Desktop.desktop, MessageManager
471 .getString("label.no_colour_selection_in_scheme"),
472 MessageManager.getString("label.no_colour_selection_warn"),
473 JvOptionPane.WARNING_MESSAGE);
477 applyButton_actionPerformed();
481 frame.setClosed(true);
482 } catch (Exception ex)
489 * Returns true if the user has not made any colour selection (including if
490 * 'case-sensitive' selected and no lower-case colour chosen).
494 protected boolean isNoSelectionMade()
496 final boolean noUpperCaseSelected = upperCaseButtons == null
497 || upperCaseButtons.isEmpty();
498 final boolean noLowerCaseSelected = caseSensitive.isSelected()
499 && (lowerCaseButtons == null || lowerCaseButtons.isEmpty());
500 final boolean noSelectionMade = noUpperCaseSelected
501 || noLowerCaseSelected;
502 return noSelectionMade;
506 * Applies the current colour scheme to the alignment, sequence group or
510 protected void applyButton_actionPerformed()
512 if (isNoSelectionMade())
514 JvOptionPane.showMessageDialog(Desktop.desktop, MessageManager
515 .getString("label.no_colour_selection_in_scheme"),
516 MessageManager.getString("label.no_colour_selection_warn"),
517 JvOptionPane.WARNING_MESSAGE);
520 UserColourScheme ucs = getSchemeFromButtons();
522 if (seqGroup != null)
525 ap.paintAlignment(true);
529 ap.alignFrame.changeColour(ucs);
531 else if (structureViewer != null)
533 structureViewer.setJalviewColourScheme(ucs);
537 UserColourScheme getSchemeFromButtons()
540 Color[] newColours = new Color[24];
542 int length = upperCaseButtons.size();
546 for (JButton btn : upperCaseButtons)
548 newColours[i] = btn.getBackground();
554 for (int i = 0; i < 24; i++)
556 JButton button = upperCaseButtons.get(i);
557 newColours[i] = button.getBackground();
561 UserColourScheme ucs = new UserColourScheme(newColours);
562 ucs.setName(schemeName.getText());
564 if (caseSensitive.isSelected())
566 newColours = new Color[23];
567 length = lowerCaseButtons.size();
571 for (JButton btn : lowerCaseButtons)
573 newColours[i] = btn.getBackground();
579 for (int i = 0; i < 23; i++)
581 JButton button = lowerCaseButtons.get(i);
582 newColours[i] = button.getBackground();
585 ucs.setLowerCaseColours(newColours);
590 ucs.setThreshold(0, ap.av.isIgnoreGapsConsensus());
603 protected void loadbutton_actionPerformed(ActionEvent e)
605 upperCaseButtons = new ArrayList<JButton>();
606 lowerCaseButtons = new ArrayList<JButton>();
608 JalviewFileChooser chooser = new JalviewFileChooser(
609 Cache.getProperty(LAST_DIRECTORY), "jc", "Jalview User Colours");
610 chooser.setFileView(new JalviewFileView());
611 chooser.setDialogTitle(MessageManager
612 .getString("label.load_colour_scheme"));
613 chooser.setToolTipText(MessageManager.getString("action.load"));
615 int value = chooser.showOpenDialog(this);
617 if (value != JalviewFileChooser.APPROVE_OPTION)
621 File choice = chooser.getSelectedFile();
622 Cache.setProperty(LAST_DIRECTORY, choice.getParent());
624 UserColourScheme ucs = ColourSchemes.loadColourScheme(choice.getAbsolutePath());
625 Color[] colors = ucs.getColours();
626 schemeName.setText(ucs.getSchemeName());
628 if (ucs.getLowerCaseColours() != null)
630 caseSensitive.setSelected(true);
631 lcaseColour.setEnabled(true);
632 resetButtonPanel(true);
633 for (int i = 0; i < lowerCaseButtons.size(); i++)
635 JButton button = lowerCaseButtons.get(i);
636 button.setBackground(ucs.getLowerCaseColours()[i]);
641 caseSensitive.setSelected(false);
642 lcaseColour.setEnabled(false);
643 resetButtonPanel(false);
646 for (int i = 0; i < upperCaseButtons.size(); i++)
648 JButton button = upperCaseButtons.get(i);
649 button.setBackground(colors[i]);
652 addNewColourScheme(choice.getPath());
656 * Loads the user-defined colour scheme from the first file listed in property
657 * "USER_DEFINED_COLOURS". If this fails, returns an all-white colour scheme.
661 public static UserColourScheme loadDefaultColours()
663 UserColourScheme ret = null;
665 String colours = Cache.getProperty(USER_DEFINED_COLOURS);
668 if (colours.indexOf("|") > -1)
670 colours = colours.substring(0, colours.indexOf("|"));
672 ret = ColourSchemes.loadColourScheme(colours);
677 ret = new UserColourScheme("white");
690 protected void savebutton_actionPerformed(ActionEvent e)
692 String name = schemeName.getText().trim();
693 if (name.length() < 1)
695 JvOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
696 .getString("label.user_colour_scheme_must_have_name"),
697 MessageManager.getString("label.no_name_colour_scheme"),
698 JvOptionPane.WARNING_MESSAGE);
702 if (ColourSchemes.getInstance().nameExists(name))
704 int reply = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
705 MessageManager.formatMessage(
706 "label.colour_scheme_exists_overwrite", new Object[] {
708 MessageManager.getString("label.duplicate_scheme_name"),
709 JvOptionPane.YES_NO_OPTION);
710 if (reply != JvOptionPane.YES_OPTION)
714 ColourSchemes.getInstance().removeColourScheme(name);
716 JalviewFileChooser chooser = new JalviewFileChooser(
717 Cache.getProperty(LAST_DIRECTORY), "jc",
718 "Jalview User Colours");
720 JalviewFileView fileView = new JalviewFileView();
721 chooser.setFileView(fileView);
722 chooser.setDialogTitle(MessageManager
723 .getString("label.save_colour_scheme"));
724 chooser.setToolTipText(MessageManager.getString("action.save"));
726 int value = chooser.showSaveDialog(this);
728 if (value == JalviewFileChooser.APPROVE_OPTION)
730 File file = chooser.getSelectedFile();
731 addNewColourScheme(file.getPath());
737 * Adds the current colour scheme to the Jalview properties file so it is
738 * loaded on next startup, and updates the Colour menu in the parent
739 * AlignFrame (if there is one). Note this action does not including applying
744 protected void addNewColourScheme(String filePath)
747 * update the delimited list of user defined colour files in
748 * Jalview property USER_DEFINED_COLOURS
750 String defaultColours = Cache
751 .getDefault(USER_DEFINED_COLOURS, filePath);
752 if (defaultColours.indexOf(filePath) == -1)
754 if (defaultColours.length() > 0)
756 defaultColours = defaultColours.concat("|");
758 defaultColours = defaultColours.concat(filePath);
760 Cache.setProperty(USER_DEFINED_COLOURS, defaultColours);
763 * construct and register the colour scheme
765 UserColourScheme ucs = getSchemeFromButtons();
766 ColourSchemes.getInstance().registerColourScheme(ucs);
769 * update the Colour menu items
773 ap.alignFrame.buildColourMenu();
778 * Saves the colour scheme to file in XML format
782 protected void saveToFile(File toFile)
785 * build a Java model of colour scheme as XML, and
788 JalviewUserColours ucs = new JalviewUserColours();
789 ucs.setSchemeName(schemeName.getText());
792 PrintWriter out = new PrintWriter(new OutputStreamWriter(
793 new FileOutputStream(toFile), "UTF-8"));
795 for (int i = 0; i < buttonPanel.getComponentCount(); i++)
797 JButton button = (JButton) buttonPanel.getComponent(i);
798 Colour col = new Colour();
799 col.setName(button.getText());
800 col.setRGB(Format.getHexString(button.getBackground()));
805 } catch (Exception ex)
807 ex.printStackTrace();
812 * On cancel, restores the colour scheme before the dialogue was opened
817 protected void cancelButton_actionPerformed(ActionEvent e)
821 if (seqGroup != null)
823 seqGroup.cs = oldColourScheme;
827 ap.alignFrame.changeColour(oldColourScheme);
829 ap.paintAlignment(true);
832 if (structureViewer != null)
834 structureViewer.setJalviewColourScheme(oldColourScheme);
839 frame.setClosed(true);
840 } catch (Exception ex)
846 public void caseSensitive_actionPerformed(ActionEvent e)
848 resetButtonPanel(caseSensitive.isSelected());
849 lcaseColour.setEnabled(caseSensitive.isSelected());
853 public void lcaseColour_actionPerformed(ActionEvent e)
855 if (selectedButtons == null)
857 selectedButtons = new ArrayList<JButton>();
861 selectedButtons.clear();
863 selectedButtons.add(lcaseColour);