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.datamodel.SequenceGroup;
25 import jalview.io.JalviewFileChooser;
26 import jalview.io.JalviewFileView;
27 import jalview.jbgui.GUserDefinedColours;
28 import jalview.schemes.ColourSchemeI;
29 import jalview.schemes.ResidueProperties;
30 import jalview.schemes.UserColourScheme;
31 import jalview.util.ColorUtils;
32 import jalview.util.MessageManager;
34 import java.awt.Color;
36 import java.awt.event.ActionEvent;
37 import java.awt.event.MouseEvent;
39 import java.io.FileInputStream;
40 import java.io.FileOutputStream;
41 import java.io.InputStreamReader;
42 import java.io.OutputStreamWriter;
43 import java.io.PrintWriter;
44 import java.util.ArrayList;
45 import java.util.Hashtable;
46 import java.util.StringTokenizer;
48 import javax.swing.JButton;
49 import javax.swing.JInternalFrame;
50 import javax.swing.event.ChangeEvent;
51 import javax.swing.event.ChangeListener;
54 * This panel allows the user to assign colours to Amino Acid residue codes, and
55 * save the colour scheme.
57 * @author Andrew Waterhouse
58 * @author Mungo Carstairs
60 public class UserDefinedColours extends GUserDefinedColours implements
63 private static final int MY_FRAME_HEIGHT = 420;
65 private static final int MY_FRAME_WIDTH = 810;
67 private static final int MY_FRAME_WIDTH_CASE_SENSITIVE = 970;
71 SequenceGroup seqGroup;
73 ColourSchemeI oldColourScheme;
77 JalviewStructureDisplayI jmol;
79 ArrayList<JButton> upperCaseButtons;
81 ArrayList<JButton> lowerCaseButtons;
84 * Creates a new UserDefinedColours object.
91 public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)
95 lcaseColour.setEnabled(false);
100 if (seqGroup != null)
102 oldColourScheme = seqGroup.cs;
106 oldColourScheme = ap.av.getGlobalColourScheme();
109 if (oldColourScheme instanceof UserColourScheme)
111 schemeName.setText(((UserColourScheme) oldColourScheme).getName());
112 if (((UserColourScheme) oldColourScheme).getLowerCaseColours() != null)
114 caseSensitive.setSelected(true);
115 lcaseColour.setEnabled(true);
116 resetButtonPanel(true);
120 resetButtonPanel(false);
125 resetButtonPanel(false);
131 public UserDefinedColours(JalviewStructureDisplayI jmol,
137 colorChooser.getSelectionModel().addChangeListener(this);
139 oldColourScheme = oldcs;
141 if (oldColourScheme instanceof UserColourScheme)
143 schemeName.setText(((UserColourScheme) oldColourScheme).getName());
146 resetButtonPanel(false);
154 colorChooser.getSelectionModel().addChangeListener(this);
155 frame = new JInternalFrame();
156 frame.setContentPane(this);
157 Desktop.addInternalFrame(frame,
158 MessageManager.getString("label.user_defined_colours"),
159 MY_FRAME_WIDTH, MY_FRAME_HEIGHT, true);
161 if (seqGroup != null)
163 frame.setTitle(frame.getTitle() + " (" + seqGroup.getName() + ")");
167 void resetButtonPanel(boolean caseSensitive)
169 buttonPanel.removeAll();
171 if (upperCaseButtons == null)
173 upperCaseButtons = new ArrayList<JButton>();
178 for (int i = 0; i < 20; i++)
182 label = ResidueProperties.aa[i];
186 label = ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i])
190 button = makeButton(label, ResidueProperties.aa[i], upperCaseButtons,
193 buttonPanel.add(button);
196 buttonPanel.add(makeButton("B", "B", upperCaseButtons, 20));
197 buttonPanel.add(makeButton("Z", "Z", upperCaseButtons, 21));
198 buttonPanel.add(makeButton("X", "X", upperCaseButtons, 22));
199 buttonPanel.add(makeButton("Gap", "-", upperCaseButtons, 23));
203 gridLayout.setRows(6);
204 gridLayout.setColumns(4);
208 gridLayout.setRows(7);
210 gridLayout.setColumns(cols + 1);
212 if (lowerCaseButtons == null)
214 lowerCaseButtons = new ArrayList<JButton>();
217 for (int i = 0; i < 20; i++)
219 int row = i / cols + 1;
220 int index = (row * cols) + i;
221 button = makeButton(ResidueProperties.aa[i].toLowerCase(),
222 ResidueProperties.aa[i].toLowerCase(), lowerCaseButtons, i);
224 buttonPanel.add(button, index);
230 buttonPanel.add(makeButton("b", "b", lowerCaseButtons, 20));
231 buttonPanel.add(makeButton("z", "z", lowerCaseButtons, 21));
232 buttonPanel.add(makeButton("x", "x", lowerCaseButtons, 22));
235 // JAL-1360 widen the frame dynamically to accommodate case-sensitive AA
237 if (this.frame != null)
239 int newWidth = caseSensitive ? MY_FRAME_WIDTH_CASE_SENSITIVE
241 this.frame.setSize(newWidth, this.frame.getHeight());
244 buttonPanel.validate();
249 * ChangeListener handler for when a colour is picked in the colour chooser.
250 * The action is to apply the colour to all selected buttons as their
251 * background colour. Foreground colour (text) is set to a lighter shade in
252 * order to highlight which buttons are selected. If 'Lower Case Colour' is
253 * active, then the colour is applied to all lower case buttons (as well as
254 * the Lower Case Colour button itself).
259 public void stateChanged(ChangeEvent evt)
261 JButton button = null;
262 final Color newColour = colorChooser.getColor();
263 for (int i = 0; i < selectedButtons.size(); i++)
265 button = selectedButtons.get(i);
266 button.setBackground(newColour);
267 button.setForeground(ColorUtils.brighterThan(newColour));
269 if (button == lcaseColour)
271 button.setForeground(Color.black);
272 for (int i = 0; i < lowerCaseButtons.size(); i++)
274 button = lowerCaseButtons.get(i);
275 button.setBackground(newColour);
276 button.setForeground(ColorUtils.brighterThan(button.getBackground()));
282 * Performs actions when a residue button is clicked. This manages the button
283 * selection set (highlighted by brighter foreground text).
285 * On select button(s) with Ctrl/click or Shift/click: set button foreground
286 * text to brighter than background.
288 * On unselect button(s) with Ctrl/click on selected, or click to release
289 * current selection: reset foreground text to darker than background.
291 * Simple click: clear selection (resetting foreground to darker); set clicked
292 * button foreground to brighter
294 * Finally, synchronize the colour chooser to the colour of the first button
295 * in the selected set.
299 public void colourButtonPressed(MouseEvent e)
301 JButton pressed = (JButton) e.getSource();
305 JButton start, end = (JButton) e.getSource();
306 if (selectedButtons.size() > 0)
308 start = selectedButtons.get(selectedButtons.size() - 1);
312 start = (JButton) e.getSource();
315 int startIndex = 0, endIndex = 0;
316 for (int b = 0; b < buttonPanel.getComponentCount(); b++)
318 if (buttonPanel.getComponent(b) == start)
322 if (buttonPanel.getComponent(b) == end)
328 if (startIndex > endIndex)
330 int temp = startIndex;
331 startIndex = endIndex;
335 for (int b = startIndex; b <= endIndex; b++)
337 JButton button = (JButton) buttonPanel.getComponent(b);
338 if (!selectedButtons.contains(button))
340 button.setForeground(ColorUtils.brighterThan(button
342 selectedButtons.add(button);
346 else if (!e.isControlDown())
348 for (int b = 0; b < selectedButtons.size(); b++)
350 JButton button = selectedButtons.get(b);
351 button.setForeground(ColorUtils.darkerThan(button.getBackground()));
353 selectedButtons.clear();
354 pressed.setForeground(ColorUtils.brighterThan(pressed.getBackground()));
355 selectedButtons.add(pressed);
358 else if (e.isControlDown())
360 if (selectedButtons.contains(pressed))
362 pressed.setForeground(ColorUtils.darkerThan(pressed.getBackground()));
363 selectedButtons.remove(pressed);
367 pressed.setForeground(ColorUtils.brighterThan(pressed
369 selectedButtons.add(pressed);
373 if (selectedButtons.size() > 0)
375 colorChooser.setColor((selectedButtons.get(0)).getBackground());
387 JButton makeButton(String label, String aa,
388 ArrayList<JButton> caseSensitiveButtons, int buttonIndex)
390 final JButton button;
393 if (buttonIndex < caseSensitiveButtons.size())
395 button = caseSensitiveButtons.get(buttonIndex);
396 col = button.getBackground();
400 button = new JButton();
401 button.addMouseListener(new java.awt.event.MouseAdapter()
404 public void mouseClicked(MouseEvent e)
406 colourButtonPressed(e);
410 caseSensitiveButtons.add(button);
413 if (oldColourScheme != null)
417 col = oldColourScheme.findColour(aa.charAt(0), -1, null);
418 } catch (Exception ex)
424 if (caseSensitive.isSelected())
426 button.setMargin(new java.awt.Insets(2, 2, 2, 2));
430 button.setMargin(new java.awt.Insets(2, 14, 2, 14));
433 button.setOpaque(true); // required for the next line to have effect
434 button.setBackground(col);
435 button.setText(label);
436 button.setForeground(ColorUtils.darkerThan(col));
437 button.setFont(new java.awt.Font("Verdana", Font.BOLD, 10));
449 protected void okButton_actionPerformed(ActionEvent e)
451 if (isNoSelectionMade())
453 JvOptionPane.showMessageDialog(Desktop.desktop, MessageManager
454 .getString("label.no_colour_selection_in_scheme"),
455 MessageManager.getString("label.no_colour_selection_warn"),
456 JvOptionPane.WARNING_MESSAGE);
460 applyButton_actionPerformed(null);
464 frame.setClosed(true);
465 } catch (Exception ex)
472 * Returns true if the user has not made any colour selection (including if
473 * 'case-sensitive' selected and no lower-case colour chosen).
477 protected boolean isNoSelectionMade()
479 final boolean noUpperCaseSelected = upperCaseButtons == null
480 || upperCaseButtons.isEmpty();
481 final boolean noLowerCaseSelected = caseSensitive.isSelected()
482 && (lowerCaseButtons == null || lowerCaseButtons.isEmpty());
483 final boolean noSelectionMade = noUpperCaseSelected
484 || noLowerCaseSelected;
485 return noSelectionMade;
495 protected void applyButton_actionPerformed(ActionEvent e)
497 if (isNoSelectionMade())
499 JvOptionPane.showMessageDialog(Desktop.desktop, MessageManager
500 .getString("label.no_colour_selection_in_scheme"),
501 MessageManager.getString("label.no_colour_selection_warn"),
502 JvOptionPane.WARNING_MESSAGE);
505 UserColourScheme ucs = getSchemeFromButtons();
506 ucs.setName(schemeName.getText());
508 if (seqGroup != null)
511 ap.paintAlignment(true);
515 ap.alignFrame.changeColour(ucs);
517 else if (jmol != null)
519 jmol.setJalviewColourScheme(ucs);
523 UserColourScheme getSchemeFromButtons()
526 Color[] newColours = new Color[24];
528 int length = upperCaseButtons.size();
532 for (JButton btn : upperCaseButtons)
534 newColours[i] = btn.getBackground();
540 for (int i = 0; i < 24; i++)
542 JButton button = upperCaseButtons.get(i);
543 newColours[i] = button.getBackground();
547 UserColourScheme ucs = new UserColourScheme(newColours);
549 if (caseSensitive.isSelected())
551 newColours = new Color[23];
552 length = lowerCaseButtons.size();
556 for (JButton btn : lowerCaseButtons)
558 newColours[i] = btn.getBackground();
564 for (int i = 0; i < 23; i++)
566 JButton button = lowerCaseButtons.get(i);
567 newColours[i] = button.getBackground();
570 ucs.setLowerCaseColours(newColours);
575 ucs.setThreshold(0, ap.av.isIgnoreGapsConsensus());
588 protected void loadbutton_actionPerformed(ActionEvent e)
590 upperCaseButtons = new ArrayList<JButton>();
591 lowerCaseButtons = new ArrayList<JButton>();
593 JalviewFileChooser chooser = new JalviewFileChooser("jc",
594 "Jalview User Colours");
595 chooser.setFileView(new JalviewFileView());
596 chooser.setDialogTitle(MessageManager
597 .getString("label.load_colour_scheme"));
598 chooser.setToolTipText(MessageManager.getString("action.load"));
600 int value = chooser.showOpenDialog(this);
602 if (value == JalviewFileChooser.APPROVE_OPTION)
604 File choice = chooser.getSelectedFile();
605 jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
606 String defaultColours = jalview.bin.Cache.getDefault(
607 "USER_DEFINED_COLOURS", choice.getPath());
608 if (defaultColours.indexOf(choice.getPath()) == -1)
610 defaultColours = defaultColours.concat("|")
611 .concat(choice.getPath());
614 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
616 UserColourScheme ucs = loadColours(choice.getAbsolutePath());
617 Color[] colors = ucs.getColours();
618 schemeName.setText(ucs.getName());
620 if (ucs.getLowerCaseColours() != null)
622 caseSensitive.setSelected(true);
623 lcaseColour.setEnabled(true);
624 resetButtonPanel(true);
625 for (int i = 0; i < lowerCaseButtons.size(); i++)
627 JButton button = lowerCaseButtons.get(i);
628 button.setBackground(ucs.getLowerCaseColours()[i]);
634 caseSensitive.setSelected(false);
635 lcaseColour.setEnabled(false);
636 resetButtonPanel(false);
639 for (int i = 0; i < upperCaseButtons.size(); i++)
641 JButton button = upperCaseButtons.get(i);
642 button.setBackground(colors[i]);
651 * @return DOCUMENT ME!
653 public static UserColourScheme loadDefaultColours()
655 UserColourScheme ret = null;
657 String colours = jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS");
660 if (colours.indexOf("|") > -1)
662 colours = colours.substring(0, colours.indexOf("|"));
665 ret = loadColours(colours);
670 Color[] newColours = new Color[24];
671 for (int i = 0; i < 24; i++)
673 newColours[i] = Color.white;
675 ret = new UserColourScheme(newColours);
687 * @return DOCUMENT ME!
689 static UserColourScheme loadColours(String file)
691 UserColourScheme ucs = null;
692 Color[] newColours = null;
695 InputStreamReader in = new InputStreamReader(
696 new FileInputStream(file), "UTF-8");
698 jalview.schemabinding.version2.JalviewUserColours jucs = new jalview.schemabinding.version2.JalviewUserColours();
700 org.exolab.castor.xml.Unmarshaller unmar = new org.exolab.castor.xml.Unmarshaller(
702 jucs = (jalview.schemabinding.version2.JalviewUserColours) unmar
705 newColours = new Color[24];
707 Color[] lowerCase = null;
708 boolean caseSensitive = false;
712 for (int i = 0; i < jucs.getColourCount(); i++)
714 name = jucs.getColour(i).getName();
715 if (ResidueProperties.aa3Hash.containsKey(name))
717 index = ResidueProperties.aa3Hash.get(name).intValue();
721 index = ResidueProperties.aaIndex[name.charAt(0)];
728 if (name.toLowerCase().equals(name))
730 if (lowerCase == null)
732 lowerCase = new Color[23];
734 caseSensitive = true;
735 lowerCase[index] = new Color(Integer.parseInt(jucs.getColour(i)
740 newColours[index] = new Color(Integer.parseInt(jucs.getColour(i)
745 if (newColours != null)
747 ucs = new UserColourScheme(newColours);
748 ucs.setName(jucs.getSchemeName());
751 ucs.setLowerCaseColours(lowerCase);
755 } catch (Exception ex)
757 // Could be Archive Jalview format
760 InputStreamReader in = new InputStreamReader(new FileInputStream(
763 jalview.binding.JalviewUserColours jucs = new jalview.binding.JalviewUserColours();
765 jucs = jucs.unmarshal(in);
767 newColours = new Color[jucs.getColourCount()];
769 for (int i = 0; i < 24; i++)
771 newColours[i] = new Color(Integer.parseInt(jucs.getColour(i)
774 if (newColours != null)
776 ucs = new UserColourScheme(newColours);
777 ucs.setName(jucs.getSchemeName());
779 } catch (Exception ex2)
781 ex2.printStackTrace();
784 if (newColours == null)
786 System.out.println("Error loading User ColourFile\n" + ex);
800 protected void savebutton_actionPerformed(ActionEvent e)
802 if (schemeName.getText().trim().length() < 1)
804 JvOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
805 .getString("label.user_colour_scheme_must_have_name"),
806 MessageManager.getString("label.no_name_colour_scheme"),
807 JvOptionPane.WARNING_MESSAGE);
811 if (userColourSchemes != null
812 && userColourSchemes.containsKey(schemeName.getText()))
814 int reply = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
815 MessageManager.formatMessage(
816 "label.colour_scheme_exists_overwrite", new Object[] {
817 schemeName.getText(), schemeName.getText() }),
818 MessageManager.getString("label.duplicate_scheme_name"),
819 JvOptionPane.YES_NO_OPTION);
820 if (reply != JvOptionPane.YES_OPTION)
825 userColourSchemes.remove(schemeName.getText());
827 JalviewFileChooser chooser = new JalviewFileChooser("jc",
828 "Jalview User Colours");
830 chooser.setFileView(new JalviewFileView());
831 chooser.setDialogTitle(MessageManager
832 .getString("label.save_colour_scheme"));
833 chooser.setToolTipText(MessageManager.getString("action.save"));
835 int value = chooser.showSaveDialog(this);
837 if (value == JalviewFileChooser.APPROVE_OPTION)
839 String choice = chooser.getSelectedFile().getPath();
840 String defaultColours = jalview.bin.Cache.getDefault(
841 "USER_DEFINED_COLOURS", choice);
842 if (defaultColours.indexOf(choice) == -1)
844 if (defaultColours.length() > 0)
846 defaultColours = defaultColours.concat("|");
848 defaultColours = defaultColours.concat(choice);
851 userColourSchemes.put(schemeName.getText(), getSchemeFromButtons());
853 ap.alignFrame.updateUserColourMenu();
855 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
857 jalview.schemabinding.version2.JalviewUserColours ucs = new jalview.schemabinding.version2.JalviewUserColours();
859 ucs.setSchemeName(schemeName.getText());
862 PrintWriter out = new PrintWriter(new OutputStreamWriter(
863 new FileOutputStream(choice), "UTF-8"));
865 for (int i = 0; i < buttonPanel.getComponentCount(); i++)
867 JButton button = (JButton) buttonPanel.getComponent(i);
868 jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
869 col.setName(button.getText());
870 col.setRGB(jalview.util.Format.getHexString(button
877 } catch (Exception ex)
879 ex.printStackTrace();
891 protected void cancelButton_actionPerformed(ActionEvent e)
895 if (seqGroup != null)
897 seqGroup.cs = oldColourScheme;
901 ap.av.setGlobalColourScheme(oldColourScheme);
903 ap.paintAlignment(true);
908 jmol.setJalviewColourScheme(oldColourScheme);
913 frame.setClosed(true);
914 } catch (Exception ex)
919 static Hashtable userColourSchemes;
921 public static Hashtable getUserColourSchemes()
923 return userColourSchemes;
926 public static void initUserColourSchemes(String files)
928 userColourSchemes = new Hashtable();
930 if (files == null || files.length() == 0)
935 // In case colours can't be loaded, we'll remove them
936 // from the default list here.
937 StringBuffer coloursFound = new StringBuffer();
938 StringTokenizer st = new StringTokenizer(files, "|");
939 while (st.hasMoreElements())
941 String file = st.nextToken();
944 UserColourScheme ucs = loadColours(file);
947 if (coloursFound.length() > 0)
949 coloursFound.append("|");
951 coloursFound.append(file);
952 userColourSchemes.put(ucs.getName(), ucs);
954 } catch (Exception ex)
956 System.out.println("Error loading User ColourFile\n" + ex);
959 if (!files.equals(coloursFound.toString()))
961 if (coloursFound.toString().length() > 1)
963 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS",
964 coloursFound.toString());
968 jalview.bin.Cache.applicationProperties
969 .remove("USER_DEFINED_COLOURS");
974 public static void removeColourFromDefaults(String target)
976 // The only way to find colours by name is to load them in
977 // In case colours can't be loaded, we'll remove them
978 // from the default list here.
980 userColourSchemes = new Hashtable();
982 StringBuffer coloursFound = new StringBuffer();
983 StringTokenizer st = new StringTokenizer(
984 jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS"), "|");
986 while (st.hasMoreElements())
988 String file = st.nextToken();
991 UserColourScheme ucs = loadColours(file);
992 if (ucs != null && !ucs.getName().equals(target))
994 if (coloursFound.length() > 0)
996 coloursFound.append("|");
998 coloursFound.append(file);
999 userColourSchemes.put(ucs.getName(), ucs);
1001 } catch (Exception ex)
1003 System.out.println("Error loading User ColourFile\n" + ex);
1007 if (coloursFound.toString().length() > 1)
1009 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS",
1010 coloursFound.toString());
1014 jalview.bin.Cache.applicationProperties
1015 .remove("USER_DEFINED_COLOURS");
1021 public void caseSensitive_actionPerformed(ActionEvent e)
1023 boolean selected = caseSensitive.isSelected();
1024 resetButtonPanel(selected);
1025 lcaseColour.setEnabled(selected);
1026 lcaseColour.setForeground(Color.GRAY);
1030 * Action on clicking 'Lower case colour', which results in changing colour of
1031 * all lower-case buttons when a colour is picked. A second click of the
1032 * button turns off this behaviour.
1035 public void lcaseColour_actionPerformed(ActionEvent e)
1037 boolean enable = !selectedButtons.contains(lcaseColour);
1038 selectedButtons.clear();
1041 selectedButtons.add(lcaseColour);
1042 lcaseColour.setForeground(lowerCaseButtons.get(0).getForeground());
1043 lcaseColour.setForeground(Color.black);
1047 lcaseColour.setBackground(Color.white);
1048 lcaseColour.setForeground(Color.gray);