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);
376 if (oldColourScheme!=null)
380 col = oldColourScheme.findColour(aa.charAt(0), -1);
381 } catch (Exception ex)
387 if (caseSensitive.isSelected())
389 button.setMargin(new java.awt.Insets(2, 2, 2, 2));
393 button.setMargin(new java.awt.Insets(2, 14, 2, 14));
396 button.setBackground(col);
397 button.setText(label);
398 button.setForeground(col.darker().darker().darker());
399 button.setFont(new java.awt.Font("Verdana", Font.BOLD, 10));
410 protected void okButton_actionPerformed(ActionEvent e)
412 applyButton_actionPerformed(null);
416 frame.setClosed(true);
417 } catch (Exception ex)
428 protected void applyButton_actionPerformed(ActionEvent e)
430 UserColourScheme ucs = getSchemeFromButtons();
431 ucs.setName(schemeName.getText());
433 if (seqGroup != null)
436 ap.paintAlignment(true);
440 ap.alignFrame.changeColour(ucs);
442 else if (jmol != null)
444 jmol.setJalviewColourScheme(ucs);
448 UserColourScheme getSchemeFromButtons()
451 Color[] newColours = new Color[24];
453 for (int i = 0; i < 24; i++)
455 JButton button = (JButton) upperCaseButtons.elementAt(i);
456 newColours[i] = button.getBackground();
459 UserColourScheme ucs = new UserColourScheme(newColours);
461 if (caseSensitive.isSelected())
463 newColours = new Color[23];
464 for (int i = 0; i < 23; i++)
466 JButton button = (JButton) lowerCaseButtons.elementAt(i);
467 newColours[i] = button.getBackground();
469 ucs.setLowerCaseColours(newColours);
474 ucs.setThreshold(0, ap.av.getIgnoreGapsConsensus());
486 protected void loadbutton_actionPerformed(ActionEvent e)
488 upperCaseButtons = new Vector();
489 lowerCaseButtons = new Vector();
491 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache
492 .getProperty("LAST_DIRECTORY"), new String[]
493 { "jc" }, new String[]
494 { "Jalview User Colours" }, "Jalview User Colours");
495 chooser.setFileView(new jalview.io.JalviewFileView());
496 chooser.setDialogTitle("Load colour scheme");
497 chooser.setToolTipText("Load");
499 int value = chooser.showOpenDialog(this);
501 if (value == JalviewFileChooser.APPROVE_OPTION)
503 File choice = chooser.getSelectedFile();
504 jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
505 String defaultColours = jalview.bin.Cache.getDefault(
506 "USER_DEFINED_COLOURS", choice.getPath());
507 if (defaultColours.indexOf(choice.getPath()) == -1)
509 defaultColours = defaultColours.concat("|")
510 .concat(choice.getPath());
513 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
515 UserColourScheme ucs = loadColours(choice.getAbsolutePath());
516 Color[] colors = ucs.getColours();
517 schemeName.setText(ucs.getName());
519 if (ucs.getLowerCaseColours() != null)
521 caseSensitive.setSelected(true);
522 lcaseColour.setEnabled(true);
523 resetButtonPanel(true);
524 for (int i = 0; i < lowerCaseButtons.size(); i++)
526 JButton button = (JButton) lowerCaseButtons.elementAt(i);
527 button.setBackground(ucs.getLowerCaseColours()[i]);
533 caseSensitive.setSelected(false);
534 lcaseColour.setEnabled(false);
535 resetButtonPanel(false);
538 for (int i = 0; i < upperCaseButtons.size(); i++)
540 JButton button = (JButton) upperCaseButtons.elementAt(i);
541 button.setBackground(colors[i]);
550 * @return DOCUMENT ME!
552 public static UserColourScheme loadDefaultColours()
554 UserColourScheme ret = null;
556 String colours = jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS");
559 if (colours.indexOf("|") > -1)
561 colours = colours.substring(0, colours.indexOf("|"));
564 ret = loadColours(colours);
569 Color[] newColours = new Color[24];
570 for (int i = 0; i < 24; i++)
572 newColours[i] = Color.white;
574 ret = new UserColourScheme(newColours);
586 * @return DOCUMENT ME!
588 static UserColourScheme loadColours(String file)
590 UserColourScheme ucs = null;
591 Color[] newColours = null;
594 InputStreamReader in = new InputStreamReader(
595 new FileInputStream(file), "UTF-8");
597 jalview.schemabinding.version2.JalviewUserColours jucs = new jalview.schemabinding.version2.JalviewUserColours();
599 org.exolab.castor.xml.Unmarshaller unmar = new org.exolab.castor.xml.Unmarshaller(
601 jucs = (jalview.schemabinding.version2.JalviewUserColours) unmar
604 newColours = new Color[24];
606 Color[] lowerCase = null;
607 boolean caseSensitive = false;
611 for (int i = 0; i < jucs.getColourCount(); i++)
613 name = jucs.getColour(i).getName();
614 if (ResidueProperties.aa3Hash.containsKey(name))
616 index = ((Integer) ResidueProperties.aa3Hash.get(name))
621 index = ResidueProperties.aaIndex[name.charAt(0)];
628 if (name.toLowerCase().equals(name))
630 if (lowerCase == null)
632 lowerCase = new Color[23];
634 caseSensitive = true;
635 lowerCase[index] = new Color(Integer.parseInt(jucs.getColour(i)
640 newColours[index] = new Color(Integer.parseInt(jucs.getColour(i)
645 if (newColours != null)
647 ucs = new UserColourScheme(newColours);
648 ucs.setName(jucs.getSchemeName());
651 ucs.setLowerCaseColours(lowerCase);
655 } catch (Exception ex)
657 // Could be Archive Jalview format
660 InputStreamReader in = new InputStreamReader(new FileInputStream(
663 jalview.binding.JalviewUserColours jucs = new jalview.binding.JalviewUserColours();
665 jucs = (jalview.binding.JalviewUserColours) jucs.unmarshal(in);
667 newColours = new Color[jucs.getColourCount()];
669 for (int i = 0; i < 24; i++)
671 newColours[i] = new Color(Integer.parseInt(jucs.getColour(i)
674 if (newColours != null)
676 ucs = new UserColourScheme(newColours);
677 ucs.setName(jucs.getSchemeName());
679 } catch (Exception ex2)
681 ex2.printStackTrace();
684 if (newColours == null)
686 System.out.println("Error loading User ColourFile\n" + ex);
699 protected void savebutton_actionPerformed(ActionEvent e)
701 if (schemeName.getText().trim().length() < 1)
703 JOptionPane.showInternalMessageDialog(Desktop.desktop,
704 "User colour scheme must have a name!",
705 "No name for colour scheme", JOptionPane.WARNING_MESSAGE);
709 if (userColourSchemes != null
710 && userColourSchemes.containsKey(schemeName.getText()))
712 int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
713 "Colour scheme " + schemeName.getText() + " exists."
714 + "\nContinue saving colour scheme as "
715 + schemeName.getText() + "?",
716 "Duplicate scheme name", JOptionPane.YES_NO_OPTION);
717 if (reply != JOptionPane.YES_OPTION)
722 userColourSchemes.remove(schemeName.getText());
724 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache
725 .getProperty("LAST_DIRECTORY"), new String[]
726 { "jc" }, new String[]
727 { "Jalview User Colours" }, "Jalview User Colours");
729 chooser.setFileView(new jalview.io.JalviewFileView());
730 chooser.setDialogTitle("Save colour scheme");
731 chooser.setToolTipText("Save");
733 int value = chooser.showSaveDialog(this);
735 if (value == JalviewFileChooser.APPROVE_OPTION)
737 String choice = chooser.getSelectedFile().getPath();
738 String defaultColours = jalview.bin.Cache.getDefault(
739 "USER_DEFINED_COLOURS", choice);
740 if (defaultColours.indexOf(choice) == -1)
742 if (defaultColours.length() > 0)
744 defaultColours = defaultColours.concat("|");
746 defaultColours = defaultColours.concat(choice);
749 userColourSchemes.put(schemeName.getText(), getSchemeFromButtons());
751 ap.alignFrame.updateUserColourMenu();
753 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
755 jalview.schemabinding.version2.JalviewUserColours ucs = new jalview.schemabinding.version2.JalviewUserColours();
757 ucs.setSchemeName(schemeName.getText());
760 PrintWriter out = new PrintWriter(new OutputStreamWriter(
761 new FileOutputStream(choice), "UTF-8"));
763 for (int i = 0; i < buttonPanel.getComponentCount(); i++)
765 JButton button = (JButton) buttonPanel.getComponent(i);
766 jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
767 col.setName(button.getText());
768 col.setRGB(jalview.util.Format.getHexString(button
775 } catch (Exception ex)
777 ex.printStackTrace();
788 protected void cancelButton_actionPerformed(ActionEvent e)
792 if (seqGroup != null)
794 seqGroup.cs = oldColourScheme;
798 ap.av.setGlobalColourScheme(oldColourScheme);
800 ap.paintAlignment(true);
805 jmol.setJalviewColourScheme(oldColourScheme);
810 frame.setClosed(true);
811 } catch (Exception ex)
816 static Hashtable userColourSchemes;
818 public static Hashtable getUserColourSchemes()
820 return userColourSchemes;
823 public static void initUserColourSchemes(String files)
825 userColourSchemes = new Hashtable();
827 if (files == null || files.length() == 0)
832 // In case colours can't be loaded, we'll remove them
833 // from the default list here.
834 StringBuffer coloursFound = new StringBuffer();
835 StringTokenizer st = new StringTokenizer(files, "|");
836 while (st.hasMoreElements())
838 String file = st.nextToken();
841 UserColourScheme ucs = loadColours(file);
844 if (coloursFound.length() > 0)
846 coloursFound.append("|");
848 coloursFound.append(file);
849 userColourSchemes.put(ucs.getName(), ucs);
851 } catch (Exception ex)
853 System.out.println("Error loading User ColourFile\n" + ex);
856 if (!files.equals(coloursFound.toString()))
858 if (coloursFound.toString().length() > 1)
860 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", coloursFound
865 jalview.bin.Cache.applicationProperties
866 .remove("USER_DEFINED_COLOURS");
871 public static void removeColourFromDefaults(String target)
873 // The only way to find colours by name is to load them in
874 // In case colours can't be loaded, we'll remove them
875 // from the default list here.
877 userColourSchemes = new Hashtable();
879 StringBuffer coloursFound = new StringBuffer();
880 StringTokenizer st = new StringTokenizer(jalview.bin.Cache
881 .getProperty("USER_DEFINED_COLOURS"), "|");
883 while (st.hasMoreElements())
885 String file = st.nextToken();
888 UserColourScheme ucs = loadColours(file);
889 if (ucs != null && !ucs.getName().equals(target))
891 if (coloursFound.length() > 0)
893 coloursFound.append("|");
895 coloursFound.append(file);
896 userColourSchemes.put(ucs.getName(), ucs);
898 } catch (Exception ex)
900 System.out.println("Error loading User ColourFile\n" + ex);
904 if (coloursFound.toString().length() > 1)
906 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", coloursFound
911 jalview.bin.Cache.applicationProperties
912 .remove("USER_DEFINED_COLOURS");
917 public void caseSensitive_actionPerformed(ActionEvent e)
919 resetButtonPanel(caseSensitive.isSelected());
920 lcaseColour.setEnabled(caseSensitive.isSelected());
923 public void lcaseColour_actionPerformed(ActionEvent e)
925 if (selectedButtons == null)
927 selectedButtons = new Vector();
931 selectedButtons.clear();
933 selectedButtons.add(lcaseColour);