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.FileInputStream;
47 import java.io.FileOutputStream;
48 import java.io.InputStreamReader;
49 import java.io.OutputStreamWriter;
50 import java.io.PrintWriter;
51 import java.util.ArrayList;
52 import java.util.List;
53 import java.util.SortedMap;
54 import java.util.StringTokenizer;
55 import java.util.TreeMap;
57 import javax.swing.JButton;
58 import javax.swing.JInternalFrame;
59 import javax.swing.event.ChangeEvent;
60 import javax.swing.event.ChangeListener;
63 * This panel allows the user to assign colours to Amino Acid residue codes, and
64 * save the colour scheme.
66 * @author Andrew Waterhouse
67 * @author Mungo Carstairs
69 public class UserDefinedColours extends GUserDefinedColours implements
72 private static final Font VERDANA_BOLD_10 = new Font("Verdana", Font.BOLD, 10);
74 private static final String USER_DEFINED_COLOURS = "USER_DEFINED_COLOURS";
76 private static final String LAST_DIRECTORY = "LAST_DIRECTORY";
78 private static final int MY_FRAME_HEIGHT = 420;
80 private static final int MY_FRAME_WIDTH = 810;
82 private static final int MY_FRAME_WIDTH_CASE_SENSITIVE = 970;
84 static SortedMap<String, UserColourScheme> userColourSchemes;
88 SequenceGroup seqGroup;
90 List<JButton> selectedButtons;
92 ColourSchemeI oldColourScheme;
96 JalviewStructureDisplayI structureViewer;
98 List<JButton> upperCaseButtons;
100 List<JButton> lowerCaseButtons;
103 * Creates a new UserDefinedColours object.
108 public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)
112 lcaseColour.setEnabled(false);
117 if (seqGroup != null)
119 oldColourScheme = seqGroup.cs;
123 oldColourScheme = ap.av.getGlobalColourScheme();
126 if (oldColourScheme instanceof UserColourScheme)
128 schemeName.setText(((UserColourScheme) oldColourScheme).getSchemeName());
129 if (((UserColourScheme) oldColourScheme).getLowerCaseColours() != null)
131 caseSensitive.setSelected(true);
132 lcaseColour.setEnabled(true);
133 resetButtonPanel(true);
137 resetButtonPanel(false);
142 resetButtonPanel(false);
148 public UserDefinedColours(JalviewStructureDisplayI viewer,
152 this.structureViewer = viewer;
154 colorChooser.getSelectionModel().addChangeListener(this);
156 oldColourScheme = oldcs;
158 if (oldColourScheme instanceof UserColourScheme)
160 schemeName.setText(((UserColourScheme) oldColourScheme).getSchemeName());
163 resetButtonPanel(false);
171 colorChooser.getSelectionModel().addChangeListener(this);
172 frame = new JInternalFrame();
173 frame.setContentPane(this);
174 Desktop.addInternalFrame(frame,
175 MessageManager.getString("label.user_defined_colours"),
176 MY_FRAME_WIDTH, MY_FRAME_HEIGHT, true);
178 if (seqGroup != null)
180 frame.setTitle(frame.getTitle() + " (" + seqGroup.getName() + ")");
184 void resetButtonPanel(boolean caseSensitive)
186 buttonPanel.removeAll();
188 if (upperCaseButtons == null)
190 upperCaseButtons = new ArrayList<JButton>();
195 for (int i = 0; i < 20; i++)
199 label = ResidueProperties.aa[i];
203 label = ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i])
207 button = makeButton(label, ResidueProperties.aa[i], upperCaseButtons,
210 buttonPanel.add(button);
213 buttonPanel.add(makeButton("B", "B", upperCaseButtons, 20));
214 buttonPanel.add(makeButton("Z", "Z", upperCaseButtons, 21));
215 buttonPanel.add(makeButton("X", "X", upperCaseButtons, 22));
216 buttonPanel.add(makeButton("Gap", "-", upperCaseButtons, 23));
220 gridLayout.setRows(6);
221 gridLayout.setColumns(4);
225 gridLayout.setRows(7);
227 gridLayout.setColumns(cols + 1);
229 if (lowerCaseButtons == null)
231 lowerCaseButtons = new ArrayList<JButton>();
234 for (int i = 0; i < 20; i++)
236 int row = i / cols + 1;
237 int index = (row * cols) + i;
238 button = makeButton(ResidueProperties.aa[i].toLowerCase(),
239 ResidueProperties.aa[i].toLowerCase(), lowerCaseButtons, i);
241 buttonPanel.add(button, index);
247 buttonPanel.add(makeButton("b", "b", lowerCaseButtons, 20));
248 buttonPanel.add(makeButton("z", "z", lowerCaseButtons, 21));
249 buttonPanel.add(makeButton("x", "x", lowerCaseButtons, 22));
252 // JAL-1360 widen the frame dynamically to accommodate case-sensitive AA
254 if (this.frame != null)
256 int newWidth = caseSensitive ? MY_FRAME_WIDTH_CASE_SENSITIVE
258 this.frame.setSize(newWidth, this.frame.getHeight());
261 buttonPanel.validate();
272 public void stateChanged(ChangeEvent evt)
274 if (selectedButtons != null)
276 JButton button = null;
277 final Color newColour = colorChooser.getColor();
278 for (int i = 0; i < selectedButtons.size(); i++)
280 button = selectedButtons.get(i);
281 button.setBackground(newColour);
282 button.setForeground(ColorUtils.brighterThan(newColour));
284 if (button == lcaseColour)
286 for (int i = 0; i < lowerCaseButtons.size(); i++)
288 button = lowerCaseButtons.get(i);
289 button.setBackground(newColour);
290 button.setForeground(ColorUtils.brighterThan(button
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 if (selectedButtons == null)
319 selectedButtons = new ArrayList<JButton>();
322 JButton pressed = (JButton) e.getSource();
326 JButton start, end = (JButton) e.getSource();
327 if (selectedButtons.size() > 0)
329 start = selectedButtons.get(selectedButtons.size() - 1);
333 start = (JButton) e.getSource();
336 int startIndex = 0, endIndex = 0;
337 for (int b = 0; b < buttonPanel.getComponentCount(); b++)
339 if (buttonPanel.getComponent(b) == start)
343 if (buttonPanel.getComponent(b) == end)
349 if (startIndex > endIndex)
351 int temp = startIndex;
352 startIndex = endIndex;
356 for (int b = startIndex; b <= endIndex; b++)
358 JButton button = (JButton) buttonPanel.getComponent(b);
359 if (!selectedButtons.contains(button))
361 button.setForeground(ColorUtils.brighterThan(button
363 selectedButtons.add(button);
367 else if (!e.isControlDown())
369 for (int b = 0; b < selectedButtons.size(); b++)
371 JButton button = selectedButtons.get(b);
372 button.setForeground(ColorUtils.darkerThan(button.getBackground()));
374 selectedButtons.clear();
375 pressed.setForeground(ColorUtils.brighterThan(pressed.getBackground()));
376 selectedButtons.add(pressed);
379 else if (e.isControlDown())
381 if (selectedButtons.contains(pressed))
383 pressed.setForeground(ColorUtils.darkerThan(pressed.getBackground()));
384 selectedButtons.remove(pressed);
388 pressed.setForeground(ColorUtils.brighterThan(pressed
390 selectedButtons.add(pressed);
394 if (selectedButtons.size() > 0)
396 colorChooser.setColor((selectedButtons.get(0)).getBackground());
401 * A helper method to update or make a colour button, whose background colour
402 * is the associated colour, and text colour a darker shade of the same. If
403 * the button is already in the list, then its text and margins are updated,
404 * if not then it is created and added. This method supports toggling between
405 * case-sensitive and case-insensitive button panels. The case-sensitive
406 * version has abbreviated button text in order to fit in more buttons.
413 * the button's position in the list
415 JButton makeButton(String label, String residue,
416 List<JButton> buttons, int buttonIndex)
418 final JButton button;
421 if (buttonIndex < buttons.size())
423 button = buttons.get(buttonIndex);
424 col = button.getBackground();
428 button = new JButton();
429 button.addMouseListener(new MouseAdapter()
432 public void mouseClicked(MouseEvent e)
434 colourButtonPressed(e);
441 if (oldColourScheme != null)
445 col = oldColourScheme.findColour(residue.charAt(0), -1, null);
446 } catch (Exception ex)
452 if (caseSensitive.isSelected())
454 button.setMargin(new Insets(2, 2, 2, 2));
458 button.setMargin(new Insets(2, 14, 2, 14));
461 button.setOpaque(true); // required for the next line to have effect
462 button.setBackground(col);
463 button.setText(label);
464 button.setForeground(ColorUtils.darkerThan(col));
465 button.setFont(VERDANA_BOLD_10);
471 * On 'OK', check that at least one colour has been assigned to a residue (and
472 * if not issue a warning), and apply the chosen colour scheme and close the
476 protected void okButton_actionPerformed()
478 if (isNoSelectionMade())
480 JvOptionPane.showMessageDialog(Desktop.desktop, MessageManager
481 .getString("label.no_colour_selection_in_scheme"),
482 MessageManager.getString("label.no_colour_selection_warn"),
483 JvOptionPane.WARNING_MESSAGE);
487 applyButton_actionPerformed();
491 frame.setClosed(true);
492 } catch (Exception ex)
499 * Returns true if the user has not made any colour selection (including if
500 * 'case-sensitive' selected and no lower-case colour chosen).
504 protected boolean isNoSelectionMade()
506 final boolean noUpperCaseSelected = upperCaseButtons == null
507 || upperCaseButtons.isEmpty();
508 final boolean noLowerCaseSelected = caseSensitive.isSelected()
509 && (lowerCaseButtons == null || lowerCaseButtons.isEmpty());
510 final boolean noSelectionMade = noUpperCaseSelected
511 || noLowerCaseSelected;
512 return noSelectionMade;
516 * Applies the current colour scheme to the alignment, sequence group or
520 protected void applyButton_actionPerformed()
522 if (isNoSelectionMade())
524 JvOptionPane.showMessageDialog(Desktop.desktop, MessageManager
525 .getString("label.no_colour_selection_in_scheme"),
526 MessageManager.getString("label.no_colour_selection_warn"),
527 JvOptionPane.WARNING_MESSAGE);
530 UserColourScheme ucs = getSchemeFromButtons();
531 ucs.setName(schemeName.getText());
533 if (seqGroup != null)
536 ap.paintAlignment(true);
540 ap.alignFrame.changeColour(ucs);
542 else if (structureViewer != null)
544 structureViewer.setJalviewColourScheme(ucs);
548 UserColourScheme getSchemeFromButtons()
551 Color[] newColours = new Color[24];
553 int length = upperCaseButtons.size();
557 for (JButton btn : upperCaseButtons)
559 newColours[i] = btn.getBackground();
565 for (int i = 0; i < 24; i++)
567 JButton button = upperCaseButtons.get(i);
568 newColours[i] = button.getBackground();
572 UserColourScheme ucs = new UserColourScheme(newColours);
574 if (caseSensitive.isSelected())
576 newColours = new Color[23];
577 length = lowerCaseButtons.size();
581 for (JButton btn : lowerCaseButtons)
583 newColours[i] = btn.getBackground();
589 for (int i = 0; i < 23; i++)
591 JButton button = lowerCaseButtons.get(i);
592 newColours[i] = button.getBackground();
595 ucs.setLowerCaseColours(newColours);
600 ucs.setThreshold(0, ap.av.isIgnoreGapsConsensus());
613 protected void loadbutton_actionPerformed(ActionEvent e)
615 upperCaseButtons = new ArrayList<JButton>();
616 lowerCaseButtons = new ArrayList<JButton>();
618 JalviewFileChooser chooser = new JalviewFileChooser(
619 Cache.getProperty(LAST_DIRECTORY), "jc", "Jalview User Colours");
620 chooser.setFileView(new JalviewFileView());
621 chooser.setDialogTitle(MessageManager
622 .getString("label.load_colour_scheme"));
623 chooser.setToolTipText(MessageManager.getString("action.load"));
625 int value = chooser.showOpenDialog(this);
627 if (value != JalviewFileChooser.APPROVE_OPTION)
631 File choice = chooser.getSelectedFile();
632 Cache.setProperty(LAST_DIRECTORY, choice.getParent());
634 UserColourScheme ucs = loadColours(choice.getAbsolutePath());
635 Color[] colors = ucs.getColours();
636 schemeName.setText(ucs.getSchemeName());
638 if (ucs.getLowerCaseColours() != null)
640 caseSensitive.setSelected(true);
641 lcaseColour.setEnabled(true);
642 resetButtonPanel(true);
643 for (int i = 0; i < lowerCaseButtons.size(); i++)
645 JButton button = lowerCaseButtons.get(i);
646 button.setBackground(ucs.getLowerCaseColours()[i]);
651 caseSensitive.setSelected(false);
652 lcaseColour.setEnabled(false);
653 resetButtonPanel(false);
656 for (int i = 0; i < upperCaseButtons.size(); i++)
658 JButton button = upperCaseButtons.get(i);
659 button.setBackground(colors[i]);
662 addNewColourScheme(choice.getPath());
668 * @return DOCUMENT ME!
670 public static UserColourScheme loadDefaultColours()
672 UserColourScheme ret = null;
674 String colours = Cache.getProperty(USER_DEFINED_COLOURS);
677 if (colours.indexOf("|") > -1)
679 colours = colours.substring(0, colours.indexOf("|"));
682 ret = loadColours(colours);
687 Color[] newColours = new Color[24];
688 for (int i = 0; i < 24; i++)
690 newColours[i] = Color.white;
692 ret = new UserColourScheme(newColours);
704 * @return DOCUMENT ME!
706 static UserColourScheme loadColours(String file)
708 UserColourScheme ucs = null;
709 Color[] newColours = null;
712 InputStreamReader in = new InputStreamReader(
713 new FileInputStream(file), "UTF-8");
715 jalview.schemabinding.version2.JalviewUserColours jucs = new jalview.schemabinding.version2.JalviewUserColours();
717 org.exolab.castor.xml.Unmarshaller unmar = new org.exolab.castor.xml.Unmarshaller(
719 jucs = (jalview.schemabinding.version2.JalviewUserColours) unmar
722 newColours = new Color[24];
724 Color[] lowerCase = null;
725 boolean caseSensitive = false;
729 for (int i = 0; i < jucs.getColourCount(); i++)
731 name = jucs.getColour(i).getName();
732 if (ResidueProperties.aa3Hash.containsKey(name))
734 index = ResidueProperties.aa3Hash.get(name).intValue();
738 index = ResidueProperties.aaIndex[name.charAt(0)];
745 if (name.toLowerCase().equals(name))
747 if (lowerCase == null)
749 lowerCase = new Color[23];
751 caseSensitive = true;
752 lowerCase[index] = new Color(Integer.parseInt(jucs.getColour(i)
757 newColours[index] = new Color(Integer.parseInt(jucs.getColour(i)
762 if (newColours != null)
764 ucs = new UserColourScheme(newColours);
765 ucs.setName(jucs.getSchemeName());
768 ucs.setLowerCaseColours(lowerCase);
772 } catch (Exception ex)
774 // Could be Archive Jalview format
777 InputStreamReader in = new InputStreamReader(new FileInputStream(
780 jalview.binding.JalviewUserColours jucs = new jalview.binding.JalviewUserColours();
782 jucs = jucs.unmarshal(in);
784 newColours = new Color[jucs.getColourCount()];
786 for (int i = 0; i < 24; i++)
788 newColours[i] = new Color(Integer.parseInt(jucs.getColour(i)
791 if (newColours != null)
793 ucs = new UserColourScheme(newColours);
794 ucs.setName(jucs.getSchemeName());
796 } catch (Exception ex2)
798 ex2.printStackTrace();
801 if (newColours == null)
803 System.out.println("Error loading User ColourFile\n" + ex);
817 protected void savebutton_actionPerformed(ActionEvent e)
819 if (schemeName.getText().trim().length() < 1)
821 JvOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
822 .getString("label.user_colour_scheme_must_have_name"),
823 MessageManager.getString("label.no_name_colour_scheme"),
824 JvOptionPane.WARNING_MESSAGE);
828 if (userColourSchemes != null
829 && userColourSchemes.containsKey(schemeName.getText()))
831 int reply = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
832 MessageManager.formatMessage(
833 "label.colour_scheme_exists_overwrite", new Object[] {
834 schemeName.getText(), schemeName.getText() }),
835 MessageManager.getString("label.duplicate_scheme_name"),
836 JvOptionPane.YES_NO_OPTION);
837 if (reply != JvOptionPane.YES_OPTION)
842 userColourSchemes.remove(schemeName.getText());
844 JalviewFileChooser chooser = new JalviewFileChooser(
845 Cache.getProperty(LAST_DIRECTORY), "jc",
846 "Jalview User Colours");
848 chooser.setFileView(new JalviewFileView());
849 chooser.setDialogTitle(MessageManager
850 .getString("label.save_colour_scheme"));
851 chooser.setToolTipText(MessageManager.getString("action.save"));
853 int value = chooser.showSaveDialog(this);
855 if (value == JalviewFileChooser.APPROVE_OPTION)
857 String choice = chooser.getSelectedFile().getPath();
858 addNewColourScheme(choice);
864 * Adds the current colour scheme to the Jalview properties file so it is
865 * loaded on next startup, and updates the Colour menu in the parent
866 * AlignFrame (if there is one). Note this action does not including applying
871 protected void addNewColourScheme(String filePath)
874 * update the delimited list of user defined colour files in
875 * Jalview property USER_DEFINED_COLOURS
877 String defaultColours = Cache
878 .getDefault(USER_DEFINED_COLOURS, filePath);
879 if (defaultColours.indexOf(filePath) == -1)
881 if (defaultColours.length() > 0)
883 defaultColours = defaultColours.concat("|");
885 defaultColours = defaultColours.concat(filePath);
887 Cache.setProperty(USER_DEFINED_COLOURS, defaultColours);
890 * add to the cache in this object
892 UserColourScheme ucs = getSchemeFromButtons();
893 String name = schemeName.getText();
894 userColourSchemes.put(name, ucs);
896 ColourSchemes.getInstance().registerColourScheme(ucs);
899 * update the Colour menu items
903 ap.alignFrame.buildColourMenu();
908 * Saves the colour scheme to file in XML format
912 protected void saveToFile(String filePath)
915 * build a Java model of colour scheme as XML, and
918 JalviewUserColours ucs = new JalviewUserColours();
919 ucs.setSchemeName(schemeName.getText());
922 PrintWriter out = new PrintWriter(new OutputStreamWriter(
923 new FileOutputStream(filePath), "UTF-8"));
925 for (int i = 0; i < buttonPanel.getComponentCount(); i++)
927 JButton button = (JButton) buttonPanel.getComponent(i);
928 Colour col = new Colour();
929 col.setName(button.getText());
930 col.setRGB(Format.getHexString(button.getBackground()));
935 } catch (Exception ex)
937 ex.printStackTrace();
942 * On cancel, restores the colour scheme before the dialogue was opened
947 protected void cancelButton_actionPerformed(ActionEvent e)
951 if (seqGroup != null)
953 seqGroup.cs = oldColourScheme;
957 ap.alignFrame.changeColour(oldColourScheme);
959 ap.paintAlignment(true);
962 if (structureViewer != null)
964 structureViewer.setJalviewColourScheme(oldColourScheme);
969 frame.setClosed(true);
970 } catch (Exception ex)
975 public static SortedMap<String, UserColourScheme> getUserColourSchemes()
977 return userColourSchemes;
980 public static void initUserColourSchemes(String files)
982 userColourSchemes = new TreeMap<String, UserColourScheme>();
984 if (files == null || files.length() == 0)
989 // In case colours can't be loaded, we'll remove them
990 // from the default list here.
991 StringBuffer coloursFound = new StringBuffer();
992 StringTokenizer st = new StringTokenizer(files, "|");
993 while (st.hasMoreElements())
995 String file = st.nextToken();
998 UserColourScheme ucs = loadColours(file);
1001 if (coloursFound.length() > 0)
1003 coloursFound.append("|");
1005 coloursFound.append(file);
1006 String name = ucs.getName();
1007 userColourSchemes.put(name, ucs);
1008 ColourSchemes.getInstance().registerColourScheme(ucs);
1010 } catch (Exception ex)
1012 System.out.println("Error loading User ColourFile\n" + ex);
1015 if (!files.equals(coloursFound.toString()))
1017 if (coloursFound.toString().length() > 1)
1019 Cache.setProperty(USER_DEFINED_COLOURS, coloursFound.toString());
1023 Cache.applicationProperties.remove(USER_DEFINED_COLOURS);
1028 public static void removeColourFromDefaults(String target)
1030 // The only way to find colours by name is to load them in
1031 // In case colours can't be loaded, we'll remove them
1032 // from the default list here.
1034 userColourSchemes = new TreeMap<String, UserColourScheme>();
1036 StringBuffer coloursFound = new StringBuffer();
1037 StringTokenizer st = new StringTokenizer(
1038 Cache.getProperty(USER_DEFINED_COLOURS), "|");
1040 while (st.hasMoreElements())
1042 String file = st.nextToken();
1045 UserColourScheme ucs = loadColours(file);
1046 if (ucs != null && !ucs.getSchemeName().equals(target))
1048 if (coloursFound.length() > 0)
1050 coloursFound.append("|");
1052 coloursFound.append(file);
1053 userColourSchemes.put(ucs.getSchemeName(), ucs);
1055 } catch (Exception ex)
1057 System.out.println("Error loading User ColourFile\n" + ex);
1061 if (coloursFound.toString().length() > 1)
1063 Cache.setProperty(USER_DEFINED_COLOURS, coloursFound.toString());
1067 Cache.applicationProperties.remove(USER_DEFINED_COLOURS);
1073 public void caseSensitive_actionPerformed(ActionEvent e)
1075 resetButtonPanel(caseSensitive.isSelected());
1076 lcaseColour.setEnabled(caseSensitive.isSelected());
1080 public void lcaseColour_actionPerformed(ActionEvent e)
1082 if (selectedButtons == null)
1084 selectedButtons = new ArrayList<JButton>();
1088 selectedButtons.clear();
1090 selectedButtons.add(lcaseColour);