/* * Jalview - A Sequence Alignment Editor and Viewer * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ package jalview.gui; import java.io.*; import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import jalview.datamodel.*; import jalview.io.*; import jalview.jbgui.*; import jalview.schemes.*; public class UserDefinedColours extends GUserDefinedColours implements ChangeListener { AlignmentPanel ap; SequenceGroup seqGroup; JButton selectedButton; Vector oldColours = new Vector(); ColourSchemeI oldColourScheme; JInternalFrame frame; public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg) { super(); frame = new JInternalFrame(); frame.setContentPane(this); Desktop.addInternalFrame(frame, "User Defined Colours", 450, 530, false); if (System.getProperty("os.name").startsWith("Mac")) { frame.setSize(450, 560); } if (sg != null) { frame.setTitle(frame.getTitle() + " (" + sg.getName() + ")"); } colorChooser.getSelectionModel().addChangeListener(this); this.ap = ap; seqGroup = sg; if (seqGroup != null) { oldColourScheme = seqGroup.cs; } else { oldColourScheme = ap.av.getGlobalColourScheme(); } for (int i = 0; i < 20; i++) { makeButton(ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i]) + "", ResidueProperties.aa[i]); } makeButton("B", "B"); makeButton("Z", "Z"); makeButton("X", "X"); makeButton("Gap", "'.','-',' '"); if (jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR") != null) { loadColours(jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR")); } } public void stateChanged(ChangeEvent evt) { if (selectedButton != null) { selectedButton.setBackground(colorChooser.getColor()); } } public void colourButtonPressed(MouseEvent e) { selectedButton = (JButton) e.getSource(); colorChooser.setColor(selectedButton.getBackground()); } void makeButton(String label, String aa) { final JButton button = new JButton(); Color col = Color.white; try { col = oldColourScheme.findColour(aa, -1); } catch (Exception ex) { } button.setBackground(col); oldColours.addElement(col); button.setText(label); button.setForeground(col.darker().darker().darker()); button.setFont(new java.awt.Font("Verdana", 1, 10)); button.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(MouseEvent e) { colourButtonPressed(e); } }); buttonPanel.add(button, null); } protected void okButton_actionPerformed(ActionEvent e) { applyButton_actionPerformed(null); try { frame.setClosed(true); } catch (Exception ex) { } } protected void applyButton_actionPerformed(ActionEvent e) { Color[] newColours = new Color[24]; for (int i = 0; i < 24; i++) { JButton button = (JButton) buttonPanel.getComponent(i); newColours[i] = button.getBackground(); } UserColourScheme ucs = new UserColourScheme(newColours); ucs.setThreshold(0); if (seqGroup != null) { seqGroup.cs = ucs; ap.repaint(); } else { ap.alignFrame.changeColour(ucs); } } protected void loadbutton_actionPerformed(ActionEvent e) { JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache. getProperty( "LAST_DIRECTORY"), new String[] {"jc"}, new String[] {"Jalview User Colours"}, "Jalview User Colours"); chooser.setFileView(new jalview.io.JalviewFileView()); chooser.setDialogTitle("Load colour scheme"); chooser.setToolTipText("Load"); int value = chooser.showOpenDialog(this); if (value == JalviewFileChooser.APPROVE_OPTION) { File choice = chooser.getSelectedFile(); jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent()); jalview.bin.Cache.setProperty("USER_DEFINED_COLOUR", choice.getPath()); Color[] colors = loadColours(choice.getAbsolutePath()); for (int i = 0; i < colors.length; i++) { JButton button = (JButton) buttonPanel.getComponent(i); button.setBackground(colors[i]); } } } public static UserColourScheme loadDefaultColours() { if (jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR") != null) { return loadDefaultColours(jalview.bin.Cache.getProperty( "USER_DEFINED_COLOUR")); } else { return null; } } public static UserColourScheme loadDefaultColours(String file) { UserColourScheme ucs = null; Color[] cols = loadColours(file); if (cols != null) { ucs = new UserColourScheme(cols); ucs.setThreshold(0); } return ucs; } static Color[] loadColours(String file) { Color[] newColours = null; try { InputStreamReader in = new InputStreamReader(new FileInputStream( file), "UTF-8"); jalview.binding.JalviewUserColours ucs = new jalview.binding. JalviewUserColours(); ucs = (jalview.binding.JalviewUserColours) ucs.unmarshal(in); newColours = new Color[ucs.getColourCount()]; for (int i = 0; i < 24; i++) { newColours[i] = new Color(Integer.parseInt( ucs.getColour(i).getRGB(), 16)); } } catch (Exception ex) { System.out.println("Error loading UserColourFile " + file); } return newColours; } protected void savebutton_actionPerformed(ActionEvent e) { JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache. getProperty( "LAST_DIRECTORY"), new String[] {"jc"}, new String[] {"Jalview User Colours"}, "Jalview User Colours"); chooser.setFileView(new jalview.io.JalviewFileView()); chooser.setDialogTitle("Save colour scheme"); chooser.setToolTipText("Save"); int value = chooser.showSaveDialog(this); if (value == JalviewFileChooser.APPROVE_OPTION) { String choice = chooser.getSelectedFile().getPath(); jalview.bin.Cache.setProperty("USER_DEFINED_COLOUR", choice); jalview.binding.JalviewUserColours ucs = new jalview.binding. JalviewUserColours(); try { PrintWriter out = new PrintWriter(new OutputStreamWriter( new FileOutputStream(choice), "UTF-8")); for (int i = 0; i < 24; i++) { JButton button = (JButton) buttonPanel.getComponent(i); jalview.binding.Colour col = new jalview.binding.Colour(); col.setName(button.getText()); col.setRGB(jalview.util.Format.getHexString( button.getBackground())); ucs.addColour(col); } ucs.marshal(out); out.close(); } catch (Exception ex) { ex.printStackTrace(); } } } protected void cancelButton_actionPerformed(ActionEvent e) { Color[] newColours = new Color[24]; for (int i = 0; i < 24; i++) { newColours[i] = (Color) oldColours.elementAt(i); buttonPanel.getComponent(i).setBackground(newColours[i]); } UserColourScheme ucs = new UserColourScheme(newColours); if (seqGroup != null) { seqGroup.cs = ucs; } else { ap.av.setGlobalColourScheme(ucs); } ap.repaint(); try { frame.setClosed(true); } catch (Exception ex) { } } }