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;
27 import java.awt.Color;
29 import java.awt.event.ActionEvent;
30 import java.awt.event.MouseEvent;
32 import java.io.FileInputStream;
33 import java.io.FileOutputStream;
34 import java.io.InputStreamReader;
35 import java.io.OutputStreamWriter;
36 import java.io.PrintWriter;
37 import java.util.Hashtable;
38 import java.util.StringTokenizer;
39 import java.util.Vector;
41 import javax.swing.JButton;
42 import javax.swing.JInternalFrame;
43 import javax.swing.JOptionPane;
44 import javax.swing.event.ChangeEvent;
45 import javax.swing.event.ChangeListener;
53 public class UserDefinedColours extends GUserDefinedColours implements
58 SequenceGroup seqGroup;
60 Vector selectedButtons;
62 ColourSchemeI oldColourScheme;
68 Vector upperCaseButtons;
70 Vector lowerCaseButtons;
73 * Creates a new UserDefinedColours object.
80 public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)
84 lcaseColour.setEnabled(false);
91 oldColourScheme = seqGroup.cs;
95 oldColourScheme = ap.av.getGlobalColourScheme();
98 if (oldColourScheme instanceof UserColourScheme)
100 schemeName.setText(((UserColourScheme) oldColourScheme).getName());
101 if (((UserColourScheme) oldColourScheme).getLowerCaseColours() != null)
103 caseSensitive.setSelected(true);
104 lcaseColour.setEnabled(true);
105 resetButtonPanel(true);
109 resetButtonPanel(false);
114 resetButtonPanel(false);
120 public UserDefinedColours(AppJmol jmol, ColourSchemeI oldcs)
125 colorChooser.getSelectionModel().addChangeListener(this);
127 oldColourScheme = oldcs;
129 if (oldColourScheme instanceof UserColourScheme)
131 schemeName.setText(((UserColourScheme) oldColourScheme).getName());
134 resetButtonPanel(false);
142 colorChooser.getSelectionModel().addChangeListener(this);
143 frame = new JInternalFrame();
144 frame.setContentPane(this);
145 Desktop.addInternalFrame(frame, "User Defined Colours", 720, 370, true);
147 if (seqGroup != null)
149 frame.setTitle(frame.getTitle() + " (" + seqGroup.getName() + ")");
152 if (new jalview.util.Platform().isAMac())
154 frame.setSize(760, 370);
158 void resetButtonPanel(boolean caseSensitive)
160 buttonPanel.removeAll();
162 if (upperCaseButtons == null)
164 upperCaseButtons = new Vector();
169 for (int i = 0; i < 20; i++)
173 label = ResidueProperties.aa[i];
177 label = ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i])
181 button = makeButton(label, ResidueProperties.aa[i], upperCaseButtons,
184 buttonPanel.add(button);
187 buttonPanel.add(makeButton("B", "B", upperCaseButtons, 20));
188 buttonPanel.add(makeButton("Z", "Z", upperCaseButtons, 21));
189 buttonPanel.add(makeButton("X", "X", upperCaseButtons, 22));
190 buttonPanel.add(makeButton("Gap", "-", upperCaseButtons, 23));
194 gridLayout.setRows(6);
195 gridLayout.setColumns(4);
199 gridLayout.setRows(7);
201 gridLayout.setColumns(cols + 1);
203 if (lowerCaseButtons == null)
205 lowerCaseButtons = new Vector();
208 for (int i = 0; i < 20; i++)
210 int row = i / cols + 1;
211 int index = (row * cols) + i;
212 button = makeButton(ResidueProperties.aa[i].toLowerCase(),
213 ResidueProperties.aa[i].toLowerCase(), lowerCaseButtons, i);
215 buttonPanel.add(button, index);
221 buttonPanel.add(makeButton("b", "b", lowerCaseButtons, 20));
222 buttonPanel.add(makeButton("z", "z", lowerCaseButtons, 21));
223 buttonPanel.add(makeButton("x", "x", lowerCaseButtons, 22));
226 buttonPanel.validate();
236 public void stateChanged(ChangeEvent evt)
238 if (selectedButtons != null)
240 JButton button = null;
241 for (int i = 0; i < selectedButtons.size(); i++)
243 button = (JButton) selectedButtons.elementAt(i);
244 button.setBackground(colorChooser.getColor());
245 button.setForeground(button.getBackground().brighter().brighter()
248 if (button == lcaseColour)
250 for (int i = 0; i < lowerCaseButtons.size(); i++)
252 button = (JButton) lowerCaseButtons.elementAt(i);
253 button.setBackground(colorChooser.getColor());
254 button.setForeground(button.getBackground().brighter().brighter()
267 public void colourButtonPressed(MouseEvent e)
269 if (selectedButtons == null)
271 selectedButtons = new Vector();
274 JButton pressed = (JButton) e.getSource();
278 JButton start, end = (JButton) e.getSource();
279 if (selectedButtons.size() > 0)
281 start = (JButton) selectedButtons
282 .elementAt(selectedButtons.size() - 1);
286 start = (JButton) e.getSource();
289 int startIndex = 0, endIndex = 0;
290 for (int b = 0; b < buttonPanel.getComponentCount(); b++)
292 if (buttonPanel.getComponent(b) == start)
296 if (buttonPanel.getComponent(b) == end)
302 if (startIndex > endIndex)
304 int temp = startIndex;
305 startIndex = endIndex;
309 for (int b = startIndex; b <= endIndex; b++)
311 JButton button = (JButton) buttonPanel.getComponent(b);
312 if (!selectedButtons.contains(button))
314 button.setForeground(button.getBackground().brighter().brighter());
315 selectedButtons.add(button);
319 else if (!e.isControlDown())
321 for (int b = 0; b < selectedButtons.size(); b++)
323 JButton button = (JButton) selectedButtons.elementAt(b);
324 button.setForeground(button.getBackground().darker().darker());
326 selectedButtons.clear();
327 pressed.setForeground(pressed.getBackground().brighter().brighter());
328 selectedButtons.addElement(pressed);
331 else if (e.isControlDown())
333 if (selectedButtons.contains(pressed))
335 pressed.setForeground(pressed.getBackground().darker().darker());
336 selectedButtons.remove(pressed);
340 pressed.setForeground(pressed.getBackground().brighter().brighter());
341 selectedButtons.addElement(pressed);
345 if (selectedButtons.size() > 0)
347 colorChooser.setColor(((JButton) selectedButtons.elementAt(0))
360 JButton makeButton(String label, String aa, Vector caseSensitiveButtons,
363 final JButton button;
366 if (buttonIndex < caseSensitiveButtons.size())
368 button = (JButton) caseSensitiveButtons.elementAt(buttonIndex);
369 col = button.getBackground();
373 button = new JButton();
374 button.addMouseListener(new java.awt.event.MouseAdapter()
376 public void mouseClicked(MouseEvent e)
378 colourButtonPressed(e);
382 caseSensitiveButtons.addElement(button);
385 if (oldColourScheme != null)
389 col = oldColourScheme.findColour(aa.charAt(0), -1, null);
390 } catch (Exception ex)
396 if (caseSensitive.isSelected())
398 button.setMargin(new java.awt.Insets(2, 2, 2, 2));
402 button.setMargin(new java.awt.Insets(2, 14, 2, 14));
405 button.setBackground(col);
406 button.setText(label);
407 button.setForeground(col.darker().darker().darker());
408 button.setFont(new java.awt.Font("Verdana", Font.BOLD, 10));
419 protected void okButton_actionPerformed(ActionEvent e)
421 applyButton_actionPerformed(null);
425 frame.setClosed(true);
426 } catch (Exception ex)
437 protected void applyButton_actionPerformed(ActionEvent e)
439 UserColourScheme ucs = getSchemeFromButtons();
440 ucs.setName(schemeName.getText());
442 if (seqGroup != null)
445 ap.paintAlignment(true);
449 ap.alignFrame.changeColour(ucs);
451 else if (jmol != null)
453 jmol.setJalviewColourScheme(ucs);
457 UserColourScheme getSchemeFromButtons()
460 Color[] newColours = new Color[24];
462 for (int i = 0; i < 24; i++)
464 JButton button = (JButton) upperCaseButtons.elementAt(i);
465 newColours[i] = button.getBackground();
468 UserColourScheme ucs = new UserColourScheme(newColours);
470 if (caseSensitive.isSelected())
472 newColours = new Color[23];
473 for (int i = 0; i < 23; i++)
475 JButton button = (JButton) lowerCaseButtons.elementAt(i);
476 newColours[i] = button.getBackground();
478 ucs.setLowerCaseColours(newColours);
483 ucs.setThreshold(0, ap.av.getIgnoreGapsConsensus());
495 protected void loadbutton_actionPerformed(ActionEvent e)
497 upperCaseButtons = new Vector();
498 lowerCaseButtons = new Vector();
500 JalviewFileChooser chooser = new JalviewFileChooser(
501 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
502 { "jc" }, new String[]
503 { "Jalview User Colours" }, "Jalview User Colours");
504 chooser.setFileView(new jalview.io.JalviewFileView());
505 chooser.setDialogTitle("Load colour scheme");
506 chooser.setToolTipText("Load");
508 int value = chooser.showOpenDialog(this);
510 if (value == JalviewFileChooser.APPROVE_OPTION)
512 File choice = chooser.getSelectedFile();
513 jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
514 String defaultColours = jalview.bin.Cache.getDefault(
515 "USER_DEFINED_COLOURS", choice.getPath());
516 if (defaultColours.indexOf(choice.getPath()) == -1)
518 defaultColours = defaultColours.concat("|")
519 .concat(choice.getPath());
522 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
524 UserColourScheme ucs = loadColours(choice.getAbsolutePath());
525 Color[] colors = ucs.getColours();
526 schemeName.setText(ucs.getName());
528 if (ucs.getLowerCaseColours() != null)
530 caseSensitive.setSelected(true);
531 lcaseColour.setEnabled(true);
532 resetButtonPanel(true);
533 for (int i = 0; i < lowerCaseButtons.size(); i++)
535 JButton button = (JButton) lowerCaseButtons.elementAt(i);
536 button.setBackground(ucs.getLowerCaseColours()[i]);
542 caseSensitive.setSelected(false);
543 lcaseColour.setEnabled(false);
544 resetButtonPanel(false);
547 for (int i = 0; i < upperCaseButtons.size(); i++)
549 JButton button = (JButton) upperCaseButtons.elementAt(i);
550 button.setBackground(colors[i]);
559 * @return DOCUMENT ME!
561 public static UserColourScheme loadDefaultColours()
563 UserColourScheme ret = null;
565 String colours = jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS");
568 if (colours.indexOf("|") > -1)
570 colours = colours.substring(0, colours.indexOf("|"));
573 ret = loadColours(colours);
578 Color[] newColours = new Color[24];
579 for (int i = 0; i < 24; i++)
581 newColours[i] = Color.white;
583 ret = new UserColourScheme(newColours);
595 * @return DOCUMENT ME!
597 static UserColourScheme loadColours(String file)
599 UserColourScheme ucs = null;
600 Color[] newColours = null;
603 InputStreamReader in = new InputStreamReader(
604 new FileInputStream(file), "UTF-8");
606 jalview.schemabinding.version2.JalviewUserColours jucs = new jalview.schemabinding.version2.JalviewUserColours();
608 org.exolab.castor.xml.Unmarshaller unmar = new org.exolab.castor.xml.Unmarshaller(
610 jucs = (jalview.schemabinding.version2.JalviewUserColours) unmar
613 newColours = new Color[24];
615 Color[] lowerCase = null;
616 boolean caseSensitive = false;
620 for (int i = 0; i < jucs.getColourCount(); i++)
622 name = jucs.getColour(i).getName();
623 if (ResidueProperties.aa3Hash.containsKey(name))
625 index = ((Integer) ResidueProperties.aa3Hash.get(name))
630 index = ResidueProperties.aaIndex[name.charAt(0)];
637 if (name.toLowerCase().equals(name))
639 if (lowerCase == null)
641 lowerCase = new Color[23];
643 caseSensitive = true;
644 lowerCase[index] = new Color(Integer.parseInt(jucs.getColour(i)
649 newColours[index] = new Color(Integer.parseInt(jucs.getColour(i)
654 if (newColours != null)
656 ucs = new UserColourScheme(newColours);
657 ucs.setName(jucs.getSchemeName());
660 ucs.setLowerCaseColours(lowerCase);
664 } catch (Exception ex)
666 // Could be Archive Jalview format
669 InputStreamReader in = new InputStreamReader(new FileInputStream(
672 jalview.binding.JalviewUserColours jucs = new jalview.binding.JalviewUserColours();
674 jucs = (jalview.binding.JalviewUserColours) jucs.unmarshal(in);
676 newColours = new Color[jucs.getColourCount()];
678 for (int i = 0; i < 24; i++)
680 newColours[i] = new Color(Integer.parseInt(jucs.getColour(i)
683 if (newColours != null)
685 ucs = new UserColourScheme(newColours);
686 ucs.setName(jucs.getSchemeName());
688 } catch (Exception ex2)
690 ex2.printStackTrace();
693 if (newColours == null)
695 System.out.println("Error loading User ColourFile\n" + ex);
708 protected void savebutton_actionPerformed(ActionEvent e)
710 if (schemeName.getText().trim().length() < 1)
712 JOptionPane.showInternalMessageDialog(Desktop.desktop,
713 "User colour scheme must have a name!",
714 "No name for colour scheme", JOptionPane.WARNING_MESSAGE);
718 if (userColourSchemes != null
719 && userColourSchemes.containsKey(schemeName.getText()))
721 int reply = JOptionPane.showInternalConfirmDialog(
723 "Colour scheme " + schemeName.getText() + " exists."
724 + "\nContinue saving colour scheme as "
725 + schemeName.getText() + "?",
726 "Duplicate scheme name", JOptionPane.YES_NO_OPTION);
727 if (reply != JOptionPane.YES_OPTION)
732 userColourSchemes.remove(schemeName.getText());
734 JalviewFileChooser chooser = new JalviewFileChooser(
735 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
736 { "jc" }, new String[]
737 { "Jalview User Colours" }, "Jalview User Colours");
739 chooser.setFileView(new jalview.io.JalviewFileView());
740 chooser.setDialogTitle("Save colour scheme");
741 chooser.setToolTipText("Save");
743 int value = chooser.showSaveDialog(this);
745 if (value == JalviewFileChooser.APPROVE_OPTION)
747 String choice = chooser.getSelectedFile().getPath();
748 String defaultColours = jalview.bin.Cache.getDefault(
749 "USER_DEFINED_COLOURS", choice);
750 if (defaultColours.indexOf(choice) == -1)
752 if (defaultColours.length() > 0)
754 defaultColours = defaultColours.concat("|");
756 defaultColours = defaultColours.concat(choice);
759 userColourSchemes.put(schemeName.getText(), getSchemeFromButtons());
761 ap.alignFrame.updateUserColourMenu();
763 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
765 jalview.schemabinding.version2.JalviewUserColours ucs = new jalview.schemabinding.version2.JalviewUserColours();
767 ucs.setSchemeName(schemeName.getText());
770 PrintWriter out = new PrintWriter(new OutputStreamWriter(
771 new FileOutputStream(choice), "UTF-8"));
773 for (int i = 0; i < buttonPanel.getComponentCount(); i++)
775 JButton button = (JButton) buttonPanel.getComponent(i);
776 jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
777 col.setName(button.getText());
778 col.setRGB(jalview.util.Format.getHexString(button
785 } catch (Exception ex)
787 ex.printStackTrace();
798 protected void cancelButton_actionPerformed(ActionEvent e)
802 if (seqGroup != null)
804 seqGroup.cs = oldColourScheme;
808 ap.av.setGlobalColourScheme(oldColourScheme);
810 ap.paintAlignment(true);
815 jmol.setJalviewColourScheme(oldColourScheme);
820 frame.setClosed(true);
821 } catch (Exception ex)
826 static Hashtable userColourSchemes;
828 public static Hashtable getUserColourSchemes()
830 return userColourSchemes;
833 public static void initUserColourSchemes(String files)
835 userColourSchemes = new Hashtable();
837 if (files == null || files.length() == 0)
842 // In case colours can't be loaded, we'll remove them
843 // from the default list here.
844 StringBuffer coloursFound = new StringBuffer();
845 StringTokenizer st = new StringTokenizer(files, "|");
846 while (st.hasMoreElements())
848 String file = st.nextToken();
851 UserColourScheme ucs = loadColours(file);
854 if (coloursFound.length() > 0)
856 coloursFound.append("|");
858 coloursFound.append(file);
859 userColourSchemes.put(ucs.getName(), ucs);
861 } catch (Exception ex)
863 System.out.println("Error loading User ColourFile\n" + ex);
866 if (!files.equals(coloursFound.toString()))
868 if (coloursFound.toString().length() > 1)
870 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS",
871 coloursFound.toString());
875 jalview.bin.Cache.applicationProperties
876 .remove("USER_DEFINED_COLOURS");
881 public static void removeColourFromDefaults(String target)
883 // The only way to find colours by name is to load them in
884 // In case colours can't be loaded, we'll remove them
885 // from the default list here.
887 userColourSchemes = new Hashtable();
889 StringBuffer coloursFound = new StringBuffer();
890 StringTokenizer st = new StringTokenizer(
891 jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS"), "|");
893 while (st.hasMoreElements())
895 String file = st.nextToken();
898 UserColourScheme ucs = loadColours(file);
899 if (ucs != null && !ucs.getName().equals(target))
901 if (coloursFound.length() > 0)
903 coloursFound.append("|");
905 coloursFound.append(file);
906 userColourSchemes.put(ucs.getName(), ucs);
908 } catch (Exception ex)
910 System.out.println("Error loading User ColourFile\n" + ex);
914 if (coloursFound.toString().length() > 1)
916 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS",
917 coloursFound.toString());
921 jalview.bin.Cache.applicationProperties
922 .remove("USER_DEFINED_COLOURS");
927 public void caseSensitive_actionPerformed(ActionEvent e)
929 resetButtonPanel(caseSensitive.isSelected());
930 lcaseColour.setEnabled(caseSensitive.isSelected());
933 public void lcaseColour_actionPerformed(ActionEvent e)
935 if (selectedButtons == null)
937 selectedButtons = new Vector();
941 selectedButtons.clear();
943 selectedButtons.add(lcaseColour);