2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 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
58 * Creates a new UserDefinedColours object.
\r
60 * @param ap DOCUMENT ME!
\r
61 * @param sg DOCUMENT ME!
\r
63 public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)
\r
66 frame = new JInternalFrame();
\r
67 frame.setContentPane(this);
\r
68 Desktop.addInternalFrame(frame, "User Defined Colours", 720, 370, true);
\r
70 if (System.getProperty("os.name").startsWith("Mac"))
\r
72 frame.setSize(760, 370);
\r
77 frame.setTitle(frame.getTitle() + " (" + sg.getName() + ")");
\r
80 colorChooser.getSelectionModel().addChangeListener(this);
\r
85 if (seqGroup != null)
\r
87 oldColourScheme = seqGroup.cs;
\r
91 oldColourScheme = ap.av.getGlobalColourScheme();
\r
94 if (oldColourScheme instanceof UserColourScheme)
\r
96 schemeName.setText( ( (UserColourScheme) oldColourScheme).getName());
\r
98 for (int i = 0; i < 20; i++)
\r
100 makeButton(ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i]) +
\r
101 "", ResidueProperties.aa[i]);
\r
104 makeButton("B", "B");
\r
105 makeButton("Z", "Z");
\r
106 makeButton("X", "X");
\r
107 makeButton("Gap", "-");
\r
110 public UserDefinedColours(MCview.PDBCanvas pdb, ColourSchemeI oldcs)
\r
113 frame = new JInternalFrame();
\r
114 frame.setContentPane(this);
\r
115 Desktop.addInternalFrame(frame, "User Defined Colours", 720, 370, true);
\r
118 if (System.getProperty("os.name").startsWith("Mac"))
\r
120 frame.setSize(760, 370);
\r
123 colorChooser.getSelectionModel().addChangeListener(this);
\r
125 oldColourScheme = oldcs;
\r
127 if (oldColourScheme instanceof UserColourScheme)
\r
129 schemeName.setText( ( (UserColourScheme) oldColourScheme).getName());
\r
131 for (int i = 0; i < 20; i++)
\r
133 makeButton(ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i]) +
\r
134 "", ResidueProperties.aa[i]);
\r
137 makeButton("B", "B");
\r
138 makeButton("Z", "Z");
\r
139 makeButton("X", "X");
\r
140 makeButton("Gap", "-");
\r
147 * @param evt DOCUMENT ME!
\r
149 public void stateChanged(ChangeEvent evt)
\r
151 if (selectedButtons != null)
\r
154 for(int i=0; i<selectedButtons.size(); i++)
\r
156 button = (JButton)selectedButtons.elementAt(i);
\r
157 button.setBackground(colorChooser.getColor());
\r
158 button.setForeground( button.getBackground().brighter().brighter().brighter());
\r
166 * @param e DOCUMENT ME!
\r
168 public void colourButtonPressed(MouseEvent e)
\r
170 if(selectedButtons == null)
\r
171 selectedButtons = new Vector();
\r
173 JButton pressed = (JButton) e.getSource();
\r
175 if(e.isShiftDown())
\r
177 JButton start, end = (JButton) e.getSource();
\r
178 if(selectedButtons.size()>0)
\r
179 start = (JButton)selectedButtons.elementAt(selectedButtons.size()-1);
\r
181 start = (JButton) e.getSource();
\r
183 int startIndex=0, endIndex=0;
\r
184 for(int b=0; b<buttonPanel.getComponentCount(); b++)
\r
186 if(buttonPanel.getComponent(b)==start)
\r
188 if(buttonPanel.getComponent(b)==end)
\r
192 if(startIndex > endIndex)
\r
194 int temp = startIndex;
\r
195 startIndex = endIndex;
\r
199 for(int b=startIndex; b<=endIndex; b++)
\r
201 JButton button = (JButton)buttonPanel.getComponent(b);
\r
202 if(!selectedButtons.contains(button))
\r
204 button.setForeground(button.getBackground().brighter().brighter());
\r
205 selectedButtons.add(button);
\r
209 else if(!e.isControlDown())
\r
211 for(int b=0; b<selectedButtons.size(); b++)
\r
213 JButton button = (JButton)selectedButtons.elementAt(b);
\r
214 button.setForeground(button.getBackground().darker().darker());
\r
216 selectedButtons.clear();
\r
217 pressed.setForeground( pressed.getBackground().brighter().brighter());
\r
218 selectedButtons.addElement(pressed);
\r
221 else if(e.isControlDown())
\r
223 if(selectedButtons.contains(pressed))
\r
225 pressed.setForeground(pressed.getBackground().darker().darker());
\r
226 selectedButtons.remove(pressed);
\r
230 pressed.setForeground( pressed.getBackground().brighter().brighter());
\r
231 selectedButtons.addElement(pressed);
\r
235 if(selectedButtons.size()>0)
\r
236 colorChooser.setColor( ((JButton)selectedButtons.elementAt(0)).getBackground());
\r
242 * @param label DOCUMENT ME!
\r
243 * @param aa DOCUMENT ME!
\r
245 void makeButton(String label, String aa)
\r
247 final JButton button = new JButton();
\r
248 Color col = Color.white;
\r
252 col = oldColourScheme.findColour(aa, -1);
\r
254 catch (Exception ex)
\r
256 // ex.printStackTrace();
\r
259 button.setBackground(col);
\r
260 button.setText(label);
\r
261 button.setForeground(col.darker().darker().darker());
\r
262 button.setFont(new java.awt.Font("Verdana", 1, 10));
\r
263 button.addMouseListener(new java.awt.event.MouseAdapter()
\r
265 public void mouseClicked(MouseEvent e)
\r
267 colourButtonPressed(e);
\r
271 buttonPanel.add(button, null);
\r
277 * @param e DOCUMENT ME!
\r
279 protected void okButton_actionPerformed(ActionEvent e)
\r
281 applyButton_actionPerformed(null);
\r
285 frame.setClosed(true);
\r
287 catch (Exception ex)
\r
295 * @param e DOCUMENT ME!
\r
297 protected void applyButton_actionPerformed(ActionEvent e)
\r
299 UserColourScheme ucs = getSchemeFromGUI();
\r
300 ucs.setName(schemeName.getText());
\r
302 if (seqGroup != null)
\r
309 ap.alignFrame.changeColour(ucs);
\r
311 else if(pdbcanvas!=null)
\r
313 pdbcanvas.pdb.setColours(ucs);
\r
314 pdbcanvas.updateSeqColours();
\r
318 UserColourScheme getSchemeFromGUI()
\r
320 Color[] newColours = new Color[24];
\r
322 for (int i = 0; i < 24; i++)
\r
324 JButton button = (JButton) buttonPanel.getComponent(i);
\r
325 newColours[i] = button.getBackground();
\r
328 UserColourScheme ucs = new UserColourScheme(newColours);
\r
330 ucs.setThreshold(0, ap.av.getIgnoreGapsConsensus());
\r
341 * @param e DOCUMENT ME!
\r
343 protected void loadbutton_actionPerformed(ActionEvent e)
\r
345 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
\r
346 "LAST_DIRECTORY"), new String[] { "jc" },
\r
347 new String[] { "Jalview User Colours" }, "Jalview User Colours");
\r
348 chooser.setFileView(new jalview.io.JalviewFileView());
\r
349 chooser.setDialogTitle("Load colour scheme");
\r
350 chooser.setToolTipText("Load");
\r
352 int value = chooser.showOpenDialog(this);
\r
354 if (value == JalviewFileChooser.APPROVE_OPTION)
\r
356 File choice = chooser.getSelectedFile();
\r
357 jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
\r
358 String defaultColours = jalview.bin.Cache.getDefault("USER_DEFINED_COLOURS",
\r
360 if (defaultColours.indexOf(choice.getPath()) == -1)
\r
361 defaultColours = defaultColours.concat("|").concat(choice.getPath());
\r
363 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
\r
366 UserColourScheme ucs = loadColours(choice.getAbsolutePath());
\r
367 Color[] colors = ucs.getColours();
\r
368 schemeName.setText(ucs.getName());
\r
370 for (int i = 0; i < colors.length; i++)
\r
372 JButton button = (JButton) buttonPanel.getComponent(i);
\r
373 button.setBackground(colors[i]);
\r
381 * @return DOCUMENT ME!
\r
383 public static UserColourScheme loadDefaultColours()
\r
385 UserColourScheme ret = null;
\r
387 String colours = jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS");
\r
388 if ( colours != null )
\r
390 if(colours.indexOf("|")>-1)
\r
391 colours = colours.substring(0, colours.indexOf("|"));
\r
393 ret = loadColours(colours);
\r
398 Color[] newColours = new Color[24];
\r
399 for (int i = 0; i < 24; i++)
\r
401 newColours[i] = Color.white;
\r
403 ret = new UserColourScheme(newColours);
\r
412 * @param file DOCUMENT ME!
\r
414 * @return DOCUMENT ME!
\r
416 static UserColourScheme loadColours(String file)
\r
418 UserColourScheme ucs = null;
\r
419 Color[] newColours = null;
\r
423 InputStreamReader in = new InputStreamReader(new FileInputStream(
\r
426 jalview.schemabinding.version2.JalviewUserColours jucs
\r
427 = new jalview.schemabinding.version2.JalviewUserColours();
\r
429 org.exolab.castor.xml.Unmarshaller unmar
\r
430 = new org.exolab.castor.xml.Unmarshaller(jucs);
\r
431 jucs = (jalview.schemabinding.version2.JalviewUserColours) unmar.unmarshal( in );
\r
433 newColours = new Color[jucs.getColourCount()];
\r
435 for (int i = 0; i < 24; i++)
\r
437 newColours[i] = new Color(Integer.parseInt(
\r
438 jucs.getColour(i).getRGB(), 16));
\r
440 if (newColours != null)
\r
442 ucs = new UserColourScheme(newColours);
\r
443 ucs.setName( jucs.getSchemeName() );
\r
447 catch (Exception ex)
\r
449 //Could be Archive Jalview format
\r
451 InputStreamReader in = new InputStreamReader(new FileInputStream(
\r
454 jalview.binding.JalviewUserColours jucs
\r
455 = new jalview.binding.JalviewUserColours();
\r
457 jucs = (jalview.binding.JalviewUserColours) jucs.unmarshal(in);
\r
459 newColours = new Color[jucs.getColourCount()];
\r
461 for (int i = 0; i < 24; i++)
\r
463 newColours[i] = new Color(Integer.parseInt(
\r
464 jucs.getColour(i).getRGB(), 16));
\r
466 if (newColours != null)
\r
468 ucs = new UserColourScheme(newColours);
\r
469 ucs.setName(jucs.getSchemeName());
\r
471 }catch(Exception ex2)
\r
472 { ex2.printStackTrace(); }
\r
474 if(newColours==null)
\r
475 System.out.println("Error loading User ColourFile\n"+ex);
\r
484 * @param e DOCUMENT ME!
\r
486 protected void savebutton_actionPerformed(ActionEvent e)
\r
488 if(schemeName.getText().trim().length()<1)
\r
490 JOptionPane.showInternalMessageDialog(Desktop.desktop,
\r
491 "User colour scheme must have a name!",
\r
492 "No name for colour scheme",
\r
493 JOptionPane.WARNING_MESSAGE);
\r
497 if(userColourSchemes!=null && userColourSchemes.containsKey(schemeName.getText()) )
\r
499 int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop, "Colour scheme "+schemeName.getText()+ " exists."
\r
500 +"\nContinue saving colour scheme as "+schemeName.getText()+"?",
\r
501 "Duplicate scheme name", JOptionPane.YES_NO_OPTION);
\r
502 if(reply != JOptionPane.YES_OPTION)
\r
505 userColourSchemes.remove(schemeName.getText());
\r
507 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
\r
508 "LAST_DIRECTORY"), new String[] { "jc" },
\r
509 new String[] { "Jalview User Colours" }, "Jalview User Colours");
\r
511 chooser.setFileView(new jalview.io.JalviewFileView());
\r
512 chooser.setDialogTitle("Save colour scheme");
\r
513 chooser.setToolTipText("Save");
\r
515 int value = chooser.showSaveDialog(this);
\r
517 if (value == JalviewFileChooser.APPROVE_OPTION)
\r
519 String choice = chooser.getSelectedFile().getPath();
\r
520 String defaultColours = jalview.bin.Cache.getDefault("USER_DEFINED_COLOURS", choice);
\r
521 if(defaultColours.indexOf(choice)==-1)
\r
523 if(defaultColours.length()>0)
\r
524 defaultColours = defaultColours.concat("|");
\r
525 defaultColours = defaultColours.concat(choice);
\r
528 userColourSchemes.put(schemeName.getText(), getSchemeFromGUI());
\r
530 ap.alignFrame.updateUserColourMenu();
\r
532 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
\r
534 jalview.binding.JalviewUserColours ucs = new jalview.binding.JalviewUserColours();
\r
535 ucs.setSchemeName(schemeName.getText());
\r
538 PrintWriter out = new PrintWriter(new OutputStreamWriter(
\r
539 new FileOutputStream(choice), "UTF-8"));
\r
541 for (int i = 0; i < 24; i++)
\r
543 JButton button = (JButton) buttonPanel.getComponent(i);
\r
544 jalview.binding.Colour col = new jalview.binding.Colour();
\r
545 col.setName(button.getText());
\r
546 col.setRGB(jalview.util.Format.getHexString(
\r
547 button.getBackground()));
\r
548 ucs.addColour(col);
\r
554 catch (Exception ex)
\r
556 ex.printStackTrace();
\r
564 * @param e DOCUMENT ME!
\r
566 protected void cancelButton_actionPerformed(ActionEvent e)
\r
570 if (seqGroup != null)
\r
572 seqGroup.cs = oldColourScheme;
\r
574 else if (ap != null)
\r
576 ap.av.setGlobalColourScheme(oldColourScheme);
\r
581 if(pdbcanvas!=null)
\r
583 pdbcanvas.pdb.setColours(oldColourScheme);
\r
588 frame.setClosed(true);
\r
590 catch (Exception ex)
\r
596 static Hashtable userColourSchemes;
\r
598 public static Hashtable getUserColourSchemes()
\r
600 return userColourSchemes;
\r
603 public static void initUserColourSchemes(String files)
\r
605 userColourSchemes = new Hashtable();
\r
607 if(files==null || files.length()==0)
\r
611 // In case colours can't be loaded, we'll remove them
\r
612 // from the default list here.
\r
613 StringBuffer coloursFound = new StringBuffer();
\r
614 StringTokenizer st = new StringTokenizer(files, "|");
\r
615 while (st.hasMoreElements())
\r
617 String file = st.nextToken();
\r
620 UserColourScheme ucs = loadColours(file);
\r
623 if (coloursFound.length() > 0)
\r
624 coloursFound.append("|");
\r
625 coloursFound.append(file);
\r
626 userColourSchemes.put(ucs.getName(), ucs);
\r
629 catch (Exception ex)
\r
631 System.out.println("Error loading User ColourFile\n" + ex);
\r
634 if (!files.equals(coloursFound.toString()))
\r
636 if (coloursFound.toString().length() > 1)
\r
637 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS",
\r
638 coloursFound.toString());
\r
640 jalview.bin.Cache.applicationProperties.remove("USER_DEFINED_COLOURS");
\r
644 public static void removeColourFromDefaults(String target)
\r
646 // The only way to find colours by name is to load them in
\r
647 // In case colours can't be loaded, we'll remove them
\r
648 // from the default list here.
\r
650 userColourSchemes = new Hashtable();
\r
652 StringBuffer coloursFound = new StringBuffer();
\r
653 StringTokenizer st = new StringTokenizer(
\r
654 jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS"), "|");
\r
656 while (st.hasMoreElements())
\r
658 String file = st.nextToken();
\r
661 UserColourScheme ucs = loadColours(file);
\r
662 if (ucs != null && !ucs.getName().equals(target))
\r
664 if (coloursFound.length() > 0)
\r
665 coloursFound.append("|");
\r
666 coloursFound.append(file);
\r
667 userColourSchemes.put(ucs.getName(), ucs);
\r
670 catch (Exception ex)
\r
672 System.out.println("Error loading User ColourFile\n" + ex);
\r
676 if (coloursFound.toString().length() > 1)
\r
677 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", coloursFound.toString());
\r
679 jalview.bin.Cache.applicationProperties.remove("USER_DEFINED_COLOURS");
\r