JAL-1976 Added progress indicators for HTML_SVG and BioJS export operations
[jalview.git] / src / jalview / io / HtmlSvgOutput.java
index c8d6975..b53cacd 100644 (file)
@@ -1,3 +1,23 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.io;
 
 import jalview.api.AlignExportSettingI;
@@ -7,6 +27,7 @@ import jalview.datamodel.SequenceI;
 import jalview.gui.AlignViewport;
 import jalview.gui.AlignmentPanel;
 import jalview.gui.HTMLOptions;
+import jalview.gui.IProgressIndicator;
 import jalview.math.AlignmentDimension;
 import jalview.util.MessageManager;
 
@@ -26,8 +47,8 @@ public class HtmlSvgOutput
   AlignViewport av;
 
   FeatureRenderer fr;
-  AlignmentPanel ap;
 
+  AlignmentPanel ap;
 
   public HtmlSvgOutput(File file, AlignmentPanel ap)
   {
@@ -39,23 +60,43 @@ public class HtmlSvgOutput
 
   public void generateHtmlSvgOutput(File file)
   {
+    IProgressIndicator pIndicator = ap.alignFrame;
+    long pSessionId = System.currentTimeMillis();
     try
     {
+      boolean headless = (System.getProperty("java.awt.headless") != null && System
+              .getProperty("java.awt.headless").equals("true"));
       if (file == null)
       {
+        if (pIndicator != null && !headless)
+        {
+          pIndicator.setProgressBar(MessageManager.formatMessage(
+                  "status.waiting_for_user_to_select_output_file", "HTML"),
+                  pSessionId);
+        }
+        JalviewFileChooser chooser = getHTMLChooser();
+        chooser.setFileView(new jalview.io.JalviewFileView());
+        chooser.setDialogTitle(ap.alignFrame.getTitle());
+        chooser.setToolTipText(MessageManager.getString("action.save"));
+        int value = chooser.showSaveDialog(ap.alignFrame);
 
-      JalviewFileChooser chooser = getHTMLChooser();
-      chooser.setFileView(new jalview.io.JalviewFileView());
-      chooser.setDialogTitle(ap.alignFrame.getTitle());
-      chooser.setToolTipText(MessageManager.getString("action.save"));
-      int value = chooser.showSaveDialog(ap.alignFrame);
+        if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
+        {
+          jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
+                  .getSelectedFile().getParent());
+          file = chooser.getSelectedFile();
+        }
+        else
+        {
 
-      if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
-      {
-        jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
-                .getSelectedFile().getParent());
-        file = chooser.getSelectedFile();
-      }
+          if (pIndicator != null && !headless)
+        {
+            pIndicator.setProgressBar(MessageManager.formatMessage(
+                    "status.cancelled_image_export_operation", "HTML"),
+                    pSessionId);
+          }
+          return;
+        }
       }
 
       AlignmentDimension aDimension = ap.getAlignmentDimension();
@@ -99,7 +140,8 @@ public class HtmlSvgOutput
               .getDefault("EXPORT_EMBBED_BIOJSON", "true"));
       if (isEmbbedBioJSON)
       {
-        AlignExportSettingI exportSettings = new AlignExportSettingI(){
+        AlignExportSettingI exportSettings = new AlignExportSettingI()
+        {
           @Override
           public boolean isExportHiddenSequences()
           {
@@ -135,14 +177,15 @@ public class HtmlSvgOutput
           {
             return false;
           }
-          
+
         };
         AlignmentExportData exportData = jalview.gui.AlignFrame
                 .getAlignmentForExport(JSONFile.FILE_DESC, av,
                         exportSettings);
         jsonData = new FormatAdapter(ap, exportData.getSettings())
-              .formatSequences(JSONFile.FILE_DESC, exportData
-                      .getAlignment(), exportData.getOmitHidden(),
+                .formatSequences(JSONFile.FILE_DESC,
+                        exportData.getAlignment(),
+                        exportData.getOmitHidden(),
                         exportData.getStartEndPostions(),
                         av.getColumnSelection());
       }
@@ -154,20 +197,28 @@ public class HtmlSvgOutput
       if (!(System.getProperty("java.awt.headless") != null && System
               .getProperty("java.awt.headless").equals("true")))
       {
-      jalview.util.BrowserLauncher.openURL("file:///" + file);
+        jalview.util.BrowserLauncher.openURL("file:///" + file);
+      }
+      if (pIndicator != null && !headless)
+      {
+        pIndicator.setProgressBar(MessageManager.formatMessage(
+                "status.export_complete", "HTML"), pSessionId);
       }
     } catch (Exception e)
     {
       e.printStackTrace();
+      pIndicator.setProgressBar(MessageManager.formatMessage(
+              "info.error_creating_file", "HTML"), pSessionId);
     }
   }
-  
+
   static JalviewFileChooser getHTMLChooser()
   {
     return new jalview.io.JalviewFileChooser(
-            jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
-            { "html" }, new String[]
-            { "Hypertext Markup Language" }, "Hypertext Markup Language");
+            jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
+            new String[] { "html" },
+            new String[] { "Hypertext Markup Language" },
+            "Hypertext Markup Language");
   }
 
   public int printUnwrapped(int pwidth, int pheight, int pi, Graphics... pg)
@@ -266,9 +317,7 @@ public class HtmlSvgOutput
                 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
                 - 4;
       }
-      pg[0].drawString(
-              seq.getDisplayId(av.getShowJVSuffix()),
-              xPos,
+      pg[0].drawString(seq.getDisplayId(av.getShowJVSuffix()), xPos,
               (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
                       - (av.getCharHeight() / 5));
     }
@@ -302,7 +351,7 @@ public class HtmlSvgOutput
 
     return Printable.PAGE_EXISTS;
   }
-  
+
   private String getHtml(String titleSvg, String alignmentSvg,
           String jsonData)
   {
@@ -355,9 +404,7 @@ public class HtmlSvgOutput
     htmlSvg.append("<div class=\"align\" >");
     htmlSvg.append(
             "<div class=\"sub-category-container\"> <div style=\"overflow-x: scroll;\">")
-            .append(alignmentSvg)
-.append("</div></div>")
-            .append("</div>");
+            .append(alignmentSvg).append("</div></div>").append("</div>");
     htmlSvg.append("</div>");
 
     htmlSvg.append("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js\"></script>\n"
@@ -657,7 +704,7 @@ public class HtmlSvgOutput
     }
 
     htmlSvg.append("</script>\n");
-    htmlSvg.append("</hmtl>");
+    htmlSvg.append("</html>");
     return htmlSvg.toString();
   }
 }