2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 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))
300 button.setForeground(button.getBackground().brighter().brighter());
301 selectedButtons.add(button);
305 else if (!e.isControlDown())
307 for (int b = 0; b < selectedButtons.size(); b++)
309 JButton button = (JButton) selectedButtons.elementAt(b);
310 button.setForeground(button.getBackground().darker().darker());
312 selectedButtons.clear();
313 pressed.setForeground(pressed.getBackground().brighter().brighter());
314 selectedButtons.addElement(pressed);
317 else if (e.isControlDown())
319 if (selectedButtons.contains(pressed))
321 pressed.setForeground(pressed.getBackground().darker().darker());
322 selectedButtons.remove(pressed);
326 pressed.setForeground(pressed.getBackground().brighter().brighter());
327 selectedButtons.addElement(pressed);
331 if (selectedButtons.size() > 0)
333 colorChooser.setColor(((JButton) selectedButtons.elementAt(0))
346 JButton makeButton(String label, String aa, Vector caseSensitiveButtons,
349 final JButton button;
352 if (buttonIndex < caseSensitiveButtons.size())
354 button = (JButton) caseSensitiveButtons.elementAt(buttonIndex);
355 col = button.getBackground();
359 button = new JButton();
360 button.addMouseListener(new java.awt.event.MouseAdapter()
362 public void mouseClicked(MouseEvent e)
364 colourButtonPressed(e);
368 caseSensitiveButtons.addElement(button);
371 if (oldColourScheme != null)
375 col = oldColourScheme.findColour(aa.charAt(0), -1);
376 } catch (Exception ex)
382 if (caseSensitive.isSelected())
384 button.setMargin(new java.awt.Insets(2, 2, 2, 2));
388 button.setMargin(new java.awt.Insets(2, 14, 2, 14));
391 button.setBackground(col);
392 button.setText(label);
393 button.setForeground(col.darker().darker().darker());
394 button.setFont(new java.awt.Font("Verdana", Font.BOLD, 10));
405 protected void okButton_actionPerformed(ActionEvent e)
407 applyButton_actionPerformed(null);
411 frame.setClosed(true);
412 } catch (Exception ex)
423 protected void applyButton_actionPerformed(ActionEvent e)
425 UserColourScheme ucs = getSchemeFromButtons();
426 ucs.setName(schemeName.getText());
428 if (seqGroup != null)
431 ap.paintAlignment(true);
435 ap.alignFrame.changeColour(ucs);
437 else if (jmol != null)
439 jmol.setJalviewColourScheme(ucs);
443 UserColourScheme getSchemeFromButtons()
446 Color[] newColours = new Color[24];
448 for (int i = 0; i < 24; i++)
450 JButton button = (JButton) upperCaseButtons.elementAt(i);
451 newColours[i] = button.getBackground();
454 UserColourScheme ucs = new UserColourScheme(newColours);
456 if (caseSensitive.isSelected())
458 newColours = new Color[23];
459 for (int i = 0; i < 23; i++)
461 JButton button = (JButton) lowerCaseButtons.elementAt(i);
462 newColours[i] = button.getBackground();
464 ucs.setLowerCaseColours(newColours);
469 ucs.setThreshold(0, ap.av.getIgnoreGapsConsensus());
481 protected void loadbutton_actionPerformed(ActionEvent e)
483 upperCaseButtons = new Vector();
484 lowerCaseButtons = new Vector();
486 JalviewFileChooser chooser = new JalviewFileChooser(
487 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
488 { "jc" }, new String[]
489 { "Jalview User Colours" }, "Jalview User Colours");
490 chooser.setFileView(new jalview.io.JalviewFileView());
491 chooser.setDialogTitle("Load colour scheme");
492 chooser.setToolTipText("Load");
494 int value = chooser.showOpenDialog(this);
496 if (value == JalviewFileChooser.APPROVE_OPTION)
498 File choice = chooser.getSelectedFile();
499 jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
500 String defaultColours = jalview.bin.Cache.getDefault(
501 "USER_DEFINED_COLOURS", choice.getPath());
502 if (defaultColours.indexOf(choice.getPath()) == -1)
504 defaultColours = defaultColours.concat("|")
505 .concat(choice.getPath());
508 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
510 UserColourScheme ucs = loadColours(choice.getAbsolutePath());
511 Color[] colors = ucs.getColours();
512 schemeName.setText(ucs.getName());
514 if (ucs.getLowerCaseColours() != null)
516 caseSensitive.setSelected(true);
517 lcaseColour.setEnabled(true);
518 resetButtonPanel(true);
519 for (int i = 0; i < lowerCaseButtons.size(); i++)
521 JButton button = (JButton) lowerCaseButtons.elementAt(i);
522 button.setBackground(ucs.getLowerCaseColours()[i]);
528 caseSensitive.setSelected(false);
529 lcaseColour.setEnabled(false);
530 resetButtonPanel(false);
533 for (int i = 0; i < upperCaseButtons.size(); i++)
535 JButton button = (JButton) upperCaseButtons.elementAt(i);
536 button.setBackground(colors[i]);
545 * @return DOCUMENT ME!
547 public static UserColourScheme loadDefaultColours()
549 UserColourScheme ret = null;
551 String colours = jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS");
554 if (colours.indexOf("|") > -1)
556 colours = colours.substring(0, colours.indexOf("|"));
559 ret = loadColours(colours);
564 Color[] newColours = new Color[24];
565 for (int i = 0; i < 24; i++)
567 newColours[i] = Color.white;
569 ret = new UserColourScheme(newColours);
581 * @return DOCUMENT ME!
583 static UserColourScheme loadColours(String file)
585 UserColourScheme ucs = null;
586 Color[] newColours = null;
589 InputStreamReader in = new InputStreamReader(
590 new FileInputStream(file), "UTF-8");
592 jalview.schemabinding.version2.JalviewUserColours jucs = new jalview.schemabinding.version2.JalviewUserColours();
594 org.exolab.castor.xml.Unmarshaller unmar = new org.exolab.castor.xml.Unmarshaller(
596 jucs = (jalview.schemabinding.version2.JalviewUserColours) unmar
599 newColours = new Color[24];
601 Color[] lowerCase = null;
602 boolean caseSensitive = false;
606 for (int i = 0; i < jucs.getColourCount(); i++)
608 name = jucs.getColour(i).getName();
609 if (ResidueProperties.aa3Hash.containsKey(name))
611 index = ((Integer) ResidueProperties.aa3Hash.get(name))
616 index = ResidueProperties.aaIndex[name.charAt(0)];
623 if (name.toLowerCase().equals(name))
625 if (lowerCase == null)
627 lowerCase = new Color[23];
629 caseSensitive = true;
630 lowerCase[index] = new Color(Integer.parseInt(jucs.getColour(i)
635 newColours[index] = new Color(Integer.parseInt(jucs.getColour(i)
640 if (newColours != null)
642 ucs = new UserColourScheme(newColours);
643 ucs.setName(jucs.getSchemeName());
646 ucs.setLowerCaseColours(lowerCase);
650 } catch (Exception ex)
652 // Could be Archive Jalview format
655 InputStreamReader in = new InputStreamReader(new FileInputStream(
658 jalview.binding.JalviewUserColours jucs = new jalview.binding.JalviewUserColours();
660 jucs = (jalview.binding.JalviewUserColours) jucs.unmarshal(in);
662 newColours = new Color[jucs.getColourCount()];
664 for (int i = 0; i < 24; i++)
666 newColours[i] = new Color(Integer.parseInt(jucs.getColour(i)
669 if (newColours != null)
671 ucs = new UserColourScheme(newColours);
672 ucs.setName(jucs.getSchemeName());
674 } catch (Exception ex2)
676 ex2.printStackTrace();
679 if (newColours == null)
681 System.out.println("Error loading User ColourFile\n" + ex);
694 protected void savebutton_actionPerformed(ActionEvent e)
696 if (schemeName.getText().trim().length() < 1)
698 JOptionPane.showInternalMessageDialog(Desktop.desktop,
699 "User colour scheme must have a name!",
700 "No name for colour scheme", JOptionPane.WARNING_MESSAGE);
704 if (userColourSchemes != null
705 && userColourSchemes.containsKey(schemeName.getText()))
707 int reply = JOptionPane.showInternalConfirmDialog(
709 "Colour scheme " + schemeName.getText() + " exists."
710 + "\nContinue saving colour scheme as "
711 + schemeName.getText() + "?",
712 "Duplicate scheme name", JOptionPane.YES_NO_OPTION);
713 if (reply != JOptionPane.YES_OPTION)
718 userColourSchemes.remove(schemeName.getText());
720 JalviewFileChooser chooser = new JalviewFileChooser(
721 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
722 { "jc" }, new String[]
723 { "Jalview User Colours" }, "Jalview User Colours");
725 chooser.setFileView(new jalview.io.JalviewFileView());
726 chooser.setDialogTitle("Save colour scheme");
727 chooser.setToolTipText("Save");
729 int value = chooser.showSaveDialog(this);
731 if (value == JalviewFileChooser.APPROVE_OPTION)
733 String choice = chooser.getSelectedFile().getPath();
734 String defaultColours = jalview.bin.Cache.getDefault(
735 "USER_DEFINED_COLOURS", choice);
736 if (defaultColours.indexOf(choice) == -1)
738 if (defaultColours.length() > 0)
740 defaultColours = defaultColours.concat("|");
742 defaultColours = defaultColours.concat(choice);
745 userColourSchemes.put(schemeName.getText(), getSchemeFromButtons());
747 ap.alignFrame.updateUserColourMenu();
749 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
751 jalview.schemabinding.version2.JalviewUserColours ucs = new jalview.schemabinding.version2.JalviewUserColours();
753 ucs.setSchemeName(schemeName.getText());
756 PrintWriter out = new PrintWriter(new OutputStreamWriter(
757 new FileOutputStream(choice), "UTF-8"));
759 for (int i = 0; i < buttonPanel.getComponentCount(); i++)
761 JButton button = (JButton) buttonPanel.getComponent(i);
762 jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
763 col.setName(button.getText());
764 col.setRGB(jalview.util.Format.getHexString(button
771 } catch (Exception ex)
773 ex.printStackTrace();
784 protected void cancelButton_actionPerformed(ActionEvent e)
788 if (seqGroup != null)
790 seqGroup.cs = oldColourScheme;
794 ap.av.setGlobalColourScheme(oldColourScheme);
796 ap.paintAlignment(true);
801 jmol.setJalviewColourScheme(oldColourScheme);
806 frame.setClosed(true);
807 } catch (Exception ex)
812 static Hashtable userColourSchemes;
814 public static Hashtable getUserColourSchemes()
816 return userColourSchemes;
819 public static void initUserColourSchemes(String files)
821 userColourSchemes = new Hashtable();
823 if (files == null || files.length() == 0)
828 // In case colours can't be loaded, we'll remove them
829 // from the default list here.
830 StringBuffer coloursFound = new StringBuffer();
831 StringTokenizer st = new StringTokenizer(files, "|");
832 while (st.hasMoreElements())
834 String file = st.nextToken();
837 UserColourScheme ucs = loadColours(file);
840 if (coloursFound.length() > 0)
842 coloursFound.append("|");
844 coloursFound.append(file);
845 userColourSchemes.put(ucs.getName(), ucs);
847 } catch (Exception ex)
849 System.out.println("Error loading User ColourFile\n" + ex);
852 if (!files.equals(coloursFound.toString()))
854 if (coloursFound.toString().length() > 1)
856 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS",
857 coloursFound.toString());
861 jalview.bin.Cache.applicationProperties
862 .remove("USER_DEFINED_COLOURS");
867 public static void removeColourFromDefaults(String target)
869 // The only way to find colours by name is to load them in
870 // In case colours can't be loaded, we'll remove them
871 // from the default list here.
873 userColourSchemes = new Hashtable();
875 StringBuffer coloursFound = new StringBuffer();
876 StringTokenizer st = new StringTokenizer(
877 jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS"), "|");
879 while (st.hasMoreElements())
881 String file = st.nextToken();
884 UserColourScheme ucs = loadColours(file);
885 if (ucs != null && !ucs.getName().equals(target))
887 if (coloursFound.length() > 0)
889 coloursFound.append("|");
891 coloursFound.append(file);
892 userColourSchemes.put(ucs.getName(), ucs);
894 } catch (Exception ex)
896 System.out.println("Error loading User ColourFile\n" + ex);
900 if (coloursFound.toString().length() > 1)
902 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS",
903 coloursFound.toString());
907 jalview.bin.Cache.applicationProperties
908 .remove("USER_DEFINED_COLOURS");
913 public void caseSensitive_actionPerformed(ActionEvent e)
915 resetButtonPanel(caseSensitive.isSelected());
916 lcaseColour.setEnabled(caseSensitive.isSelected());
919 public void lcaseColour_actionPerformed(ActionEvent e)
921 if (selectedButtons == null)
923 selectedButtons = new Vector();
927 selectedButtons.clear();
929 selectedButtons.add(lcaseColour);