Jalview-JS/JAL-3253-applet additional static final preferences
[jalview.git] / src / jalview / bin / Cache.java
index 09f8de7..4dac4df 100755 (executable)
@@ -20,8 +20,9 @@
  */
 package jalview.bin;
 
+import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
 import jalview.datamodel.PDBEntry;
-import jalview.gui.UserDefinedColours;
+import jalview.gui.Preferences;
 import jalview.schemes.ColourSchemeLoader;
 import jalview.schemes.ColourSchemes;
 import jalview.schemes.UserColourScheme;
@@ -207,9 +208,14 @@ import org.apache.log4j.SimpleLayout;
  * @author $author$
  * @version $Revision$
  */
-public class Cache
+public class Cache implements ApplicationSingletonI
 {
 
+  private Cache()
+  {
+    // private singleton
+  }
+
   /**
    * In Java, this will be a static field instance, which will be
    * application-specific; in JavaScript it will be an applet-specific instance
@@ -217,10 +223,9 @@ public class Cache
    * 
    * @return
    */
-  public static Cache getInstance()
+  private static Cache getInstance()
   {
-    Jalview j = Jalview.getInstance();
-    return (j.cache == null ? j.cache = new Cache() : j.cache);
+    return (Cache) ApplicationSingletonProvider.getInstance(Cache.class);
   }
 
   /**
@@ -278,11 +283,6 @@ public class Cache
    */
   public static Logger log;
 
-  private Cache()
-  {
-    // inaccessible
-  }
-
   /** Jalview Properties */
   // BH 2019.05.08 was static
   @SuppressWarnings("serial")
@@ -297,8 +297,7 @@ public class Cache
   };
 
   /** Default file is ~/.jalview_properties */
-  // BH 2019.05.07 note: Instances of Jalview will share this file.
-  static String propertiesFile;
+  private String propertiesFile;
 
   /**
    * flag to possibly allow properties to be written to a property file
@@ -511,7 +510,7 @@ public class Cache
             getDefault("sifts_cache_threshold_in_days",
                     DEFAULT_CACHE_THRESHOLD_IN_DAYS));
 
-    IdOrgSettings.setUrl(getDefault("ID_ORG_HOSTURL",
+    IdOrgSettings.setUrl(getDefault(Preferences.ID_ORG_HOSTURL,
             "http://www.jalview.org/services/identifiers"));
     IdOrgSettings.setDownloadLocation(ID_ORG_FILE);
 
@@ -519,7 +518,7 @@ public class Cache
             .println("Jalview Version: " + codeVersion + codeInstallation);
 
     StructureImportSettings.setDefaultStructureFileFormat(jalview.bin.Cache
-            .getDefault("PDB_DOWNLOAD_FORMAT", PDB_DOWNLOAD_FORMAT));
+            .getDefault(Preferences.PDB_DOWNLOAD_FORMAT, PDB_DOWNLOAD_FORMAT));
     StructureImportSettings
             .setDefaultPDBFileParser(DEFAULT_PDB_FILE_PARSER);
     // StructureImportSettings
@@ -599,7 +598,7 @@ public class Cache
     setProperty("VERSION", codeVersion);
 
     // LOAD USERDEFINED COLOURS
-    initUserColourSchemes(getProperty("USER_DEFINED_COLOURS"));
+    initUserColourSchemes(getProperty(Preferences.USER_DEFINED_COLOURS));
   }
 
   private void deleteBuildProperties()
@@ -624,18 +623,7 @@ public class Cache
    */
   public static String getProperty(String key)
   {
-    return getInstance().getPropertyImpl(key);
-  }
-
-  private String getPropertyImpl(String key)
-  {
-    String prop = applicationProperties.getProperty(key);
-    if (prop == null && Platform.isJS())
-    {
-      prop = applicationProperties.getProperty(Platform.getUniqueAppletID()
-              + "_" + JS_PROPERTY_PREFIX + key);
-    }
-    return prop;
+    return getInstance().applicationProperties.getProperty(key);
   }
 
   /**
@@ -708,7 +696,7 @@ public class Cache
     try
     {
       oldValue = applicationProperties.setProperty(key, obj);
-      if (andSave && !propsAreReadOnly)
+      if (andSave && !propsAreReadOnly && propertiesFile != null)
       {
         FileOutputStream out = new FileOutputStream(propertiesFile);
         applicationProperties.store(out, "---JalviewX Properties File---");
@@ -1149,13 +1137,13 @@ public class Cache
     {
       if (coloursFound.toString().length() > 1)
       {
-        setProperty(UserDefinedColours.USER_DEFINED_COLOURS,
+        setProperty(Preferences.USER_DEFINED_COLOURS,
                 coloursFound.toString());
       }
       else
       {
         getInstance().applicationProperties
-                .remove(UserDefinedColours.USER_DEFINED_COLOURS);
+                .remove(Preferences.USER_DEFINED_COLOURS);
       }
     }
   }