X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FUserDefinedColours.java;h=18382fbe4d9e9a96d257cc59fbfaac882bbf7bf6;hb=b14411143cbfb4d311f68b3d3b73a645bf8200e8;hp=05edac538869178c56a28ffd8bcdd70038bd8f9f;hpb=c300ec003b843d3166983a893effc1f4faafed08;p=jalview.git diff --git a/src/jalview/gui/UserDefinedColours.java b/src/jalview/gui/UserDefinedColours.java index 05edac5..18382fb 100755 --- a/src/jalview/gui/UserDefinedColours.java +++ b/src/jalview/gui/UserDefinedColours.java @@ -1,20 +1,39 @@ +/* + * 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 jalview.datamodel.*; -import jalview.io.*; -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 { - AlignmentPanel ap; SequenceGroup seqGroup; JButton selectedButton; @@ -22,48 +41,66 @@ public class UserDefinedColours extends GUserDefinedColours implements ChangeLis ColourSchemeI oldColourScheme; JInternalFrame frame; - - public UserDefinedColours(JInternalFrame f, AlignmentPanel ap, SequenceGroup sg) + public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg) { super(); - colorChooser.getSelectionModel().addChangeListener(this); - this.ap = ap; - frame = f; - seqGroup = sg; + 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 (seqGroup != null) - oldColourScheme = seqGroup.cs; - else - oldColourScheme = ap.av.getGlobalColourScheme(); + if (sg != null) + { + frame.setTitle(frame.getTitle() + " (" + sg.getName() + ")"); + } - for (int i = 0; i < 20; i++) - makeButton(ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i]) + - "", ResidueProperties.aa[i]); + colorChooser.getSelectionModel().addChangeListener(this); - makeButton("B", "B"); - makeButton("Z", "Z"); - makeButton("X", "X"); - makeButton("'.','-',' '", "-"); + this.ap = ap; + seqGroup = sg; - if(jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR")!=null) - { - loadColour(jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR")); - } + 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) @@ -71,15 +108,18 @@ public class UserDefinedColours extends GUserDefinedColours implements ChangeLis final JButton button = new JButton(); Color col = Color.white; - - try{ + try + { col = oldColourScheme.findColour(aa, -1); - }catch(Exception ex){} + } + catch (Exception ex) + { + } - button.setBackground( col ); - oldColours.addElement( col ); - button.setText( label ); - button.setForeground( col.darker().darker().darker() ); + 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() { @@ -89,120 +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 ); - if(seqGroup!=null) + if (seqGroup != null) + { seqGroup.cs = ucs; + ap.repaint(); + } else - ap.av.setGlobalColourScheme(ucs); - - ap.RefreshPanels(); + { + ap.alignFrame.changeColour(ucs); + } } protected void loadbutton_actionPerformed(ActionEvent e) { - JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty("LAST_DIRECTORY"), - new String[]{".jvuc"}, "Jalview User Colours"); + 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) { - String choice = chooser.getSelectedFile().getPath(); - jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice); - loadColour(choice); + 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; } - void loadColour(String file) + static Color[] loadColours(String file) { + Color[] newColours = null; + try { - BufferedReader in = new BufferedReader(new FileReader(file)); + 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++) { - JButton button = (JButton) buttonPanel.getComponent(i); - Color c = new Color(Integer.parseInt(in.readLine())); - button.setBackground(c); + 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")); + 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); - 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 ); + UserColourScheme ucs = new UserColourScheme(newColours); + if (seqGroup != null) + { seqGroup.cs = ucs; + } else + { ap.av.setGlobalColourScheme(ucs); + } - ap.RefreshPanels(); + ap.repaint(); + try + { + frame.setClosed(true); + } + catch (Exception ex) + { + } } - - }