house keeping
[jalview.git] / src / jalview / io / BioJsHTMLOutput.java
index ff55400..73be79f 100644 (file)
@@ -1,9 +1,8 @@
 package jalview.io;
 
+import jalview.api.AlignmentViewPanel;
+import jalview.datamodel.AlignmentExportData;
 import jalview.exceptions.NoFileSelectedException;
-import jalview.gui.AlignViewport;
-import jalview.gui.AlignmentPanel;
-import jalview.gui.FeatureRenderer;
 import jalview.json.binding.v1.BioJSReleasePojo;
 import jalview.json.binding.v1.BioJSRepositoryPojo;
 import jalview.util.MessageManager;
@@ -23,27 +22,28 @@ import java.util.TreeMap;
 
 public class BioJsHTMLOutput
 {
-  private AlignViewport av;
+  private AlignmentViewPanel ap;
 
   private static File currentBJSTemplateFile;
 
   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(AlignmentViewPanel ap)
   {
     if (ap != null)
     {
-      this.av = ap.av;
-      av.setFeatureRenderer(new FeatureRenderer(ap));
+      this.ap = ap;
     }
   }
 
@@ -52,7 +52,20 @@ public class BioJsHTMLOutput
     try
     {
       String outputFile = getOutputFile();
-      String jalviewAlignmentJson = JSONFile.getJSONData(av);
+      // String jalviewAlignmentJson = JSONFile.getJSONData(ap);
+      AlignmentExportData exportData = jalview.gui.AlignFrame
+              .getAlignmentForExport(
+              JSONFile.FILE_DESC, ap.getAlignViewport());
+      if (exportData.getSettings().isCancelled())
+      {
+        return;
+      }
+      String jalviewAlignmentJson = new FormatAdapter(ap,
+              exportData.getSettings()).formatSequences(JSONFile.FILE_DESC,
+              exportData.getAlignment(), exportData.getOmitHidden(),
+              exportData.getStartEndPostions(), ap.getAlignViewport()
+                      .getColumnSelection());
+
       String bioJSTemplateString = getBioJsTemplateAsString();
       String generatedBioJsWithJalviewAlignmentAsJson = bioJSTemplateString
               .replaceAll(
@@ -83,7 +96,6 @@ public class BioJsHTMLOutput
             { "HTML files" }, "HTML files");
     jvFileChooser.setFileView(new JalviewFileView());
 
-    // TODO uncomment when supported by MassageManager
     jvFileChooser.setDialogTitle(MessageManager
             .getString("label.save_as_biojs_html"));
     jvFileChooser.setDialogTitle("save as BioJs HTML");
@@ -146,11 +158,10 @@ public class BioJsHTMLOutput
     return sb.toString();
   }
 
-  public void refreshBioJSVersionsInfo(String dirName)
+  public static 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>();
@@ -180,23 +191,34 @@ public class BioJsHTMLOutput
     setBioJsMSAVersions(versionFileMap);
   }
 
-  public void updateBioJS()
+  public static 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);
+          if (gitRepoPkgJson != null)
+          {
+            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
+  public static void syncUpdates(String localDir, BioJSRepositoryPojo repo)
   {
     for (BioJSReleasePojo bjsRelease : repo.getReleases())
     {
@@ -207,23 +229,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
         {