X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FUserDefinedColours.java;h=18382fbe4d9e9a96d257cc59fbfaac882bbf7bf6;hb=4ebc6d5b362bc093c39312aa1a69836e3dd6ae84;hp=a12672e25decd56de4392ba22d3572191e7eab2b;hpb=25effdb93ea664f42294ebf56d3b640d2f58a5f4;p=jalview.git diff --git a/src/jalview/gui/UserDefinedColours.java b/src/jalview/gui/UserDefinedColours.java index a12672e..18382fb 100755 --- a/src/jalview/gui/UserDefinedColours.java +++ b/src/jalview/gui/UserDefinedColours.java @@ -1,60 +1,125 @@ +/* + * 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 jalview.jbgui.GUserDefinedColours; -import javax.swing.*; +import java.io.*; +import java.util.*; + import java.awt.*; import java.awt.event.*; +import javax.swing.*; import javax.swing.event.*; -import jalview.schemes.*; -import java.io.*; -import java.util.*; +import jalview.datamodel.*; +import jalview.io.*; +import jalview.jbgui.*; +import jalview.schemes.*; -public class UserDefinedColours extends GUserDefinedColours implements ChangeListener +public class UserDefinedColours + extends GUserDefinedColours implements ChangeListener { - - AlignViewport av; - ColourSchemeI colourScheme; + AlignmentPanel ap; + SequenceGroup seqGroup; JButton selectedButton; Vector oldColours = new Vector(); + ColourSchemeI oldColourScheme; JInternalFrame frame; - public UserDefinedColours(JInternalFrame f, AlignViewport av) + 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.av = av; - frame = f; - colourScheme = av.getGlobalColourScheme(); - for(int i=0; i<20; i++) - makeButton(ResidueProperties.aa2Triplet.get( ResidueProperties.aa[i] )+"", ResidueProperties.aa[i]); + this.ap = ap; + seqGroup = sg; - makeButton("B","B"); - makeButton("Z","Z"); - makeButton("X","X"); - makeButton("'.','-',' '", "-"); + 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 stateChanged(ChangeEvent evt) + { + if (selectedButton != null) + { + selectedButton.setBackground(colorChooser.getColor()); + } + } public void colourButtonPressed(MouseEvent e) { - selectedButton = (JButton)e.getSource(); - colorChooser.setColor(selectedButton.getBackground()); + selectedButton = (JButton) e.getSource(); + colorChooser.setColor(selectedButton.getBackground()); } void makeButton(String label, String aa) { final JButton button = new JButton(); - button.setBackground( colourScheme.findColour(null,aa,-1,null) ); - oldColours.addElement( colourScheme.findColour(null,aa,-1,null) ); - button.setText( label ); + 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() { @@ -64,101 +129,210 @@ public class UserDefinedColours extends GUserDefinedColours implements ChangeLis } }); - buttonPanel.add(button); + buttonPanel.add(button, null); } - protected void okButton_actionPerformed(ActionEvent e) { applyButton_actionPerformed(null); - try{ + + try + { frame.setClosed(true); - }catch(Exception ex){} + } + catch (Exception ex) + { + } } protected void applyButton_actionPerformed(ActionEvent e) { - UserColourScheme ucs = new UserColourScheme(); - Color [] newColours = new Color[24]; - for(int i=0; i<24; i++) + Color[] newColours = new Color[24]; + + for (int i = 0; i < 24; i++) { - JButton button = (JButton)buttonPanel.getComponent(i); - newColours[i] = button.getBackground(); + JButton button = (JButton) buttonPanel.getComponent(i); + newColours[i] = button.getBackground(); } + UserColourScheme ucs = new UserColourScheme(newColours); ucs.setThreshold(0); - ucs.setColourScheme( newColours ); - av.setGlobalColourScheme( ucs ); + + if (seqGroup != null) + { + seqGroup.cs = ucs; + ap.repaint(); + } + else + { + ap.alignFrame.changeColour(ucs); + } } protected void loadbutton_actionPerformed(ActionEvent e) { - JFileChooser chooser = new JFileChooser(jalview.bin.Cache.LAST_DIRECTORY); + 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 == JFileChooser.APPROVE_OPTION) + + if (value == JalviewFileChooser.APPROVE_OPTION) { - String choice = chooser.getSelectedFile().getPath(); - jalview.bin.Cache.LAST_DIRECTORY = choice; + File choice = chooser.getSelectedFile(); + jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent()); + jalview.bin.Cache.setProperty("USER_DEFINED_COLOUR", + choice.getPath()); - try{ - BufferedReader in = new BufferedReader(new FileReader(choice)); - for(int i=0; i<24; i++) - { - JButton button = (JButton)buttonPanel.getComponent(i); - Color c = new Color(Integer.parseInt(in.readLine())); - button.setBackground(c); - } + Color[] colors = loadColours(choice.getAbsolutePath()); + for (int i = 0; i < colors.length; i++) + { + JButton button = (JButton) buttonPanel.getComponent(i); + button.setBackground(colors[i]); } - catch(Exception ex) - {} + } + } + + 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) { - JFileChooser chooser = new JFileChooser(jalview.bin.Cache.LAST_DIRECTORY); + 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 == JFileChooser.APPROVE_OPTION) + + if (value == JalviewFileChooser.APPROVE_OPTION) { String choice = chooser.getSelectedFile().getPath(); - jalview.bin.Cache.LAST_DIRECTORY = choice; + jalview.bin.Cache.setProperty("USER_DEFINED_COLOUR", choice); - try{ - PrintWriter out = new PrintWriter(new FileWriter(choice)); - for(int i=0; i<24; i++) + 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); - out.println(button.getBackground().getRGB() + ""); + 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();} + catch (Exception ex) + { + ex.printStackTrace(); + } } - } protected void cancelButton_actionPerformed(ActionEvent e) { - UserColourScheme ucs = new UserColourScheme(); - Color [] newColours = new Color[24]; - for(int i=0; i<24; i++) + Color[] newColours = new Color[24]; + + for (int i = 0; i < 24; i++) { - newColours[i] = (Color)oldColours.elementAt(i); - buttonPanel.getComponent(i).setBackground(newColours[i]); + newColours[i] = (Color) oldColours.elementAt(i); + buttonPanel.getComponent(i).setBackground(newColours[i]); } - ucs.setColourScheme( newColours ); - av.setGlobalColourScheme( ucs ); + 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) + { + } + } }