X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FUserDefinedColours.java;h=ede020c6101fd71d3c79968e9024763c3adb4123;hb=e02486484d33e20dedb020a34f31f6024d5720a2;hp=651f40c60b5335f9624162745f96e0c34e589c67;hpb=efc31b4a8d5cee63555586804a2b79c06bdb5a14;p=jalview.git diff --git a/src/jalview/gui/UserDefinedColours.java b/src/jalview/gui/UserDefinedColours.java index 651f40c..ede020c 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,9 +50,9 @@ public class UserDefinedColours extends GUserDefinedColours AlignmentPanel ap; SequenceGroup seqGroup; Vector selectedButtons; - Vector oldColours = new Vector(); ColourSchemeI oldColourScheme; JInternalFrame frame; + MCview.PDBCanvas pdbcanvas; /** * Creates a new UserDefinedColours object. @@ -64,11 +65,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 +91,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,14 +104,43 @@ public class UserDefinedColours extends GUserDefinedColours makeButton("B", "B"); makeButton("Z", "Z"); makeButton("X", "X"); - makeButton("Gap", "'.','-',' '"); + makeButton("Gap", "-"); + } + + public UserDefinedColours(MCview.PDBCanvas pdb, ColourSchemeI oldcs) + { + super(); + frame = new JInternalFrame(); + frame.setContentPane(this); + Desktop.addInternalFrame(frame, "User Defined Colours", 720, 370, true); + pdbcanvas = pdb; - if (jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR") != null) + if (System.getProperty("os.name").startsWith("Mac")) + { + frame.setSize(760, 370); + } + + colorChooser.getSelectionModel().addChangeListener(this); + + oldColourScheme = oldcs; + + if (oldColourScheme instanceof UserColourScheme) + { + schemeName.setText( ( (UserColourScheme) oldColourScheme).getName()); + } + for (int i = 0; i < 20; i++) { - loadColours(jalview.bin.Cache.getProperty("USER_DEFINED_COLOUR")); + makeButton(ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i]) + + "", ResidueProperties.aa[i]); } + + makeButton("B", "B"); + makeButton("Z", "Z"); + makeButton("X", "X"); + makeButton("Gap", "-"); } + /** * DOCUMENT ME! * @@ -140,8 +174,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()>0) + 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,46 +413,69 @@ 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.schemabinding.version2.JalviewUserColours jucs + = new jalview.schemabinding.version2.JalviewUserColours(); + + org.exolab.castor.xml.Unmarshaller unmar + = new org.exolab.castor.xml.Unmarshaller(jucs); + jucs = (jalview.schemabinding.version2.JalviewUserColours) unmar.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); + //Could be Archive Jalview format + try{ + InputStreamReader in = new InputStreamReader(new FileInputStream( + file), "UTF-8"); + + jalview.binding.JalviewUserColours jucs + = new jalview.binding.JalviewUserColours(); + + jucs = (jalview.binding.JalviewUserColours) jucs.unmarshal(in); + + newColours = new Color[jucs.getColourCount()]; + + for (int i = 0; i < 24; i++) + { + newColours[i] = new Color(Integer.parseInt( + jucs.getColour(i).getRGB(), 16)); + } + if (newColours != null) + { + ucs = new UserColourScheme(newColours); + ucs.setName(jucs.getSchemeName()); + } + }catch(Exception ex2) + { ex2.printStackTrace(); } + + if(newColours==null) + System.out.println("Error loading User ColourFile\n"+ex); } - return newColours; + return ucs; } /** @@ -387,6 +485,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 +517,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 +565,118 @@ public class UserDefinedColours extends GUserDefinedColours */ protected void cancelButton_actionPerformed(ActionEvent e) { - Color[] newColours = new Color[24]; + if (ap != null) + { + if (seqGroup != null) + { + seqGroup.cs = oldColourScheme; + } + else if (ap != null) + { + ap.av.setGlobalColourScheme(oldColourScheme); + } + ap.repaint(); + } - for (int i = 0; i < 24; i++) + if(pdbcanvas!=null) + { + pdbcanvas.pdb.setColours(oldColourScheme); + } + + 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"); + } }