2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.gui;
\r
21 import jalview.datamodel.*;
\r
23 import jalview.io.*;
\r
25 import jalview.jbgui.*;
\r
27 import jalview.schemes.*;
\r
30 import java.awt.event.*;
\r
36 import javax.swing.*;
\r
37 import javax.swing.event.*;
\r
45 * @version $Revision$
\r
47 public class UserDefinedColours extends GUserDefinedColours
\r
48 implements ChangeListener
\r
51 SequenceGroup seqGroup;
\r
52 Vector selectedButtons;
\r
53 ColourSchemeI oldColourScheme;
\r
54 JInternalFrame frame;
\r
55 MCview.PDBCanvas pdbcanvas;
\r
56 Vector upperCaseButtons;
\r
57 Vector lowerCaseButtons;
\r
61 * Creates a new UserDefinedColours object.
\r
63 * @param ap DOCUMENT ME!
\r
64 * @param sg DOCUMENT ME!
\r
66 public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)
\r
70 //AW - LOWER CASE DISABLED IN 2.1.01 bug fix release
\r
71 casePanel.setVisible(false);
\r
74 lcaseColour.setEnabled(false);
\r
79 if (seqGroup != null)
\r
80 oldColourScheme = seqGroup.cs;
\r
82 oldColourScheme = ap.av.getGlobalColourScheme();
\r
84 if (oldColourScheme instanceof UserColourScheme)
\r
86 schemeName.setText( ( (UserColourScheme) oldColourScheme).getName());
\r
87 if(( (UserColourScheme) oldColourScheme).getLowerCaseColours()!=null)
\r
89 caseSensitive.setSelected(true);
\r
90 lcaseColour.setEnabled(true);
\r
91 resetButtonPanel(true);
\r
94 resetButtonPanel(false);
\r
97 resetButtonPanel(false);
\r
102 public UserDefinedColours(MCview.PDBCanvas pdb, ColourSchemeI oldcs)
\r
107 colorChooser.getSelectionModel().addChangeListener(this);
\r
109 oldColourScheme = oldcs;
\r
111 if (oldColourScheme instanceof UserColourScheme)
\r
113 schemeName.setText( ( (UserColourScheme) oldColourScheme).getName());
\r
116 resetButtonPanel(false);
\r
124 colorChooser.getSelectionModel().addChangeListener(this);
\r
125 frame = new JInternalFrame();
\r
126 frame.setContentPane(this);
\r
127 Desktop.addInternalFrame(frame, "User Defined Colours", 720, 370, true);
\r
129 if (seqGroup != null)
\r
130 frame.setTitle(frame.getTitle() + " (" + seqGroup.getName() + ")");
\r
132 if (System.getProperty("os.name").startsWith("Mac"))
\r
134 frame.setSize(760, 370);
\r
138 void resetButtonPanel(boolean caseSensitive)
\r
140 //AW - LOWER CASE DISABLED IN 2.1.01 bug fix release
\r
141 caseSensitive = false;
\r
143 buttonPanel.removeAll();
\r
146 if(upperCaseButtons==null)
\r
147 upperCaseButtons = new Vector();
\r
151 for (int i = 0; i < 20; i++)
\r
154 label = ResidueProperties.aa[i];
\r
156 label = ResidueProperties.aa2Triplet.get
\r
157 (ResidueProperties.aa[i]).toString();
\r
159 button = makeButton(label,
\r
160 ResidueProperties.aa[i],
\r
161 upperCaseButtons, i);
\r
163 buttonPanel.add(button);
\r
166 buttonPanel.add(makeButton("B", "B", upperCaseButtons, 20));
\r
167 buttonPanel.add(makeButton("Z", "Z", upperCaseButtons, 21));
\r
168 buttonPanel.add(makeButton("X", "X", upperCaseButtons, 22));
\r
169 buttonPanel.add(makeButton("Gap", "-", upperCaseButtons, 23));
\r
173 gridLayout.setRows(6);
\r
174 gridLayout.setColumns(4);
\r
178 gridLayout.setRows(7);
\r
180 gridLayout.setColumns(cols+1);
\r
182 if(lowerCaseButtons==null)
\r
183 lowerCaseButtons = new Vector();
\r
186 for (int i = 0; i < 20; i++)
\r
188 int row = i / cols + 1;
\r
189 int index = (row * cols) + i;
\r
190 button = makeButton(
\r
191 ResidueProperties.aa[i].toLowerCase(),
\r
192 ResidueProperties.aa[i].toLowerCase(),
\r
196 buttonPanel.add(button, index);
\r
203 buttonPanel.add(makeButton("b", "b", lowerCaseButtons, 20));
\r
204 buttonPanel.add(makeButton("z", "z", lowerCaseButtons, 21));
\r
205 buttonPanel.add(makeButton("x", "x", lowerCaseButtons, 22));
\r
208 buttonPanel.validate();
\r
216 * @param evt DOCUMENT ME!
\r
218 public void stateChanged(ChangeEvent evt)
\r
220 if (selectedButtons != null)
\r
222 JButton button=null;
\r
223 for(int i=0; i<selectedButtons.size(); i++)
\r
225 button = (JButton)selectedButtons.elementAt(i);
\r
226 button.setBackground(colorChooser.getColor());
\r
227 button.setForeground( button.getBackground().brighter().brighter().brighter());
\r
229 if(button==lcaseColour)
\r
231 for(int i=0; i<lowerCaseButtons.size(); i++)
\r
233 button = (JButton)lowerCaseButtons.elementAt(i);
\r
234 button.setBackground(colorChooser.getColor());
\r
235 button.setForeground( button.getBackground().brighter().brighter().brighter());
\r
244 * @param e DOCUMENT ME!
\r
246 public void colourButtonPressed(MouseEvent e)
\r
248 if(selectedButtons == null)
\r
249 selectedButtons = new Vector();
\r
251 JButton pressed = (JButton) e.getSource();
\r
253 if(e.isShiftDown())
\r
255 JButton start, end = (JButton) e.getSource();
\r
256 if(selectedButtons.size()>0)
\r
257 start = (JButton)selectedButtons.elementAt(selectedButtons.size()-1);
\r
259 start = (JButton) e.getSource();
\r
261 int startIndex=0, endIndex=0;
\r
262 for(int b=0; b<buttonPanel.getComponentCount(); b++)
\r
264 if(buttonPanel.getComponent(b)==start)
\r
266 if(buttonPanel.getComponent(b)==end)
\r
270 if(startIndex > endIndex)
\r
272 int temp = startIndex;
\r
273 startIndex = endIndex;
\r
277 for(int b=startIndex; b<=endIndex; b++)
\r
279 JButton button = (JButton)buttonPanel.getComponent(b);
\r
280 if(!selectedButtons.contains(button))
\r
282 button.setForeground(button.getBackground().brighter().brighter());
\r
283 selectedButtons.add(button);
\r
287 else if(!e.isControlDown())
\r
289 for(int b=0; b<selectedButtons.size(); b++)
\r
291 JButton button = (JButton)selectedButtons.elementAt(b);
\r
292 button.setForeground(button.getBackground().darker().darker());
\r
294 selectedButtons.clear();
\r
295 pressed.setForeground( pressed.getBackground().brighter().brighter());
\r
296 selectedButtons.addElement(pressed);
\r
299 else if(e.isControlDown())
\r
301 if(selectedButtons.contains(pressed))
\r
303 pressed.setForeground(pressed.getBackground().darker().darker());
\r
304 selectedButtons.remove(pressed);
\r
308 pressed.setForeground( pressed.getBackground().brighter().brighter());
\r
309 selectedButtons.addElement(pressed);
\r
313 if(selectedButtons.size()>0)
\r
314 colorChooser.setColor( ((JButton)selectedButtons.elementAt(0)).getBackground());
\r
320 * @param label DOCUMENT ME!
\r
321 * @param aa DOCUMENT ME!
\r
323 JButton makeButton(String label,
\r
325 Vector caseSensitiveButtons,
\r
328 final JButton button;
\r
331 if(buttonIndex<caseSensitiveButtons.size())
\r
333 button = (JButton) caseSensitiveButtons.elementAt(buttonIndex);
\r
334 col = button.getBackground();
\r
338 button = new JButton();
\r
339 button.addMouseListener(new java.awt.event.MouseAdapter()
\r
341 public void mouseClicked(MouseEvent e)
\r
343 colourButtonPressed(e);
\r
347 caseSensitiveButtons.addElement(button);
\r
353 col = oldColourScheme.findColour(aa, -1);
\r
355 catch (Exception ex)
\r
359 /* //AW - LOWER CASE DISABLED IN 2.1.01 bug fix release
\r
360 if(caseSensitive.isSelected())
\r
361 button.setMargin(new java.awt.Insets(2,2,2,2));
\r
363 button.setMargin(new java.awt.Insets(2,14,2,14));
\r
366 button.setBackground(col);
\r
367 button.setText(label);
\r
368 button.setForeground(col.darker().darker().darker());
\r
369 button.setFont(new java.awt.Font("Verdana", Font.BOLD, 10));
\r
377 * @param e DOCUMENT ME!
\r
379 protected void okButton_actionPerformed(ActionEvent e)
\r
381 applyButton_actionPerformed(null);
\r
385 frame.setClosed(true);
\r
387 catch (Exception ex)
\r
395 * @param e DOCUMENT ME!
\r
397 protected void applyButton_actionPerformed(ActionEvent e)
\r
399 UserColourScheme ucs = getSchemeFromButtons();
\r
400 ucs.setName(schemeName.getText());
\r
402 if (seqGroup != null)
\r
409 ap.alignFrame.changeColour(ucs);
\r
411 else if(pdbcanvas!=null)
\r
413 pdbcanvas.pdb.setColours(ucs);
\r
414 pdbcanvas.updateSeqColours();
\r
418 UserColourScheme getSchemeFromButtons()
\r
421 Color[] newColours = new Color[24];
\r
423 for (int i = 0; i < 24; i++)
\r
425 JButton button = (JButton) upperCaseButtons.elementAt(i);
\r
426 newColours[i] = button.getBackground();
\r
429 UserColourScheme ucs = new UserColourScheme(newColours);
\r
432 if(caseSensitive.isSelected())
\r
434 newColours = new Color[23];
\r
435 for (int i = 0; i < 23; i++)
\r
437 JButton button = (JButton) lowerCaseButtons.elementAt(i);
\r
438 newColours[i] = button.getBackground();
\r
440 ucs.setLowerCaseColours(newColours);
\r
444 ucs.setThreshold(0, ap.av.getIgnoreGapsConsensus());
\r
455 * @param e DOCUMENT ME!
\r
457 protected void loadbutton_actionPerformed(ActionEvent e)
\r
459 upperCaseButtons = new Vector();
\r
460 lowerCaseButtons = new Vector();
\r
462 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
\r
463 "LAST_DIRECTORY"), new String[] { "jc" },
\r
464 new String[] { "Jalview User Colours" }, "Jalview User Colours");
\r
465 chooser.setFileView(new jalview.io.JalviewFileView());
\r
466 chooser.setDialogTitle("Load colour scheme");
\r
467 chooser.setToolTipText("Load");
\r
469 int value = chooser.showOpenDialog(this);
\r
471 if (value == JalviewFileChooser.APPROVE_OPTION)
\r
473 File choice = chooser.getSelectedFile();
\r
474 jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
\r
475 String defaultColours = jalview.bin.Cache.getDefault("USER_DEFINED_COLOURS",
\r
477 if (defaultColours.indexOf(choice.getPath()) == -1)
\r
478 defaultColours = defaultColours.concat("|").concat(choice.getPath());
\r
480 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
\r
483 UserColourScheme ucs = loadColours(choice.getAbsolutePath());
\r
484 Color[] colors = ucs.getColours();
\r
485 schemeName.setText(ucs.getName());
\r
487 if(ucs.getLowerCaseColours()!=null)
\r
489 caseSensitive.setSelected(true);
\r
490 lcaseColour.setEnabled(true);
\r
491 resetButtonPanel(true);
\r
492 for (int i = 0; i < lowerCaseButtons.size(); i++)
\r
494 JButton button = (JButton) lowerCaseButtons.elementAt(i);
\r
495 button.setBackground(ucs.getLowerCaseColours()[i]);
\r
501 caseSensitive.setSelected(false);
\r
502 lcaseColour.setEnabled(false);
\r
503 resetButtonPanel(false);
\r
506 for (int i = 0; i < upperCaseButtons.size(); i++)
\r
508 JButton button = (JButton) upperCaseButtons.elementAt(i);
\r
509 button.setBackground(colors[i]);
\r
518 * @return DOCUMENT ME!
\r
520 public static UserColourScheme loadDefaultColours()
\r
522 UserColourScheme ret = null;
\r
524 String colours = jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS");
\r
525 if ( colours != null )
\r
527 if(colours.indexOf("|")>-1)
\r
528 colours = colours.substring(0, colours.indexOf("|"));
\r
530 ret = loadColours(colours);
\r
535 Color[] newColours = new Color[24];
\r
536 for (int i = 0; i < 24; i++)
\r
538 newColours[i] = Color.white;
\r
540 ret = new UserColourScheme(newColours);
\r
549 * @param file DOCUMENT ME!
\r
551 * @return DOCUMENT ME!
\r
553 static UserColourScheme loadColours(String file)
\r
555 UserColourScheme ucs = null;
\r
556 Color[] newColours = null;
\r
559 InputStreamReader in = new InputStreamReader(new FileInputStream(
\r
562 jalview.schemabinding.version2.JalviewUserColours jucs
\r
563 = new jalview.schemabinding.version2.JalviewUserColours();
\r
565 org.exolab.castor.xml.Unmarshaller unmar
\r
566 = new org.exolab.castor.xml.Unmarshaller(jucs);
\r
567 jucs = (jalview.schemabinding.version2.JalviewUserColours) unmar.unmarshal( in );
\r
569 newColours = new Color[24];
\r
571 Color [] lowerCase = null;
\r
572 boolean caseSensitive = false;
\r
577 for (int i = 0; i < jucs.getColourCount(); i++)
\r
579 name = jucs.getColour(i).getName();
\r
580 if(ResidueProperties.aa3Hash.containsKey(name))
\r
582 index = ((Integer)ResidueProperties.aa3Hash.get(name)).intValue();
\r
586 index = ResidueProperties.aaIndex[name.charAt(0)];
\r
591 if(name.toLowerCase().equals(name))
\r
593 if(lowerCase==null)
\r
594 lowerCase = new Color[23];
\r
595 caseSensitive = true;
\r
596 lowerCase[index] = new Color(Integer.parseInt(
\r
597 jucs.getColour(i).getRGB(), 16));
\r
601 newColours[index] = new Color(Integer.parseInt(
\r
602 jucs.getColour(i).getRGB(), 16));
\r
606 if (newColours != null)
\r
608 ucs = new UserColourScheme(newColours);
\r
609 ucs.setName( jucs.getSchemeName() );
\r
612 ucs.setLowerCaseColours(lowerCase);
\r
617 catch (Exception ex)
\r
619 //Could be Archive Jalview format
\r
621 InputStreamReader in = new InputStreamReader(new FileInputStream(
\r
624 jalview.binding.JalviewUserColours jucs
\r
625 = new jalview.binding.JalviewUserColours();
\r
627 jucs = (jalview.binding.JalviewUserColours) jucs.unmarshal(in);
\r
629 newColours = new Color[jucs.getColourCount()];
\r
631 for (int i = 0; i < 24; i++)
\r
633 newColours[i] = new Color(Integer.parseInt(
\r
634 jucs.getColour(i).getRGB(), 16));
\r
636 if (newColours != null)
\r
638 ucs = new UserColourScheme(newColours);
\r
639 ucs.setName(jucs.getSchemeName());
\r
641 }catch(Exception ex2)
\r
642 { ex2.printStackTrace(); }
\r
644 if(newColours==null)
\r
645 System.out.println("Error loading User ColourFile\n"+ex);
\r
654 * @param e DOCUMENT ME!
\r
656 protected void savebutton_actionPerformed(ActionEvent e)
\r
658 if(schemeName.getText().trim().length()<1)
\r
660 JOptionPane.showInternalMessageDialog(Desktop.desktop,
\r
661 "User colour scheme must have a name!",
\r
662 "No name for colour scheme",
\r
663 JOptionPane.WARNING_MESSAGE);
\r
667 if(userColourSchemes!=null && userColourSchemes.containsKey(schemeName.getText()) )
\r
669 int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop, "Colour scheme "+schemeName.getText()+ " exists."
\r
670 +"\nContinue saving colour scheme as "+schemeName.getText()+"?",
\r
671 "Duplicate scheme name", JOptionPane.YES_NO_OPTION);
\r
672 if(reply != JOptionPane.YES_OPTION)
\r
675 userColourSchemes.remove(schemeName.getText());
\r
677 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
\r
678 "LAST_DIRECTORY"), new String[] { "jc" },
\r
679 new String[] { "Jalview User Colours" }, "Jalview User Colours");
\r
681 chooser.setFileView(new jalview.io.JalviewFileView());
\r
682 chooser.setDialogTitle("Save colour scheme");
\r
683 chooser.setToolTipText("Save");
\r
685 int value = chooser.showSaveDialog(this);
\r
687 if (value == JalviewFileChooser.APPROVE_OPTION)
\r
689 String choice = chooser.getSelectedFile().getPath();
\r
690 String defaultColours = jalview.bin.Cache.getDefault("USER_DEFINED_COLOURS", choice);
\r
691 if(defaultColours.indexOf(choice)==-1)
\r
693 if(defaultColours.length()>0)
\r
694 defaultColours = defaultColours.concat("|");
\r
695 defaultColours = defaultColours.concat(choice);
\r
698 userColourSchemes.put(schemeName.getText(), getSchemeFromButtons());
\r
700 ap.alignFrame.updateUserColourMenu();
\r
702 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
\r
704 jalview.schemabinding.version2.JalviewUserColours ucs
\r
705 = new jalview.schemabinding.version2.JalviewUserColours();
\r
707 ucs.setSchemeName(schemeName.getText());
\r
710 PrintWriter out = new PrintWriter(new OutputStreamWriter(
\r
711 new FileOutputStream(choice), "UTF-8"));
\r
713 for (int i = 0; i < buttonPanel.getComponentCount(); i++)
\r
715 JButton button = (JButton) buttonPanel.getComponent(i);
\r
716 jalview.schemabinding.version2.Colour col
\r
717 = new jalview.schemabinding.version2.Colour();
\r
718 col.setName(button.getText());
\r
719 col.setRGB(jalview.util.Format.getHexString(
\r
720 button.getBackground()));
\r
721 ucs.addColour(col);
\r
727 catch (Exception ex)
\r
729 ex.printStackTrace();
\r
737 * @param e DOCUMENT ME!
\r
739 protected void cancelButton_actionPerformed(ActionEvent e)
\r
743 if (seqGroup != null)
\r
745 seqGroup.cs = oldColourScheme;
\r
747 else if (ap != null)
\r
749 ap.av.setGlobalColourScheme(oldColourScheme);
\r
754 if(pdbcanvas!=null)
\r
756 pdbcanvas.pdb.setColours(oldColourScheme);
\r
761 frame.setClosed(true);
\r
763 catch (Exception ex)
\r
769 static Hashtable userColourSchemes;
\r
771 public static Hashtable getUserColourSchemes()
\r
773 return userColourSchemes;
\r
776 public static void initUserColourSchemes(String files)
\r
778 userColourSchemes = new Hashtable();
\r
780 if(files==null || files.length()==0)
\r
784 // In case colours can't be loaded, we'll remove them
\r
785 // from the default list here.
\r
786 StringBuffer coloursFound = new StringBuffer();
\r
787 StringTokenizer st = new StringTokenizer(files, "|");
\r
788 while (st.hasMoreElements())
\r
790 String file = st.nextToken();
\r
793 UserColourScheme ucs = loadColours(file);
\r
796 if (coloursFound.length() > 0)
\r
797 coloursFound.append("|");
\r
798 coloursFound.append(file);
\r
799 userColourSchemes.put(ucs.getName(), ucs);
\r
802 catch (Exception ex)
\r
804 System.out.println("Error loading User ColourFile\n" + ex);
\r
807 if (!files.equals(coloursFound.toString()))
\r
809 if (coloursFound.toString().length() > 1)
\r
810 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS",
\r
811 coloursFound.toString());
\r
813 jalview.bin.Cache.applicationProperties.remove("USER_DEFINED_COLOURS");
\r
817 public static void removeColourFromDefaults(String target)
\r
819 // The only way to find colours by name is to load them in
\r
820 // In case colours can't be loaded, we'll remove them
\r
821 // from the default list here.
\r
823 userColourSchemes = new Hashtable();
\r
825 StringBuffer coloursFound = new StringBuffer();
\r
826 StringTokenizer st = new StringTokenizer(
\r
827 jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS"), "|");
\r
829 while (st.hasMoreElements())
\r
831 String file = st.nextToken();
\r
834 UserColourScheme ucs = loadColours(file);
\r
835 if (ucs != null && !ucs.getName().equals(target))
\r
837 if (coloursFound.length() > 0)
\r
838 coloursFound.append("|");
\r
839 coloursFound.append(file);
\r
840 userColourSchemes.put(ucs.getName(), ucs);
\r
843 catch (Exception ex)
\r
845 System.out.println("Error loading User ColourFile\n" + ex);
\r
849 if (coloursFound.toString().length() > 1)
\r
850 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", coloursFound.toString());
\r
852 jalview.bin.Cache.applicationProperties.remove("USER_DEFINED_COLOURS");
\r
856 public void caseSensitive_actionPerformed(ActionEvent e)
\r
858 resetButtonPanel(caseSensitive.isSelected());
\r
859 lcaseColour.setEnabled(caseSensitive.isSelected());
\r
862 public void lcaseColour_actionPerformed(ActionEvent e)
\r
864 if(selectedButtons==null)
\r
865 selectedButtons = new Vector();
\r
867 selectedButtons.clear();
\r
868 selectedButtons.add(lcaseColour);
\r