X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FUserDefinedColours.java;h=ec4ad5526196ea40068f3f33e2ae59039709102b;hb=a0d1b1826f8ab0792c589946746d52ad0316fc99;hp=4ad129b39d92747b34516c077932c571d283958f;hpb=9fff97021c2c637e426f6971d54dcf3fd4191985;p=jalview.git diff --git a/src/jalview/gui/UserDefinedColours.java b/src/jalview/gui/UserDefinedColours.java index 4ad129b..ec4ad55 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", "-"); } /** @@ -140,8 +139,12 @@ public class UserDefinedColours extends GUserDefinedColours if(e.isShiftDown()) { - JButton start = (JButton)selectedButtons.elementAt(selectedButtons.size()-1); - JButton end = (JButton) e.getSource(); + JButton start, end = (JButton) e.getSource(); + if(selectedButtons.size()>1) + start = (JButton)selectedButtons.elementAt(selectedButtons.size()-1); + else + start = (JButton) e.getSource(); + int startIndex=0, endIndex=0; for(int b=0; b-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,45 +371,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); - } - - 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; } /** @@ -386,6 +414,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"); @@ -399,10 +446,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( @@ -435,33 +494,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); + } - for (int i = 0; i < 24; i++) + ap.repaint(); + + try + { + frame.setClosed(true); + } + catch (Exception ex) { - newColours[i] = (Color) oldColours.elementAt(i); - buttonPanel.getComponent(i).setBackground(newColours[i]); } + } - 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"); + } }