JAL-2322 added runnable interface to HTMLOutput class, abstracted anonymous threads...
authortcofoegbu <tcnofoegbu@dundee.ac.uk>
Tue, 22 Nov 2016 12:48:20 +0000 (12:48 +0000)
committertcofoegbu <tcnofoegbu@dundee.ac.uk>
Tue, 22 Nov 2016 12:48:20 +0000 (12:48 +0000)
src/jalview/io/BioJsHTMLOutput.java
src/jalview/io/HTMLOutput.java
src/jalview/io/HtmlSvgOutput.java

index 1be97f5..fd9c584 100644 (file)
@@ -68,41 +68,25 @@ public class BioJsHTMLOutput extends HTMLOutput
     exportStarted();
     try
     {
-
       if (outputFile == null)
       {
         outputFile = getOutputFile();
       }
       generatedFile = new File(outputFile);
-
-      String bioJSON = getBioJSONData();
-      String bioJSTemplateString = HTMLOutput.readFileAsString(getCurrentBJSTemplateFile());
-      String generatedBioJsWithJalviewAlignmentAsJson = bioJSTemplateString
-              .replaceAll("#sequenceData#", bioJSON).toString();
-
-      PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(
-              generatedFile));
-      out.print(generatedBioJsWithJalviewAlignmentAsJson);
-      out.flush();
-      out.close();
-      exportCompleted();
-      setProgressMessage(MessageManager.formatMessage(
-              "status.export_complete", "BioJS"));
-
-    } catch (NoFileSelectedException ex)
-    {
-      // do noting if no file was selected
-    } catch (OutOfMemoryError err)
+    } catch (NoFileSelectedException e)
     {
-      System.out.println("########################\n" + "OUT OF MEMORY "
-              + outputFile + "\n" + "########################");
-      new OOMWarning("Creating Image for " + outputFile, err);
+      setProgressMessage(MessageManager.formatMessage(
+              "status.cancelled_image_export_operation", "BioJS MSA"));
+      return;
     } catch (Exception e)
     {
       setProgressMessage(MessageManager.formatMessage(
-              "info.error_creating_file", "HTML"));
+              "info.error_creating_file", "BioJS MSA"));
       e.printStackTrace();
+      return;
     }
+    new Thread(this).start();
+
   }
 
 
@@ -294,4 +278,38 @@ public class BioJsHTMLOutput extends HTMLOutput
     return generatedFile;
   }
 
+  @Override
+  public void run()
+  {
+    try
+    {
+      String bioJSON = getBioJSONData();
+      String bioJSTemplateString = HTMLOutput
+              .readFileAsString(getCurrentBJSTemplateFile());
+      String generatedBioJsWithJalviewAlignmentAsJson = bioJSTemplateString
+              .replaceAll("#sequenceData#", bioJSON).toString();
+
+      PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(
+              generatedFile));
+      out.print(generatedBioJsWithJalviewAlignmentAsJson);
+      out.flush();
+      out.close();
+      setProgressMessage(MessageManager.formatMessage(
+              "status.export_complete", "BioJS"));
+      exportCompleted();
+
+    } catch (OutOfMemoryError err)
+    {
+      System.out.println("########################\n" + "OUT OF MEMORY "
+              + generatedFile + "\n" + "########################");
+      new OOMWarning("Creating Image for " + generatedFile, err);
+    } catch (Exception e)
+    {
+      setProgressMessage(MessageManager.formatMessage(
+              "info.error_creating_file", "HTML"));
+      e.printStackTrace();
+    }
+
+  }
+
 }
index a422a38..d58bd67 100755 (executable)
@@ -35,7 +35,7 @@ import java.net.URL;
 import java.util.Objects;
 
 
-public abstract class HTMLOutput
+public abstract class HTMLOutput implements Runnable
 {
   protected AlignmentPanel ap;
 
@@ -54,52 +54,58 @@ public abstract class HTMLOutput
     }
   }
 
-
   public String getBioJSONData()
   {
+    return getBioJSONData(null);
+  }
+
+  public String getBioJSONData(AlignExportSettingI exportSettings)
+  {
     if (!isEmbedData())
     {
       return null;
     }
-    AlignExportSettingI exportSettings = new AlignExportSettingI()
+    if (exportSettings == null)
     {
-      @Override
-      public boolean isExportHiddenSequences()
+      exportSettings = new AlignExportSettingI()
       {
-        return true;
-      }
-
-      @Override
-      public boolean isExportHiddenColumns()
-      {
-        return true;
-      }
+        @Override
+        public boolean isExportHiddenSequences()
+        {
+          return true;
+        }
 
-      @Override
-      public boolean isExportAnnotations()
-      {
-        return true;
-      }
+        @Override
+        public boolean isExportHiddenColumns()
+        {
+          return true;
+        }
 
-      @Override
-      public boolean isExportFeatures()
-      {
-        return true;
-      }
+        @Override
+        public boolean isExportAnnotations()
+        {
+          return true;
+        }
 
-      @Override
-      public boolean isExportGroups()
-      {
-        return true;
-      }
+        @Override
+        public boolean isExportFeatures()
+        {
+          return true;
+        }
 
-      @Override
-      public boolean isCancelled()
-      {
-        return false;
-      }
+        @Override
+        public boolean isExportGroups()
+        {
+          return true;
+        }
 
-    };
+        @Override
+        public boolean isCancelled()
+        {
+          return false;
+        }
+      };
+    }
     AlignmentExportData exportData = jalview.gui.AlignFrame
             .getAlignmentForExport(JSONFile.FILE_DESC,
                     ap.getAlignViewport(), exportSettings);
