2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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/>.
20 import jalview.datamodel.SequenceGroup;
21 import jalview.io.JalviewFileChooser;
22 import jalview.jbgui.GUserDefinedColours;
23 import jalview.schemes.ColourSchemeI;
24 import jalview.schemes.ResidueProperties;
25 import jalview.schemes.UserColourScheme;
26 import jalview.util.MessageManager;
28 import java.awt.Color;
30 import java.awt.event.ActionEvent;
31 import java.awt.event.MouseEvent;
33 import java.io.FileInputStream;
34 import java.io.FileOutputStream;
35 import java.io.InputStreamReader;
36 import java.io.OutputStreamWriter;
37 import java.io.PrintWriter;
38 import java.util.Hashtable;
39 import java.util.StringTokenizer;
40 import java.util.Vector;
42 import javax.swing.JButton;
43 import javax.swing.JInternalFrame;
44 import javax.swing.JOptionPane;
45 import javax.swing.event.ChangeEvent;
46 import javax.swing.event.ChangeListener;
54 public class UserDefinedColours extends GUserDefinedColours implements
59 SequenceGroup seqGroup;
61 Vector selectedButtons;
63 ColourSchemeI oldColourScheme;
69 Vector upperCaseButtons;
71 Vector lowerCaseButtons;
74 * Creates a new UserDefinedColours object.
81 public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)
85 lcaseColour.setEnabled(false);
92 oldColourScheme = seqGroup.cs;
96 oldColourScheme = ap.av.getGlobalColourScheme();
99 if (oldColourScheme instanceof UserColourScheme)
101 schemeName.setText(((UserColourScheme) oldColourScheme).getName());
102 if (((UserColourScheme) oldColourScheme).getLowerCaseColours() != null)
104 caseSensitive.setSelected(true);
105 lcaseColour.setEnabled(true);
106 resetButtonPanel(true);
110 resetButtonPanel(false);
115 resetButtonPanel(false);
121 public UserDefinedColours(AppJmol jmol, ColourSchemeI oldcs)
126 colorChooser.getSelectionModel().addChangeListener(this);
128 oldColourScheme = oldcs;
130 if (oldColourScheme instanceof UserColourScheme)
132 schemeName.setText(((UserColourScheme) oldColourScheme).getName());
135 resetButtonPanel(false);
143 colorChooser.getSelectionModel().addChangeListener(this);
144 frame = new JInternalFrame();
145 frame.setContentPane(this);
146 Desktop.addInternalFrame(frame, "User Defined Colours", 720, 370, true);
148 if (seqGroup != null)
150 frame.setTitle(frame.getTitle() + " (" + seqGroup.getName() + ")");
153 if (new jalview.util.Platform().isAMac())
155 frame.setSize(760, 370);
159 void resetButtonPanel(boolean caseSensitive)
161 buttonPanel.removeAll();
163 if (upperCaseButtons == null)
165 upperCaseButtons = new Vector();
170 for (int i = 0; i < 20; i++)
174 label = ResidueProperties.aa[i];
178 label = ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i])
182 button = makeButton(label, ResidueProperties.aa[i], upperCaseButtons,
185 buttonPanel.add(button);
188 buttonPanel.add(makeButton("B", "B", upperCaseButtons, 20));
189 buttonPanel.add(makeButton("Z", "Z", upperCaseButtons, 21));
190 buttonPanel.add(makeButton("X", "X", upperCaseButtons, 22));
191 buttonPanel.add(makeButton("Gap", "-", upperCaseButtons, 23));
195 gridLayout.setRows(6);
196 gridLayout.setColumns(4);
200 gridLayout.setRows(7);
202 gridLayout.setColumns(cols + 1);
204 if (lowerCaseButtons == null)
206 lowerCaseButtons = new Vector();
209 for (int i = 0; i < 20; i++)
211 int row = i / cols + 1;
212 int index = (row * cols) + i;
213 button = makeButton(ResidueProperties.aa[i].toLowerCase(),
214 ResidueProperties.aa[i].toLowerCase(), lowerCaseButtons, i);
216 buttonPanel.add(button, index);
222 buttonPanel.add(makeButton("b", "b", lowerCaseButtons, 20));
223 buttonPanel.add(makeButton("z", "z", lowerCaseButtons, 21));
224 buttonPanel.add(makeButton("x", "x", lowerCaseButtons, 22));
227 buttonPanel.validate();
237 public void stateChanged(ChangeEvent evt)
239 if (selectedButtons != null)
241 JButton button = null;
242 for (int i = 0; i < selectedButtons.size(); i++)
244 button = (JButton) selectedButtons.elementAt(i);
245 button.setBackground(colorChooser.getColor());
246 button.setForeground(button.getBackground().brighter().brighter()
249 if (button == lcaseColour)
251 for (int i = 0; i < lowerCaseButtons.size(); i++)
253 button = (JButton) lowerCaseButtons.elementAt(i);
254 button.setBackground(colorChooser.getColor());
255 button.setForeground(button.getBackground().brighter().brighter()
268 public void colourButtonPressed(MouseEvent e)
270 if (selectedButtons == null)
272 selectedButtons = new Vector();
275 JButton pressed = (JButton) e.getSource();
279 JButton start, end = (JButton) e.getSource();
280 if (selectedButtons.size() > 0)
282 start = (JButton) selectedButtons
283 .elementAt(selectedButtons.size() - 1);
287 start = (JButton) e.getSource();
290 int startIndex = 0, endIndex = 0;
291 for (int b = 0; b < buttonPanel.getComponentCount(); b++)
293 if (buttonPanel.getComponent(b) == start)
297 if (buttonPanel.getComponent(b) == end)
303 if (startIndex > endIndex)
305 int temp = startIndex;
306 startIndex = endIndex;
310 for (int b = startIndex; b <= endIndex; b++)
312 JButton button = (JButton) buttonPanel.getComponent(b);
313 if (!selectedButtons.contains(button))
315 button.setForeground(button.getBackground().brighter().brighter());
316 selectedButtons.add(button);
320 else if (!e.isControlDown())
322 for (int b = 0; b < selectedButtons.size(); b++)
324 JButton button = (JButton) selectedButtons.elementAt(b);
325 button.setForeground(button.getBackground().darker().darker());
327 selectedButtons.clear();
328 pressed.setForeground(pressed.getBackground().brighter().brighter());
329 selectedButtons.addElement(pressed);
332 else if (e.isControlDown())
334 if (selectedButtons.contains(pressed))
336 pressed.setForeground(pressed.getBackground().darker().darker());
337 selectedButtons.remove(pressed);
341 pressed.setForeground(pressed.getBackground().brighter().brighter());
342 selectedButtons.addElement(pressed);
346 if (selectedButtons.size() > 0)
348 colorChooser.setColor(((JButton) selectedButtons.elementAt(0))
361 JButton makeButton(String label, String aa, Vector caseSensitiveButtons,
364 final JButton button;
367 if (buttonIndex < caseSensitiveButtons.size())
369 button = (JButton) caseSensitiveButtons.elementAt(buttonIndex);
370 col = button.getBackground();
374 button = new JButton();
375 button.addMouseListener(new java.awt.event.MouseAdapter()
377 public void mouseClicked(MouseEvent e)
379 colourButtonPressed(e);
383 caseSensitiveButtons.addElement(button);
386 if (oldColourScheme != null)
390 col = oldColourScheme.findColour(aa.charAt(0), -1, null);
391 } catch (Exception ex)
397 if (caseSensitive.isSelected())
399 button.setMargin(new java.awt.Insets(2, 2, 2, 2));
403 button.setMargin(new java.awt.Insets(2, 14, 2, 14));
406 button.setBackground(col);
407 button.setText(label);
408 button.setForeground(col.darker().darker().darker());
409 button.setFont(new java.awt.Font("Verdana", Font.BOLD, 10));
420 protected void okButton_actionPerformed(ActionEvent e)
422 applyButton_actionPerformed(null);
426 frame.setClosed(true);
427 } catch (Exception ex)
438 protected void applyButton_actionPerformed(ActionEvent e)
440 UserColourScheme ucs = getSchemeFromButtons();
441 ucs.setName(schemeName.getText());
443 if (seqGroup != null)
446 ap.paintAlignment(true);
450 ap.alignFrame.changeColour(ucs);
452 else if (jmol != null)
454 jmol.setJalviewColourScheme(ucs);
458 UserColourScheme getSchemeFromButtons()
461 Color[] newColours = new Color[24];
463 for (int i = 0; i < 24; i++)
465 JButton button = (JButton) upperCaseButtons.elementAt(i);
466 newColours[i] = button.getBackground();
469 UserColourScheme ucs = new UserColourScheme(newColours);
471 if (caseSensitive.isSelected())
473 newColours = new Color[23];
474 for (int i = 0; i < 23; i++)
476 JButton button = (JButton) lowerCaseButtons.elementAt(i);
477 newColours[i] = button.getBackground();
479 ucs.setLowerCaseColours(newColours);
484 ucs.setThreshold(0, ap.av.getIgnoreGapsConsensus());
496 protected void loadbutton_actionPerformed(ActionEvent e)
498 upperCaseButtons = new Vector();
499 lowerCaseButtons = new Vector();
501 JalviewFileChooser chooser = new JalviewFileChooser(
502 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
503 { "jc" }, new String[]
504 { "Jalview User Colours" }, "Jalview User Colours");
505 chooser.setFileView(new jalview.io.JalviewFileView());
506 chooser.setDialogTitle("Load colour scheme");
507 chooser.setToolTipText("Load");
509 int value = chooser.showOpenDialog(this);
511 if (value == JalviewFileChooser.APPROVE_OPTION)
513 File choice = chooser.getSelectedFile();
514 jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
515 String defaultColours = jalview.bin.Cache.getDefault(
516 "USER_DEFINED_COLOURS", choice.getPath());
517 if (defaultColours.indexOf(choice.getPath()) == -1)
519 defaultColours = defaultColours.concat("|")
520 .concat(choice.getPath());
523 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
525 UserColourScheme ucs = loadColours(choice.getAbsolutePath());
526 Color[] colors = ucs.getColours();
527 schemeName.setText(ucs.getName());
529 if (ucs.getLowerCaseColours() != null)
531 caseSensitive.setSelected(true);
532 lcaseColour.setEnabled(true);
533 resetButtonPanel(true);
534 for (int i = 0; i < lowerCaseButtons.size(); i++)
536 JButton button = (JButton) lowerCaseButtons.elementAt(i);
537 button.setBackground(ucs.getLowerCaseColours()[i]);
543 caseSensitive.setSelected(false);
544 lcaseColour.setEnabled(false);
545 resetButtonPanel(false);
548 for (int i = 0; i < upperCaseButtons.size(); i++)
550 JButton button = (JButton) upperCaseButtons.elementAt(i);
551 button.setBackground(colors[i]);
560 * @return DOCUMENT ME!
562 public static UserColourScheme loadDefaultColours()
564 UserColourScheme ret = null;
566 String colours = jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS");
569 if (colours.indexOf("|") > -1)
571 colours = colours.substring(0, colours.indexOf("|"));
574 ret = loadColours(colours);
579 Color[] newColours = new Color[24];
580 for (int i = 0; i < 24; i++)
582 newColours[i] = Color.white;
584 ret = new UserColourScheme(newColours);
596 * @return DOCUMENT ME!
598 static UserColourScheme loadColours(String file)
600 UserColourScheme ucs = null;
601 Color[] newColours = null;
604 InputStreamReader in = new InputStreamReader(
605 new FileInputStream(file), "UTF-8");
607 jalview.schemabinding.version2.JalviewUserColours jucs = new jalview.schemabinding.version2.JalviewUserColours();
609 org.exolab.castor.xml.Unmarshaller unmar = new org.exolab.castor.xml.Unmarshaller(
611 jucs = (jalview.schemabinding.version2.JalviewUserColours) unmar
614 newColours = new Color[24];
616 Color[] lowerCase = null;
617 boolean caseSensitive = false;
621 for (int i = 0; i < jucs.getColourCount(); i++)
623 name = jucs.getColour(i).getName();
624 if (ResidueProperties.aa3Hash.containsKey(name))
626 index = ((Integer) ResidueProperties.aa3Hash.get(name))
631 index = ResidueProperties.aaIndex[name.charAt(0)];
638 if (name.toLowerCase().equals(name))
640 if (lowerCase == null)
642 lowerCase = new Color[23];
644 caseSensitive = true;
645 lowerCase[index] = new Color(Integer.parseInt(jucs.getColour(i)
650 newColours[index] = new Color(Integer.parseInt(jucs.getColour(i)
655 if (newColours != null)
657 ucs = new UserColourScheme(newColours);
658 ucs.setName(jucs.getSchemeName());
661 ucs.setLowerCaseColours(lowerCase);
665 } catch (Exception ex)
667 // Could be Archive Jalview format
670 InputStreamReader in = new InputStreamReader(new FileInputStream(
673 jalview.binding.JalviewUserColours jucs = new jalview.binding.JalviewUserColours();
675 jucs = (jalview.binding.JalviewUserColours) jucs.unmarshal(in);
677 newColours = new Color[jucs.getColourCount()];
679 for (int i = 0; i < 24; i++)
681 newColours[i] = new Color(Integer.parseInt(jucs.getColour(i)
684 if (newColours != null)
686 ucs = new UserColourScheme(newColours);
687 ucs.setName(jucs.getSchemeName());
689 } catch (Exception ex2)
691 ex2.printStackTrace();
694 if (newColours == null)
696 System.out.println("Error loading User ColourFile\n" + ex);
709 protected void savebutton_actionPerformed(ActionEvent e)
711 if (schemeName.getText().trim().length() < 1)
713 JOptionPane.showInternalMessageDialog(Desktop.desktop,
714 MessageManager.getString("label.user_colour_scheme_must_have_name"),
715 MessageManager.getString("label.no_name_colour_scheme"), JOptionPane.WARNING_MESSAGE);
719 if (userColourSchemes != null
720 && userColourSchemes.containsKey(schemeName.getText()))
722 int reply = JOptionPane.showInternalConfirmDialog(
724 MessageManager.formatMessage("label.colour_scheme_exists_overwrite", new String[]{schemeName.getText(),schemeName.getText()}),
725 MessageManager.getString("label.duplicate_scheme_name"), JOptionPane.YES_NO_OPTION);
726 if (reply != JOptionPane.YES_OPTION)
731 userColourSchemes.remove(schemeName.getText());
733 JalviewFileChooser chooser = new JalviewFileChooser(
734 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
735 { "jc" }, new String[]
736 { "Jalview User Colours" }, "Jalview User Colours");
738 chooser.setFileView(new jalview.io.JalviewFileView());
739 chooser.setDialogTitle("Save colour scheme");
740 chooser.setToolTipText("Save");
742 int value = chooser.showSaveDialog(this);
744 if (value == JalviewFileChooser.APPROVE_OPTION)
746 String choice = chooser.getSelectedFile().getPath();
747 String defaultColours = jalview.bin.Cache.getDefault(
748 "USER_DEFINED_COLOURS", choice);
749 if (defaultColours.indexOf(choice) == -1)
751 if (defaultColours.length() > 0)
753 defaultColours = defaultColours.concat("|");
755 defaultColours = defaultColours.concat(choice);
758 userColourSchemes.put(schemeName.getText(), getSchemeFromButtons());
760 ap.alignFrame.updateUserColourMenu();
762 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
764 jalview.schemabinding.version2.JalviewUserColours ucs = new jalview.schemabinding.version2.JalviewUserColours();
766 ucs.setSchemeName(schemeName.getText());
769 PrintWriter out = new PrintWriter(new OutputStreamWriter(
770 new FileOutputStream(choice), "UTF-8"));
772 for (int i = 0; i < buttonPanel.getComponentCount(); i++)
774 JButton button = (JButton) buttonPanel.getComponent(i);
775 jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
776 col.setName(button.getText());
777 col.setRGB(jalview.util.Format.getHexString(button
784 } catch (Exception ex)
786 ex.printStackTrace();
797 protected void cancelButton_actionPerformed(ActionEvent e)
801 if (seqGroup != null)
803 seqGroup.cs = oldColourScheme;
807 ap.av.setGlobalColourScheme(oldColourScheme);
809 ap.paintAlignment(true);
814 jmol.setJalviewColourScheme(oldColourScheme);
819 frame.setClosed(true);
820 } catch (Exception ex)
825 static Hashtable userColourSchemes;
827 public static Hashtable getUserColourSchemes()
829 return userColourSchemes;
832 public static void initUserColourSchemes(String files)
834 userColourSchemes = new Hashtable();
836 if (files == null || files.length() == 0)
841 // In case colours can't be loaded, we'll remove them
842 // from the default list here.
843 StringBuffer coloursFound = new StringBuffer();
844 StringTokenizer st = new StringTokenizer(files, "|");
845 while (st.hasMoreElements())
847 String file = st.nextToken();
850 UserColourScheme ucs = loadColours(file);
853 if (coloursFound.length() > 0)
855 coloursFound.append("|");
857 coloursFound.append(file);
858 userColourSchemes.put(ucs.getName(), ucs);
860 } catch (Exception ex)
862 System.out.println("Error loading User ColourFile\n" + ex);
865 if (!files.equals(coloursFound.toString()))
867 if (coloursFound.toString().length() > 1)
869 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS",
870 coloursFound.toString());
874 jalview.bin.Cache.applicationProperties
875 .remove("USER_DEFINED_COLOURS");
880 public static void removeColourFromDefaults(String target)
882 // The only way to find colours by name is to load them in
883 // In case colours can't be loaded, we'll remove them
884 // from the default list here.
886 userColourSchemes = new Hashtable();
888 StringBuffer coloursFound = new StringBuffer();
889 StringTokenizer st = new StringTokenizer(
890 jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS"), "|");
892 while (st.hasMoreElements())
894 String file = st.nextToken();
897 UserColourScheme ucs = loadColours(file);
898 if (ucs != null && !ucs.getName().equals(target))
900 if (coloursFound.length() > 0)
902 coloursFound.append("|");
904 coloursFound.append(file);
905 userColourSchemes.put(ucs.getName(), ucs);
907 } catch (Exception ex)
909 System.out.println("Error loading User ColourFile\n" + ex);
913 if (coloursFound.toString().length() > 1)
915 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS",
916 coloursFound.toString());
920 jalview.bin.Cache.applicationProperties
921 .remove("USER_DEFINED_COLOURS");
926 public void caseSensitive_actionPerformed(ActionEvent e)
928 resetButtonPanel(caseSensitive.isSelected());
929 lcaseColour.setEnabled(caseSensitive.isSelected());
932 public void lcaseColour_actionPerformed(ActionEvent e)
934 if (selectedButtons == null)
936 selectedButtons = new Vector();
940 selectedButtons.clear();
942 selectedButtons.add(lcaseColour);