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.ResidueProperties;
33 import jalview.schemes.UserColourScheme;
34 import jalview.util.ColorUtils;
35 import jalview.util.Format;
36 import jalview.util.MessageManager;
38 import java.awt.Color;
40 import java.awt.Insets;
41 import java.awt.event.ActionEvent;
42 import java.awt.event.MouseAdapter;
43 import java.awt.event.MouseEvent;
45 import java.io.FileInputStream;
46 import java.io.FileOutputStream;
47 import java.io.InputStreamReader;
48 import java.io.OutputStreamWriter;
49 import java.io.PrintWriter;
50 import java.util.ArrayList;
51 import java.util.List;
52 import java.util.SortedMap;
53 import java.util.StringTokenizer;
54 import java.util.TreeMap;
56 import javax.swing.JButton;
57 import javax.swing.JInternalFrame;
58 import javax.swing.event.ChangeEvent;
59 import javax.swing.event.ChangeListener;
62 * This panel allows the user to assign colours to Amino Acid residue codes, and
63 * save the colour scheme.
65 * @author Andrew Waterhouse
66 * @author Mungo Carstairs
68 public class UserDefinedColours extends GUserDefinedColours implements
71 private static final Font VERDANA_BOLD_10 = new Font("Verdana", Font.BOLD, 10);
73 private static final String USER_DEFINED_COLOURS = "USER_DEFINED_COLOURS";
75 private static final String LAST_DIRECTORY = "LAST_DIRECTORY";
77 private static final int MY_FRAME_HEIGHT = 420;
79 private static final int MY_FRAME_WIDTH = 810;
81 private static final int MY_FRAME_WIDTH_CASE_SENSITIVE = 970;
83 static SortedMap<String, UserColourScheme> userColourSchemes;
87 SequenceGroup seqGroup;
89 List<JButton> selectedButtons;
91 ColourSchemeI oldColourScheme;
95 JalviewStructureDisplayI structureViewer;
97 List<JButton> upperCaseButtons;
99 List<JButton> lowerCaseButtons;
102 * Creates a new UserDefinedColours object.
107 public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)
111 lcaseColour.setEnabled(false);
116 if (seqGroup != null)
118 oldColourScheme = seqGroup.cs;
122 oldColourScheme = ap.av.getGlobalColourScheme();
125 if (oldColourScheme instanceof UserColourScheme)
127 schemeName.setText(((UserColourScheme) oldColourScheme).getName());
128 if (((UserColourScheme) oldColourScheme).getLowerCaseColours() != null)
130 caseSensitive.setSelected(true);
131 lcaseColour.setEnabled(true);
132 resetButtonPanel(true);
136 resetButtonPanel(false);
141 resetButtonPanel(false);
147 public UserDefinedColours(JalviewStructureDisplayI viewer,
151 this.structureViewer = viewer;
153 colorChooser.getSelectionModel().addChangeListener(this);
155 oldColourScheme = oldcs;
157 if (oldColourScheme instanceof UserColourScheme)
159 schemeName.setText(((UserColourScheme) oldColourScheme).getName());
162 resetButtonPanel(false);
170 colorChooser.getSelectionModel().addChangeListener(this);
171 frame = new JInternalFrame();
172 frame.setContentPane(this);
173 Desktop.addInternalFrame(frame,
174 MessageManager.getString("label.user_defined_colours"),
175 MY_FRAME_WIDTH, MY_FRAME_HEIGHT, true);
177 if (seqGroup != null)
179 frame.setTitle(frame.getTitle() + " (" + seqGroup.getName() + ")");
183 void resetButtonPanel(boolean caseSensitive)
185 buttonPanel.removeAll();
187 if (upperCaseButtons == null)
189 upperCaseButtons = new ArrayList<JButton>();
194 for (int i = 0; i < 20; i++)
198 label = ResidueProperties.aa[i];
202 label = ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i])
206 button = makeButton(label, ResidueProperties.aa[i], upperCaseButtons,
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));
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 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 = caseSensitive ? MY_FRAME_WIDTH_CASE_SENSITIVE
257 this.frame.setSize(newWidth, this.frame.getHeight());
260 buttonPanel.validate();
271 public void stateChanged(ChangeEvent evt)
273 if (selectedButtons != null)
275 JButton button = null;
276 final Color newColour = colorChooser.getColor();
277 for (int i = 0; i < selectedButtons.size(); i++)
279 button = selectedButtons.get(i);
280 button.setBackground(newColour);
281 button.setForeground(ColorUtils.brighterThan(newColour));
283 if (button == lcaseColour)
285 for (int i = 0; i < lowerCaseButtons.size(); i++)
287 button = lowerCaseButtons.get(i);
288 button.setBackground(newColour);
289 button.setForeground(ColorUtils.brighterThan(button
297 * Performs actions when a residue button is clicked. This manages the button
298 * selection set (highlighted by brighter foreground text).
300 * On select button(s) with Ctrl/click or Shift/click: set button foreground
301 * text to brighter than background.
303 * On unselect button(s) with Ctrl/click on selected, or click to release
304 * current selection: reset foreground text to darker than background.
306 * Simple click: clear selection (resetting foreground to darker); set clicked
307 * button foreground to brighter
309 * Finally, synchronize the colour chooser to the colour of the first button
310 * in the selected set.
314 public void colourButtonPressed(MouseEvent e)
316 if (selectedButtons == null)
318 selectedButtons = new ArrayList<JButton>();
321 JButton pressed = (JButton) e.getSource();
325 JButton start, end = (JButton) e.getSource();
326 if (selectedButtons.size() > 0)
328 start = selectedButtons.get(selectedButtons.size() - 1);
332 start = (JButton) e.getSource();
335 int startIndex = 0, endIndex = 0;
336 for (int b = 0; b < buttonPanel.getComponentCount(); b++)
338 if (buttonPanel.getComponent(b) == start)
342 if (buttonPanel.getComponent(b) == end)
348 if (startIndex > endIndex)
350 int temp = startIndex;
351 startIndex = endIndex;
355 for (int b = startIndex; b <= endIndex; b++)
357 JButton button = (JButton) buttonPanel.getComponent(b);
358 if (!selectedButtons.contains(button))
360 button.setForeground(ColorUtils.brighterThan(button
362 selectedButtons.add(button);
366 else if (!e.isControlDown())
368 for (int b = 0; b < selectedButtons.size(); b++)
370 JButton button = selectedButtons.get(b);
371 button.setForeground(ColorUtils.darkerThan(button.getBackground()));
373 selectedButtons.clear();
374 pressed.setForeground(ColorUtils.brighterThan(pressed.getBackground()));
375 selectedButtons.add(pressed);
378 else if (e.isControlDown())
380 if (selectedButtons.contains(pressed))
382 pressed.setForeground(ColorUtils.darkerThan(pressed.getBackground()));
383 selectedButtons.remove(pressed);
387 pressed.setForeground(ColorUtils.brighterThan(pressed
389 selectedButtons.add(pressed);
393 if (selectedButtons.size() > 0)
395 colorChooser.setColor((selectedButtons.get(0)).getBackground());
400 * A helper method to update or make a colour button, whose background colour
401 * is the associated colour, and text colour a darker shade of the same. If
402 * the button is already in the list, then its text and margins are updated,
403 * if not then it is created and added. This method supports toggling between
404 * case-sensitive and case-insensitive button panels. The case-sensitive
405 * version has abbreviated button text in order to fit in more buttons.
412 * the button's position in the list
414 JButton makeButton(String label, String residue,
415 List<JButton> buttons, int buttonIndex)
417 final JButton button;
420 if (buttonIndex < buttons.size())
422 button = buttons.get(buttonIndex);
423 col = button.getBackground();
427 button = new JButton();
428 button.addMouseListener(new MouseAdapter()
431 public void mouseClicked(MouseEvent e)
433 colourButtonPressed(e);
440 if (oldColourScheme != null)
444 col = oldColourScheme.findColour(residue.charAt(0), -1, null);
445 } catch (Exception ex)
451 if (caseSensitive.isSelected())
453 button.setMargin(new Insets(2, 2, 2, 2));
457 button.setMargin(new Insets(2, 14, 2, 14));
460 button.setOpaque(true); // required for the next line to have effect
461 button.setBackground(col);
462 button.setText(label);
463 button.setForeground(ColorUtils.darkerThan(col));
464 button.setFont(VERDANA_BOLD_10);
470 * On 'OK', check that at least one colour has been assigned to a residue (and
471 * if not issue a warning), and apply the chosen colour scheme and close the
475 protected void okButton_actionPerformed()
477 if (isNoSelectionMade())
479 JvOptionPane.showMessageDialog(Desktop.desktop, MessageManager
480 .getString("label.no_colour_selection_in_scheme"),
481 MessageManager.getString("label.no_colour_selection_warn"),
482 JvOptionPane.WARNING_MESSAGE);
486 applyButton_actionPerformed();
490 frame.setClosed(true);
491 } catch (Exception ex)
498 * Returns true if the user has not made any colour selection (including if
499 * 'case-sensitive' selected and no lower-case colour chosen).
503 protected boolean isNoSelectionMade()
505 final boolean noUpperCaseSelected = upperCaseButtons == null
506 || upperCaseButtons.isEmpty();
507 final boolean noLowerCaseSelected = caseSensitive.isSelected()
508 && (lowerCaseButtons == null || lowerCaseButtons.isEmpty());
509 final boolean noSelectionMade = noUpperCaseSelected
510 || noLowerCaseSelected;
511 return noSelectionMade;
515 * Applies the current colour scheme to the alignment, sequence group or
519 protected void applyButton_actionPerformed()
521 if (isNoSelectionMade())
523 JvOptionPane.showMessageDialog(Desktop.desktop, MessageManager
524 .getString("label.no_colour_selection_in_scheme"),
525 MessageManager.getString("label.no_colour_selection_warn"),
526 JvOptionPane.WARNING_MESSAGE);
529 UserColourScheme ucs = getSchemeFromButtons();
530 ucs.setName(schemeName.getText());
532 if (seqGroup != null)
535 ap.paintAlignment(true);
539 ap.alignFrame.changeColour(ucs);
541 else if (structureViewer != null)
543 structureViewer.setJalviewColourScheme(ucs);
547 UserColourScheme getSchemeFromButtons()
550 Color[] newColours = new Color[24];
552 int length = upperCaseButtons.size();
556 for (JButton btn : upperCaseButtons)
558 newColours[i] = btn.getBackground();
564 for (int i = 0; i < 24; i++)
566 JButton button = upperCaseButtons.get(i);
567 newColours[i] = button.getBackground();
571 UserColourScheme ucs = new UserColourScheme(newColours);
573 if (caseSensitive.isSelected())
575 newColours = new Color[23];
576 length = lowerCaseButtons.size();
580 for (JButton btn : lowerCaseButtons)
582 newColours[i] = btn.getBackground();
588 for (int i = 0; i < 23; i++)
590 JButton button = lowerCaseButtons.get(i);
591 newColours[i] = button.getBackground();
594 ucs.setLowerCaseColours(newColours);
599 ucs.setThreshold(0, ap.av.isIgnoreGapsConsensus());
612 protected void loadbutton_actionPerformed(ActionEvent e)
614 upperCaseButtons = new ArrayList<JButton>();
615 lowerCaseButtons = new ArrayList<JButton>();
617 JalviewFileChooser chooser = new JalviewFileChooser(
618 Cache.getProperty(LAST_DIRECTORY), "jc", "Jalview User Colours");
619 chooser.setFileView(new JalviewFileView());
620 chooser.setDialogTitle(MessageManager
621 .getString("label.load_colour_scheme"));
622 chooser.setToolTipText(MessageManager.getString("action.load"));
624 int value = chooser.showOpenDialog(this);
626 if (value != JalviewFileChooser.APPROVE_OPTION)
630 File choice = chooser.getSelectedFile();
631 Cache.setProperty(LAST_DIRECTORY, choice.getParent());
633 UserColourScheme ucs = loadColours(choice.getAbsolutePath());
634 Color[] colors = ucs.getColours();
635 schemeName.setText(ucs.getName());
637 if (ucs.getLowerCaseColours() != null)
639 caseSensitive.setSelected(true);
640 lcaseColour.setEnabled(true);
641 resetButtonPanel(true);
642 for (int i = 0; i < lowerCaseButtons.size(); i++)
644 JButton button = lowerCaseButtons.get(i);
645 button.setBackground(ucs.getLowerCaseColours()[i]);
650 caseSensitive.setSelected(false);
651 lcaseColour.setEnabled(false);
652 resetButtonPanel(false);
655 for (int i = 0; i < upperCaseButtons.size(); i++)
657 JButton button = upperCaseButtons.get(i);
658 button.setBackground(colors[i]);
661 addNewColourScheme(choice.getPath());
667 * @return DOCUMENT ME!
669 public static UserColourScheme loadDefaultColours()
671 UserColourScheme ret = null;
673 String colours = Cache.getProperty(USER_DEFINED_COLOURS);
676 if (colours.indexOf("|") > -1)
678 colours = colours.substring(0, colours.indexOf("|"));
681 ret = loadColours(colours);
686 Color[] newColours = new Color[24];
687 for (int i = 0; i < 24; i++)
689 newColours[i] = Color.white;
691 ret = new UserColourScheme(newColours);
703 * @return DOCUMENT ME!
705 static UserColourScheme loadColours(String file)
707 UserColourScheme ucs = null;
708 Color[] newColours = null;
711 InputStreamReader in = new InputStreamReader(
712 new FileInputStream(file), "UTF-8");
714 jalview.schemabinding.version2.JalviewUserColours jucs = new jalview.schemabinding.version2.JalviewUserColours();
716 org.exolab.castor.xml.Unmarshaller unmar = new org.exolab.castor.xml.Unmarshaller(
718 jucs = (jalview.schemabinding.version2.JalviewUserColours) unmar
721 newColours = new Color[24];
723 Color[] lowerCase = null;
724 boolean caseSensitive = false;
728 for (int i = 0; i < jucs.getColourCount(); i++)
730 name = jucs.getColour(i).getName();
731 if (ResidueProperties.aa3Hash.containsKey(name))
733 index = ResidueProperties.aa3Hash.get(name).intValue();
737 index = ResidueProperties.aaIndex[name.charAt(0)];
744 if (name.toLowerCase().equals(name))
746 if (lowerCase == null)
748 lowerCase = new Color[23];
750 caseSensitive = true;
751 lowerCase[index] = new Color(Integer.parseInt(jucs.getColour(i)
756 newColours[index] = new Color(Integer.parseInt(jucs.getColour(i)
761 if (newColours != null)
763 ucs = new UserColourScheme(newColours);
764 ucs.setName(jucs.getSchemeName());
767 ucs.setLowerCaseColours(lowerCase);
771 } catch (Exception ex)
773 // Could be Archive Jalview format
776 InputStreamReader in = new InputStreamReader(new FileInputStream(
779 jalview.binding.JalviewUserColours jucs = new jalview.binding.JalviewUserColours();
781 jucs = jucs.unmarshal(in);
783 newColours = new Color[jucs.getColourCount()];
785 for (int i = 0; i < 24; i++)
787 newColours[i] = new Color(Integer.parseInt(jucs.getColour(i)
790 if (newColours != null)
792 ucs = new UserColourScheme(newColours);
793 ucs.setName(jucs.getSchemeName());
795 } catch (Exception ex2)
797 ex2.printStackTrace();
800 if (newColours == null)
802 System.out.println("Error loading User ColourFile\n" + ex);
816 protected void savebutton_actionPerformed(ActionEvent e)
818 if (schemeName.getText().trim().length() < 1)
820 JvOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
821 .getString("label.user_colour_scheme_must_have_name"),
822 MessageManager.getString("label.no_name_colour_scheme"),
823 JvOptionPane.WARNING_MESSAGE);
827 if (userColourSchemes != null
828 && userColourSchemes.containsKey(schemeName.getText()))
830 int reply = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
831 MessageManager.formatMessage(
832 "label.colour_scheme_exists_overwrite", new Object[] {
833 schemeName.getText(), schemeName.getText() }),
834 MessageManager.getString("label.duplicate_scheme_name"),
835 JvOptionPane.YES_NO_OPTION);
836 if (reply != JvOptionPane.YES_OPTION)
841 userColourSchemes.remove(schemeName.getText());
843 JalviewFileChooser chooser = new JalviewFileChooser(
844 Cache.getProperty(LAST_DIRECTORY), "jc",
845 "Jalview User Colours");
847 chooser.setFileView(new JalviewFileView());
848 chooser.setDialogTitle(MessageManager
849 .getString("label.save_colour_scheme"));
850 chooser.setToolTipText(MessageManager.getString("action.save"));
852 int value = chooser.showSaveDialog(this);
854 if (value == JalviewFileChooser.APPROVE_OPTION)
856 String choice = chooser.getSelectedFile().getPath();
857 addNewColourScheme(choice);
863 * Adds the current colour scheme to the Jalview properties file so it is
864 * loaded on next startup, and updates the Colour menu in the parent
865 * AlignFrame (if there is one). Note this action does not including applying
870 protected void addNewColourScheme(String filePath)
873 * update the delimited list of user defined colour files in
874 * Jalview property USER_DEFINED_COLOURS
876 String defaultColours = Cache
877 .getDefault(USER_DEFINED_COLOURS, filePath);
878 if (defaultColours.indexOf(filePath) == -1)
880 if (defaultColours.length() > 0)
882 defaultColours = defaultColours.concat("|");
884 defaultColours = defaultColours.concat(filePath);
886 Cache.setProperty(USER_DEFINED_COLOURS, defaultColours);
889 * add to the cache in this object
891 userColourSchemes.put(schemeName.getText(), getSchemeFromButtons());
894 * update the Colour menu items
898 ap.alignFrame.buildColourMenu();// updateUserColourMenu();
903 * Saves the colour scheme to file in XML format
907 protected void saveToFile(String filePath)
910 * build a Java model of colour scheme as XML, and
913 JalviewUserColours ucs = new JalviewUserColours();
914 ucs.setSchemeName(schemeName.getText());
917 PrintWriter out = new PrintWriter(new OutputStreamWriter(
918 new FileOutputStream(filePath), "UTF-8"));
920 for (int i = 0; i < buttonPanel.getComponentCount(); i++)
922 JButton button = (JButton) buttonPanel.getComponent(i);
923 Colour col = new Colour();
924 col.setName(button.getText());
925 col.setRGB(Format.getHexString(button.getBackground()));
930 } catch (Exception ex)
932 ex.printStackTrace();
943 protected void cancelButton_actionPerformed(ActionEvent e)
947 if (seqGroup != null)
949 seqGroup.cs = oldColourScheme;
953 ap.av.setGlobalColourScheme(oldColourScheme);
955 ap.paintAlignment(true);
958 if (structureViewer != null)
960 structureViewer.setJalviewColourScheme(oldColourScheme);
965 frame.setClosed(true);
966 } catch (Exception ex)
971 public static SortedMap<String, UserColourScheme> getUserColourSchemes()
973 return userColourSchemes;
976 public static void initUserColourSchemes(String files)
978 userColourSchemes = new TreeMap<String, UserColourScheme>();
980 if (files == null || files.length() == 0)
985 // In case colours can't be loaded, we'll remove them
986 // from the default list here.
987 StringBuffer coloursFound = new StringBuffer();
988 StringTokenizer st = new StringTokenizer(files, "|");
989 while (st.hasMoreElements())
991 String file = st.nextToken();
994 UserColourScheme ucs = loadColours(file);
997 if (coloursFound.length() > 0)
999 coloursFound.append("|");
1001 coloursFound.append(file);
1002 userColourSchemes.put(ucs.getName(), ucs);
1004 } catch (Exception ex)
1006 System.out.println("Error loading User ColourFile\n" + ex);
1009 if (!files.equals(coloursFound.toString()))
1011 if (coloursFound.toString().length() > 1)
1013 Cache.setProperty(USER_DEFINED_COLOURS, coloursFound.toString());
1017 Cache.applicationProperties.remove(USER_DEFINED_COLOURS);
1022 public static void removeColourFromDefaults(String target)
1024 // The only way to find colours by name is to load them in
1025 // In case colours can't be loaded, we'll remove them
1026 // from the default list here.
1028 userColourSchemes = new TreeMap<String, UserColourScheme>();
1030 StringBuffer coloursFound = new StringBuffer();
1031 StringTokenizer st = new StringTokenizer(
1032 Cache.getProperty(USER_DEFINED_COLOURS), "|");
1034 while (st.hasMoreElements())
1036 String file = st.nextToken();
1039 UserColourScheme ucs = loadColours(file);
1040 if (ucs != null && !ucs.getName().equals(target))
1042 if (coloursFound.length() > 0)
1044 coloursFound.append("|");
1046 coloursFound.append(file);
1047 userColourSchemes.put(ucs.getName(), ucs);
1049 } catch (Exception ex)
1051 System.out.println("Error loading User ColourFile\n" + ex);
1055 if (coloursFound.toString().length() > 1)
1057 Cache.setProperty(USER_DEFINED_COLOURS, coloursFound.toString());
1061 Cache.applicationProperties.remove(USER_DEFINED_COLOURS);
1067 public void caseSensitive_actionPerformed(ActionEvent e)
1069 resetButtonPanel(caseSensitive.isSelected());
1070 lcaseColour.setEnabled(caseSensitive.isSelected());
1074 public void lcaseColour_actionPerformed(ActionEvent e)
1076 if (selectedButtons == null)
1078 selectedButtons = new ArrayList<JButton>();
1082 selectedButtons.clear();
1084 selectedButtons.add(lcaseColour);