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.getColourScheme();
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();
259 * ChangeListener handler for when a colour is picked in the colour chooser.
260 * The action is to apply the colour to all selected buttons as their
261 * background colour. Foreground colour (text) is set to a lighter shade in
262 * order to highlight which buttons are selected. If 'Lower Case Colour' is
263 * active, then the colour is applied to all lower case buttons (as well as
264 * the Lower Case Colour button itself).
269 public void stateChanged(ChangeEvent evt)
271 JButton button = null;
272 final Color newColour = colorChooser.getColor();
273 for (int i = 0; i < selectedButtons.size(); i++)
275 button = selectedButtons.get(i);
276 button.setBackground(newColour);
277 button.setForeground(ColorUtils.brighterThan(newColour));
279 if (button == lcaseColour)
281 button.setForeground(Color.black);
282 for (int i = 0; i < lowerCaseButtons.size(); i++)
284 button = lowerCaseButtons.get(i);
285 button.setBackground(newColour);
286 button.setForeground(ColorUtils.brighterThan(button.getBackground()));
292 * Performs actions when a residue button is clicked. This manages the button
293 * selection set (highlighted by brighter foreground text).
295 * On select button(s) with Ctrl/click or Shift/click: set button foreground
296 * text to brighter than background.
298 * On unselect button(s) with Ctrl/click on selected, or click to release
299 * current selection: reset foreground text to darker than background.
301 * Simple click: clear selection (resetting foreground to darker); set clicked
302 * button foreground to brighter
304 * Finally, synchronize the colour chooser to the colour of the first button
305 * in the selected set.
309 public void colourButtonPressed(MouseEvent e)
311 JButton pressed = (JButton) e.getSource();
315 JButton start, end = (JButton) e.getSource();
316 if (selectedButtons.size() > 0)
318 start = selectedButtons.get(selectedButtons.size() - 1);
322 start = (JButton) e.getSource();
325 int startIndex = 0, endIndex = 0;
326 for (int b = 0; b < buttonPanel.getComponentCount(); b++)
328 if (buttonPanel.getComponent(b) == start)
332 if (buttonPanel.getComponent(b) == end)
338 if (startIndex > endIndex)
340 int temp = startIndex;
341 startIndex = endIndex;
345 for (int b = startIndex; b <= endIndex; b++)
347 JButton button = (JButton) buttonPanel.getComponent(b);
348 if (!selectedButtons.contains(button))
350 button.setForeground(ColorUtils.brighterThan(button
352 selectedButtons.add(button);
356 else if (!e.isControlDown())
358 for (int b = 0; b < selectedButtons.size(); b++)
360 JButton button = selectedButtons.get(b);
361 button.setForeground(ColorUtils.darkerThan(button.getBackground()));
363 selectedButtons.clear();
364 pressed.setForeground(ColorUtils.brighterThan(pressed.getBackground()));
365 selectedButtons.add(pressed);
368 else if (e.isControlDown())
370 if (selectedButtons.contains(pressed))
372 pressed.setForeground(ColorUtils.darkerThan(pressed.getBackground()));
373 selectedButtons.remove(pressed);
377 pressed.setForeground(ColorUtils.brighterThan(pressed
379 selectedButtons.add(pressed);
383 if (selectedButtons.size() > 0)
385 colorChooser.setColor((selectedButtons.get(0)).getBackground());
390 * A helper method to update or make a colour button, whose background colour
391 * is the associated colour, and text colour a darker shade of the same. If
392 * the button is already in the list, then its text and margins are updated,
393 * if not then it is created and added. This method supports toggling between
394 * case-sensitive and case-insensitive button panels. The case-sensitive
395 * version has abbreviated button text in order to fit in more buttons.
402 * the button's position in the list
404 JButton makeButton(String label, String residue, List<JButton> buttons,
407 final JButton button;
410 if (buttonIndex < buttons.size())
412 button = buttons.get(buttonIndex);
413 col = button.getBackground();
417 button = new JButton();
418 button.addMouseListener(new MouseAdapter()
421 public void mouseClicked(MouseEvent e)
423 colourButtonPressed(e);
430 * make initial button colour that of the current colour scheme,
431 * if it is a simple per-residue colouring, else white
434 if (oldColourScheme != null && oldColourScheme.isSimple())
436 col = oldColourScheme.findColour(residue.charAt(0), 0, null, null,
441 if (caseSensitive.isSelected())
443 button.setMargin(new Insets(2, 2, 2, 2));
447 button.setMargin(new Insets(2, 14, 2, 14));
450 button.setOpaque(true); // required for the next line to have effect
451 button.setBackground(col);
452 button.setText(label);
453 button.setForeground(ColorUtils.darkerThan(col));
454 button.setFont(VERDANA_BOLD_10);
460 * On 'OK', check that at least one colour has been assigned to a residue (and
461 * if not issue a warning), and apply the chosen colour scheme and close the
465 protected void okButton_actionPerformed()
467 if (isNoSelectionMade())
469 JvOptionPane.showMessageDialog(Desktop.desktop, MessageManager
470 .getString("label.no_colour_selection_in_scheme"),
471 MessageManager.getString("label.no_colour_selection_warn"),
472 JvOptionPane.WARNING_MESSAGE);
476 applyButton_actionPerformed();
480 frame.setClosed(true);
481 } catch (Exception ex)
488 * Returns true if the user has not made any colour selection (including if
489 * 'case-sensitive' selected and no lower-case colour chosen).
493 protected boolean isNoSelectionMade()
495 final boolean noUpperCaseSelected = upperCaseButtons == null
496 || upperCaseButtons.isEmpty();
497 final boolean noLowerCaseSelected = caseSensitive.isSelected()
498 && (lowerCaseButtons == null || lowerCaseButtons.isEmpty());
499 final boolean noSelectionMade = noUpperCaseSelected
500 || noLowerCaseSelected;
501 return noSelectionMade;
505 * Applies the current colour scheme to the alignment, sequence group or
509 protected void applyButton_actionPerformed()
511 if (isNoSelectionMade())
513 JvOptionPane.showMessageDialog(Desktop.desktop, MessageManager
514 .getString("label.no_colour_selection_in_scheme"),
515 MessageManager.getString("label.no_colour_selection_warn"),
516 JvOptionPane.WARNING_MESSAGE);
519 UserColourScheme ucs = getSchemeFromButtons();
521 if (seqGroup != null)
523 seqGroup.setColourScheme(ucs);
524 ap.paintAlignment(true);
528 ap.alignFrame.changeColour(ucs);
530 else if (structureViewer != null)
532 structureViewer.setJalviewColourScheme(ucs);
536 UserColourScheme getSchemeFromButtons()
539 Color[] newColours = new Color[24];
541 int length = upperCaseButtons.size();
545 for (JButton btn : upperCaseButtons)
547 newColours[i] = btn.getBackground();
553 for (int i = 0; i < 24; i++)
555 JButton button = upperCaseButtons.get(i);
556 newColours[i] = button.getBackground();
560 UserColourScheme ucs = new UserColourScheme(newColours);
561 ucs.setName(schemeName.getText());
563 if (caseSensitive.isSelected())
565 newColours = new Color[23];
566 length = lowerCaseButtons.size();
570 for (JButton btn : lowerCaseButtons)
572 newColours[i] = btn.getBackground();
578 for (int i = 0; i < 23; i++)
580 JButton button = lowerCaseButtons.get(i);
581 newColours[i] = button.getBackground();
584 ucs.setLowerCaseColours(newColours);
589 // ucs.setThreshold(0, ap.av.isIgnoreGapsConsensus());
602 protected void loadbutton_actionPerformed(ActionEvent e)
604 upperCaseButtons = new ArrayList<JButton>();
605 lowerCaseButtons = new ArrayList<JButton>();
607 JalviewFileChooser chooser = new JalviewFileChooser("jc",
608 "Jalview User Colours");
609 chooser.setFileView(new JalviewFileView());
610 chooser.setDialogTitle(MessageManager
611 .getString("label.load_colour_scheme"));
612 chooser.setToolTipText(MessageManager.getString("action.load"));
614 int value = chooser.showOpenDialog(this);
616 if (value != JalviewFileChooser.APPROVE_OPTION)
620 File choice = chooser.getSelectedFile();
621 Cache.setProperty(LAST_DIRECTORY, choice.getParent());
623 UserColourScheme ucs = ColourSchemes.loadColourScheme(choice
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("jc",
717 "Jalview User Colours");
719 JalviewFileView fileView = new JalviewFileView();
720 chooser.setFileView(fileView);
721 chooser.setDialogTitle(MessageManager
722 .getString("label.save_colour_scheme"));
723 chooser.setToolTipText(MessageManager.getString("action.save"));
725 int value = chooser.showSaveDialog(this);
727 if (value == JalviewFileChooser.APPROVE_OPTION)
729 File file = chooser.getSelectedFile();
730 addNewColourScheme(file.getPath());
736 * Adds the current colour scheme to the Jalview properties file so it is
737 * loaded on next startup, and updates the Colour menu in the parent
738 * AlignFrame (if there is one). Note this action does not including applying
743 protected void addNewColourScheme(String filePath)
746 * update the delimited list of user defined colour files in
747 * Jalview property USER_DEFINED_COLOURS
749 String defaultColours = Cache
750 .getDefault(USER_DEFINED_COLOURS, filePath);
751 if (defaultColours.indexOf(filePath) == -1)
753 if (defaultColours.length() > 0)
755 defaultColours = defaultColours.concat("|");
757 defaultColours = defaultColours.concat(filePath);
759 Cache.setProperty(USER_DEFINED_COLOURS, defaultColours);
762 * construct and register the colour scheme
764 UserColourScheme ucs = getSchemeFromButtons();
765 ColourSchemes.getInstance().registerColourScheme(ucs);
768 * update the Colour menu items
772 ap.alignFrame.buildColourMenu();
777 * Saves the colour scheme to file in XML format
781 protected void saveToFile(File toFile)
784 * build a Java model of colour scheme as XML, and
787 JalviewUserColours ucs = new JalviewUserColours();
788 ucs.setSchemeName(schemeName.getText());
791 PrintWriter out = new PrintWriter(new OutputStreamWriter(
792 new FileOutputStream(toFile), "UTF-8"));
794 for (int i = 0; i < buttonPanel.getComponentCount(); i++)
796 JButton button = (JButton) buttonPanel.getComponent(i);
797 Colour col = new Colour();
798 col.setName(button.getText());
799 col.setRGB(Format.getHexString(button.getBackground()));
804 } catch (Exception ex)
806 ex.printStackTrace();
811 * On cancel, restores the colour scheme before the dialogue was opened
816 protected void cancelButton_actionPerformed(ActionEvent e)
820 if (seqGroup != null)
822 seqGroup.setColourScheme(oldColourScheme);
826 ap.alignFrame.changeColour(oldColourScheme);
828 ap.paintAlignment(true);
831 if (structureViewer != null)
833 structureViewer.setJalviewColourScheme(oldColourScheme);
838 frame.setClosed(true);
839 } catch (Exception ex)
845 public void caseSensitive_actionPerformed(ActionEvent e)
847 boolean selected = caseSensitive.isSelected();
848 resetButtonPanel(selected);
849 lcaseColour.setEnabled(selected);
850 lcaseColour.setForeground(Color.GRAY);
854 * Action on clicking 'Lower case colour', which results in changing colour of
855 * all lower-case buttons when a colour is picked. A second click of the
856 * button turns off this behaviour.
859 public void lcaseColour_actionPerformed(ActionEvent e)
861 boolean enable = !selectedButtons.contains(lcaseColour);
862 selectedButtons.clear();
865 selectedButtons.add(lcaseColour);
866 lcaseColour.setForeground(lowerCaseButtons.get(0).getForeground());
867 lcaseColour.setForeground(Color.black);
871 lcaseColour.setBackground(Color.white);
872 lcaseColour.setForeground(Color.gray);