2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 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 extends GUserDefinedColours implements
45 SequenceGroup seqGroup;
47 Vector selectedButtons;
49 ColourSchemeI oldColourScheme;
55 Vector upperCaseButtons;
57 Vector lowerCaseButtons;
60 * Creates a new UserDefinedColours object.
67 public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)
71 lcaseColour.setEnabled(false);
78 oldColourScheme = seqGroup.cs;
82 oldColourScheme = ap.av.getGlobalColourScheme();
85 if (oldColourScheme instanceof UserColourScheme)
87 schemeName.setText(((UserColourScheme) oldColourScheme).getName());
88 if (((UserColourScheme) oldColourScheme).getLowerCaseColours() != null)
90 caseSensitive.setSelected(true);
91 lcaseColour.setEnabled(true);
92 resetButtonPanel(true);
96 resetButtonPanel(false);
101 resetButtonPanel(false);
107 public UserDefinedColours(AppJmol jmol, ColourSchemeI oldcs)
112 colorChooser.getSelectionModel().addChangeListener(this);
114 oldColourScheme = oldcs;
116 if (oldColourScheme instanceof UserColourScheme)
118 schemeName.setText(((UserColourScheme) oldColourScheme).getName());
121 resetButtonPanel(false);
129 colorChooser.getSelectionModel().addChangeListener(this);
130 frame = new JInternalFrame();
131 frame.setContentPane(this);
132 Desktop.addInternalFrame(frame, "User Defined Colours", 720, 370, true);
134 if (seqGroup != null)
136 frame.setTitle(frame.getTitle() + " (" + seqGroup.getName() + ")");
139 if (new jalview.util.Platform().isAMac())
141 frame.setSize(760, 370);
145 void resetButtonPanel(boolean caseSensitive)
147 buttonPanel.removeAll();
149 if (upperCaseButtons == null)
151 upperCaseButtons = new Vector();
156 for (int i = 0; i < 20; i++)
160 label = ResidueProperties.aa[i];
164 label = ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i])
168 button = makeButton(label, ResidueProperties.aa[i], upperCaseButtons,
171 buttonPanel.add(button);
174 buttonPanel.add(makeButton("B", "B", upperCaseButtons, 20));
175 buttonPanel.add(makeButton("Z", "Z", upperCaseButtons, 21));
176 buttonPanel.add(makeButton("X", "X", upperCaseButtons, 22));
177 buttonPanel.add(makeButton("Gap", "-", upperCaseButtons, 23));
181 gridLayout.setRows(6);
182 gridLayout.setColumns(4);
186 gridLayout.setRows(7);
188 gridLayout.setColumns(cols + 1);
190 if (lowerCaseButtons == null)
192 lowerCaseButtons = new Vector();
195 for (int i = 0; i < 20; i++)
197 int row = i / cols + 1;
198 int index = (row * cols) + i;
199 button = makeButton(ResidueProperties.aa[i].toLowerCase(),
200 ResidueProperties.aa[i].toLowerCase(), lowerCaseButtons, i);
202 buttonPanel.add(button, index);
208 buttonPanel.add(makeButton("b", "b", lowerCaseButtons, 20));
209 buttonPanel.add(makeButton("z", "z", lowerCaseButtons, 21));
210 buttonPanel.add(makeButton("x", "x", lowerCaseButtons, 22));
213 buttonPanel.validate();
223 public void stateChanged(ChangeEvent evt)
225 if (selectedButtons != null)
227 JButton button = null;
228 for (int i = 0; i < selectedButtons.size(); i++)
230 button = (JButton) selectedButtons.elementAt(i);
231 button.setBackground(colorChooser.getColor());
232 button.setForeground(button.getBackground().brighter().brighter()
235 if (button == lcaseColour)
237 for (int i = 0; i < lowerCaseButtons.size(); i++)
239 button = (JButton) lowerCaseButtons.elementAt(i);
240 button.setBackground(colorChooser.getColor());
241 button.setForeground(button.getBackground().brighter().brighter()
254 public void colourButtonPressed(MouseEvent e)
256 if (selectedButtons == null)
258 selectedButtons = new Vector();
261 JButton pressed = (JButton) e.getSource();
265 JButton start, end = (JButton) e.getSource();
266 if (selectedButtons.size() > 0)
268 start = (JButton) selectedButtons
269 .elementAt(selectedButtons.size() - 1);
273 start = (JButton) e.getSource();
276 int startIndex = 0, endIndex = 0;
277 for (int b = 0; b < buttonPanel.getComponentCount(); b++)
279 if (buttonPanel.getComponent(b) == start)
283 if (buttonPanel.getComponent(b) == end)
289 if (startIndex > endIndex)
291 int temp = startIndex;
292 startIndex = endIndex;
296 for (int b = startIndex; b <= endIndex; b++)
298 JButton button = (JButton) buttonPanel.getComponent(b);
299 if (!selectedButtons.contains(button))
302 .setForeground(button.getBackground().brighter()
304 selectedButtons.add(button);
308 else if (!e.isControlDown())
310 for (int b = 0; b < selectedButtons.size(); b++)
312 JButton button = (JButton) selectedButtons.elementAt(b);
313 button.setForeground(button.getBackground().darker().darker());
315 selectedButtons.clear();
316 pressed.setForeground(pressed.getBackground().brighter().brighter());
317 selectedButtons.addElement(pressed);
320 else if (e.isControlDown())
322 if (selectedButtons.contains(pressed))
324 pressed.setForeground(pressed.getBackground().darker().darker());
325 selectedButtons.remove(pressed);
330 .setForeground(pressed.getBackground().brighter()
332 selectedButtons.addElement(pressed);
336 if (selectedButtons.size() > 0)
338 colorChooser.setColor(((JButton) selectedButtons.elementAt(0))
351 JButton makeButton(String label, String aa, Vector caseSensitiveButtons,
354 final JButton button;
357 if (buttonIndex < caseSensitiveButtons.size())
359 button = (JButton) caseSensitiveButtons.elementAt(buttonIndex);
360 col = button.getBackground();
364 button = new JButton();
365 button.addMouseListener(new java.awt.event.MouseAdapter()
367 public void mouseClicked(MouseEvent e)
369 colourButtonPressed(e);
373 caseSensitiveButtons.addElement(button);
379 col = oldColourScheme.findColour(aa.charAt(0), -1);
380 } catch (Exception ex)
385 if (caseSensitive.isSelected())
387 button.setMargin(new java.awt.Insets(2, 2, 2, 2));
391 button.setMargin(new java.awt.Insets(2, 14, 2, 14));
394 button.setBackground(col);
395 button.setText(label);
396 button.setForeground(col.darker().darker().darker());
397 button.setFont(new java.awt.Font("Verdana", Font.BOLD, 10));
408 protected void okButton_actionPerformed(ActionEvent e)
410 applyButton_actionPerformed(null);
414 frame.setClosed(true);
415 } catch (Exception ex)
426 protected void applyButton_actionPerformed(ActionEvent e)
428 UserColourScheme ucs = getSchemeFromButtons();
429 ucs.setName(schemeName.getText());
431 if (seqGroup != null)
434 ap.paintAlignment(true);
438 ap.alignFrame.changeColour(ucs);
440 else if (jmol != null)
442 jmol.setJalviewColourScheme(ucs);
446 UserColourScheme getSchemeFromButtons()
449 Color[] newColours = new Color[24];
451 for (int i = 0; i < 24; i++)
453 JButton button = (JButton) upperCaseButtons.elementAt(i);
454 newColours[i] = button.getBackground();
457 UserColourScheme ucs = new UserColourScheme(newColours);
459 if (caseSensitive.isSelected())
461 newColours = new Color[23];
462 for (int i = 0; i < 23; i++)
464 JButton button = (JButton) lowerCaseButtons.elementAt(i);
465 newColours[i] = button.getBackground();
467 ucs.setLowerCaseColours(newColours);
472 ucs.setThreshold(0, ap.av.getIgnoreGapsConsensus());
484 protected void loadbutton_actionPerformed(ActionEvent e)
486 upperCaseButtons = new Vector();
487 lowerCaseButtons = new Vector();
489 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache
490 .getProperty("LAST_DIRECTORY"), new String[]
491 { "jc" }, new String[]
492 { "Jalview User Colours" }, "Jalview User Colours");
493 chooser.setFileView(new jalview.io.JalviewFileView());
494 chooser.setDialogTitle("Load colour scheme");
495 chooser.setToolTipText("Load");
497 int value = chooser.showOpenDialog(this);
499 if (value == JalviewFileChooser.APPROVE_OPTION)
501 File choice = chooser.getSelectedFile();
502 jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
503 String defaultColours = jalview.bin.Cache.getDefault(
504 "USER_DEFINED_COLOURS", choice.getPath());
505 if (defaultColours.indexOf(choice.getPath()) == -1)
507 defaultColours = defaultColours.concat("|")
508 .concat(choice.getPath());
511 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
513 UserColourScheme ucs = loadColours(choice.getAbsolutePath());
514 Color[] colors = ucs.getColours();
515 schemeName.setText(ucs.getName());
517 if (ucs.getLowerCaseColours() != null)
519 caseSensitive.setSelected(true);
520 lcaseColour.setEnabled(true);
521 resetButtonPanel(true);
522 for (int i = 0; i < lowerCaseButtons.size(); i++)
524 JButton button = (JButton) lowerCaseButtons.elementAt(i);
525 button.setBackground(ucs.getLowerCaseColours()[i]);
531 caseSensitive.setSelected(false);
532 lcaseColour.setEnabled(false);
533 resetButtonPanel(false);
536 for (int i = 0; i < upperCaseButtons.size(); i++)
538 JButton button = (JButton) upperCaseButtons.elementAt(i);
539 button.setBackground(colors[i]);
548 * @return DOCUMENT ME!
550 public static UserColourScheme loadDefaultColours()
552 UserColourScheme ret = null;
554 String colours = jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS");
557 if (colours.indexOf("|") > -1)
559 colours = colours.substring(0, colours.indexOf("|"));
562 ret = loadColours(colours);
567 Color[] newColours = new Color[24];
568 for (int i = 0; i < 24; i++)
570 newColours[i] = Color.white;
572 ret = new UserColourScheme(newColours);
584 * @return DOCUMENT ME!
586 static UserColourScheme loadColours(String file)
588 UserColourScheme ucs = null;
589 Color[] newColours = null;
592 InputStreamReader in = new InputStreamReader(
593 new FileInputStream(file), "UTF-8");
595 jalview.schemabinding.version2.JalviewUserColours jucs = new jalview.schemabinding.version2.JalviewUserColours();
597 org.exolab.castor.xml.Unmarshaller unmar = new org.exolab.castor.xml.Unmarshaller(
599 jucs = (jalview.schemabinding.version2.JalviewUserColours) unmar
602 newColours = new Color[24];
604 Color[] lowerCase = null;
605 boolean caseSensitive = false;
609 for (int i = 0; i < jucs.getColourCount(); i++)
611 name = jucs.getColour(i).getName();
612 if (ResidueProperties.aa3Hash.containsKey(name))
614 index = ((Integer) ResidueProperties.aa3Hash.get(name))
619 index = ResidueProperties.aaIndex[name.charAt(0)];
626 if (name.toLowerCase().equals(name))
628 if (lowerCase == null)
630 lowerCase = new Color[23];
632 caseSensitive = true;
633 lowerCase[index] = new Color(Integer.parseInt(jucs.getColour(i)
638 newColours[index] = new Color(Integer.parseInt(jucs.getColour(i)
643 if (newColours != null)
645 ucs = new UserColourScheme(newColours);
646 ucs.setName(jucs.getSchemeName());
649 ucs.setLowerCaseColours(lowerCase);
653 } catch (Exception ex)
655 // Could be Archive Jalview format
658 InputStreamReader in = new InputStreamReader(new FileInputStream(
661 jalview.binding.JalviewUserColours jucs = new jalview.binding.JalviewUserColours();
663 jucs = (jalview.binding.JalviewUserColours) jucs.unmarshal(in);
665 newColours = new Color[jucs.getColourCount()];
667 for (int i = 0; i < 24; i++)
669 newColours[i] = new Color(Integer.parseInt(jucs.getColour(i)
672 if (newColours != null)
674 ucs = new UserColourScheme(newColours);
675 ucs.setName(jucs.getSchemeName());
677 } catch (Exception ex2)
679 ex2.printStackTrace();
682 if (newColours == null)
684 System.out.println("Error loading User ColourFile\n" + ex);
697 protected void savebutton_actionPerformed(ActionEvent e)
699 if (schemeName.getText().trim().length() < 1)
701 JOptionPane.showInternalMessageDialog(Desktop.desktop,
702 "User colour scheme must have a name!",
703 "No name for colour scheme", JOptionPane.WARNING_MESSAGE);
707 if (userColourSchemes != null
708 && userColourSchemes.containsKey(schemeName.getText()))
710 int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
711 "Colour scheme " + schemeName.getText() + " exists."
712 + "\nContinue saving colour scheme as "
713 + schemeName.getText() + "?",
714 "Duplicate scheme name", JOptionPane.YES_NO_OPTION);
715 if (reply != JOptionPane.YES_OPTION)
720 userColourSchemes.remove(schemeName.getText());
722 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache
723 .getProperty("LAST_DIRECTORY"), new String[]
724 { "jc" }, new String[]
725 { "Jalview User Colours" }, "Jalview User Colours");
727 chooser.setFileView(new jalview.io.JalviewFileView());
728 chooser.setDialogTitle("Save colour scheme");
729 chooser.setToolTipText("Save");
731 int value = chooser.showSaveDialog(this);
733 if (value == JalviewFileChooser.APPROVE_OPTION)
735 String choice = chooser.getSelectedFile().getPath();
736 String defaultColours = jalview.bin.Cache.getDefault(
737 "USER_DEFINED_COLOURS", choice);
738 if (defaultColours.indexOf(choice) == -1)
740 if (defaultColours.length() > 0)
742 defaultColours = defaultColours.concat("|");
744 defaultColours = defaultColours.concat(choice);
747 userColourSchemes.put(schemeName.getText(), getSchemeFromButtons());
749 ap.alignFrame.updateUserColourMenu();
751 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
753 jalview.schemabinding.version2.JalviewUserColours ucs = new jalview.schemabinding.version2.JalviewUserColours();
755 ucs.setSchemeName(schemeName.getText());
758 PrintWriter out = new PrintWriter(new OutputStreamWriter(
759 new FileOutputStream(choice), "UTF-8"));
761 for (int i = 0; i < buttonPanel.getComponentCount(); i++)
763 JButton button = (JButton) buttonPanel.getComponent(i);
764 jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
765 col.setName(button.getText());
766 col.setRGB(jalview.util.Format.getHexString(button
773 } catch (Exception ex)
775 ex.printStackTrace();
786 protected void cancelButton_actionPerformed(ActionEvent e)
790 if (seqGroup != null)
792 seqGroup.cs = oldColourScheme;
796 ap.av.setGlobalColourScheme(oldColourScheme);
798 ap.paintAlignment(true);
803 jmol.setJalviewColourScheme(oldColourScheme);
808 frame.setClosed(true);
809 } catch (Exception ex)
814 static Hashtable userColourSchemes;
816 public static Hashtable getUserColourSchemes()
818 return userColourSchemes;
821 public static void initUserColourSchemes(String files)
823 userColourSchemes = new Hashtable();
825 if (files == null || files.length() == 0)
830 // In case colours can't be loaded, we'll remove them
831 // from the default list here.
832 StringBuffer coloursFound = new StringBuffer();
833 StringTokenizer st = new StringTokenizer(files, "|");
834 while (st.hasMoreElements())
836 String file = st.nextToken();
839 UserColourScheme ucs = loadColours(file);
842 if (coloursFound.length() > 0)
844 coloursFound.append("|");
846 coloursFound.append(file);
847 userColourSchemes.put(ucs.getName(), ucs);
849 } catch (Exception ex)
851 System.out.println("Error loading User ColourFile\n" + ex);
854 if (!files.equals(coloursFound.toString()))
856 if (coloursFound.toString().length() > 1)
858 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", coloursFound
863 jalview.bin.Cache.applicationProperties
864 .remove("USER_DEFINED_COLOURS");
869 public static void removeColourFromDefaults(String target)
871 // The only way to find colours by name is to load them in
872 // In case colours can't be loaded, we'll remove them
873 // from the default list here.
875 userColourSchemes = new Hashtable();
877 StringBuffer coloursFound = new StringBuffer();
878 StringTokenizer st = new StringTokenizer(jalview.bin.Cache
879 .getProperty("USER_DEFINED_COLOURS"), "|");
881 while (st.hasMoreElements())
883 String file = st.nextToken();
886 UserColourScheme ucs = loadColours(file);
887 if (ucs != null && !ucs.getName().equals(target))
889 if (coloursFound.length() > 0)
891 coloursFound.append("|");
893 coloursFound.append(file);
894 userColourSchemes.put(ucs.getName(), ucs);
896 } catch (Exception ex)
898 System.out.println("Error loading User ColourFile\n" + ex);
902 if (coloursFound.toString().length() > 1)
904 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", coloursFound
909 jalview.bin.Cache.applicationProperties
910 .remove("USER_DEFINED_COLOURS");
915 public void caseSensitive_actionPerformed(ActionEvent e)
917 resetButtonPanel(caseSensitive.isSelected());
918 lcaseColour.setEnabled(caseSensitive.isSelected());
921 public void lcaseColour_actionPerformed(ActionEvent e)
923 if (selectedButtons == null)
925 selectedButtons = new Vector();
929 selectedButtons.clear();
931 selectedButtons.add(lcaseColour);