X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FColourMenuHelper.java;h=b2b95748106511d3666391a955740cbfe67ccf68;hb=807f5945ffa954c38f07cbf9d2a4ebc22cfe5eb9;hp=8e0765b841e5acea620c03eb2887ad3e0796dc5c;hpb=f831ddf7f52d6c4a1918e87d94877b22bd322648;p=jalview.git diff --git a/src/jalview/gui/ColourMenuHelper.java b/src/jalview/gui/ColourMenuHelper.java index 8e0765b..b2b9574 100644 --- a/src/jalview/gui/ColourMenuHelper.java +++ b/src/jalview/gui/ColourMenuHelper.java @@ -1,7 +1,9 @@ package jalview.gui; +import jalview.bin.Cache; import jalview.datamodel.AnnotatedCollectionI; import jalview.schemes.ColourSchemeI; +import jalview.schemes.ColourSchemeLoader; import jalview.schemes.ColourSchemes; import jalview.schemes.ResidueColourScheme; import jalview.schemes.UserColourScheme; @@ -21,6 +23,12 @@ public class ColourMenuHelper { public interface ColourChangeListener { + /** + * Change colour scheme to the selected scheme + * + * @param name + * the registered (unique) name of a colour scheme + */ void changeColour_actionPerformed(String name); } @@ -35,7 +43,7 @@ public class ColourMenuHelper *
  • Clustal
  • *
  • ...other 'built-in' colours
  • *
  • ...any user-defined colours
  • - *
  • User Defined..
  • + *
  • User Defined..(only for AlignFrame menu)
  • * * * @param colourMenu @@ -75,8 +83,8 @@ public class ColourMenuHelper } /* - * scan registered colour schemes (built-in or user-defined - * and add them to the menu (in the order they were added) + * scan registered colour schemes (built-in or user-defined) + * and add them to the menu (in the order they were registered) */ Iterable colourSchemes = ColourSchemes.getInstance() .getColourSchemes(); @@ -102,14 +110,14 @@ public class ColourMenuHelper /* * user-defined colour scheme loaded on startup or during the * Jalview session; right-click on this offers the option to - * remove it as a colour choice + * remove it as a colour choice (unless currently selected) */ radioItem.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent evt) { - if (evt.isPopupTrigger()) // Mac + if (evt.isPopupTrigger() && !radioItem.isSelected()) // Mac { offerRemoval(); } @@ -118,7 +126,7 @@ public class ColourMenuHelper @Override public void mouseReleased(MouseEvent evt) { - if (evt.isPopupTrigger()) // Windows + if (evt.isPopupTrigger() && !radioItem.isSelected()) // Windows { offerRemoval(); } @@ -136,11 +144,10 @@ public class ColourMenuHelper JvOptionPane.YES_NO_OPTION); if (option == JvOptionPane.YES_OPTION) { - UserDefinedColours.removeColourFromDefaults(radioItem - .getName()); ColourSchemes.getInstance().removeColourScheme( radioItem.getName()); colourMenu.remove(radioItem); + updatePreferences(); } else { @@ -170,6 +177,7 @@ public class ColourMenuHelper final String label = MessageManager.getString("action.user_defined"); JRadioButtonMenuItem userDefinedColour = new JRadioButtonMenuItem( label); + userDefinedColour.setName(ResidueColourScheme.USER_DEFINED); userDefinedColour.addActionListener(new ActionListener() { @Override @@ -186,20 +194,22 @@ public class ColourMenuHelper } /** - * Marks as selected the colour menu item matching the given name, or the - * first item ('None') if no match is found + * Marks as selected the colour menu item matching the given colour scheme, or + * the first item ('None') if no match is found. If the colour scheme is a + * user defined scheme, but not in the menu (this arises if a new scheme is + * defined and applied but not saved to file), then menu option + * "User Defined.." is selected. * * @param colourMenu - * @param colourName + * @param cs */ - public static void setColourSelected(JMenu colourMenu, String colourName) + public static void setColourSelected(JMenu colourMenu, ColourSchemeI cs) { - if (colourName == null) - { - return; - } + String colourName = cs == null ? ResidueColourScheme.NONE : cs + .getSchemeName(); JRadioButtonMenuItem none = null; + JRadioButtonMenuItem userDefined = null; /* * select the radio button whose name matches the colour name @@ -209,34 +219,79 @@ public class ColourMenuHelper { if (menuItem instanceof JRadioButtonMenuItem) { - String buttonName = ((JRadioButtonMenuItem) menuItem).getName(); - if (colourName.equals(buttonName)) + JRadioButtonMenuItem radioButton = (JRadioButtonMenuItem) menuItem; + String buttonName = radioButton.getName(); + if (buttonName.equals(colourName)) { - ((JRadioButtonMenuItem) menuItem).setSelected(true); + radioButton.setSelected(true); return; } if (ResidueColourScheme.NONE.equals(buttonName)) { - none = (JRadioButtonMenuItem) menuItem; + none = radioButton; + } + if (ResidueColourScheme.USER_DEFINED.equals(buttonName)) + { + userDefined = radioButton; } } } - if (none != null) + + /* + * no match by name; select User Defined.. if current scheme is a + * user defined one, else select None + */ + if (cs instanceof UserColourScheme && userDefined != null) + { + userDefined.setSelected(true); + } + else if (none != null) { none.setSelected(true); } } /** - * Marks as selected the colour menu item matching the given colour scheme, or - * the first item ('None') if no match is found - * - * @param colourMenu - * @param cs + * Updates the USER_DEFINE_COLOURS preference to remove any de-registered + * colour scheme */ - public static void setColourSelected(JMenu colourMenu, ColourSchemeI cs) + static void updatePreferences() { - setColourSelected(colourMenu, cs == null ? ResidueColourScheme.NONE - : cs.getSchemeName()); + StringBuilder coloursFound = new StringBuilder(); + String[] files = Cache.getProperty("USER_DEFINED_COLOURS").split("\\|"); + + /* + * the property does not include the scheme name, it is in the file; + * so just load the colour schemes and discard any whose name is not + * registered + */ + for (String file : files) + { + try + { + UserColourScheme ucs = ColourSchemeLoader.loadColourScheme(file); + if (ucs != null + && ColourSchemes.getInstance().nameExists(ucs.getName())) + { + if (coloursFound.length() > 0) + { + coloursFound.append("|"); + } + coloursFound.append(file); + } + } catch (Exception ex) + { + System.out.println("Error loading User ColourFile\n" + ex); + } + } + + if (coloursFound.toString().length() > 1) + { + Cache.setProperty("USER_DEFINED_COLOURS", coloursFound.toString()); + } + else + { + Cache.applicationProperties.remove("USER_DEFINED_COLOURS"); + } } }