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
44 * @version $Revision$
\r
46 public class UserDefinedColours extends GUserDefinedColours
\r
47 implements ChangeListener
\r
50 SequenceGroup seqGroup;
\r
51 Vector selectedButtons;
\r
52 Vector oldColours = new Vector();
\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", 450, 530, false);
\r
69 if (System.getProperty("os.name").startsWith("Mac"))
\r
71 frame.setSize(450, 560);
\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 for (int i = 0; i < 20; i++)
\r
95 makeButton(ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i]) +
\r
96 "", ResidueProperties.aa[i]);
\r
99 makeButton("B", "B");
\r
100 makeButton("Z", "Z");
\r
101 makeButton("X", "X");
\r
102 makeButton("Gap", "'.','-',' '");
\r
104 if (jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR") != null)
\r
106 loadColours(jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR"));
\r
113 * @param evt DOCUMENT ME!
\r
115 public void stateChanged(ChangeEvent evt)
\r
117 if (selectedButtons != null)
\r
120 for(int i=0; i<selectedButtons.size(); i++)
\r
122 button = (JButton)selectedButtons.elementAt(i);
\r
123 button.setBackground(colorChooser.getColor());
\r
124 button.setForeground( button.getBackground().brighter().brighter().brighter());
\r
132 * @param e DOCUMENT ME!
\r
134 public void colourButtonPressed(MouseEvent e)
\r
136 if(selectedButtons == null)
\r
137 selectedButtons = new Vector();
\r
139 JButton pressed = (JButton) e.getSource();
\r
141 if(e.isShiftDown())
\r
143 JButton start = (JButton)selectedButtons.elementAt(selectedButtons.size()-1);
\r
144 JButton end = (JButton) e.getSource();
\r
145 int startIndex=0, endIndex=0;
\r
146 for(int b=0; b<buttonPanel.getComponentCount(); b++)
\r
148 if(buttonPanel.getComponent(b)==start)
\r
150 if(buttonPanel.getComponent(b)==end)
\r
154 if(startIndex > endIndex)
\r
156 int temp = startIndex;
\r
157 startIndex = endIndex;
\r
161 for(int b=startIndex; b<=endIndex; b++)
\r
163 JButton button = (JButton)buttonPanel.getComponent(b);
\r
164 if(!selectedButtons.contains(button))
\r
166 button.setForeground(button.getBackground().brighter().brighter());
\r
167 selectedButtons.add(button);
\r
171 else if(!e.isControlDown())
\r
173 for(int b=0; b<selectedButtons.size(); b++)
\r
175 JButton button = (JButton)selectedButtons.elementAt(b);
\r
176 button.setForeground(button.getBackground().darker().darker());
\r
178 selectedButtons.clear();
\r
179 pressed.setForeground( pressed.getBackground().brighter().brighter());
\r
180 selectedButtons.addElement(pressed);
\r
183 else if(e.isControlDown())
\r
185 if(selectedButtons.contains(pressed))
\r
187 pressed.setForeground(pressed.getBackground().darker().darker());
\r
188 selectedButtons.remove(pressed);
\r
192 pressed.setForeground( pressed.getBackground().brighter().brighter());
\r
193 selectedButtons.addElement(pressed);
\r
197 if(selectedButtons.size()>0)
\r
198 colorChooser.setColor( ((JButton)selectedButtons.elementAt(0)).getBackground());
\r
204 * @param label DOCUMENT ME!
\r
205 * @param aa DOCUMENT ME!
\r
207 void makeButton(String label, String aa)
\r
209 final JButton button = new JButton();
\r
210 Color col = Color.white;
\r
214 col = oldColourScheme.findColour(aa, -1);
\r
216 catch (Exception ex)
\r
220 button.setBackground(col);
\r
221 oldColours.addElement(col);
\r
222 button.setText(label);
\r
223 button.setForeground(col.darker().darker().darker());
\r
224 button.setFont(new java.awt.Font("Verdana", 1, 10));
\r
225 button.addMouseListener(new java.awt.event.MouseAdapter()
\r
227 public void mouseClicked(MouseEvent e)
\r
229 colourButtonPressed(e);
\r
233 buttonPanel.add(button, null);
\r
239 * @param e DOCUMENT ME!
\r
241 protected void okButton_actionPerformed(ActionEvent e)
\r
243 applyButton_actionPerformed(null);
\r
247 frame.setClosed(true);
\r
249 catch (Exception ex)
\r
257 * @param e DOCUMENT ME!
\r
259 protected void applyButton_actionPerformed(ActionEvent e)
\r
261 Color[] newColours = new Color[24];
\r
263 for (int i = 0; i < 24; i++)
\r
265 JButton button = (JButton) buttonPanel.getComponent(i);
\r
266 newColours[i] = button.getBackground();
\r
269 UserColourScheme ucs = new UserColourScheme(newColours);
\r
270 ucs.setThreshold(0);
\r
272 if (seqGroup != null)
\r
279 ap.alignFrame.changeColour(ucs);
\r
286 * @param e DOCUMENT ME!
\r
288 protected void loadbutton_actionPerformed(ActionEvent e)
\r
290 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
\r
291 "LAST_DIRECTORY"), new String[] { "jc" },
\r
292 new String[] { "Jalview User Colours" }, "Jalview User Colours");
\r
293 chooser.setFileView(new jalview.io.JalviewFileView());
\r
294 chooser.setDialogTitle("Load colour scheme");
\r
295 chooser.setToolTipText("Load");
\r
297 int value = chooser.showOpenDialog(this);
\r
299 if (value == JalviewFileChooser.APPROVE_OPTION)
\r
301 File choice = chooser.getSelectedFile();
\r
302 jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
\r
303 jalview.bin.Cache.setProperty("USER_DEFINED_COLOUR",
\r
306 Color[] colors = loadColours(choice.getAbsolutePath());
\r
308 for (int i = 0; i < colors.length; i++)
\r
310 JButton button = (JButton) buttonPanel.getComponent(i);
\r
311 button.setBackground(colors[i]);
\r
319 * @return DOCUMENT ME!
\r
321 public static UserColourScheme loadDefaultColours()
\r
323 if (jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR") != null)
\r
325 return loadDefaultColours(jalview.bin.Cache.getProperty(
\r
326 "USER_DEFINED_COLOUR"));
\r
337 * @param file DOCUMENT ME!
\r
339 * @return DOCUMENT ME!
\r
341 public static UserColourScheme loadDefaultColours(String file)
\r
343 UserColourScheme ucs = null;
\r
344 Color[] cols = loadColours(file);
\r
348 ucs = new UserColourScheme(cols);
\r
349 ucs.setThreshold(0);
\r
355 static Color[] loadColours(String file)
\r
357 Color[] newColours = null;
\r
361 InputStreamReader in = new InputStreamReader(new FileInputStream(
\r
364 jalview.binding.JalviewUserColours ucs = new jalview.binding.JalviewUserColours();
\r
365 ucs = (jalview.binding.JalviewUserColours) ucs.unmarshal(in);
\r
367 newColours = new Color[ucs.getColourCount()];
\r
369 for (int i = 0; i < 24; i++)
\r
371 newColours[i] = new Color(Integer.parseInt(
\r
372 ucs.getColour(i).getRGB(), 16));
\r
375 catch (Exception ex)
\r
377 System.out.println("Error loading UserColourFile " + file);
\r
386 * @param e DOCUMENT ME!
\r
388 protected void savebutton_actionPerformed(ActionEvent e)
\r
390 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
\r
391 "LAST_DIRECTORY"), new String[] { "jc" },
\r
392 new String[] { "Jalview User Colours" }, "Jalview User Colours");
\r
394 chooser.setFileView(new jalview.io.JalviewFileView());
\r
395 chooser.setDialogTitle("Save colour scheme");
\r
396 chooser.setToolTipText("Save");
\r
398 int value = chooser.showSaveDialog(this);
\r
400 if (value == JalviewFileChooser.APPROVE_OPTION)
\r
402 String choice = chooser.getSelectedFile().getPath();
\r
403 jalview.bin.Cache.setProperty("USER_DEFINED_COLOUR", choice);
\r
405 jalview.binding.JalviewUserColours ucs = new jalview.binding.JalviewUserColours();
\r
409 PrintWriter out = new PrintWriter(new OutputStreamWriter(
\r
410 new FileOutputStream(choice), "UTF-8"));
\r
412 for (int i = 0; i < 24; i++)
\r
414 JButton button = (JButton) buttonPanel.getComponent(i);
\r
415 jalview.binding.Colour col = new jalview.binding.Colour();
\r
416 col.setName(button.getText());
\r
417 col.setRGB(jalview.util.Format.getHexString(
\r
418 button.getBackground()));
\r
419 ucs.addColour(col);
\r
425 catch (Exception ex)
\r
427 ex.printStackTrace();
\r
435 * @param e DOCUMENT ME!
\r
437 protected void cancelButton_actionPerformed(ActionEvent e)
\r
439 Color[] newColours = new Color[24];
\r
441 for (int i = 0; i < 24; i++)
\r
443 newColours[i] = (Color) oldColours.elementAt(i);
\r
444 buttonPanel.getComponent(i).setBackground(newColours[i]);
\r
447 UserColourScheme ucs = new UserColourScheme(newColours);
\r
449 if (seqGroup != null)
\r
455 ap.av.setGlobalColourScheme(ucs);
\r
462 frame.setClosed(true);
\r
464 catch (Exception ex)
\r