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.ColourSchemeLoader;
33 import jalview.schemes.ColourSchemes;
34 import jalview.schemes.ResidueProperties;
35 import jalview.schemes.UserColourScheme;
36 import jalview.util.ColorUtils;
37 import jalview.util.Format;
38 import jalview.util.MessageManager;
40 import java.awt.Color;
42 import java.awt.Insets;
43 import java.awt.event.ActionEvent;
44 import java.awt.event.MouseAdapter;
45 import java.awt.event.MouseEvent;
47 import java.io.FileOutputStream;
48 import java.io.OutputStreamWriter;
49 import java.io.PrintWriter;
50 import java.util.ArrayList;
51 import java.util.List;
53 import javax.swing.JButton;
54 import javax.swing.JInternalFrame;
55 import javax.swing.event.ChangeEvent;
56 import javax.swing.event.ChangeListener;
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 implements
68 private static final Font VERDANA_BOLD_10 = new Font("Verdana",
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;
83 SequenceGroup seqGroup;
85 List<JButton> selectedButtons;
87 ColourSchemeI oldColourScheme;
91 JalviewStructureDisplayI structureViewer;
93 List<JButton> upperCaseButtons;
95 List<JButton> lowerCaseButtons;
98 * Creates a new UserDefinedColours object.
103 public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)
107 lcaseColour.setEnabled(false);
112 if (seqGroup != null)
114 oldColourScheme = seqGroup.getColourScheme();
118 oldColourScheme = ap.av.getGlobalColourScheme();
121 if (oldColourScheme instanceof UserColourScheme)
123 schemeName.setText(oldColourScheme.getSchemeName());
124 if (((UserColourScheme) oldColourScheme).getLowerCaseColours() != null)
126 caseSensitive.setSelected(true);
127 lcaseColour.setEnabled(true);
128 resetButtonPanel(true);
132 resetButtonPanel(false);
137 resetButtonPanel(false);
143 public UserDefinedColours(JalviewStructureDisplayI viewer,
147 this.structureViewer = viewer;
149 colorChooser.getSelectionModel().addChangeListener(this);
151 oldColourScheme = oldcs;
153 if (oldColourScheme instanceof UserColourScheme)
155 schemeName.setText(((UserColourScheme) oldColourScheme)
159 resetButtonPanel(false);
165 public UserDefinedColours()
168 selectedButtons = new ArrayList<JButton>();
173 colorChooser.getSelectionModel().addChangeListener(this);
174 frame = new JInternalFrame();
175 frame.setContentPane(this);
176 Desktop.addInternalFrame(frame,
177 MessageManager.getString("label.user_defined_colours"),
178 MY_FRAME_WIDTH, MY_FRAME_HEIGHT, true);
179 if (seqGroup != null)
181 frame.setTitle(frame.getTitle() + " (" + seqGroup.getName() + ")");
186 * Rebuilds the panel with coloured buttons for residues. If not case
187 * sensitive colours, show 3-letter amino acid code as button text. If case
188 * sensitive, just show the single letter code, in order to make space for the
189 * additional buttons.
191 * @param isCaseSensitive
193 void resetButtonPanel(boolean isCaseSensitive)
195 buttonPanel.removeAll();
197 if (upperCaseButtons == null)
199 upperCaseButtons = new ArrayList<JButton>();
202 for (int i = 0; i < 20; i++)
204 String label = isCaseSensitive ? ResidueProperties.aa[i]
205 : ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i])
207 JButton button = makeButton(label, ResidueProperties.aa[i],
208 upperCaseButtons, i);
209 buttonPanel.add(button);
212 buttonPanel.add(makeButton("B", "B", upperCaseButtons, 20));
213 buttonPanel.add(makeButton("Z", "Z", upperCaseButtons, 21));
214 buttonPanel.add(makeButton("X", "X", upperCaseButtons, 22));
215 buttonPanel.add(makeButton("Gap", "-", upperCaseButtons, 23));
217 if (!isCaseSensitive)
219 gridLayout.setRows(6);
220 gridLayout.setColumns(4);
224 gridLayout.setRows(7);
226 gridLayout.setColumns(cols + 1);
228 if (lowerCaseButtons == null)
230 lowerCaseButtons = new ArrayList<JButton>();
233 for (int i = 0; i < 20; i++)
235 int row = i / cols + 1;
236 int index = (row * cols) + i;
237 JButton button = makeButton(ResidueProperties.aa[i].toLowerCase(),
238 ResidueProperties.aa[i].toLowerCase(), lowerCaseButtons, i);
240 buttonPanel.add(button, index);
246 buttonPanel.add(makeButton("b", "b", lowerCaseButtons, 20));
247 buttonPanel.add(makeButton("z", "z", lowerCaseButtons, 21));
248 buttonPanel.add(makeButton("x", "x", lowerCaseButtons, 22));
251 // JAL-1360 widen the frame dynamically to accommodate case-sensitive AA
253 if (this.frame != null)
255 int newWidth = isCaseSensitive ? MY_FRAME_WIDTH_CASE_SENSITIVE
257 this.frame.setSize(newWidth, this.frame.getHeight());
260 buttonPanel.validate();
265 * ChangeListener handler for when a colour is picked in the colour chooser.
266 * The action is to apply the colour to all selected buttons as their
267 * background colour. Foreground colour (text) is set to a lighter shade in
268 * order to highlight which buttons are selected. If 'Lower Case Colour' is
269 * active, then the colour is applied to all lower case buttons (as well as
270 * the Lower Case Colour button itself).
275 public void stateChanged(ChangeEvent evt)
277 JButton button = null;
278 final Color newColour = colorChooser.getColor();
279 if (lcaseColour.isSelected())
281 selectedButtons.clear();
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()));
289 for (int i = 0; i < selectedButtons.size(); i++)
291 button = selectedButtons.get(i);
292 button.setBackground(newColour);
293 button.setForeground(ColorUtils.brighterThan(newColour));
298 * Performs actions when a residue button is clicked. This manages the button
299 * selection set (highlighted by brighter foreground text).
301 * On select button(s) with Ctrl/click or Shift/click: set button foreground
302 * text to brighter than background.
304 * On unselect button(s) with Ctrl/click on selected, or click to release
305 * current selection: reset foreground text to darker than background.
307 * Simple click: clear selection (resetting foreground to darker); set clicked
308 * button foreground to brighter
310 * Finally, synchronize the colour chooser to the colour of the first button
311 * in the selected set.
315 public void colourButtonPressed(MouseEvent e)
317 JButton pressed = (JButton) e.getSource();
321 JButton start, end = (JButton) e.getSource();
322 if (selectedButtons.size() > 0)
324 start = selectedButtons.get(selectedButtons.size() - 1);
328 start = (JButton) e.getSource();
331 int startIndex = 0, endIndex = 0;
332 for (int b = 0; b < buttonPanel.getComponentCount(); b++)
334 if (buttonPanel.getComponent(b) == start)
338 if (buttonPanel.getComponent(b) == end)
344 if (startIndex > endIndex)
346 int temp = startIndex;
347 startIndex = endIndex;
351 for (int b = startIndex; b <= endIndex; b++)
353 JButton button = (JButton) buttonPanel.getComponent(b);
354 if (!selectedButtons.contains(button))
356 button.setForeground(ColorUtils.brighterThan(button
358 selectedButtons.add(button);
362 else if (!e.isControlDown())
364 for (int b = 0; b < selectedButtons.size(); b++)
366 JButton button = selectedButtons.get(b);
367 button.setForeground(ColorUtils.darkerThan(button.getBackground()));
369 selectedButtons.clear();
370 pressed.setForeground(ColorUtils.brighterThan(pressed.getBackground()));
371 selectedButtons.add(pressed);
374 else if (e.isControlDown())
376 if (selectedButtons.contains(pressed))
378 pressed.setForeground(ColorUtils.darkerThan(pressed.getBackground()));
379 selectedButtons.remove(pressed);
383 pressed.setForeground(ColorUtils.brighterThan(pressed
385 selectedButtons.add(pressed);
389 if (selectedButtons.size() > 0)
391 colorChooser.setColor((selectedButtons.get(0)).getBackground());
396 * A helper method to update or make a colour button, whose background colour
397 * is the associated colour, and text colour a darker shade of the same. If
398 * the button is already in the list, then its text and margins are updated,
399 * if not then it is created and added. This method supports toggling between
400 * case-sensitive and case-insensitive button panels. The case-sensitive
401 * version has abbreviated button text in order to fit in more buttons.
408 * the button's position in the list
410 JButton makeButton(String label, String residue, List<JButton> buttons,
413 final JButton button;
416 if (buttonIndex < buttons.size())
418 button = buttons.get(buttonIndex);
419 col = button.getBackground();
423 button = new JButton();
424 button.addMouseListener(new MouseAdapter()
427 public void mouseClicked(MouseEvent e)
429 colourButtonPressed(e);
436 * make initial button colour that of the current colour scheme,
437 * if it is a simple per-residue colouring, else white
440 if (oldColourScheme != null && oldColourScheme.isSimple())
442 col = oldColourScheme.findColour(residue.charAt(0), 0, null, null,
447 if (caseSensitive.isSelected())
449 button.setMargin(new Insets(2, 2, 2, 2));
453 button.setMargin(new Insets(2, 14, 2, 14));
456 button.setOpaque(true); // required for the next line to have effect
457 button.setBackground(col);
458 button.setText(label);
459 button.setForeground(ColorUtils.darkerThan(col));
460 button.setFont(VERDANA_BOLD_10);
466 * On 'OK', check that at least one colour has been assigned to a residue (and
467 * if not issue a warning), and apply the chosen colour scheme and close the
471 protected void okButton_actionPerformed()
473 if (isNoSelectionMade())
475 JvOptionPane.showMessageDialog(Desktop.desktop, MessageManager
476 .getString("label.no_colour_selection_in_scheme"),
477 MessageManager.getString("label.no_colour_selection_warn"),
478 JvOptionPane.WARNING_MESSAGE);
482 applyButton_actionPerformed();
486 frame.setClosed(true);
487 } catch (Exception ex)
494 * Returns true if the user has not made any colour selection (including if
495 * 'case-sensitive' selected and no lower-case colour chosen).
499 protected boolean isNoSelectionMade()
501 final boolean noUpperCaseSelected = upperCaseButtons == null
502 || upperCaseButtons.isEmpty();
503 final boolean noLowerCaseSelected = caseSensitive.isSelected()
504 && (lowerCaseButtons == null || lowerCaseButtons.isEmpty());
505 final boolean noSelectionMade = noUpperCaseSelected
506 || noLowerCaseSelected;
507 return noSelectionMade;
511 * Applies the current colour scheme to the alignment, sequence group or
515 protected void applyButton_actionPerformed()
517 if (isNoSelectionMade())
519 JvOptionPane.showMessageDialog(Desktop.desktop, MessageManager
520 .getString("label.no_colour_selection_in_scheme"),
521 MessageManager.getString("label.no_colour_selection_warn"),
522 JvOptionPane.WARNING_MESSAGE);
525 UserColourScheme ucs = getSchemeFromButtons();
527 if (seqGroup != null)
529 seqGroup.setColourScheme(ucs);
530 ap.paintAlignment(true);
534 ap.alignFrame.changeColour(ucs);
536 else if (structureViewer != null)
538 structureViewer.setJalviewColourScheme(ucs);
542 UserColourScheme getSchemeFromButtons()
545 Color[] newColours = new Color[24];
547 int length = upperCaseButtons.size();
551 for (JButton btn : upperCaseButtons)
553 newColours[i] = btn.getBackground();
559 for (int i = 0; i < 24; i++)
561 JButton button = upperCaseButtons.get(i);
562 newColours[i] = button.getBackground();
566 UserColourScheme ucs = new UserColourScheme(newColours);
567 ucs.setName(schemeName.getText());
569 if (caseSensitive.isSelected())
571 newColours = new Color[23];
572 length = lowerCaseButtons.size();
576 for (JButton btn : lowerCaseButtons)
578 newColours[i] = btn.getBackground();
584 for (int i = 0; i < 23; i++)
586 JButton button = lowerCaseButtons.get(i);
587 newColours[i] = button.getBackground();
590 ucs.setLowerCaseColours(newColours);
603 protected void loadbutton_actionPerformed(ActionEvent e)
605 upperCaseButtons = new ArrayList<JButton>();
606 lowerCaseButtons = new ArrayList<JButton>();
608 JalviewFileChooser chooser = new JalviewFileChooser("jc",
609 "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 = ColourSchemeLoader.loadColourScheme(choice
626 Color[] colors = ucs.getColours();
627 schemeName.setText(ucs.getSchemeName());
629 if (ucs.getLowerCaseColours() != null)
631 caseSensitive.setSelected(true);
632 lcaseColour.setEnabled(true);
633 resetButtonPanel(true);
634 for (int i = 0; i < lowerCaseButtons.size(); i++)
636 JButton button = lowerCaseButtons.get(i);
637 button.setBackground(ucs.getLowerCaseColours()[i]);
642 caseSensitive.setSelected(false);
643 lcaseColour.setEnabled(false);
644 resetButtonPanel(false);
647 for (int i = 0; i < upperCaseButtons.size(); i++)
649 JButton button = upperCaseButtons.get(i);
650 button.setBackground(colors[i]);
653 addNewColourScheme(choice.getPath());
657 * Loads the user-defined colour scheme from the first file listed in property
658 * "USER_DEFINED_COLOURS". If this fails, returns an all-white colour scheme.
662 public static UserColourScheme loadDefaultColours()
664 UserColourScheme ret = null;
666 String colours = Cache.getProperty(USER_DEFINED_COLOURS);
669 if (colours.indexOf("|") > -1)
671 colours = colours.substring(0, colours.indexOf("|"));
673 ret = ColourSchemeLoader.loadColourScheme(colours);
678 ret = new UserColourScheme("white");
691 protected void savebutton_actionPerformed(ActionEvent e)
693 String name = schemeName.getText().trim();
694 if (name.length() < 1)
696 JvOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
697 .getString("label.user_colour_scheme_must_have_name"),
698 MessageManager.getString("label.no_name_colour_scheme"),
699 JvOptionPane.WARNING_MESSAGE);
703 if (ColourSchemes.getInstance().nameExists(name))
705 int reply = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
706 MessageManager.formatMessage(
707 "label.colour_scheme_exists_overwrite", new Object[] {
709 MessageManager.getString("label.duplicate_scheme_name"),
710 JvOptionPane.YES_NO_OPTION);
711 if (reply != JvOptionPane.YES_OPTION)
715 ColourSchemes.getInstance().removeColourScheme(name);
717 JalviewFileChooser chooser = new JalviewFileChooser("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.setColourScheme(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 boolean selected = caseSensitive.isSelected();
849 resetButtonPanel(selected);
850 lcaseColour.setEnabled(selected);