@@ -264,7 +270,7 @@ public abstract class HTMLOutput
     jvFileChooser.setFileView(new JalviewFileView());
 
     jvFileChooser.setDialogTitle(MessageManager
-            .getString("label.save_as_biojs_html"));
+            .getString("label.save_as_html"));
     jvFileChooser.setToolTipText(MessageManager.getString("action.save"));
 
     int fileChooserOpt = jvFileChooser.showSaveDialog(null);
@@ -276,9 +282,6 @@ public abstract class HTMLOutput
     }
     else
     {
-      pIndicator.setProgressBar(MessageManager.formatMessage(
-              "status.cancelled_image_export_operation", "BioJS"),
-              pSessionId);
       throw new NoFileSelectedException("No file was selected.");
     }
     return selectedFile;
index e60824a..2fabe9a 100644 (file)
@@ -20,6 +20,7 @@
  */
 package jalview.io;
 
+import jalview.exceptions.NoFileSelectedException;
 import jalview.gui.AlignmentPanel;
 import jalview.gui.HTMLOptions;
 import jalview.gui.OOMWarning;
@@ -38,7 +39,6 @@ import org.jfree.graphics2d.svg.SVGHints;
 public class HtmlSvgOutput extends HTMLOutput
 {
 
-  private File generatedFile;
 
   public HtmlSvgOutput(AlignmentPanel ap)
   {
@@ -46,16 +46,21 @@ public class HtmlSvgOutput extends HTMLOutput
   }
 
   @Override
-  public void exportHTML(String file)
+  public void exportHTML(String outputFile)
   {
     exportStarted();
     try
     {
-      if (file == null)
+      if (outputFile == null)
       {
-        file = getOutputFile();
+        outputFile = getOutputFile();
       }
-      generatedFile = new File(file);
+      generatedFile = new File(outputFile);
+    } catch (NoFileSelectedException e)
+    {
+      setProgressMessage(MessageManager.formatMessage(
+              "status.cancelled_image_export_operation", "HTML"));
+      return;
     } catch (Exception e)
     {
       setProgressMessage(MessageManager.formatMessage(
@@ -63,89 +68,7 @@ public class HtmlSvgOutput extends HTMLOutput
       e.printStackTrace();
       return;
     }
-    new Thread()
-    {
-      @Override
-      public void run()
-      {
-        try
-        {
-          setProgressMessage(null);
-          setProgressMessage(MessageManager.formatMessage(
-                  "status.exporting_alignment_as_x_file", "HTML"));
-          AlignmentDimension aDimension = ap.getAlignmentDimension();
-          SVGGraphics2D idPanelGraphics = new SVGGraphics2D(
-                  aDimension.getWidth(),
-                  aDimension.getHeight());
-          SVGGraphics2D alignPanelGraphics = new SVGGraphics2D(
-                  aDimension.getWidth(),
-                  aDimension.getHeight());
-
-          String renderStyle = jalview.bin.Cache.getDefault(
-                  "HTML_RENDERING", "Prompt each time");
-
-          // If we need to prompt, and if the GUI is visible then
-          // Prompt for rendering style
-          if (renderStyle.equalsIgnoreCase("Prompt each time")
-                  && !isHeadless())
-          {
-            HTMLOptions svgOption = new HTMLOptions();
-            renderStyle = svgOption.getValue();
-
-            if (renderStyle == null || svgOption.cancelled)
-            {
-              setProgressMessage(MessageManager.formatMessage(
-                      "status.cancelled_image_export_operation", "HTML"));
-              return;
-            }
-          }
-
-          if (renderStyle.equalsIgnoreCase("Lineart"))
-          {
-            idPanelGraphics.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
-                    SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
-            alignPanelGraphics.setRenderingHint(
-                    SVGHints.KEY_DRAW_STRING_TYPE,
-                    SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
-          }
-          if (ap.av.getWrapAlignment())
-          {
-            printWrapped(aDimension.getWidth(), aDimension.getHeight(), 0,
-                    alignPanelGraphics);
-          }
-          else
-          {
-          printUnwrapped(aDimension.getWidth(), aDimension.getHeight(), 0,
- idPanelGraphics, alignPanelGraphics);
-          }
-
-          String idPanelSvgData = idPanelGraphics.getSVGDocument();
-          String alignPanelSvgData = alignPanelGraphics.getSVGDocument();
-          String jsonData = getBioJSONData();
-          String htmlData = getHtml(idPanelSvgData, alignPanelSvgData, jsonData,
- ap.av.getWrapAlignment());
-          FileOutputStream out = new FileOutputStream(generatedFile);
-          out.write(htmlData.getBytes());
-          out.flush();
-          out.close();
-          exportCompleted();
-        } catch (OutOfMemoryError err)
-        {
-          System.out.println("########################\n"
-                  + "OUT OF MEMORY " + generatedFile + "\n"
-                  + "########################");
-          new OOMWarning("Creating Image for " + generatedFile, err);
-        } catch (Exception e)
-        {
-          e.printStackTrace();
-          setProgressMessage(MessageManager.formatMessage(
-                  "info.error_creating_file", "HTML"));
-        }
-        setProgressMessage(MessageManager.formatMessage(
-                "status.export_complete", "HTML"));
-      }
-    }.start();
-
+    new Thread(this).start();
   }
 
 
@@ -241,11 +164,9 @@ public class HtmlSvgOutput extends HTMLOutput
               .append(alignmentSvg).append("</div>");
       htmlSvg.append("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js\"></script>\n"
               + "<script language=\"JavaScript\" type=\"text/javascript\"  src=\"http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js\"></script>\n");
-
     }
 
     // javascript for launching file in Jalview
-
     htmlSvg.append("<script language=\"JavaScript\">\n");
     htmlSvg.append("function openJalviewUsingCurrentUrl(){\n");
     htmlSvg.append("    var json = JSON.parse(document.getElementById(\"seqData\").innerHTML);\n");
@@ -270,9 +191,9 @@ public class HtmlSvgOutput extends HTMLOutput
     htmlSvg.append("    document.body.removeChild(myForm);\n");
     htmlSvg.append("}\n");
 
-    // jquery facebox for displaying raw BioJSON data");
     if (jsonData != null)
     {
+      // JQuery FaceBox for displaying raw BioJSON data");
       File faceBoxJsFile = new File("examples/javascript/facebox-1.3.js");
       try
       {
@@ -306,4 +227,79 @@ public class HtmlSvgOutput extends HTMLOutput
   {
     return generatedFile;
   }
+
+  @Override
+  public void run()
+  {
+    try
+    {
+      setProgressMessage(null);
+      setProgressMessage(MessageManager.formatMessage(
+              "status.exporting_alignment_as_x_file", "HTML"));
+      AlignmentDimension aDimension = ap.getAlignmentDimension();
+      SVGGraphics2D idPanelGraphics = new SVGGraphics2D(
+              aDimension.getWidth(), aDimension.getHeight());
+      SVGGraphics2D alignPanelGraphics = new SVGGraphics2D(
+              aDimension.getWidth(), aDimension.getHeight());
+
+      String renderStyle = jalview.bin.Cache.getDefault("HTML_RENDERING",
+              "Prompt each time");
+
+      // If we need to prompt, and if the GUI is visible then
+      // Prompt for rendering style
+      if (renderStyle.equalsIgnoreCase("Prompt each time") && !isHeadless())
+      {
+        HTMLOptions svgOption = new HTMLOptions();
+        renderStyle = svgOption.getValue();
+
+        if (renderStyle == null || svgOption.cancelled)
+        {
+          setProgressMessage(MessageManager.formatMessage(
+                  "status.cancelled_image_export_operation", "HTML"));
+          return;
+        }
+      }
+
+      if (renderStyle.equalsIgnoreCase("Lineart"))
+      {
+        idPanelGraphics.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
+                SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
+        alignPanelGraphics.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
+                SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
+      }
+      if (ap.av.getWrapAlignment())
+      {
+        printWrapped(aDimension.getWidth(), aDimension.getHeight(), 0,
+                alignPanelGraphics);
+      }
+      else
+      {
+        printUnwrapped(aDimension.getWidth(), aDimension.getHeight(), 0,
+                idPanelGraphics, alignPanelGraphics);
+      }
+
+      String idPanelSvgData = idPanelGraphics.getSVGDocument();
+      String alignPanelSvgData = alignPanelGraphics.getSVGDocument();
+      String jsonData = getBioJSONData();
+      String htmlData = getHtml(idPanelSvgData, alignPanelSvgData,
+              jsonData, ap.av.getWrapAlignment());
+      FileOutputStream out = new FileOutputStream(generatedFile);
+      out.write(htmlData.getBytes());
+      out.flush();
+      out.close();
+      setProgressMessage(MessageManager.formatMessage(
+              "status.export_complete", "HTML"));
+      exportCompleted();
+    } catch (OutOfMemoryError err)
+    {
+      System.out.println("########################\n" + "OUT OF MEMORY "
+              + generatedFile + "\n" + "########################");
+      new OOMWarning("Creating Image for " + generatedFile, err);
+    } catch (Exception e)
+    {
+      e.printStackTrace();
+      setProgressMessage(MessageManager.formatMessage(
+              "info.error_creating_file", "HTML"));
+    }
+  }
 }