JAL-1756 bug-fix
authortcofoegbu <tcnofoegbu@dundee.ac.uk>
Thu, 4 Jun 2015 09:42:08 +0000 (10:42 +0100)
committertcofoegbu <tcnofoegbu@dundee.ac.uk>
Thu, 4 Jun 2015 09:42:08 +0000 (10:42 +0100)
src/jalview/io/BioJsHTMLOutput.java

index ff55400..b49c2e0 100644 (file)
@@ -29,16 +29,18 @@ public class BioJsHTMLOutput
 
   private static TreeMap<String, File> bioJsMSAVersions;
 
+  public static final String DEFAULT_DIR = System.getProperty("user.home")
+          + File.separatorChar + ".biojs_templates" + File.separatorChar;
+
   public static final String BJS_TEMPLATES_LOCAL_DIRECTORY = jalview.bin.Cache
-          .getDefault("biojs_template_directory", "/biojs_templates/");
+          .getDefault("biojs_template_directory", DEFAULT_DIR);
 
   public static final String BJS_TEMPLATE_GIT_REPO = jalview.bin.Cache
           .getDefault(
                   "biojs_template_git_repo",
                   "https://raw.githubusercontent.com/tcofoegbu/bjs-template/master/package.json");
 
-  public BioJsHTMLOutput(AlignmentPanel ap,
-          FeatureRenderer fr1)
+  public BioJsHTMLOutput(AlignmentPanel ap, FeatureRenderer fr1)
   {
     if (ap != null)
     {
@@ -149,8 +151,7 @@ public class BioJsHTMLOutput
   public void refreshBioJSVersionsInfo(String dirName)
           throws URISyntaxException
   {
-    URL url = getClass().getResource(dirName);
-    File directory = new File(url.toURI());
+    File directory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
     Objects.requireNonNull(dirName, "dirName MUST not be null!");
     Objects.requireNonNull(directory, "directory MUST not be null!");
     TreeMap<String, File> versionFileMap = new TreeMap<String, File>();
@@ -182,21 +183,29 @@ public class BioJsHTMLOutput
 
   public void updateBioJS()
   {
-    try
-    {
-      String gitRepoPkgJson = getURLContentAsString(BJS_TEMPLATE_GIT_REPO);
-      BioJSRepositoryPojo release = new BioJSRepositoryPojo(gitRepoPkgJson);
-      syncUpdates(BJS_TEMPLATES_LOCAL_DIRECTORY, release);
-      refreshBioJSVersionsInfo(BJS_TEMPLATES_LOCAL_DIRECTORY);
-    } catch (URISyntaxException e)
+    Thread updateThread = new Thread()
     {
-      e.printStackTrace();
-    }
+      public void run()
+      {
+        try
+        {
+          String gitRepoPkgJson = getURLContentAsString(BJS_TEMPLATE_GIT_REPO);
+          BioJSRepositoryPojo release = new BioJSRepositoryPojo(
+                  gitRepoPkgJson);
+          syncUpdates(BJS_TEMPLATES_LOCAL_DIRECTORY, release);
+          refreshBioJSVersionsInfo(BJS_TEMPLATES_LOCAL_DIRECTORY);
+        } catch (URISyntaxException e)
+        {
+          e.printStackTrace();
+        }
+      }
+    };
+    updateThread.start();
+
   }
 
 
   public void syncUpdates(String localDir, BioJSRepositoryPojo repo)
-          throws URISyntaxException
   {
     for (BioJSReleasePojo bjsRelease : repo.getReleases())
     {
@@ -207,23 +216,22 @@ public class BioJsHTMLOutput
       {
         releaseFile = "Latest_BioJsMSA_" + releaseVersion + ".txt";
       }
-      File file = null;
-      URL url = getClass().getResource(
-              BJS_TEMPLATES_LOCAL_DIRECTORY + releaseFile);
-      if (url == null)
-      {
-        String path = getClass().getResource(BJS_TEMPLATES_LOCAL_DIRECTORY)
-                .getPath();
-        System.out.println("------> " + path);
-        file = new File(path + releaseFile);
-      }
-      else
+
+      File biojsDirectory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
+      if (!biojsDirectory.exists())
       {
-        file = new File(url.toURI());
+        if (!biojsDirectory.mkdirs())
+        {
+          System.out.println("Couldn't create local directory : "
+                  + BJS_TEMPLATES_LOCAL_DIRECTORY);
+          return;
+        }
       }
 
+      File file = new File(BJS_TEMPLATES_LOCAL_DIRECTORY + releaseFile);
       if (!file.exists())
       {
+
         PrintWriter out = null;
         try
         {