2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 import java.awt.event.*;
27 import javax.swing.event.*;
29 import jalview.datamodel.*;
31 import jalview.jbgui.*;
32 import jalview.schemes.*;
40 public class UserDefinedColours
41 extends GUserDefinedColours implements ChangeListener
44 SequenceGroup seqGroup;
45 Vector selectedButtons;
46 ColourSchemeI oldColourScheme;
49 Vector upperCaseButtons;
50 Vector lowerCaseButtons;
53 * Creates a new UserDefinedColours object.
55 * @param ap DOCUMENT ME!
56 * @param sg DOCUMENT ME!
58 public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)
62 lcaseColour.setEnabled(false);
69 oldColourScheme = seqGroup.cs;
73 oldColourScheme = ap.av.getGlobalColourScheme();
76 if (oldColourScheme instanceof UserColourScheme)
78 schemeName.setText( ( (UserColourScheme) oldColourScheme).getName());
79 if ( ( (UserColourScheme) oldColourScheme).getLowerCaseColours() != null)
81 caseSensitive.setSelected(true);
82 lcaseColour.setEnabled(true);
83 resetButtonPanel(true);
87 resetButtonPanel(false);
92 resetButtonPanel(false);
98 public UserDefinedColours(AppJmol jmol, ColourSchemeI oldcs)
103 colorChooser.getSelectionModel().addChangeListener(this);
105 oldColourScheme = oldcs;
107 if (oldColourScheme instanceof UserColourScheme)
109 schemeName.setText( ( (UserColourScheme) oldColourScheme).getName());
112 resetButtonPanel(false);
120 colorChooser.getSelectionModel().addChangeListener(this);
121 frame = new JInternalFrame();
122 frame.setContentPane(this);
123 Desktop.addInternalFrame(frame, "User Defined Colours", 720, 370, true);
125 if (seqGroup != null)
127 frame.setTitle(frame.getTitle() + " (" + seqGroup.getName() + ")");
130 if (new jalview.util.Platform().isAMac())
132 frame.setSize(760, 370);
136 void resetButtonPanel(boolean caseSensitive)
138 buttonPanel.removeAll();
140 if (upperCaseButtons == null)
142 upperCaseButtons = new Vector();
147 for (int i = 0; i < 20; i++)
151 label = ResidueProperties.aa[i];
155 label = ResidueProperties.aa2Triplet.get
156 (ResidueProperties.aa[i]).toString();
159 button = makeButton(label,
160 ResidueProperties.aa[i],
161 upperCaseButtons, i);
163 buttonPanel.add(button);
166 buttonPanel.add(makeButton("B", "B", upperCaseButtons, 20));
167 buttonPanel.add(makeButton("Z", "Z", upperCaseButtons, 21));
168 buttonPanel.add(makeButton("X", "X", upperCaseButtons, 22));
169 buttonPanel.add(makeButton("Gap", "-", upperCaseButtons, 23));
173 gridLayout.setRows(6);
174 gridLayout.setColumns(4);
178 gridLayout.setRows(7);
180 gridLayout.setColumns(cols + 1);
182 if (lowerCaseButtons == null)
184 lowerCaseButtons = new Vector();
187 for (int i = 0; i < 20; i++)
189 int row = i / cols + 1;
190 int index = (row * cols) + i;
192 ResidueProperties.aa[i].toLowerCase(),
193 ResidueProperties.aa[i].toLowerCase(),
197 buttonPanel.add(button, index);
203 buttonPanel.add(makeButton("b", "b", lowerCaseButtons, 20));
204 buttonPanel.add(makeButton("z", "z", lowerCaseButtons, 21));
205 buttonPanel.add(makeButton("x", "x", lowerCaseButtons, 22));
208 buttonPanel.validate();
215 * @param evt DOCUMENT ME!
217 public void stateChanged(ChangeEvent evt)
219 if (selectedButtons != null)
221 JButton button = null;
222 for (int i = 0; i < selectedButtons.size(); i++)
224 button = (JButton) selectedButtons.elementAt(i);
225 button.setBackground(colorChooser.getColor());
226 button.setForeground(button.getBackground().brighter().brighter().
229 if (button == lcaseColour)
231 for (int i = 0; i < lowerCaseButtons.size(); i++)
233 button = (JButton) lowerCaseButtons.elementAt(i);
234 button.setBackground(colorChooser.getColor());
235 button.setForeground(button.getBackground().brighter().brighter().
245 * @param e DOCUMENT ME!
247 public void colourButtonPressed(MouseEvent e)
249 if (selectedButtons == null)
251 selectedButtons = new Vector();
254 JButton pressed = (JButton) e.getSource();
258 JButton start, end = (JButton) e.getSource();
259 if (selectedButtons.size() > 0)
261 start = (JButton) selectedButtons.elementAt(selectedButtons.size() - 1);
265 start = (JButton) e.getSource();
268 int startIndex = 0, endIndex = 0;
269 for (int b = 0; b < buttonPanel.getComponentCount(); b++)
271 if (buttonPanel.getComponent(b) == start)
275 if (buttonPanel.getComponent(b) == end)
281 if (startIndex > endIndex)
283 int temp = startIndex;
284 startIndex = endIndex;
288 for (int b = startIndex; b <= endIndex; b++)
290 JButton button = (JButton) buttonPanel.getComponent(b);
291 if (!selectedButtons.contains(button))
293 button.setForeground(button.getBackground().brighter().brighter());
294 selectedButtons.add(button);
298 else if (!e.isControlDown())
300 for (int b = 0; b < selectedButtons.size(); b++)
302 JButton button = (JButton) selectedButtons.elementAt(b);
303 button.setForeground(button.getBackground().darker().darker());
305 selectedButtons.clear();
306 pressed.setForeground(pressed.getBackground().brighter().brighter());
307 selectedButtons.addElement(pressed);
310 else if (e.isControlDown())
312 if (selectedButtons.contains(pressed))
314 pressed.setForeground(pressed.getBackground().darker().darker());
315 selectedButtons.remove(pressed);
319 pressed.setForeground(pressed.getBackground().brighter().brighter());
320 selectedButtons.addElement(pressed);
324 if (selectedButtons.size() > 0)
326 colorChooser.setColor( ( (JButton) selectedButtons.elementAt(0)).
334 * @param label DOCUMENT ME!
335 * @param aa DOCUMENT ME!
337 JButton makeButton(String label,
339 Vector caseSensitiveButtons,
342 final JButton button;
345 if (buttonIndex < caseSensitiveButtons.size())
347 button = (JButton) caseSensitiveButtons.elementAt(buttonIndex);
348 col = button.getBackground();
352 button = new JButton();
353 button.addMouseListener(new java.awt.event.MouseAdapter()
355 public void mouseClicked(MouseEvent e)
357 colourButtonPressed(e);
361 caseSensitiveButtons.addElement(button);
367 col = oldColourScheme.findColour(aa.charAt(0), -1);
373 if (caseSensitive.isSelected())
375 button.setMargin(new java.awt.Insets(2, 2, 2, 2));
379 button.setMargin(new java.awt.Insets(2, 14, 2, 14));
382 button.setBackground(col);
383 button.setText(label);
384 button.setForeground(col.darker().darker().darker());
385 button.setFont(new java.awt.Font("Verdana", Font.BOLD, 10));
393 * @param e DOCUMENT ME!
395 protected void okButton_actionPerformed(ActionEvent e)
397 applyButton_actionPerformed(null);
401 frame.setClosed(true);
411 * @param e DOCUMENT ME!
413 protected void applyButton_actionPerformed(ActionEvent e)
415 UserColourScheme ucs = getSchemeFromButtons();
416 ucs.setName(schemeName.getText());
418 if (seqGroup != null)
421 ap.paintAlignment(true);
425 ap.alignFrame.changeColour(ucs);
427 else if (jmol != null)
429 jmol.setJalviewColourScheme(ucs);
433 UserColourScheme getSchemeFromButtons()
436 Color[] newColours = new Color[24];
438 for (int i = 0; i < 24; i++)
440 JButton button = (JButton) upperCaseButtons.elementAt(i);
441 newColours[i] = button.getBackground();
444 UserColourScheme ucs = new UserColourScheme(newColours);
446 if (caseSensitive.isSelected())
448 newColours = new Color[23];
449 for (int i = 0; i < 23; i++)
451 JButton button = (JButton) lowerCaseButtons.elementAt(i);
452 newColours[i] = button.getBackground();
454 ucs.setLowerCaseColours(newColours);
459 ucs.setThreshold(0, ap.av.getIgnoreGapsConsensus());
468 * @param e DOCUMENT ME!
470 protected void loadbutton_actionPerformed(ActionEvent e)
472 upperCaseButtons = new Vector();
473 lowerCaseButtons = new Vector();
475 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.
477 "LAST_DIRECTORY"), new String[]
480 {"Jalview User Colours"}, "Jalview User Colours");
481 chooser.setFileView(new jalview.io.JalviewFileView());
482 chooser.setDialogTitle("Load colour scheme");
483 chooser.setToolTipText("Load");
485 int value = chooser.showOpenDialog(this);
487 if (value == JalviewFileChooser.APPROVE_OPTION)
489 File choice = chooser.getSelectedFile();
490 jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
491 String defaultColours = jalview.bin.Cache.getDefault(
492 "USER_DEFINED_COLOURS",
494 if (defaultColours.indexOf(choice.getPath()) == -1)
496 defaultColours = defaultColours.concat("|").concat(choice.getPath());
499 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
501 UserColourScheme ucs = loadColours(choice.getAbsolutePath());
502 Color[] colors = ucs.getColours();
503 schemeName.setText(ucs.getName());
505 if (ucs.getLowerCaseColours() != null)
507 caseSensitive.setSelected(true);
508 lcaseColour.setEnabled(true);
509 resetButtonPanel(true);
510 for (int i = 0; i < lowerCaseButtons.size(); i++)
512 JButton button = (JButton) lowerCaseButtons.elementAt(i);
513 button.setBackground(ucs.getLowerCaseColours()[i]);
519 caseSensitive.setSelected(false);
520 lcaseColour.setEnabled(false);
521 resetButtonPanel(false);
524 for (int i = 0; i < upperCaseButtons.size(); i++)
526 JButton button = (JButton) upperCaseButtons.elementAt(i);
527 button.setBackground(colors[i]);
536 * @return DOCUMENT ME!
538 public static UserColourScheme loadDefaultColours()
540 UserColourScheme ret = null;
542 String colours = jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS");
545 if (colours.indexOf("|") > -1)
547 colours = colours.substring(0, colours.indexOf("|"));
550 ret = loadColours(colours);
555 Color[] newColours = new Color[24];
556 for (int i = 0; i < 24; i++)
558 newColours[i] = Color.white;
560 ret = new UserColourScheme(newColours);
569 * @param file DOCUMENT ME!
571 * @return DOCUMENT ME!
573 static UserColourScheme loadColours(String file)
575 UserColourScheme ucs = null;
576 Color[] newColours = null;
579 InputStreamReader in = new InputStreamReader(new FileInputStream(
582 jalview.schemabinding.version2.JalviewUserColours jucs
583 = new jalview.schemabinding.version2.JalviewUserColours();
585 org.exolab.castor.xml.Unmarshaller unmar
586 = new org.exolab.castor.xml.Unmarshaller(jucs);
587 jucs = (jalview.schemabinding.version2.JalviewUserColours) unmar.
590 newColours = new Color[24];
592 Color[] lowerCase = null;
593 boolean caseSensitive = false;
597 for (int i = 0; i < jucs.getColourCount(); i++)
599 name = jucs.getColour(i).getName();
600 if (ResidueProperties.aa3Hash.containsKey(name))
602 index = ( (Integer) ResidueProperties.aa3Hash.get(name)).intValue();
606 index = ResidueProperties.aaIndex[name.charAt(0)];
613 if (name.toLowerCase().equals(name))
615 if (lowerCase == null)
617 lowerCase = new Color[23];
619 caseSensitive = true;
620 lowerCase[index] = new Color(Integer.parseInt(
621 jucs.getColour(i).getRGB(), 16));
625 newColours[index] = new Color(Integer.parseInt(
626 jucs.getColour(i).getRGB(), 16));
630 if (newColours != null)
632 ucs = new UserColourScheme(newColours);
633 ucs.setName(jucs.getSchemeName());
636 ucs.setLowerCaseColours(lowerCase);
643 //Could be Archive Jalview format
646 InputStreamReader in = new InputStreamReader(new FileInputStream(
649 jalview.binding.JalviewUserColours jucs
650 = new jalview.binding.JalviewUserColours();
652 jucs = (jalview.binding.JalviewUserColours) jucs.unmarshal(in);
654 newColours = new Color[jucs.getColourCount()];
656 for (int i = 0; i < 24; i++)
658 newColours[i] = new Color(Integer.parseInt(
659 jucs.getColour(i).getRGB(), 16));
661 if (newColours != null)
663 ucs = new UserColourScheme(newColours);
664 ucs.setName(jucs.getSchemeName());
667 catch (Exception ex2)
669 ex2.printStackTrace();
672 if (newColours == null)
674 System.out.println("Error loading User ColourFile\n" + ex);
684 * @param e DOCUMENT ME!
686 protected void savebutton_actionPerformed(ActionEvent e)
688 if (schemeName.getText().trim().length() < 1)
690 JOptionPane.showInternalMessageDialog(Desktop.desktop,
691 "User colour scheme must have a name!",
692 "No name for colour scheme",
693 JOptionPane.WARNING_MESSAGE);
697 if (userColourSchemes != null &&
698 userColourSchemes.containsKey(schemeName.getText()))
700 int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
701 "Colour scheme " + schemeName.getText() + " exists."
702 + "\nContinue saving colour scheme as " + schemeName.getText() + "?",
703 "Duplicate scheme name", JOptionPane.YES_NO_OPTION);
704 if (reply != JOptionPane.YES_OPTION)
709 userColourSchemes.remove(schemeName.getText());
711 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.
713 "LAST_DIRECTORY"), new String[]
716 {"Jalview User Colours"}, "Jalview User Colours");
718 chooser.setFileView(new jalview.io.JalviewFileView());
719 chooser.setDialogTitle("Save colour scheme");
720 chooser.setToolTipText("Save");
722 int value = chooser.showSaveDialog(this);
724 if (value == JalviewFileChooser.APPROVE_OPTION)
726 String choice = chooser.getSelectedFile().getPath();
727 String defaultColours = jalview.bin.Cache.getDefault(
728 "USER_DEFINED_COLOURS", choice);
729 if (defaultColours.indexOf(choice) == -1)
731 if (defaultColours.length() > 0)
733 defaultColours = defaultColours.concat("|");
735 defaultColours = defaultColours.concat(choice);
738 userColourSchemes.put(schemeName.getText(), getSchemeFromButtons());
740 ap.alignFrame.updateUserColourMenu();
742 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
744 jalview.schemabinding.version2.JalviewUserColours ucs
745 = new jalview.schemabinding.version2.JalviewUserColours();
747 ucs.setSchemeName(schemeName.getText());
750 PrintWriter out = new PrintWriter(new OutputStreamWriter(
751 new FileOutputStream(choice), "UTF-8"));
753 for (int i = 0; i < buttonPanel.getComponentCount(); i++)
755 JButton button = (JButton) buttonPanel.getComponent(i);
756 jalview.schemabinding.version2.Colour col
757 = new jalview.schemabinding.version2.Colour();
758 col.setName(button.getText());
759 col.setRGB(jalview.util.Format.getHexString(
760 button.getBackground()));
769 ex.printStackTrace();
777 * @param e DOCUMENT ME!
779 protected void cancelButton_actionPerformed(ActionEvent e)
783 if (seqGroup != null)
785 seqGroup.cs = oldColourScheme;
789 ap.av.setGlobalColourScheme(oldColourScheme);
791 ap.paintAlignment(true);
796 jmol.setJalviewColourScheme(oldColourScheme);
801 frame.setClosed(true);
808 static Hashtable userColourSchemes;
810 public static Hashtable getUserColourSchemes()
812 return userColourSchemes;
815 public static void initUserColourSchemes(String files)
817 userColourSchemes = new Hashtable();
819 if (files == null || files.length() == 0)
824 // In case colours can't be loaded, we'll remove them
825 // from the default list here.
826 StringBuffer coloursFound = new StringBuffer();
827 StringTokenizer st = new StringTokenizer(files, "|");
828 while (st.hasMoreElements())
830 String file = st.nextToken();
833 UserColourScheme ucs = loadColours(file);
836 if (coloursFound.length() > 0)
838 coloursFound.append("|");
840 coloursFound.append(file);
841 userColourSchemes.put(ucs.getName(), ucs);
846 System.out.println("Error loading User ColourFile\n" + ex);
849 if (!files.equals(coloursFound.toString()))
851 if (coloursFound.toString().length() > 1)
853 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS",
854 coloursFound.toString());
858 jalview.bin.Cache.applicationProperties.remove("USER_DEFINED_COLOURS");
863 public static void removeColourFromDefaults(String target)
865 // The only way to find colours by name is to load them in
866 // In case colours can't be loaded, we'll remove them
867 // from the default list here.
869 userColourSchemes = new Hashtable();
871 StringBuffer coloursFound = new StringBuffer();
872 StringTokenizer st = new StringTokenizer(
873 jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS"), "|");
875 while (st.hasMoreElements())
877 String file = st.nextToken();
880 UserColourScheme ucs = loadColours(file);
881 if (ucs != null && !ucs.getName().equals(target))
883 if (coloursFound.length() > 0)
885 coloursFound.append("|");
887 coloursFound.append(file);
888 userColourSchemes.put(ucs.getName(), ucs);
893 System.out.println("Error loading User ColourFile\n" + ex);
897 if (coloursFound.toString().length() > 1)
899 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS",
900 coloursFound.toString());
904 jalview.bin.Cache.applicationProperties.remove("USER_DEFINED_COLOURS");
909 public void caseSensitive_actionPerformed(ActionEvent e)
911 resetButtonPanel(caseSensitive.isSelected());
912 lcaseColour.setEnabled(caseSensitive.isSelected());
915 public void lcaseColour_actionPerformed(ActionEvent e)
917 if (selectedButtons == null)
919 selectedButtons = new Vector();
923 selectedButtons.clear();
925 selectedButtons.add(lcaseColour);