X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FUserDefinedColours.java;h=79df106d8c7eb51c381b643641203fb6b29f8448;hb=a67402c4b5b2bc1c97d2624af72c941dec51ea40;hp=651f40c60b5335f9624162745f96e0c34e589c67;hpb=efc31b4a8d5cee63555586804a2b79c06bdb5a14;p=jalview.git diff --git a/src/jalview/gui/UserDefinedColours.java b/src/jalview/gui/UserDefinedColours.java index 651f40c..79df106 100755 --- a/src/jalview/gui/UserDefinedColours.java +++ b/src/jalview/gui/UserDefinedColours.java @@ -37,6 +37,7 @@ import javax.swing.*; import javax.swing.event.*; + /** * DOCUMENT ME! * @@ -49,7 +50,6 @@ public class UserDefinedColours extends GUserDefinedColours AlignmentPanel ap; SequenceGroup seqGroup; Vector selectedButtons; - Vector oldColours = new Vector(); ColourSchemeI oldColourScheme; JInternalFrame frame; @@ -64,11 +64,11 @@ public class UserDefinedColours extends GUserDefinedColours super(); frame = new JInternalFrame(); frame.setContentPane(this); - Desktop.addInternalFrame(frame, "User Defined Colours", 450, 530, false); + Desktop.addInternalFrame(frame, "User Defined Colours", 720, 370, true); if (System.getProperty("os.name").startsWith("Mac")) { - frame.setSize(450, 560); + frame.setSize(760, 370); } if (sg != null) @@ -90,6 +90,10 @@ public class UserDefinedColours extends GUserDefinedColours oldColourScheme = ap.av.getGlobalColourScheme(); } + if (oldColourScheme instanceof UserColourScheme) + { + schemeName.setText( ( (UserColourScheme) oldColourScheme).getName()); + } for (int i = 0; i < 20; i++) { makeButton(ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i]) + @@ -99,12 +103,7 @@ public class UserDefinedColours extends GUserDefinedColours 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")); - } + makeButton("Gap", "-"); } /** @@ -215,10 +214,10 @@ public class UserDefinedColours extends GUserDefinedColours } catch (Exception ex) { + // ex.printStackTrace(); } button.setBackground(col); - oldColours.addElement(col); button.setText(label); button.setForeground(col.darker().darker().darker()); button.setFont(new java.awt.Font("Verdana", 1, 10)); @@ -258,16 +257,8 @@ public class UserDefinedColours extends GUserDefinedColours */ 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); + UserColourScheme ucs = getSchemeFromGUI(); + ucs.setName(schemeName.getText()); if (seqGroup != null) { @@ -280,6 +271,24 @@ public class UserDefinedColours extends GUserDefinedColours } } + UserColourScheme getSchemeFromGUI() + { + 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, ap.av.getIgnoreGapsConsensus()); + + return ucs; + } + + + /** * DOCUMENT ME! * @@ -300,10 +309,17 @@ public class UserDefinedColours extends GUserDefinedColours { File choice = chooser.getSelectedFile(); jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent()); - jalview.bin.Cache.setProperty("USER_DEFINED_COLOUR", + String defaultColours = jalview.bin.Cache.getDefault("USER_DEFINED_COLOURS", choice.getPath()); + if (defaultColours.indexOf(choice.getPath()) == -1) + defaultColours = defaultColours.concat("|").concat(choice.getPath()); + + jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours); + - Color[] colors = loadColours(choice.getAbsolutePath()); + UserColourScheme ucs = loadColours(choice.getAbsolutePath()); + Color[] colors = ucs.getColours(); + schemeName.setText(ucs.getName()); for (int i = 0; i < colors.length; i++) { @@ -320,15 +336,28 @@ public class UserDefinedColours extends GUserDefinedColours */ public static UserColourScheme loadDefaultColours() { - if (jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR") != null) + UserColourScheme ret = null; + + String colours = jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS"); + if ( colours != null ) { - return loadDefaultColours(jalview.bin.Cache.getProperty( - "USER_DEFINED_COLOUR")); + if(colours.indexOf("|")>-1) + colours = colours.substring(0, colours.indexOf("|")); + + ret = loadColours(colours); } - else + + if(ret == null) { - return null; + Color[] newColours = new Color[24]; + for (int i = 0; i < 24; i++) + { + newColours[i] = Color.white; + } + ret = new UserColourScheme(newColours); } + + return ret; } /** @@ -338,46 +367,40 @@ public class UserDefinedColours extends GUserDefinedColours * * @return DOCUMENT ME! */ - public static UserColourScheme loadDefaultColours(String file) + static UserColourScheme loadColours(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; + UserColourScheme ucs = null; + 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); + jalview.binding.JalviewUserColours jucs = new jalview.binding.JalviewUserColours(); + jucs = (jalview.binding.JalviewUserColours) jucs.unmarshal(in); - newColours = new Color[ucs.getColourCount()]; + newColours = new Color[jucs.getColourCount()]; for (int i = 0; i < 24; i++) { - newColours[i] = new Color(Integer.parseInt( - ucs.getColour(i).getRGB(), 16)); + newColours[i] = new Color(Integer.parseInt( + jucs.getColour(i).getRGB(), 16)); } + if (newColours != null) + { + ucs = new UserColourScheme(newColours); + ucs.setName( jucs.getSchemeName() ); + } + } catch (Exception ex) { - System.out.println("Error loading UserColourFile " + file); + System.out.println("Error loading User ColourFile\n"+ex); } - return newColours; + + return ucs; } /** @@ -387,6 +410,25 @@ public class UserDefinedColours extends GUserDefinedColours */ protected void savebutton_actionPerformed(ActionEvent e) { + if(schemeName.getText().trim().length()<1) + { + JOptionPane.showInternalMessageDialog(Desktop.desktop, + "User colour scheme must have a name!", + "No name for colour scheme", + JOptionPane.WARNING_MESSAGE); + return; + } + + if(userColourSchemes!=null && userColourSchemes.containsKey(schemeName.getText()) ) + { + int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop, "Colour scheme "+schemeName.getText()+ " exists." + +"\nContinue saving colour scheme as "+schemeName.getText()+"?", + "Duplicate scheme name", JOptionPane.YES_NO_OPTION); + if(reply != JOptionPane.YES_OPTION) + return; + + userColourSchemes.remove(schemeName.getText()); + } JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty( "LAST_DIRECTORY"), new String[] { "jc" }, new String[] { "Jalview User Colours" }, "Jalview User Colours"); @@ -400,10 +442,22 @@ public class UserDefinedColours extends GUserDefinedColours if (value == JalviewFileChooser.APPROVE_OPTION) { String choice = chooser.getSelectedFile().getPath(); - jalview.bin.Cache.setProperty("USER_DEFINED_COLOUR", choice); + String defaultColours = jalview.bin.Cache.getDefault("USER_DEFINED_COLOURS", choice); + if(defaultColours.indexOf(choice)==-1) + { + if(defaultColours.length()>0) + defaultColours = defaultColours.concat("|"); + defaultColours = defaultColours.concat(choice); + } - jalview.binding.JalviewUserColours ucs = new jalview.binding.JalviewUserColours(); + userColourSchemes.put(schemeName.getText(), getSchemeFromGUI()); + ap.alignFrame.updateUserColourMenu(); + + jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", defaultColours); + + jalview.binding.JalviewUserColours ucs = new jalview.binding.JalviewUserColours(); + ucs.setSchemeName(schemeName.getText()); try { PrintWriter out = new PrintWriter(new OutputStreamWriter( @@ -436,33 +490,111 @@ public class UserDefinedColours extends GUserDefinedColours */ protected void cancelButton_actionPerformed(ActionEvent e) { - Color[] newColours = new Color[24]; + if (seqGroup != null) + { + seqGroup.cs = oldColourScheme; + } + else + { + ap.av.setGlobalColourScheme(oldColourScheme); + } + + ap.repaint(); - for (int i = 0; i < 24; i++) + try { - newColours[i] = (Color) oldColours.elementAt(i); - buttonPanel.getComponent(i).setBackground(newColours[i]); + frame.setClosed(true); } + catch (Exception ex) + { + } + } - UserColourScheme ucs = new UserColourScheme(newColours); - if (seqGroup != null) + static Hashtable userColourSchemes; + + public static Hashtable getUserColourSchemes() + { + return userColourSchemes; + } + + public static void initUserColourSchemes(String files) + { + userColourSchemes = new Hashtable(); + + if(files==null || files.length()==0) + return; + + + // In case colours can't be loaded, we'll remove them + // from the default list here. + StringBuffer coloursFound = new StringBuffer(); + StringTokenizer st = new StringTokenizer(files, "|"); + while (st.hasMoreElements()) + { + String file = st.nextToken(); + try { - seqGroup.cs = ucs; + UserColourScheme ucs = loadColours(file); + if (ucs != null) + { + if (coloursFound.length() > 0) + coloursFound.append("|"); + coloursFound.append(file); + userColourSchemes.put(ucs.getName(), ucs); + } } - else + catch (Exception ex) { - ap.av.setGlobalColourScheme(ucs); + System.out.println("Error loading User ColourFile\n" + ex); } + } + if (!files.equals(coloursFound.toString())) + { + if (coloursFound.toString().length() > 1) + jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", + coloursFound.toString()); + else + jalview.bin.Cache.applicationProperties.remove("USER_DEFINED_COLOURS"); + } + } - ap.repaint(); + public static void removeColourFromDefaults(String target) + { + // The only way to find colours by name is to load them in + // In case colours can't be loaded, we'll remove them + // from the default list here. + userColourSchemes = new Hashtable(); + + StringBuffer coloursFound = new StringBuffer(); + StringTokenizer st = new StringTokenizer( + jalview.bin.Cache.getProperty("USER_DEFINED_COLOURS"), "|"); + + while (st.hasMoreElements()) + { + String file = st.nextToken(); try { - frame.setClosed(true); + UserColourScheme ucs = loadColours(file); + if (ucs != null && !ucs.getName().equals(target)) + { + if (coloursFound.length() > 0) + coloursFound.append("|"); + coloursFound.append(file); + userColourSchemes.put(ucs.getName(), ucs); + } } catch (Exception ex) { + System.out.println("Error loading User ColourFile\n" + ex); } + } + + if (coloursFound.toString().length() > 1) + jalview.bin.Cache.setProperty("USER_DEFINED_COLOURS", coloursFound.toString()); + else + jalview.bin.Cache.applicationProperties.remove("USER_DEFINED_COLOURS"); + } }