JAL-2371 tests added
[jalview.git] / src / jalview / gui / ColourMenuHelper.java
index 8e0765b..31780d6 100644 (file)
@@ -1,5 +1,6 @@
 package jalview.gui;
 
+import jalview.bin.Cache;
 import jalview.datamodel.AnnotatedCollectionI;
 import jalview.schemes.ColourSchemeI;
 import jalview.schemes.ColourSchemes;
@@ -76,7 +77,7 @@ public class ColourMenuHelper
 
     /*
      * scan registered colour schemes (built-in or user-defined
-     * and add them to the menu (in the order they were added)
+     * and add them to the menu (in the order they were registered)
      */
     Iterable<ColourSchemeI> colourSchemes = ColourSchemes.getInstance()
             .getColourSchemes();
@@ -136,11 +137,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
             {
@@ -239,4 +239,48 @@ public class ColourMenuHelper
     setColourSelected(colourMenu, cs == null ? ResidueColourScheme.NONE
             : cs.getSchemeName());
   }
+
+  /**
+   * Updates the USER_DEFINE_COLOURS preference to remove any de-registered
+   * colour scheme
+   */
+  static void updatePreferences()
+  {
+    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 = ColourSchemes.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");
+    }
+  }
 }