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 java.awt.Color;
25 import java.awt.event.ActionEvent;
26 import java.awt.event.MouseEvent;
28 import java.io.FileInputStream;
29 import java.io.FileOutputStream;
30 import java.io.InputStreamReader;
31 import java.io.OutputStreamWriter;
32 import java.io.PrintWriter;
33 import java.util.ArrayList;
34 import java.util.Hashtable;
35 import java.util.StringTokenizer;
37 import javax.swing.JButton;
38 import javax.swing.JInternalFrame;
39 import javax.swing.JOptionPane;
40 import javax.swing.event.ChangeEvent;
41 import javax.swing.event.ChangeListener;
43 import jalview.api.structures.JalviewStructureDisplayI;
44 import jalview.datamodel.SequenceGroup;
45 import jalview.io.JalviewFileChooser;
46 import jalview.jbgui.GUserDefinedColours;
47 import jalview.schemes.ColourSchemeI;
48 import jalview.schemes.ResidueProperties;
49 import jalview.schemes.UserColourScheme;
50 import jalview.util.ColorUtils;
51 import jalview.util.MessageManager;
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 || noLowerCaseSelected;
490 return noSelectionMade;
500 protected void applyButton_actionPerformed(ActionEvent e)
502 if (isNoSelectionMade())
504 JOptionPane.showMessageDialog(Desktop.desktop,
505 MessageManager.getString("label.no_colour_selection_in_scheme"),MessageManager.getString("label.no_colour_selection_warn"),
506 JOptionPane.WARNING_MESSAGE);
509 UserColourScheme ucs = getSchemeFromButtons();
510 ucs.setName(schemeName.getText());
512 if (seqGroup != null)
515 ap.paintAlignment(true);
519 ap.alignFrame.changeColour(ucs);
521 else if (jmol != null)
523 jmol.setJalviewColourScheme(ucs);
527 UserColourScheme getSchemeFromButtons()
530 Color[] newColours = new Color[24];
532 int length = upperCaseButtons.size();
535 for (JButton btn:upperCaseButtons){
536 newColours[i] = btn.getBackground();
540 for (int i = 0; i < 24; i++){
541 JButton button = upperCaseButtons.get(i);
542 newColours[i] = button.getBackground();
546 UserColourScheme ucs = new UserColourScheme(newColours);
548 if (caseSensitive.isSelected())
550 newColours = new Color[23];
551 length = lowerCaseButtons.size();
554 for (JButton btn:lowerCaseButtons){
555 newColours[i] = btn.getBackground();
559 for (int i = 0; i < 23; i++){
560 JButton button = lowerCaseButtons.get(i);
561 newColours[i] = button.getBackground();
564 ucs.setLowerCaseColours(newColours);
569 ucs.setThreshold(0, ap.av.isIgnoreGapsConsensus());
582 protected void loadbutton_actionPerformed(ActionEvent e)
584 upperCaseButtons = new ArrayList<JButton>();
585 lowerCaseButtons = new ArrayList<JButton>();
587 JalviewFileChooser chooser = new JalviewFileChooser(
588 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
589 { "jc" }, new String[]
590 { "Jalview User Colours" }, "Jalview User Colours");
591 chooser.setFileView(new jalview.io.JalviewFileView());
592 chooser.setDialogTitle(MessageManager
593 .getString("label.load_colour_scheme"));
594 chooser.setToolTipText(MessageManager.getString("action.load"));
596 int value = chooser.showOpenDialog(this);
598 if (value == JalviewFileChooser.APPROVE_OPTION)
600 File choice = chooser.getSelectedFile();
601 jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
602 String defaultColours = jalview.bin.Cache.getDefault(
603 "USER_DEFINED_COLOURS", choice.getPath());
604 if (defaultColours.indexOf(choice.getPath()) == -1)
606 defaultColours = defaultColours.concat("|")
607 .concat(choice.getPath());
610 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
612 UserColourScheme ucs = loadColours(choice.getAbsolutePath());
613 Color[] colors = ucs.getColours();
614 schemeName.setText(ucs.getName());
616 if (ucs.getLowerCaseColours() != null)
618 caseSensitive.setSelected(true);
619 lcaseColour.setEnabled(true);
620 resetButtonPanel(true);
621 for (int i = 0; i < lowerCaseButtons.size(); i++)
623 JButton button = lowerCaseButtons.get(i);
624 button.setBackground(ucs.getLowerCaseColours()[i]);
630 caseSensitive.setSelected(false);
631 lcaseColour.setEnabled(false);
632 resetButtonPanel(false);
635 for (int i = 0; i < upperCaseButtons.size(); i++)
637 JButton button = upperCaseButtons.get(i);
638 button.setBackground(colors[i]);
647 * @return DOCUMENT ME!
649 public static UserColourScheme loadDefaultColours()
651 UserColourScheme ret = null;
653 String colours = jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS");
656 if (colours.indexOf("|") > -1)
658 colours = colours.substring(0, colours.indexOf("|"));
661 ret = loadColours(colours);
666 Color[] newColours = new Color[24];
667 for (int i = 0; i < 24; i++)
669 newColours[i] = Color.white;
671 ret = new UserColourScheme(newColours);
683 * @return DOCUMENT ME!
685 static UserColourScheme loadColours(String file)
687 UserColourScheme ucs = null;
688 Color[] newColours = null;
691 InputStreamReader in = new InputStreamReader(
692 new FileInputStream(file), "UTF-8");
694 jalview.schemabinding.version2.JalviewUserColours jucs = new jalview.schemabinding.version2.JalviewUserColours();
696 org.exolab.castor.xml.Unmarshaller unmar = new org.exolab.castor.xml.Unmarshaller(
698 jucs = (jalview.schemabinding.version2.JalviewUserColours) unmar
701 newColours = new Color[24];
703 Color[] lowerCase = null;
704 boolean caseSensitive = false;
708 for (int i = 0; i < jucs.getColourCount(); i++)
710 name = jucs.getColour(i).getName();
711 if (ResidueProperties.aa3Hash.containsKey(name))
713 index = ResidueProperties.aa3Hash.get(name)
718 index = ResidueProperties.aaIndex[name.charAt(0)];
725 if (name.toLowerCase().equals(name))
727 if (lowerCase == null)
729 lowerCase = new Color[23];
731 caseSensitive = true;
732 lowerCase[index] = new Color(Integer.parseInt(jucs.getColour(i)
737 newColours[index] = new Color(Integer.parseInt(jucs.getColour(i)
742 if (newColours != null)
744 ucs = new UserColourScheme(newColours);
745 ucs.setName(jucs.getSchemeName());
748 ucs.setLowerCaseColours(lowerCase);
752 } catch (Exception ex)
754 // Could be Archive Jalview format
757 InputStreamReader in = new InputStreamReader(new FileInputStream(
760 jalview.binding.JalviewUserColours jucs = new jalview.binding.JalviewUserColours();
762 jucs = jucs.unmarshal(in);
764 newColours = new Color[jucs.getColourCount()];
766 for (int i = 0; i < 24; i++)
768 newColours[i] = new Color(Integer.parseInt(jucs.getColour(i)
771 if (newColours != null)
773 ucs = new UserColourScheme(newColours);
774 ucs.setName(jucs.getSchemeName());
776 } catch (Exception ex2)
778 ex2.printStackTrace();
781 if (newColours == null)
783 System.out.println("Error loading User ColourFile\n" + ex);
797 protected void savebutton_actionPerformed(ActionEvent e)
799 if (schemeName.getText().trim().length() < 1)
801 JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
802 .getString("label.user_colour_scheme_must_have_name"),
803 MessageManager.getString("label.no_name_colour_scheme"),
804 JOptionPane.WARNING_MESSAGE);
808 if (userColourSchemes != null
809 && userColourSchemes.containsKey(schemeName.getText()))
811 int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
812 MessageManager.formatMessage(
813 "label.colour_scheme_exists_overwrite", new Object[]
814 { schemeName.getText(), schemeName.getText() }),
815 MessageManager.getString("label.duplicate_scheme_name"),
816 JOptionPane.YES_NO_OPTION);
817 if (reply != JOptionPane.YES_OPTION)
822 userColourSchemes.remove(schemeName.getText());
824 JalviewFileChooser chooser = new JalviewFileChooser(
825 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
826 { "jc" }, new String[]
827 { "Jalview User Colours" }, "Jalview User Colours");
829 chooser.setFileView(new jalview.io.JalviewFileView());
830 chooser.setDialogTitle(MessageManager.getString("label.save_colour_scheme"));
831 chooser.setToolTipText(MessageManager.getString("action.save"));
833 int value = chooser.showSaveDialog(this);
835 if (value == JalviewFileChooser.APPROVE_OPTION)
837 String choice = chooser.getSelectedFile().getPath();
838 String defaultColours = jalview.bin.Cache.getDefault(
839 "USER_DEFINED_COLOURS", choice);
840 if (defaultColours.indexOf(choice) == -1)
842 if (defaultColours.length() > 0)
844 defaultColours = defaultColours.concat("|");
846 defaultColours = defaultColours.concat(choice);
849 userColourSchemes.put(schemeName.getText(), getSchemeFromButtons());
851 ap.alignFrame.updateUserColourMenu();
853 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
855 jalview.schemabinding.version2.JalviewUserColours ucs = new jalview.schemabinding.version2.JalviewUserColours();
857 ucs.setSchemeName(schemeName.getText());
860 PrintWriter out = new PrintWriter(new OutputStreamWriter(
861 new FileOutputStream(choice), "UTF-8"));
863 for (int i = 0; i < buttonPanel.getComponentCount(); i++)
865 JButton button = (JButton) buttonPanel.getComponent(i);
866 jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
867 col.setName(button.getText());
868 col.setRGB(jalview.util.Format.getHexString(button
875 } catch (Exception ex)
877 ex.printStackTrace();
889 protected void cancelButton_actionPerformed(ActionEvent e)
893 if (seqGroup != null)
895 seqGroup.cs = oldColourScheme;
899 ap.av.setGlobalColourScheme(oldColourScheme);
901 ap.paintAlignment(true);
906 jmol.setJalviewColourScheme(oldColourScheme);
911 frame.setClosed(true);
912 } catch (Exception ex)
917 static Hashtable userColourSchemes;
919 public static Hashtable getUserColourSchemes()
921 return userColourSchemes;
924 public static void initUserColourSchemes(String files)
926 userColourSchemes = new Hashtable();
928 if (files == null || files.length() == 0)
933 // In case colours can't be loaded, we'll remove them
934 // from the default list here.
935 StringBuffer coloursFound = new StringBuffer();
936 StringTokenizer st = new StringTokenizer(files, "|");
937 while (st.hasMoreElements())
939 String file = st.nextToken();
942 UserColourScheme ucs = loadColours(file);
945 if (coloursFound.length() > 0)
947 coloursFound.append("|");
949 coloursFound.append(file);
950 userColourSchemes.put(ucs.getName(), ucs);
952 } catch (Exception ex)
954 System.out.println("Error loading User ColourFile\n" + ex);
957 if (!files.equals(coloursFound.toString()))
959 if (coloursFound.toString().length() > 1)
961 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS",
962 coloursFound.toString());
966 jalview.bin.Cache.applicationProperties
967 .remove("USER_DEFINED_COLOURS");
972 public static void removeColourFromDefaults(String target)
974 // The only way to find colours by name is to load them in
975 // In case colours can't be loaded, we'll remove them
976 // from the default list here.
978 userColourSchemes = new Hashtable();
980 StringBuffer coloursFound = new StringBuffer();
981 StringTokenizer st = new StringTokenizer(
982 jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS"), "|");
984 while (st.hasMoreElements())
986 String file = st.nextToken();
989 UserColourScheme ucs = loadColours(file);
990 if (ucs != null && !ucs.getName().equals(target))
992 if (coloursFound.length() > 0)
994 coloursFound.append("|");
996 coloursFound.append(file);
997 userColourSchemes.put(ucs.getName(), ucs);
999 } catch (Exception ex)
1001 System.out.println("Error loading User ColourFile\n" + ex);
1005 if (coloursFound.toString().length() > 1)
1007 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS",
1008 coloursFound.toString());
1012 jalview.bin.Cache.applicationProperties
1013 .remove("USER_DEFINED_COLOURS");
1019 public void caseSensitive_actionPerformed(ActionEvent e)
1021 resetButtonPanel(caseSensitive.isSelected());
1022 lcaseColour.setEnabled(caseSensitive.isSelected());
1026 public void lcaseColour_actionPerformed(ActionEvent e)
1028 if (selectedButtons == null)
1030 selectedButtons = new ArrayList<JButton>();
1034 selectedButtons.clear();
1036 selectedButtons.add(lcaseColour);