From 26371ff5c38bbb092e432d5616313bc1088e87a2 Mon Sep 17 00:00:00 2001 From: tcofoegbu Date: Tue, 22 Nov 2016 12:48:20 +0000 Subject: [PATCH] JAL-2322 added runnable interface to HTMLOutput class, abstracted anonymous threads in child classes to the overriding run methods, and then other minor housekeeping --- src/jalview/io/BioJsHTMLOutput.java | 66 ++++++++----- src/jalview/io/HTMLOutput.java | 79 ++++++++-------- src/jalview/io/HtmlSvgOutput.java | 178 +++++++++++++++++------------------ 3 files changed, 170 insertions(+), 153 deletions(-) diff --git a/src/jalview/io/BioJsHTMLOutput.java b/src/jalview/io/BioJsHTMLOutput.java index 1be97f5..fd9c584 100644 --- a/src/jalview/io/BioJsHTMLOutput.java +++ b/src/jalview/io/BioJsHTMLOutput.java @@ -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(); + } + + } + } diff --git a/src/jalview/io/HTMLOutput.java b/src/jalview/io/HTMLOutput.java index a422a38..d58bd67 100755 --- a/src/jalview/io/HTMLOutput.java +++ b/src/jalview/io/HTMLOutput.java @@ -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; diff --git a/src/jalview/io/HtmlSvgOutput.java b/src/jalview/io/HtmlSvgOutput.java index e60824a..2fabe9a 100644 --- a/src/jalview/io/HtmlSvgOutput.java +++ b/src/jalview/io/HtmlSvgOutput.java @@ -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(""); htmlSvg.append("\n" + "\n"); - } // javascript for launching file in Jalview - htmlSvg.append("