Merge commit 'alpha/update_2_12_for_2_11_2_series_merge^2' into HEAD
[jalview.git] / src / jalview / bin / Cache.java
index 29f0d84..28bab15 100755 (executable)
@@ -52,6 +52,7 @@ import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
 import org.apache.log4j.SimpleLayout;
 
+import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
 import jalview.datamodel.PDBEntry;
 import jalview.gui.Preferences;
 import jalview.gui.UserDefinedColours;
@@ -220,8 +221,26 @@ import jalview.ws.sifts.SiftsSettings;
  * @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
+   * tied to the applet's ThreadGroup.
+   * 
+   * @return
+   */
+  public static Cache getInstance()
+  {
+    return (Cache) ApplicationSingletonProvider.getInstance(Cache.class);
+  }
+
   /**
    * property giving log4j level for CASTOR loggers
    */
@@ -240,10 +259,8 @@ public class Cache
   /**
    * Sifts settings
    */
-  public static final String DEFAULT_SIFTS_DOWNLOAD_DIR = System
-          .getProperty("user.home") + File.separatorChar
-          + ".sifts_downloads" + File.separatorChar;
-
+  public static final String DEFAULT_SIFTS_DOWNLOAD_DIR = Platform.getUserPath(".sifts_downloads/");
+  
   private final static String DEFAULT_CACHE_THRESHOLD_IN_DAYS = "2";
 
   private final static String DEFAULT_FAIL_SAFE_PID_THRESHOLD = "30";
@@ -251,8 +268,7 @@ public class Cache
   /**
    * Identifiers.org download settings
    */
-  private static final String ID_ORG_FILE = System.getProperty("user.home")
-          + File.separatorChar + ".identifiers.org.ids.json";
+  private static final String ID_ORG_FILE = Platform.getUserPath(".identifiers.org.ids.json");
 
   /**
    * Allowed values are PDB or mmCIF
@@ -300,7 +316,7 @@ public class Cache
   public static char[] proxyAuthPassword = null;
 
   /** Jalview Properties */
-  public static Properties applicationProperties = new Properties()
+  private Properties applicationProperties = new Properties()
   {
     // override results in properties output in alphabetical order
     @Override
@@ -375,18 +391,25 @@ public class Cache
    */
   public static void loadProperties(String propsFile)
   {
+
+    getInstance().loadPropertiesImpl(propsFile);
+
+  }
+
+  private void loadPropertiesImpl(String propsFile)
+  {
+
     propertiesFile = propsFile;
     String releasePropertiesFile = null;
     boolean defaultProperties = false;
     if (propsFile == null && !propsAreReadOnly)
     {
+      // TODO: @bsoares - for 2.12 testing: check test,develop,release props are located correctly
       String channelPrefsFilename = ChannelProperties
               .getProperty("preferences.filename");
       String releasePrefsFilename = ".jalview_properties";
-      propertiesFile = System.getProperty("user.home") + File.separatorChar
-              + channelPrefsFilename;
-      releasePropertiesFile = System.getProperty("user.home")
-              + File.separatorChar + releasePrefsFilename;
+      propertiesFile = Platform.getUserPath(channelPrefsFilename);
+      releasePropertiesFile = Platform.getUserPath(releasePrefsFilename);
       defaultProperties = true;
     }
     else
@@ -640,7 +663,7 @@ public class Cache
     return url;
   }
 
-  public static void loadBuildProperties(boolean reportVersion)
+  public void loadBuildProperties(boolean reportVersion)
   {
     String codeInstallation = getProperty("INSTALLATION");
     boolean printVersion = codeInstallation == null;
@@ -698,7 +721,7 @@ public class Cache
     }
   }
 
