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.jbgui.GUserDefinedColours;
27 import jalview.schemes.ColourSchemeI;
28 import jalview.schemes.ResidueProperties;
29 import jalview.schemes.UserColourScheme;
30 import jalview.util.ColorUtils;
31 import jalview.util.MessageManager;
33 import java.awt.Color;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.MouseEvent;
38 import java.io.FileInputStream;
39 import java.io.FileOutputStream;
40 import java.io.InputStreamReader;
41 import java.io.OutputStreamWriter;
42 import java.io.PrintWriter;
43 import java.util.ArrayList;
44 import java.util.Hashtable;
45 import java.util.StringTokenizer;
47 import javax.swing.JButton;
48 import javax.swing.JInternalFrame;
49 import javax.swing.JOptionPane;
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 ArrayList<JButton> selectedButtons;
75 ColourSchemeI oldColourScheme;
79 JalviewStructureDisplayI jmol;
81 ArrayList<JButton> upperCaseButtons;
83 ArrayList<JButton> lowerCaseButtons;
86 * Creates a new UserDefinedColours object.
93 public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)
97 lcaseColour.setEnabled(false);
102 if (seqGroup != null)
104 oldColourScheme = seqGroup.cs;
108 oldColourScheme = ap.av.getGlobalColourScheme();
111 if (oldColourScheme instanceof UserColourScheme)
113 schemeName.setText(((UserColourScheme) oldColourScheme).getName());
114 if (((UserColourScheme) oldColourScheme).getLowerCaseColours() != null)
116 caseSensitive.setSelected(true);
117 lcaseColour.setEnabled(true);
118 resetButtonPanel(true);
122 resetButtonPanel(false);
127 resetButtonPanel(false);
133 public UserDefinedColours(JalviewStructureDisplayI jmol,
139 colorChooser.getSelectionModel().addChangeListener(this);
141 oldColourScheme = oldcs;
143 if (oldColourScheme instanceof UserColourScheme)
145 schemeName.setText(((UserColourScheme) oldColourScheme).getName());
148 resetButtonPanel(false);
156 colorChooser.getSelectionModel().addChangeListener(this);
157 frame = new JInternalFrame();
158 frame.setContentPane(this);
159 Desktop.addInternalFrame(frame,
160 MessageManager.getString("label.user_defined_colours"),
161 MY_FRAME_WIDTH, MY_FRAME_HEIGHT, true);
163 if (seqGroup != null)
165 frame.setTitle(frame.getTitle() + " (" + seqGroup.getName() + ")");
169 void resetButtonPanel(boolean caseSensitive)
171 buttonPanel.removeAll();
173 if (upperCaseButtons == null)
175 upperCaseButtons = new ArrayList<JButton>();
180 for (int i = 0; i < 20; i++)
184 label = ResidueProperties.aa[i];
188 label = ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i])
192 button = makeButton(label, ResidueProperties.aa[i], upperCaseButtons,
195 buttonPanel.add(button);
198 buttonPanel.add(makeButton("B", "B", upperCaseButtons, 20));
199 buttonPanel.add(makeButton("Z", "Z", upperCaseButtons, 21));
200 buttonPanel.add(makeButton("X", "X", upperCaseButtons, 22));
201 buttonPanel.add(makeButton("Gap", "-", upperCaseButtons, 23));
205 gridLayout.setRows(6);
206 gridLayout.setColumns(4);
210 gridLayout.setRows(7);
212 gridLayout.setColumns(cols + 1);
214 if (lowerCaseButtons == null)
216 lowerCaseButtons = new ArrayList<JButton>();
219 for (int i = 0; i < 20; i++)
221 int row = i / cols + 1;
222 int index = (row * cols) + i;
223 button = makeButton(ResidueProperties.aa[i].toLowerCase(),
224 ResidueProperties.aa[i].toLowerCase(), lowerCaseButtons, i);
226 buttonPanel.add(button, index);
232 buttonPanel.add(makeButton("b", "b", lowerCaseButtons, 20));
233 buttonPanel.add(makeButton("z", "z", lowerCaseButtons, 21));
234 buttonPanel.add(makeButton("x", "x", lowerCaseButtons, 22));
237 // JAL-1360 widen the frame dynamically to accommodate case-sensitive AA
239 if (this.frame != null)
241 int newWidth = caseSensitive ? MY_FRAME_WIDTH_CASE_SENSITIVE
243 this.frame.setSize(newWidth, this.frame.getHeight());
246 buttonPanel.validate();
257 public void stateChanged(ChangeEvent evt)
259 if (selectedButtons != null)
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 for (int i = 0; i < lowerCaseButtons.size(); i++)
273 button = lowerCaseButtons.get(i);
274 button.setBackground(newColour);
275 button.setForeground(ColorUtils.brighterThan(button
283 * Performs actions when a residue button is clicked. This manages the button
284 * selection set (highlighted by brighter foreground text).
286 * On select button(s) with Ctrl/click or Shift/click: set button foreground
287 * text to brighter than background.
289 * On unselect button(s) with Ctrl/click on selected, or click to release
290 * current selection: reset foreground text to darker than background.
292 * Simple click: clear selection (resetting foreground to darker); set clicked
293 * button foreground to brighter
295 * Finally, synchronize the colour chooser to the colour of the first button
296 * in the selected set.
300 public void colourButtonPressed(MouseEvent e)
302 if (selectedButtons == null)
304 selectedButtons = new ArrayList<JButton>();
307 JButton pressed = (JButton) e.getSource();
311 JButton start, end = (JButton) e.getSource();
312 if (selectedButtons.size() > 0)
314 start = selectedButtons.get(selectedButtons.size() - 1);
318 start = (JButton) e.getSource();
321 int startIndex = 0, endIndex = 0;
322 for (int b = 0; b < buttonPanel.getComponentCount(); b++)
324 if (buttonPanel.getComponent(b) == start)
328 if (buttonPanel.getComponent(b) == end)
334 if (startIndex > endIndex)
336 int temp = startIndex;
337 startIndex = endIndex;
341 for (int b = startIndex; b <= endIndex; b++)
343 JButton button = (JButton) buttonPanel.getComponent(b);
344 if (!selectedButtons.contains(button))
346 button.setForeground(ColorUtils.brighterThan(button
348 selectedButtons.add(button);
352 else if (!e.isControlDown())
354 for (int b = 0; b < selectedButtons.size(); b++)
356 JButton button = selectedButtons.get(b);
357 button.setForeground(ColorUtils.darkerThan(button.getBackground()));
359 selectedButtons.clear();
360 pressed.setForeground(ColorUtils.brighterThan(pressed.getBackground()));
361 selectedButtons.add(pressed);
364 else if (e.isControlDown())
366 if (selectedButtons.contains(pressed))
368 pressed.setForeground(ColorUtils.darkerThan(pressed.getBackground()));
369 selectedButtons.remove(pressed);
373 pressed.setForeground(ColorUtils.brighterThan(pressed
375 selectedButtons.add(pressed);
379 if (selectedButtons.size() > 0)
381 colorChooser.setColor((selectedButtons.get(0)).getBackground());
393 JButton makeButton(String label, String aa,
394 ArrayList<JButton> caseSensitiveButtons, int buttonIndex)
396 final JButton button;
399 if (buttonIndex < caseSensitiveButtons.size())
401 button = caseSensitiveButtons.get(buttonIndex);
402 col = button.getBackground();
406 button = new JButton();
407 button.addMouseListener(new java.awt.event.MouseAdapter()
410 public void mouseClicked(MouseEvent e)
412 colourButtonPressed(e);
416 caseSensitiveButtons.add(button);
419 if (oldColourScheme != null)
423 col = oldColourScheme.findColour(aa.charAt(0), -1, null);
424 } catch (Exception ex)
430 if (caseSensitive.isSelected())
432 button.setMargin(new java.awt.Insets(2, 2, 2, 2));
436 button.setMargin(new java.awt.Insets(2, 14, 2, 14));
439 button.setOpaque(true); // required for the next line to have effect
440 button.setBackground(col);
441 button.setText(label);
442 button.setForeground(ColorUtils.darkerThan(col));
443 button.setFont(new java.awt.Font("Verdana", Font.BOLD, 10));
455 protected void okButton_actionPerformed(ActionEvent e)
457 if (isNoSelectionMade())
459 JOptionPane.showMessageDialog(Desktop.desktop, MessageManager
460 .getString("label.no_colour_selection_in_scheme"),
461 MessageManager.getString("label.no_colour_selection_warn"),
462 JOptionPane.WARNING_MESSAGE);
466 applyButton_actionPerformed(null);
470 frame.setClosed(true);
471 } catch (Exception ex)
478 * Returns true if the user has not made any colour selection (including if
479 * 'case-sensitive' selected and no lower-case colour chosen).
483 protected boolean isNoSelectionMade()
485 final boolean noUpperCaseSelected = upperCaseButtons == null
486 || upperCaseButtons.isEmpty();
487 final boolean noLowerCaseSelected = caseSensitive.isSelected()
488 && (lowerCaseButtons == null || lowerCaseButtons.isEmpty());
489 final boolean noSelectionMade = noUpperCaseSelected
490 || noLowerCaseSelected;
491 return noSelectionMade;
501 protected void applyButton_actionPerformed(ActionEvent e)
503 if (isNoSelectionMade())
505 JOptionPane.showMessageDialog(Desktop.desktop, MessageManager
506 .getString("label.no_colour_selection_in_scheme"),
507 MessageManager.getString("label.no_colour_selection_warn"),
508 JOptionPane.WARNING_MESSAGE);
511 UserColourScheme ucs = getSchemeFromButtons();
512 ucs.setName(schemeName.getText());
514 if (seqGroup != null)
517 ap.paintAlignment(true);
521 ap.alignFrame.changeColour(ucs);
523 else if (jmol != null)
525 jmol.setJalviewColourScheme(ucs);
529 UserColourScheme getSchemeFromButtons()
532 Color[] newColours = new Color[24];
534 int length = upperCaseButtons.size();
538 for (JButton btn : upperCaseButtons)
540 newColours[i] = btn.getBackground();
546 for (int i = 0; i < 24; i++)
548 JButton button = upperCaseButtons.get(i);
549 newColours[i] = button.getBackground();
553 UserColourScheme ucs = new UserColourScheme(newColours);
555 if (caseSensitive.isSelected())
557 newColours = new Color[23];
558 length = lowerCaseButtons.size();
562 for (JButton btn : lowerCaseButtons)
564 newColours[i] = btn.getBackground();
570 for (int i = 0; i < 23; i++)
572 JButton button = lowerCaseButtons.get(i);
573 newColours[i] = button.getBackground();
576 ucs.setLowerCaseColours(newColours);
581 ucs.setThreshold(0, ap.av.isIgnoreGapsConsensus());
594 protected void loadbutton_actionPerformed(ActionEvent e)
596 upperCaseButtons = new ArrayList<JButton>();
597 lowerCaseButtons = new ArrayList<JButton>();
599 JalviewFileChooser chooser = new JalviewFileChooser(
600 jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
601 new String[] { "jc" }, new String[] { "Jalview User Colours" },
602 "Jalview User Colours");
603 chooser.setFileView(new jalview.io.JalviewFileView());
604 chooser.setDialogTitle(MessageManager
605 .getString("label.load_colour_scheme"));
606 chooser.setToolTipText(MessageManager.getString("action.load"));
608 int value = chooser.showOpenDialog(this);
610 if (value == JalviewFileChooser.APPROVE_OPTION)
612 File choice = chooser.getSelectedFile();
613 jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
614 String defaultColours = jalview.bin.Cache.getDefault(
615 "USER_DEFINED_COLOURS", choice.getPath());
616 if (defaultColours.indexOf(choice.getPath()) == -1)
618 defaultColours = defaultColours.concat("|")
619 .concat(choice.getPath());
622 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
624 UserColourScheme ucs = loadColours(choice.getAbsolutePath());
625 Color[] colors = ucs.getColours();
626 schemeName.setText(ucs.getName());
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]);
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]);
659 * @return DOCUMENT ME!
661 public static UserColourScheme loadDefaultColours()
663 UserColourScheme ret = null;
665 String colours = jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS");
668 if (colours.indexOf("|") > -1)
670 colours = colours.substring(0, colours.indexOf("|"));
673 ret = loadColours(colours);
678 Color[] newColours = new Color[24];
679 for (int i = 0; i < 24; i++)
681 newColours[i] = Color.white;
683 ret = new UserColourScheme(newColours);
695 * @return DOCUMENT ME!
697 static UserColourScheme loadColours(String file)
699 UserColourScheme ucs = null;
700 Color[] newColours = null;
703 InputStreamReader in = new InputStreamReader(
704 new FileInputStream(file), "UTF-8");
706 jalview.schemabinding.version2.JalviewUserColours jucs = new jalview.schemabinding.version2.JalviewUserColours();
708 org.exolab.castor.xml.Unmarshaller unmar = new org.exolab.castor.xml.Unmarshaller(
710 jucs = (jalview.schemabinding.version2.JalviewUserColours) unmar
713 newColours = new Color[24];
715 Color[] lowerCase = null;
716 boolean caseSensitive = false;
720 for (int i = 0; i < jucs.getColourCount(); i++)
722 name = jucs.getColour(i).getName();
723 if (ResidueProperties.aa3Hash.containsKey(name))
725 index = ResidueProperties.aa3Hash.get(name).intValue();
729 index = ResidueProperties.aaIndex[name.charAt(0)];
736 if (name.toLowerCase().equals(name))
738 if (lowerCase == null)
740 lowerCase = new Color[23];
742 caseSensitive = true;
743 lowerCase[index] = new Color(Integer.parseInt(jucs.getColour(i)
748 newColours[index] = new Color(Integer.parseInt(jucs.getColour(i)
753 if (newColours != null)
755 ucs = new UserColourScheme(newColours);
756 ucs.setName(jucs.getSchemeName());
759 ucs.setLowerCaseColours(lowerCase);
763 } catch (Exception ex)
765 // Could be Archive Jalview format
768 InputStreamReader in = new InputStreamReader(new FileInputStream(
771 jalview.binding.JalviewUserColours jucs = new jalview.binding.JalviewUserColours();
773 jucs = jucs.unmarshal(in);
775 newColours = new Color[jucs.getColourCount()];
777 for (int i = 0; i < 24; i++)
779 newColours[i] = new Color(Integer.parseInt(jucs.getColour(i)
782 if (newColours != null)
784 ucs = new UserColourScheme(newColours);
785 ucs.setName(jucs.getSchemeName());
787 } catch (Exception ex2)
789 ex2.printStackTrace();
792 if (newColours == null)
794 System.out.println("Error loading User ColourFile\n" + ex);
808 protected void savebutton_actionPerformed(ActionEvent e)
810 if (schemeName.getText().trim().length() < 1)
812 JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
813 .getString("label.user_colour_scheme_must_have_name"),
814 MessageManager.getString("label.no_name_colour_scheme"),
815 JOptionPane.WARNING_MESSAGE);
819 if (userColourSchemes != null
820 && userColourSchemes.containsKey(schemeName.getText()))
822 int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
823 MessageManager.formatMessage(
824 "label.colour_scheme_exists_overwrite", new Object[] {
825 schemeName.getText(), schemeName.getText() }),
826 MessageManager.getString("label.duplicate_scheme_name"),
827 JOptionPane.YES_NO_OPTION);
828 if (reply != JOptionPane.YES_OPTION)
833 userColourSchemes.remove(schemeName.getText());
835 JalviewFileChooser chooser = new JalviewFileChooser(
836 jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
837 new String[] { "jc" }, new String[] { "Jalview User Colours" },
838 "Jalview User Colours");
840 chooser.setFileView(new jalview.io.JalviewFileView());
841 chooser.setDialogTitle(MessageManager
842 .getString("label.save_colour_scheme"));
843 chooser.setToolTipText(MessageManager.getString("action.save"));
845 int value = chooser.showSaveDialog(this);
847 if (value == JalviewFileChooser.APPROVE_OPTION)
849 String choice = chooser.getSelectedFile().getPath();
850 String defaultColours = jalview.bin.Cache.getDefault(
851 "USER_DEFINED_COLOURS", choice);
852 if (defaultColours.indexOf(choice) == -1)
854 if (defaultColours.length() > 0)
856 defaultColours = defaultColours.concat("|");
858 defaultColours = defaultColours.concat(choice);
861 userColourSchemes.put(schemeName.getText(), getSchemeFromButtons());
863 ap.alignFrame.updateUserColourMenu();
865 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
867 jalview.schemabinding.version2.JalviewUserColours ucs = new jalview.schemabinding.version2.JalviewUserColours();
869 ucs.setSchemeName(schemeName.getText());
872 PrintWriter out = new PrintWriter(new OutputStreamWriter(
873 new FileOutputStream(choice), "UTF-8"));
875 for (int i = 0; i < buttonPanel.getComponentCount(); i++)
877 JButton button = (JButton) buttonPanel.getComponent(i);
878 jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
879 col.setName(button.getText());
880 col.setRGB(jalview.util.Format.getHexString(button
887 } catch (Exception ex)
889 ex.printStackTrace();
901 protected void cancelButton_actionPerformed(ActionEvent e)
905 if (seqGroup != null)
907 seqGroup.cs = oldColourScheme;
911 ap.av.setGlobalColourScheme(oldColourScheme);
913 ap.paintAlignment(true);
918 jmol.setJalviewColourScheme(oldColourScheme);
923 frame.setClosed(true);
924 } catch (Exception ex)
929 static Hashtable userColourSchemes;
931 public static Hashtable getUserColourSchemes()
933 return userColourSchemes;
936 public static void initUserColourSchemes(String files)
938 userColourSchemes = new Hashtable();
940 if (files == null || files.length() == 0)
945 // In case colours can't be loaded, we'll remove them
946 // from the default list here.
947 StringBuffer coloursFound = new StringBuffer();
948 StringTokenizer st = new StringTokenizer(files, "|");
949 while (st.hasMoreElements())
951 String file = st.nextToken();
954 UserColourScheme ucs = loadColours(file);
957 if (coloursFound.length() > 0)
959 coloursFound.append("|");
961 coloursFound.append(file);
962 userColourSchemes.put(ucs.getName(), ucs);
964 } catch (Exception ex)
966 System.out.println("Error loading User ColourFile\n" + ex);
969 if (!files.equals(coloursFound.toString()))
971 if (coloursFound.toString().length() > 1)
973 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS",
974 coloursFound.toString());
978 jalview.bin.Cache.applicationProperties
979 .remove("USER_DEFINED_COLOURS");
984 public static void removeColourFromDefaults(String target)
986 // The only way to find colours by name is to load them in
987 // In case colours can't be loaded, we'll remove them
988 // from the default list here.
990 userColourSchemes = new Hashtable();
992 StringBuffer coloursFound = new StringBuffer();
993 StringTokenizer st = new StringTokenizer(
994 jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS"), "|");
996 while (st.hasMoreElements())
998 String file = st.nextToken();
1001 UserColourScheme ucs = loadColours(file);
1002 if (ucs != null && !ucs.getName().equals(target))
1004 if (coloursFound.length() > 0)
1006 coloursFound.append("|");
1008 coloursFound.append(file);
1009 userColourSchemes.put(ucs.getName(), ucs);
1011 } catch (Exception ex)
1013 System.out.println("Error loading User ColourFile\n" + ex);
1017 if (coloursFound.toString().length() > 1)
1019 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS",
1020 coloursFound.toString());
1024 jalview.bin.Cache.applicationProperties
1025 .remove("USER_DEFINED_COLOURS");
1031 public void caseSensitive_actionPerformed(ActionEvent e)
1033 resetButtonPanel(caseSensitive.isSelected());
1034 lcaseColour.setEnabled(caseSensitive.isSelected());
1038 public void lcaseColour_actionPerformed(ActionEvent e)
1040 if (selectedButtons == null)
1042 selectedButtons = new ArrayList<JButton>();
1046 selectedButtons.clear();
1048 selectedButtons.add(lcaseColour);