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
20 package jalview.gui;
\r
22 import jalview.jbgui.GUserDefinedColours;
\r
23 import jalview.datamodel.*;
\r
24 import jalview.io.*;
\r
25 import javax.swing.*;
\r
27 import java.awt.event.*;
\r
28 import javax.swing.event.*;
\r
29 import jalview.schemes.*;
\r
34 public class UserDefinedColours 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
45 public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)
\r
48 frame = new JInternalFrame();
\r
49 frame.setContentPane(this);
\r
50 Desktop.addInternalFrame(frame, "User Defined Colours", 450,530, false );
\r
51 if(System.getProperty("os.name").startsWith("Mac"))
\r
52 frame.setSize(450,560);
\r
55 frame.setTitle( frame.getTitle()+ " ("+sg.getName()+")");
\r
57 colorChooser.getSelectionModel().addChangeListener(this);
\r
63 if (seqGroup != null)
\r
64 oldColourScheme = seqGroup.cs;
\r
66 oldColourScheme = ap.av.getGlobalColourScheme();
\r
68 for (int i = 0; i < 20; i++)
\r
69 makeButton(ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i]) +
\r
70 "", ResidueProperties.aa[i]);
\r
72 makeButton("B", "B");
\r
73 makeButton("Z", "Z");
\r
74 makeButton("X", "X");
\r
75 makeButton("Gap", "'.','-',' '");
\r
77 if(jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR")!=null)
\r
79 loadColours(jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR"));
\r
83 public void stateChanged(ChangeEvent evt)
\r
85 if(selectedButton!=null)
\r
86 selectedButton.setBackground( colorChooser.getColor() );
\r
89 public void colourButtonPressed(MouseEvent e)
\r
91 selectedButton = (JButton)e.getSource();
\r
92 colorChooser.setColor(selectedButton.getBackground());
\r
95 void makeButton(String label, String aa)
\r
97 final JButton button = new JButton();
\r
98 Color col = Color.white;
\r
102 col = oldColourScheme.findColour(aa, -1);
\r
103 }catch(Exception ex){}
\r
105 button.setBackground( col );
\r
106 oldColours.addElement( col );
\r
107 button.setText( label );
\r
108 button.setForeground( col.darker().darker().darker() );
\r
109 button.setFont(new java.awt.Font("Verdana", 1, 10));
\r
110 button.addMouseListener(new java.awt.event.MouseAdapter()
\r
112 public void mouseClicked(MouseEvent e)
\r
114 colourButtonPressed(e);
\r
118 buttonPanel.add(button, null);
\r
122 protected void okButton_actionPerformed(ActionEvent e)
\r
124 applyButton_actionPerformed(null);
\r
126 frame.setClosed(true);
\r
127 }catch(Exception ex){}
\r
130 protected void applyButton_actionPerformed(ActionEvent e)
\r
132 Color [] newColours = new Color[24];
\r
133 for(int i=0; i<24; i++)
\r
135 JButton button = (JButton)buttonPanel.getComponent(i);
\r
136 newColours[i] = button.getBackground();
\r
139 UserColourScheme ucs = new UserColourScheme(newColours);
\r
140 ucs.setThreshold(0);
\r
148 ap.alignFrame.changeColour(ucs);
\r
152 protected void loadbutton_actionPerformed(ActionEvent e)
\r
154 JalviewFileChooser chooser = new JalviewFileChooser(
\r
155 jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
\r
156 new String[]{"jc"},
\r
157 new String[]{"Jalview User Colours"},
\r
158 "Jalview User Colours");
\r
159 chooser.setFileView(new jalview.io.JalviewFileView());
\r
160 chooser.setDialogTitle("Load colour scheme");
\r
161 chooser.setToolTipText("Load");
\r
162 int value = chooser.showOpenDialog(this);
\r
163 if (value == JalviewFileChooser.APPROVE_OPTION)
\r
165 File choice = chooser.getSelectedFile();
\r
166 jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
\r
167 jalview.bin.Cache.setProperty("USER_DEFINED_COLOUR", choice.getPath());
\r
168 Color[] colors = loadColours(choice.getAbsolutePath());
\r
169 for (int i = 0; i < colors.length; i++)
\r
171 JButton button = (JButton) buttonPanel.getComponent(i);
\r
172 button.setBackground(colors[i]);
\r
178 public static UserColourScheme loadDefaultColours()
\r
180 if(jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR")!=null)
\r
181 return loadDefaultColours(jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR"));
\r
186 public static UserColourScheme loadDefaultColours(String file)
\r
188 UserColourScheme ucs = null;
\r
189 Color cols[] = loadColours(file);
\r
192 ucs = new UserColourScheme(cols);
\r
193 ucs.setThreshold(0);
\r
198 static Color [] loadColours(String file)
\r
200 Color [] newColours=null;
\r
203 InputStreamReader in = new InputStreamReader(
\r
204 new FileInputStream(file), "UTF-8");
\r
206 jalview.binding.JalviewUserColours ucs = new jalview.binding.JalviewUserColours();
\r
207 ucs = (jalview.binding.JalviewUserColours)ucs.unmarshal(in);
\r
209 newColours = new Color[ucs.getColourCount()];
\r
210 for (int i = 0; i < 24; i++)
\r
211 newColours[i] = new Color(Integer.parseInt(ucs.getColour(i).getRGB(), 16));
\r
214 catch (Exception ex)
\r
215 {System.out.println("Error loading UserColourFile "+file);}
\r
220 protected void savebutton_actionPerformed(ActionEvent e)
\r
222 JalviewFileChooser chooser = new JalviewFileChooser(
\r
223 jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
\r
224 new String[]{"jc"},
\r
225 new String[]{"Jalview User Colours"},
\r
226 "Jalview User Colours");
\r
228 chooser.setFileView(new jalview.io.JalviewFileView());
\r
229 chooser.setDialogTitle("Save colour scheme");
\r
230 chooser.setToolTipText("Save");
\r
231 int value = chooser.showSaveDialog(this);
\r
232 if (value == JalviewFileChooser.APPROVE_OPTION)
\r
234 String choice = chooser.getSelectedFile().getPath();
\r
235 jalview.bin.Cache.setProperty("USER_DEFINED_COLOUR", choice);
\r
236 jalview.binding.JalviewUserColours ucs = new jalview.binding.JalviewUserColours();
\r
239 PrintWriter out = new PrintWriter(new OutputStreamWriter(
\r
240 new FileOutputStream(choice), "UTF-8"));
\r
241 for(int i=0; i<24; i++)
\r
243 JButton button = (JButton)buttonPanel.getComponent(i);
\r
244 jalview.binding.Colour col = new jalview.binding.Colour();
\r
245 col.setName(button.getText());
\r
246 col.setRGB( jalview.util.Format.getHexString( button.getBackground() ));
\r
247 ucs.addColour(col);
\r
253 catch(Exception ex)
\r
254 {ex.printStackTrace();}
\r
259 protected void cancelButton_actionPerformed(ActionEvent e)
\r
262 Color [] newColours = new Color[24];
\r
263 for(int i=0; i<24; i++)
\r
265 newColours[i] = (Color)oldColours.elementAt(i);
\r
266 buttonPanel.getComponent(i).setBackground(newColours[i]);
\r
269 UserColourScheme ucs = new UserColourScheme( newColours );
\r
271 if (seqGroup != null)
\r
274 ap.av.setGlobalColourScheme(ucs);
\r
279 frame.setClosed(true);
\r
280 }catch(Exception ex){}
\r