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
57 * Creates a new UserDefinedColours object.
\r
59 * @param ap DOCUMENT ME!
\r
60 * @param sg DOCUMENT ME!
\r
62 public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)
\r
65 frame = new JInternalFrame();
\r
66 frame.setContentPane(this);
\r
67 Desktop.addInternalFrame(frame, "User Defined Colours", 720, 370, true);
\r
69 if (System.getProperty("os.name").startsWith("Mac"))
\r
71 frame.setSize(760, 370);
\r
76 frame.setTitle(frame.getTitle() + " (" + sg.getName() + ")");
\r
79 colorChooser.getSelectionModel().addChangeListener(this);
\r
84 if (seqGroup != null)
\r
86 oldColourScheme = seqGroup.cs;
\r
90 oldColourScheme = ap.av.getGlobalColourScheme();
\r
93 if (oldColourScheme instanceof UserColourScheme)
\r
95 schemeName.setText( ( (UserColourScheme) oldColourScheme).getName());
\r
97 for (int i = 0; i < 20; i++)
\r
99 makeButton(ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i]) +
\r
100 "", ResidueProperties.aa[i]);
\r
103 makeButton("B", "B");
\r
104 makeButton("Z", "Z");
\r
105 makeButton("X", "X");
\r
106 makeButton("Gap", "-");
\r
112 * @param evt DOCUMENT ME!
\r
114 public void stateChanged(ChangeEvent evt)
\r
116 if (selectedButtons != null)
\r
119 for(int i=0; i<selectedButtons.size(); i++)
\r
121 button = (JButton)selectedButtons.elementAt(i);
\r
122 button.setBackground(colorChooser.getColor());
\r
123 button.setForeground( button.getBackground().brighter().brighter().brighter());
\r
131 * @param e DOCUMENT ME!
\r
133 public void colourButtonPressed(MouseEvent e)
\r
135 if(selectedButtons == null)
\r
136 selectedButtons = new Vector();
\r
138 JButton pressed = (JButton) e.getSource();
\r
140 if(e.isShiftDown())
\r
142 JButton start = (JButton)selectedButtons.elementAt(selectedButtons.size()-1);
\r
143 JButton end = (JButton) e.getSource();
\r
144 int startIndex=0, endIndex=0;
\r
145 for(int b=0; b<buttonPanel.getComponentCount(); b++)
\r
147 if(buttonPanel.getComponent(b)==start)
\r
149 if(buttonPanel.getComponent(b)==end)
\r
153 if(startIndex > endIndex)
\r
155 int temp = startIndex;
\r
156 startIndex = endIndex;
\r
160 for(int b=startIndex; b<=endIndex; b++)
\r
162 JButton button = (JButton)buttonPanel.getComponent(b);
\r
163 if(!selectedButtons.contains(button))
\r
165 button.setForeground(button.getBackground().brighter().brighter());
\r
166 selectedButtons.add(button);
\r
170 else if(!e.isControlDown())
\r
172 for(int b=0; b<selectedButtons.size(); b++)
\r
174 JButton button = (JButton)selectedButtons.elementAt(b);
\r
175 button.setForeground(button.getBackground().darker().darker());
\r
177 selectedButtons.clear();
\r
178 pressed.setForeground( pressed.getBackground().brighter().brighter());
\r
179 selectedButtons.addElement(pressed);
\r
182 else if(e.isControlDown())
\r
184 if(selectedButtons.contains(pressed))
\r
186 pressed.setForeground(pressed.getBackground().darker().darker());
\r
187 selectedButtons.remove(pressed);
\r
191 pressed.setForeground( pressed.getBackground().brighter().brighter());
\r
192 selectedButtons.addElement(pressed);
\r
196 if(selectedButtons.size()>0)
\r
197 colorChooser.setColor( ((JButton)selectedButtons.elementAt(0)).getBackground());
\r
203 * @param label DOCUMENT ME!
\r
204 * @param aa DOCUMENT ME!
\r
206 void makeButton(String label, String aa)
\r
208 final JButton button = new JButton();
\r
209 Color col = Color.white;
\r
213 col = oldColourScheme.findColour(aa, -1);
\r
215 catch (Exception ex)
\r
217 // ex.printStackTrace();
\r
220 button.setBackground(col);
\r
221 button.setText(label);
\r
222 button.setForeground(col.darker().darker().darker());
\r
223 button.setFont(new java.awt.Font("Verdana", 1, 10));
\r
224 button.addMouseListener(new java.awt.event.MouseAdapter()
\r
226 public void mouseClicked(MouseEvent e)
\r
228 colourButtonPressed(e);
\r
232 buttonPanel.add(button, null);
\r
238 * @param e DOCUMENT ME!
\r
240 protected void okButton_actionPerformed(ActionEvent e)
\r
242 applyButton_actionPerformed(null);
\r
246 frame.setClosed(true);
\r
248 catch (Exception ex)
\r
256 * @param e DOCUMENT ME!
\r
258 protected void applyButton_actionPerformed(ActionEvent e)
\r
260 UserColourScheme ucs = getSchemeFromGUI();
\r
261 ucs.setName(schemeName.getText());
\r
263 if (seqGroup != null)
\r
270 ap.alignFrame.changeColour(ucs);
\r
274 UserColourScheme getSchemeFromGUI()
\r
276 Color[] newColours = new Color[24];
\r
278 for (int i = 0; i < 24; i++)
\r
280 JButton button = (JButton) buttonPanel.getComponent(i);
\r
281 newColours[i] = button.getBackground();
\r
284 UserColourScheme ucs = new UserColourScheme(newColours);
\r
285 ucs.setThreshold(0, ap.av.getIgnoreGapsConsensus());
\r
295 * @param e DOCUMENT ME!
\r
297 protected void loadbutton_actionPerformed(ActionEvent e)
\r
299 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
\r
300 "LAST_DIRECTORY"), new String[] { "jc" },
\r
301 new String[] { "Jalview User Colours" }, "Jalview User Colours");
\r
302 chooser.setFileView(new jalview.io.JalviewFileView());
\r
303 chooser.setDialogTitle("Load colour scheme");
\r
304 chooser.setToolTipText("Load");
\r
306 int value = chooser.showOpenDialog(this);
\r
308 if (value == JalviewFileChooser.APPROVE_OPTION)
\r
310 File choice = chooser.getSelectedFile();
\r
311 jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
\r
312 String defaultColours = jalview.bin.Cache.getDefault("USER_DEFINED_COLOURS",
\r
314 if (defaultColours.indexOf(choice.getPath()) == -1)
\r
315 defaultColours = defaultColours.concat("|").concat(choice.getPath());
\r
317 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
\r
320 UserColourScheme ucs = loadColours(choice.getAbsolutePath());
\r
321 Color[] colors = ucs.getColours();
\r
322 schemeName.setText(ucs.getName());
\r
324 for (int i = 0; i < colors.length; i++)
\r
326 JButton button = (JButton) buttonPanel.getComponent(i);
\r
327 button.setBackground(colors[i]);
\r
335 * @return DOCUMENT ME!
\r
337 public static UserColourScheme loadDefaultColours()
\r
339 UserColourScheme ret = null;
\r
341 String colours = jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS");
\r
342 if ( colours != null )
\r
344 if(colours.indexOf("|")>-1)
\r
345 colours = colours.substring(0, colours.indexOf("|"));
\r
347 ret = loadColours(colours);
\r
352 Color[] newColours = new Color[24];
\r
353 for (int i = 0; i < 24; i++)
\r
355 newColours[i] = Color.white;
\r
357 ret = new UserColourScheme(newColours);
\r
366 * @param file DOCUMENT ME!
\r
368 * @return DOCUMENT ME!
\r
370 static UserColourScheme loadColours(String file)
\r
372 UserColourScheme ucs = null;
\r
373 Color[] newColours = null;
\r
377 InputStreamReader in = new InputStreamReader(new FileInputStream(
\r
380 jalview.binding.JalviewUserColours jucs = new jalview.binding.JalviewUserColours();
\r
381 jucs = (jalview.binding.JalviewUserColours) jucs.unmarshal(in);
\r
383 newColours = new Color[jucs.getColourCount()];
\r
385 for (int i = 0; i < 24; i++)
\r
387 newColours[i] = new Color(Integer.parseInt(
\r
388 jucs.getColour(i).getRGB(), 16));
\r
390 if (newColours != null)
\r
392 ucs = new UserColourScheme(newColours);
\r
393 ucs.setName( jucs.getSchemeName() );
\r
397 catch (Exception ex)
\r
399 System.out.println("Error loading User ColourFile\n"+ex);
\r
409 * @param e DOCUMENT ME!
\r
411 protected void savebutton_actionPerformed(ActionEvent e)
\r
413 if(schemeName.getText().trim().length()<1)
\r
415 JOptionPane.showInternalMessageDialog(Desktop.desktop,
\r
416 "User colour scheme must have a name!",
\r
417 "No name for colour scheme",
\r
418 JOptionPane.WARNING_MESSAGE);
\r
422 if(userColourSchemes!=null && userColourSchemes.containsKey(schemeName.getText()) )
\r
424 int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop, "Colour scheme "+schemeName.getText()+ " exists."
\r
425 +"\nContinue saving colour scheme as "+schemeName.getText()+"?",
\r
426 "Duplicate scheme name", JOptionPane.YES_NO_OPTION);
\r
427 if(reply != JOptionPane.YES_OPTION)
\r
430 userColourSchemes.remove(schemeName.getText());
\r
432 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
\r
433 "LAST_DIRECTORY"), new String[] { "jc" },
\r
434 new String[] { "Jalview User Colours" }, "Jalview User Colours");
\r
436 chooser.setFileView(new jalview.io.JalviewFileView());
\r
437 chooser.setDialogTitle("Save colour scheme");
\r
438 chooser.setToolTipText("Save");
\r
440 int value = chooser.showSaveDialog(this);
\r
442 if (value == JalviewFileChooser.APPROVE_OPTION)
\r
444 String choice = chooser.getSelectedFile().getPath();
\r
445 String defaultColours = jalview.bin.Cache.getDefault("USER_DEFINED_COLOURS", choice);
\r
446 if(defaultColours.indexOf(choice)==-1)
\r
448 if(defaultColours.length()>0)
\r
449 defaultColours = defaultColours.concat("|");
\r
450 defaultColours = defaultColours.concat(choice);
\r
453 userColourSchemes.put(schemeName.getText(), getSchemeFromGUI());
\r
455 ap.alignFrame.updateUserColourMenu();
\r
457 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours);
\r
459 jalview.binding.JalviewUserColours ucs = new jalview.binding.JalviewUserColours();
\r
460 ucs.setSchemeName(schemeName.getText());
\r
463 PrintWriter out = new PrintWriter(new OutputStreamWriter(
\r
464 new FileOutputStream(choice), "UTF-8"));
\r
466 for (int i = 0; i < 24; i++)
\r
468 JButton button = (JButton) buttonPanel.getComponent(i);
\r
469 jalview.binding.Colour col = new jalview.binding.Colour();
\r
470 col.setName(button.getText());
\r
471 col.setRGB(jalview.util.Format.getHexString(
\r
472 button.getBackground()));
\r
473 ucs.addColour(col);
\r
479 catch (Exception ex)
\r
481 ex.printStackTrace();
\r
489 * @param e DOCUMENT ME!
\r
491 protected void cancelButton_actionPerformed(ActionEvent e)
\r
493 if (seqGroup != null)
\r
495 seqGroup.cs = oldColourScheme;
\r
499 ap.av.setGlobalColourScheme(oldColourScheme);
\r
506 frame.setClosed(true);
\r
508 catch (Exception ex)
\r
514 static Hashtable userColourSchemes;
\r
516 public static Hashtable getUserColourSchemes()
\r
518 return userColourSchemes;
\r
521 public static void initUserColourSchemes(String files)
\r
523 userColourSchemes = new Hashtable();
\r
525 if(files==null || files.length()==0)
\r
529 // In case colours can't be loaded, we'll remove them
\r
530 // from the default list here.
\r
531 StringBuffer coloursFound = new StringBuffer();
\r
532 StringTokenizer st = new StringTokenizer(files, "|");
\r
533 while (st.hasMoreElements())
\r
535 String file = st.nextToken();
\r
538 UserColourScheme ucs = loadColours(file);
\r
541 if (coloursFound.length() > 0)
\r
542 coloursFound.append("|");
\r
543 coloursFound.append(file);
\r
544 userColourSchemes.put(ucs.getName(), ucs);
\r
547 catch (Exception ex)
\r
549 System.out.println("Error loading User ColourFile\n" + ex);
\r
552 if (!files.equals(coloursFound.toString()))
\r
554 if (coloursFound.toString().length() > 1)
\r
555 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS",
\r
556 coloursFound.toString());
\r
558 jalview.bin.Cache.applicationProperties.remove("USER_DEFINED_COLOURS");
\r
562 public static void removeColourFromDefaults(String target)
\r
564 // The only way to find colours by name is to load them in
\r
565 // In case colours can't be loaded, we'll remove them
\r
566 // from the default list here.
\r
568 userColourSchemes = new Hashtable();
\r
570 StringBuffer coloursFound = new StringBuffer();
\r
571 StringTokenizer st = new StringTokenizer(
\r
572 jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS"), "|");
\r
574 while (st.hasMoreElements())
\r
576 String file = st.nextToken();
\r
579 UserColourScheme ucs = loadColours(file);
\r
580 if (ucs != null && !ucs.getName().equals(target))
\r
582 if (coloursFound.length() > 0)
\r
583 coloursFound.append("|");
\r
584 coloursFound.append(file);
\r
585 userColourSchemes.put(ucs.getName(), ucs);
\r
588 catch (Exception ex)
\r
590 System.out.println("Error loading User ColourFile\n" + ex);
\r
594 if (coloursFound.toString().length() > 1)
\r
595 jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", coloursFound.toString());
\r
597 jalview.bin.Cache.applicationProperties.remove("USER_DEFINED_COLOURS");
\r