-  private static void deleteBuildProperties()
+  private void deleteBuildProperties()
   {
     applicationProperties.remove("LATEST_VERSION");
     applicationProperties.remove("VERSION");
@@ -719,12 +742,12 @@ public class Cache
    */
   public static String getProperty(String key)
   {
-    String prop = applicationProperties.getProperty(key);
-    if (prop == null && Platform.isJS())
-    {
-      prop = applicationProperties.getProperty(Platform.getUniqueAppletID()
-              + "_" + JS_PROPERTY_PREFIX + key);
-    }
+    String prop = getInstance().applicationProperties.getProperty(key);
+    // if (prop == null && Platform.isJS())
+    // {
+    // prop = applicationProperties.getProperty(Platform.getUniqueAppletID()
+    // + "_" + JS_PROPERTY_PREFIX + key);
+    // }
     return prop;
   }
 
@@ -784,33 +807,48 @@ public class Cache
    */
   public static Object setProperty(String key, String obj)
   {
-    Object oldValue = null;
-    try
-    {
-      oldValue = applicationProperties.setProperty(key, obj);
-      if (propertiesFile != null && !propsAreReadOnly)
-      {
-        FileOutputStream out = new FileOutputStream(propertiesFile);
-        applicationProperties.store(out, "---JalviewX Properties File---");
-        out.close();
-      }
-    } catch (Exception ex)
-    {
-      System.out.println(
-              "Error setting property: " + key + " " + obj + "\n" + ex);
-    }
-    return oldValue;
+    return getInstance().setPropertyImpl(key, obj, true);
+  }
+
+  /**
+   * Removes the specified property from the jalview properties file
+   * 
+   * @param key
+   */
+  public static void removeProperty(String key)
+  {
+    getInstance().removePropertyImpl(key, true);
   }
 
   /**
-   * remove the specified property from the jalview properties file
+   * Removes the named property for the running application, without saving the
+   * properties file
    * 
-   * @param string
+   * BH noting that ColourMenuHelper calls this. If the intent is to save, then
+   * simply chanet that call to removeProperty(key).
+   * 
+   * @param key
    */
-  public static void removeProperty(String string)
+  public static void removePropertyNoSave(String key)
   {
-    applicationProperties.remove(string);
-    saveProperties();
+
+    getInstance().
+
+            removePropertyImpl(key, false);
+  }
+
+  /**
+   * Removes the named property, and optionally saves the current properties to
+   * file
+   * 
+   * @param key
+   * @param andSave
+   */
+  private void removePropertyImpl(String key, boolean andSave)
+  {
+    applicationProperties.remove(key);
+    if (andSave)
+      saveProperties();
   }
 
   /**
@@ -818,6 +856,15 @@ public class Cache
    */
   public static void saveProperties()
   {
+    getInstance().savePropertiesImpl();
+  }
+
+  /**
+   * save the properties to the jalview properties path
+   */
+  private void savePropertiesImpl()
+
+  {
     if (!propsAreReadOnly)
     {
       try
@@ -1071,9 +1118,9 @@ public class Cache
    * @param property
    * @param colour
    */
-  public static void setColourProperty(String property, Color colour)
+  public static void setColourPropertyNoSave(String property, Color colour)
   {
-    setProperty(property, jalview.util.Format.getHexString(colour));
+    setPropertyNoSave(property, jalview.util.Format.getHexString(colour));
   }
 
   /**
@@ -1101,11 +1148,16 @@ public class Cache
   public static Date getDateProperty(String propertyName)
   {
     String val = getProperty(propertyName);
+    
     if (val != null)
     {
       try
       {
-        return date_format.parse(val);
+        if ((val = val.trim()).indexOf(",") < 0 && val.indexOf("-") >= 0 && val.indexOf(" ") == val.lastIndexOf(" ")) {
+          val = val.replace(" ",", ").replace('-',' ');
+        }
+        Date date =  date_format.parse(val);
+        return date;
       } catch (Exception ex)
       {
         System.err.println("Invalid or corrupt date in property '"
@@ -1154,11 +1206,11 @@ public class Cache
     }
     if (value == null || value.trim().length() < 1)
     {
-      Cache.applicationProperties.remove(propName);
+      getInstance().applicationProperties.remove(propName);
     }
     else
     {
-      Cache.applicationProperties.setProperty(propName, value);
+      getInstance().applicationProperties.setProperty(propName, value);
     }
   }
 
@@ -1208,7 +1260,7 @@ public class Cache
       }
       else
       {
-        applicationProperties
+        getInstance().applicationProperties
                 .remove(UserDefinedColours.USER_DEFINED_COLOURS);
       }
     }
@@ -1247,8 +1299,8 @@ public class Cache
             .append(" Installation: ");
     sb.append(jalview.bin.Cache.getDefault("INSTALLATION", "unknown"));
     sb.append("\n");
-    sb.append("Build Date: ");
-    sb.append(jalview.bin.Cache.getDefault("BUILD_DATE", "unknown"));
+    sb.append("Build Date: "
+            + jalview.bin.Cache.getDefault("BUILD_DATE", "unknown"));
     sb.append("\n");
     sb.append("Java version: ");
     sb.append(System.getProperty("java.version"));
@@ -1299,6 +1351,52 @@ public class Cache
     return jalview.bin.Cache.getDefault("INSTALLATION", "unknown");
   }
 
+  /**
+   * 
+   * For AppletParams and Preferences ok_actionPerformed and
+   * startupFileTextfield_mouseClicked
+   * 
+   * Sets a property value for the running application, without saving it to the
+   * properties file
+   * 
+   * @param key
+   * @param obj
+   */
+  public static void setPropertyNoSave(String key, String obj)
+  {
+    getInstance().setPropertyImpl(key, obj, false);
+  }
+
+  /**
+   * Sets a property value, and optionally also saves the current properties to
+   * file
+   * 
+   * @param key
+   * @param obj
+   * @param andSave
+   * @return
+   */
+  private Object setPropertyImpl(
+          String key, String obj, boolean andSave)
+  {
+    Object oldValue = null;
+    try
+    {
+      oldValue = applicationProperties.setProperty(key, obj);
+      if (andSave && !propsAreReadOnly && propertiesFile != null)
+      {
+        FileOutputStream out = new FileOutputStream(propertiesFile);
+        applicationProperties.store(out, "---JalviewX Properties File---");
+        out.close();
+      }
+    } catch (Exception ex)
+    {
+      System.out.println(
+              "Error setting property: " + key + " " + obj + "\n" + ex);
+    }
+    return oldValue;
+  }
+
   public static String getStackTraceString(Throwable t)
   {
     StringWriter sw = new StringWriter();