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
25 import java.awt.event.*;
\r
26 import javax.swing.*;
\r
27 import javax.swing.event.*;
\r
29 import jalview.datamodel.*;
\r
30 import jalview.io.*;
\r
31 import jalview.jbgui.*;
\r
32 import jalview.schemes.*;
\r
34 public class UserDefinedColours
\r
35 extends GUserDefinedColours implements ChangeListener
\r
38 SequenceGroup seqGroup;
\r
39 JButton selectedButton;
\r
40 Vector oldColours = new Vector();
\r
41 ColourSchemeI oldColourScheme;
\r
42 JInternalFrame frame;
\r
44 public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)
\r
47 frame = new JInternalFrame();
\r
48 frame.setContentPane(this);
\r
49 Desktop.addInternalFrame(frame, "User Defined Colours", 450, 530, false);
\r
51 if (System.getProperty("os.name").startsWith("Mac"))
\r
53 frame.setSize(450, 560);
\r
58 frame.setTitle(frame.getTitle() + " (" + sg.getName() + ")");
\r
61 colorChooser.getSelectionModel().addChangeListener(this);
\r
66 if (seqGroup != null)
\r
68 oldColourScheme = seqGroup.cs;
\r
72 oldColourScheme = ap.av.getGlobalColourScheme();
\r
75 for (int i = 0; i < 20; i++)
\r
77 makeButton(ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i]) +
\r
78 "", ResidueProperties.aa[i]);
\r
81 makeButton("B", "B");
\r
82 makeButton("Z", "Z");
\r
83 makeButton("X", "X");
\r
84 makeButton("Gap", "'.','-',' '");
\r
86 if (jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR") != null)
\r
88 loadColours(jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR"));
\r
92 public void stateChanged(ChangeEvent evt)
\r
94 if (selectedButton != null)
\r
96 selectedButton.setBackground(colorChooser.getColor());
\r
100 public void colourButtonPressed(MouseEvent e)
\r
102 selectedButton = (JButton) e.getSource();
\r
103 colorChooser.setColor(selectedButton.getBackground());
\r
106 void makeButton(String label, String aa)
\r
108 final JButton button = new JButton();
\r
109 Color col = Color.white;
\r
113 col = oldColourScheme.findColour(aa, -1);
\r
115 catch (Exception ex)
\r
119 button.setBackground(col);
\r
120 oldColours.addElement(col);
\r
121 button.setText(label);
\r
122 button.setForeground(col.darker().darker().darker());
\r
123 button.setFont(new java.awt.Font("Verdana", 1, 10));
\r
124 button.addMouseListener(new java.awt.event.MouseAdapter()
\r
126 public void mouseClicked(MouseEvent e)
\r
128 colourButtonPressed(e);
\r
132 buttonPanel.add(button, null);
\r
135 protected void okButton_actionPerformed(ActionEvent e)
\r
137 applyButton_actionPerformed(null);
\r
141 frame.setClosed(true);
\r
143 catch (Exception ex)
\r
148 protected void applyButton_actionPerformed(ActionEvent e)
\r
150 Color[] newColours = new Color[24];
\r
152 for (int i = 0; i < 24; i++)
\r
154 JButton button = (JButton) buttonPanel.getComponent(i);
\r
155 newColours[i] = button.getBackground();
\r
158 UserColourScheme ucs = new UserColourScheme(newColours);
\r
159 ucs.setThreshold(0);
\r
161 if (seqGroup != null)
\r
168 ap.alignFrame.changeColour(ucs);
\r
172 protected void loadbutton_actionPerformed(ActionEvent e)
\r
174 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.
\r
176 "LAST_DIRECTORY"), new String[]
\r
179 {"Jalview User Colours"}, "Jalview User Colours");
\r
180 chooser.setFileView(new jalview.io.JalviewFileView());
\r
181 chooser.setDialogTitle("Load colour scheme");
\r
182 chooser.setToolTipText("Load");
\r
184 int value = chooser.showOpenDialog(this);
\r
186 if (value == JalviewFileChooser.APPROVE_OPTION)
\r
188 File choice = chooser.getSelectedFile();
\r
189 jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
\r
190 jalview.bin.Cache.setProperty("USER_DEFINED_COLOUR",
\r
193 Color[] colors = loadColours(choice.getAbsolutePath());
\r
195 for (int i = 0; i < colors.length; i++)
\r
197 JButton button = (JButton) buttonPanel.getComponent(i);
\r
198 button.setBackground(colors[i]);
\r
203 public static UserColourScheme loadDefaultColours()
\r
205 if (jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR") != null)
\r
207 return loadDefaultColours(jalview.bin.Cache.getProperty(
\r
208 "USER_DEFINED_COLOUR"));
\r
216 public static UserColourScheme loadDefaultColours(String file)
\r
218 UserColourScheme ucs = null;
\r
219 Color[] cols = loadColours(file);
\r
223 ucs = new UserColourScheme(cols);
\r
224 ucs.setThreshold(0);
\r
230 static Color[] loadColours(String file)
\r
232 Color[] newColours = null;
\r
236 InputStreamReader in = new InputStreamReader(new FileInputStream(
\r
239 jalview.binding.JalviewUserColours ucs = new jalview.binding.
\r
240 JalviewUserColours();
\r
241 ucs = (jalview.binding.JalviewUserColours) ucs.unmarshal(in);
\r
243 newColours = new Color[ucs.getColourCount()];
\r
245 for (int i = 0; i < 24; i++)
\r
247 newColours[i] = new Color(Integer.parseInt(
\r
248 ucs.getColour(i).getRGB(), 16));
\r
251 catch (Exception ex)
\r
253 System.out.println("Error loading UserColourFile " + file);
\r
259 protected void savebutton_actionPerformed(ActionEvent e)
\r
261 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.
\r
263 "LAST_DIRECTORY"), new String[]
\r
266 {"Jalview User Colours"}, "Jalview User Colours");
\r
268 chooser.setFileView(new jalview.io.JalviewFileView());
\r
269 chooser.setDialogTitle("Save colour scheme");
\r
270 chooser.setToolTipText("Save");
\r
272 int value = chooser.showSaveDialog(this);
\r
274 if (value == JalviewFileChooser.APPROVE_OPTION)
\r
276 String choice = chooser.getSelectedFile().getPath();
\r
277 jalview.bin.Cache.setProperty("USER_DEFINED_COLOUR", choice);
\r
279 jalview.binding.JalviewUserColours ucs = new jalview.binding.
\r
280 JalviewUserColours();
\r
284 PrintWriter out = new PrintWriter(new OutputStreamWriter(
\r
285 new FileOutputStream(choice), "UTF-8"));
\r
287 for (int i = 0; i < 24; i++)
\r
289 JButton button = (JButton) buttonPanel.getComponent(i);
\r
290 jalview.binding.Colour col = new jalview.binding.Colour();
\r
291 col.setName(button.getText());
\r
292 col.setRGB(jalview.util.Format.getHexString(
\r
293 button.getBackground()));
\r
294 ucs.addColour(col);
\r
300 catch (Exception ex)
\r
302 ex.printStackTrace();
\r
307 protected void cancelButton_actionPerformed(ActionEvent e)
\r
309 Color[] newColours = new Color[24];
\r
311 for (int i = 0; i < 24; i++)
\r
313 newColours[i] = (Color) oldColours.elementAt(i);
\r
314 buttonPanel.getComponent(i).setBackground(newColours[i]);
\r
317 UserColourScheme ucs = new UserColourScheme(newColours);
\r
319 if (seqGroup != null)
\r
325 ap.av.setGlobalColourScheme(ucs);
\r
332 frame.setClosed(true);
\r
334 catch (Exception ex)
\r