2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
24 import java.awt.event.*;
26 import javax.swing.event.*;
28 import jalview.datamodel.*;
30 import jalview.jbgui.*;
31 import jalview.schemes.*;
39 public class UserDefinedColours extends GUserDefinedColours implements
44 SequenceGroup seqGroup;
46 Vector selectedButtons;
48 ColourSchemeI oldColourScheme;
54 Vector upperCaseButtons;
56 Vector lowerCaseButtons;
59 * Creates a new UserDefinedColours object.
66 public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)
70 lcaseColour.setEnabled(false);
77 oldColourScheme = seqGroup.cs;
81 oldColourScheme = ap.av.getGlobalColourScheme();
84 if (oldColourScheme instanceof UserColourScheme)
86 schemeName.setText(((UserColourScheme) oldColourScheme).getName());
87 if (((UserColourScheme) oldColourScheme).getLowerCaseColours() != null)
89 caseSensitive.setSelected(true);
90 lcaseColour.setEnabled(true);
91 resetButtonPanel(true);
95 resetButtonPanel(false);
100 resetButtonPanel(false);
106 public UserDefinedColours(AppJmol jmol, ColourSchemeI oldcs)
111 colorChooser.getSelectionModel().addChangeListener(this);
113 oldColourScheme = oldcs;
115 if (oldColourScheme instanceof UserColourScheme)
117 schemeName.setText(((UserColourScheme) oldColourScheme).getName());
120 resetButtonPanel(false);
128 colorChooser.getSelectionModel().addChangeListener(this);
129 frame = new JInternalFrame();
130 frame.setContentPane(this);
131 Desktop.addInternalFrame(frame, "User Defined Colours", 720, 370, true);
133 if (seqGroup != null)
135 frame.setTitle(frame.getTitle() + " (" + seqGroup.getName() + ")");
138 if (new jalview.util.Platform().isAMac())
140 frame.setSize(760, 370);
144 void resetButtonPanel(boolean caseSensitive)
146 buttonPanel.removeAll();
148 if (upperCaseButtons == null)
150 upperCaseButtons = new Vector();
155 for (int i = 0; i < 20; i++)
159 label = ResidueProperties.aa[i];
163 label = ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i])
167 button = makeButton(label, ResidueProperties.aa[i], upperCaseButtons,
170 buttonPanel.add(button);
173 buttonPanel.add(makeButton("B", "B", upperCaseButtons, 20));
174 buttonPanel.add(makeButton("Z", "Z", upperCaseButtons, 21));
175 buttonPanel.add(makeButton("X", "X", upperCaseButtons, 22));
176 buttonPanel.add(makeButton("Gap", "-", upperCaseButtons, 23));
180 gridLayout.setRows(6);
181 gridLayout.setColumns(4);
185 gridLayout.setRows(7);
187 gridLayout.setColumns(cols + 1);
189 if (lowerCaseButtons == null)
191 lowerCaseButtons = new Vector();
194 for (int i = 0; i < 20; i++)
196 int row = i / cols + 1;
197 int index = (row * cols) + i;
198 button = makeButton(ResidueProperties.aa[i].toLowerCase(),
199 ResidueProperties.aa[i].toLowerCase(), lowerCaseButtons, i);
201 buttonPanel.add(button, index);
207 buttonPanel.add(makeButton("b", "b", lowerCaseButtons, 20));
208 buttonPanel.add(makeButton("z", "z", lowerCaseButtons, 21));
209 buttonPanel.add(makeButton("x", "x", lowerCaseButtons, 22));
212 buttonPanel.validate();
222 public void stateChanged(ChangeEvent evt)
224 if (selectedButtons != null)
226 JButton button = null;
227 for (int i = 0; i < selectedButtons.size(); i++)
229 button = (JButton) selectedButtons.elementAt(i);
230 button.setBackground(colorChooser.getColor());
231 button.setForeground(button.getBackground().brighter().brighter()
234 if (button == lcaseColour)
236 for (int i = 0; i < lowerCaseButtons.size(); i++)
238 button = (JButton) lowerCaseButtons.elementAt(i);
239 button.setBackground(colorChooser.getColor());
240 button.setForeground(button.getBackground().brighter().brighter()
253 public void colourButtonPressed(MouseEvent e)
255 if (selectedButtons == null)
257 selectedButtons = new Vector();
260 JButton pressed = (JButton) e.getSource();
264 JButton start, end = (JButton) e.getSource();
265 if (selectedButtons.size() > 0)
267 start = (JButton) selectedButtons
268 .elementAt(selectedButtons.size() - 1);
272 start = (JButton) e.getSource();
275 int startIndex = 0, endIndex = 0;
276 for (int b = 0; b < buttonPanel.getComponentCount(); b++)
278 if (buttonPanel.getComponent(b) == start)
282 if (buttonPanel.getComponent(b) == end)
288 if (startIndex > endIndex)
290 int temp = startIndex;
291 startIndex = endIndex;
295 for (int b = startIndex; b <= endIndex; b++)
297 JButton button = (JButton) buttonPanel.getComponent(b);
298 if (!selectedButtons.contains(button))
301 .setForeground(button.getBackground().brighter()
303 selectedButtons.add(button);
307 else if (!e.isControlDown())
309 for (int b = 0; b < selectedButtons.size(); b++)
311 JButton button = (JButton) selectedButtons.elementAt(b);
312 button.setForeground(button.getBackground().darker().darker());
314 selectedButtons.clear();
315 pressed.setForeground(pressed.getBackground().brighter().brighter());
316 selectedButtons.addElement(pressed);
319 else if (e.isControlDown())
321 if (selectedButtons.contains(pressed))
323 pressed.setForeground(pressed.getBackground().darker().darker());
324 selectedButtons.remove(pressed);
329 .setForeground(pressed.getBackground().brighter()
331 selectedButtons.addElement(pressed);
335 if (selectedButtons.size() > 0)
337 colorChooser.setColor(((JButton) selectedButtons.elementAt(0))
350 JButton makeButton(String label, String aa, Vector caseSensitiveButtons,
353 final JButton button;
356 if (buttonIndex < caseSensitiveButtons.size())
358 button = (JButton) caseSensitiveButtons.elementAt(buttonIndex);
359 col = button.getBackground();
363 button = new JButton();
364 button.addMouseListener(new java.awt.event.MouseAdapter()
366 public void mouseClicked(MouseEvent e)
368 colourButtonPressed(e);
372 caseSensitiveButtons.addElement(button);
375 if (oldColourScheme != null)
379 col = oldColourScheme.findColour(aa.charAt(0), -1);
380 } catch (Exception ex)
386 if (caseSensitive.isSelected())
388 button.setMargin(new java.awt.Insets(2, 2, 2, 2));
392 button.setMargin(new java.awt.Insets(2, 14, 2, 14));
395 button.setBackground(col);
396 button.setText(label);
397 button.setForeground(col.darker().darker().darker());
398 button.setFont(new java.awt.Font("Verdana", Font.BOLD, 10));
409 protected void okButton_actionPerformed(ActionEvent e)
411 applyButton_actionPerformed(null);
415 frame.setClosed(true);
416 } catch (Exception ex)
427 protected void applyButton_actionPerformed(ActionEvent e)
429 UserColourScheme ucs = getSchemeFromButtons();
430 ucs.setName(schemeName.getText());
432 if (seqGroup != null)
435 ap.paintAlignment(true);
439 ap.alignFrame.changeColour(ucs);
441 else if (jmol != null)
443 jmol.setJalviewColourScheme(ucs);
447 UserColourScheme getSchemeFromButtons()
450 Color[] newColours = new Color[24];
452 for (int i = 0; i < 24; i++)
454 JButton button = (JButton) upperCaseButtons.elementAt(i);
455 newColours[i] = button.getBackground();
458 UserColourScheme ucs = new UserColourScheme(newColours);
460 if (caseSensitive.isSelected())
462 newColours = new Color[23];
463 for (int i = 0; i < 23; i++)
465 JButton button = (JButton) lowerCaseButtons.elementAt(i);
466 newColours[i] = button.getBackground();
468 ucs.setLowerCaseColours(newColours);
473 ucs.setThreshold(0, ap.av.getIgnoreGapsConsensus());
485 protected void loadbutton_actionPerformed(ActionEvent e)
487 upperCaseButtons = new Vector();
488 lowerCaseButtons = new Vector();
490 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache
491 .getProperty("LAST_DIRECTORY"), new String[]
492 { "jc" }, new String[]
493 { "Jalview User Colours" }, "Jalview User Colours");
494 chooser.setFileView(new jalview.io.JalviewFileView());
495 chooser.setDialogTitle("Load colour scheme");
496 chooser.setToolTipText("Load");
498 int value = chooser.showOpenDialog(this);
500 if (value == JalviewFileChooser.APPROVE_OPTION)
502 File choice = chooser.getSelectedFile();
503 jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
504 String defaultColours = jalview.bin.Cache.getDefault(
505 "USER_DEFINED_COLOURS", choice.getPath());
506 if (defaultColours.indexOf(choice.getPath()) == -1)
508 defaultColours = defaultColours.concat("|")
509 .concat(choice.getPath());
512 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
514 UserColourScheme ucs = loadColours(choice.getAbsolutePath());
515 Color[] colors = ucs.getColours();
516 schemeName.setText(ucs.getName());
518 if (ucs.getLowerCaseColours() != null)
520 caseSensitive.setSelected(true);
521 lcaseColour.setEnabled(true);
522 resetButtonPanel(true);
523 for (int i = 0; i < lowerCaseButtons.size(); i++)
525 JButton button = (JButton) lowerCaseButtons.elementAt(i);
526 button.setBackground(ucs.getLowerCaseColours()[i]);
532 caseSensitive.setSelected(false);
533 lcaseColour.setEnabled(false);
534 resetButtonPanel(false);
537 for (int i = 0; i < upperCaseButtons.size(); i++)
539 JButton button = (JButton) upperCaseButtons.elementAt(i);
540 button.setBackground(colors[i]);
549 * @return DOCUMENT ME!
551 public static UserColourScheme loadDefaultColours()
553 UserColourScheme ret = null;
555 String colours = jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS");
558 if (colours.indexOf("|") > -1)
560 colours = colours.substring(0, colours.indexOf("|"));
563 ret = loadColours(colours);
568 Color[] newColours = new Color[24];
569 for (int i = 0; i < 24; i++)
571 newColours[i] = Color.white;
573 ret = new UserColourScheme(newColours);
585 * @return DOCUMENT ME!
587 static UserColourScheme loadColours(String file)
589 UserColourScheme ucs = null;
590 Color[] newColours = null;
593 InputStreamReader in = new InputStreamReader(
594 new FileInputStream(file), "UTF-8");
596 jalview.schemabinding.version2.JalviewUserColours jucs = new jalview.schemabinding.version2.JalviewUserColours();
598 org.exolab.castor.xml.Unmarshaller unmar = new org.exolab.castor.xml.Unmarshaller(
600 jucs = (jalview.schemabinding.version2.JalviewUserColours) unmar
603 newColours = new Color[24];
605 Color[] lowerCase = null;
606 boolean caseSensitive = false;
610 for (int i = 0; i < jucs.getColourCount(); i++)
612 name = jucs.getColour(i).getName();
613 if (ResidueProperties.aa3Hash.containsKey(name))
615 index = ((Integer) ResidueProperties.aa3Hash.get(name))
620 index = ResidueProperties.aaIndex[name.charAt(0)];
627 if (name.toLowerCase().equals(name))
629 if (lowerCase == null)
631 lowerCase = new Color[23];
633 caseSensitive = true;
634 lowerCase[index] = new Color(Integer.parseInt(jucs.getColour(i)
639 newColours[index] = new Color(Integer.parseInt(jucs.getColour(i)
644 if (newColours != null)
646 ucs = new UserColourScheme(newColours);
647 ucs.setName(jucs.getSchemeName());
650 ucs.setLowerCaseColours(lowerCase);
654 } catch (Exception ex)
656 // Could be Archive Jalview format
659 InputStreamReader in = new InputStreamReader(new FileInputStream(
662 jalview.binding.JalviewUserColours jucs = new jalview.binding.JalviewUserColours();
664 jucs = (jalview.binding.JalviewUserColours) jucs.unmarshal(in);
666 newColours = new Color[jucs.getColourCount()];
668 for (int i = 0; i < 24; i++)
670 newColours[i] = new Color(Integer.parseInt(jucs.getColour(i)
673 if (newColours != null)
675 ucs = new UserColourScheme(newColours);
676 ucs.setName(jucs.getSchemeName());
678 } catch (Exception ex2)
680 ex2.printStackTrace();
683 if (newColours == null)
685 System.out.println("Error loading User ColourFile\n" + ex);
698 protected void savebutton_actionPerformed(ActionEvent e)
700 if (schemeName.getText().trim().length() < 1)
702 JOptionPane.showInternalMessageDialog(Desktop.desktop,
703 "User colour scheme must have a name!",
704 "No name for colour scheme", JOptionPane.WARNING_MESSAGE);
708 if (userColourSchemes != null
709 && userColourSchemes.containsKey(schemeName.getText()))
711 int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
712 "Colour scheme " + schemeName.getText() + " exists."
713 + "\nContinue saving colour scheme as "
714 + schemeName.getText() + "?",
715 "Duplicate scheme name", JOptionPane.YES_NO_OPTION);
716 if (reply != JOptionPane.YES_OPTION)
721 userColourSchemes.remove(schemeName.getText());
723 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache
724 .getProperty("LAST_DIRECTORY"), new String[]
725 { "jc" }, new String[]
726 { "Jalview User Colours" }, "Jalview User Colours");
728 chooser.setFileView(new jalview.io.JalviewFileView());
729 chooser.setDialogTitle("Save colour scheme");
730 chooser.setToolTipText("Save");
732 int value = chooser.showSaveDialog(this);
734 if (value == JalviewFileChooser.APPROVE_OPTION)
736 String choice = chooser.getSelectedFile().getPath();
737 String defaultColours = jalview.bin.Cache.getDefault(
738 "USER_DEFINED_COLOURS", choice);
739 if (defaultColours.indexOf(choice) == -1)
741 if (defaultColours.length() > 0)
743 defaultColours = defaultColours.concat("|");
745 defaultColours = defaultColours.concat(choice);
748 userColourSchemes.put(schemeName.getText(), getSchemeFromButtons());
750 ap.alignFrame.updateUserColourMenu();
752 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
754 jalview.schemabinding.version2.JalviewUserColours ucs = new jalview.schemabinding.version2.JalviewUserColours();
756 ucs.setSchemeName(schemeName.getText());
759 PrintWriter out = new PrintWriter(new OutputStreamWriter(
760 new FileOutputStream(choice), "UTF-8"));
762 for (int i = 0; i < buttonPanel.getComponentCount(); i++)
764 JButton button = (JButton) buttonPanel.getComponent(i);
765 jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
766 col.setName(button.getText());
767 col.setRGB(jalview.util.Format.getHexString(button
774 } catch (Exception ex)
776 ex.printStackTrace();
787 protected void cancelButton_actionPerformed(ActionEvent e)
791 if (seqGroup != null)
793 seqGroup.cs = oldColourScheme;
797 ap.av.setGlobalColourScheme(oldColourScheme);
799 ap.paintAlignment(true);
804 jmol.setJalviewColourScheme(oldColourScheme);
809 frame.setClosed(true);
810 } catch (Exception ex)
815 static Hashtable userColourSchemes;
817 public static Hashtable getUserColourSchemes()
819 return userColourSchemes;
822 public static void initUserColourSchemes(String files)
824 userColourSchemes = new Hashtable();
826 if (files == null || files.length() == 0)
831 // In case colours can't be loaded, we'll remove them
832 // from the default list here.
833 StringBuffer coloursFound = new StringBuffer();
834 StringTokenizer st = new StringTokenizer(files, "|");
835 while (st.hasMoreElements())
837 String file = st.nextToken();
840 UserColourScheme ucs = loadColours(file);
843 if (coloursFound.length() > 0)
845 coloursFound.append("|");
847 coloursFound.append(file);
848 userColourSchemes.put(ucs.getName(), ucs);
850 } catch (Exception ex)
852 System.out.println("Error loading User ColourFile\n" + ex);
855 if (!files.equals(coloursFound.toString()))
857 if (coloursFound.toString().length() > 1)
859 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", coloursFound
864 jalview.bin.Cache.applicationProperties
865 .remove("USER_DEFINED_COLOURS");
870 public static void removeColourFromDefaults(String target)
872 // The only way to find colours by name is to load them in
873 // In case colours can't be loaded, we'll remove them
874 // from the default list here.
876 userColourSchemes = new Hashtable();
878 StringBuffer coloursFound = new StringBuffer();
879 StringTokenizer st = new StringTokenizer(jalview.bin.Cache
880 .getProperty("USER_DEFINED_COLOURS"), "|");
882 while (st.hasMoreElements())
884 String file = st.nextToken();
887 UserColourScheme ucs = loadColours(file);
888 if (ucs != null && !ucs.getName().equals(target))
890 if (coloursFound.length() > 0)
892 coloursFound.append("|");
894 coloursFound.append(file);
895 userColourSchemes.put(ucs.getName(), ucs);
897 } catch (Exception ex)
899 System.out.println("Error loading User ColourFile\n" + ex);
903 if (coloursFound.toString().length() > 1)
905 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", coloursFound
910 jalview.bin.Cache.applicationProperties
911 .remove("USER_DEFINED_COLOURS");
916 public void caseSensitive_actionPerformed(ActionEvent e)
918 resetButtonPanel(caseSensitive.isSelected());
919 lcaseColour.setEnabled(caseSensitive.isSelected());
922 public void lcaseColour_actionPerformed(ActionEvent e)
924 if (selectedButtons == null)
926 selectedButtons = new Vector();
930 selectedButtons.clear();
932 selectedButtons.add(lcaseColour);