X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FHtmlSvgOutput.java;h=2fabe9a66c8b76d829bf137c8c073384e03cef0a;hb=26371ff5c38bbb092e432d5616313bc1088e87a2;hp=64dfc1ce8979e0f618845286730c471d11ef5914;hpb=581e7e5a0736b3b5e5ade54656c194b399b2a3ec;p=jalview.git
diff --git a/src/jalview/io/HtmlSvgOutput.java b/src/jalview/io/HtmlSvgOutput.java
index 64dfc1c..2fabe9a 100644
--- a/src/jalview/io/HtmlSvgOutput.java
+++ b/src/jalview/io/HtmlSvgOutput.java
@@ -1,342 +1,305 @@
+/*
+ * 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 .
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
package jalview.io;
-import jalview.datamodel.Alignment;
-import jalview.datamodel.AlignmentAnnotation;
-import jalview.datamodel.SequenceI;
-import jalview.gui.AlignViewport;
+import jalview.exceptions.NoFileSelectedException;
import jalview.gui.AlignmentPanel;
-import jalview.gui.AnnotationPanel;
-import jalview.gui.Desktop;
-import jalview.gui.FeatureRenderer;
import jalview.gui.HTMLOptions;
+import jalview.gui.OOMWarning;
import jalview.math.AlignmentDimension;
import jalview.util.MessageManager;
-import java.awt.Color;
-import java.awt.FontMetrics;
import java.awt.Graphics;
-import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.io.File;
import java.io.FileOutputStream;
+import java.io.IOException;
import org.jfree.graphics2d.svg.SVGGraphics2D;
import org.jfree.graphics2d.svg.SVGHints;
-public class HtmlSvgOutput
+public class HtmlSvgOutput extends HTMLOutput
{
- AlignViewport av;
- FeatureRenderer fr;
- AlignmentPanel ap;
- AnnotationPanel annotationPanel;
-
- public HtmlSvgOutput(File file, AlignmentPanel ap)
+ public HtmlSvgOutput(AlignmentPanel ap)
{
-
- this.av = ap.av;
- this.ap = ap;
- this.annotationPanel = ap.getAnnotationPanel();
- generateHtmlSvgOutput(file);
+ super(ap);
}
- public void generateHtmlSvgOutput(File file)
+ @Override
+ public void exportHTML(String outputFile)
{
+ exportStarted();
try
{
- if (file == null /*
- * && !(System.getProperty("java.awt.headless") != null
- * && System
- * .getProperty("java.awt.headless").equals("true"))
- */)
- {
-
- 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();
- }
- }
-
- AlignmentDimension aDimension = ap.getAlignmentDimension();
- SVGGraphics2D g1 = new SVGGraphics2D(aDimension.getWidth(),
- aDimension.getHeight());
- SVGGraphics2D g2 = 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")
- && !(System.getProperty("java.awt.headless") != null && System
- .getProperty("java.awt.headless").equals("true")))
- {
- HTMLOptions svgOption = new HTMLOptions();
- renderStyle = svgOption.getValue();
-
- if (renderStyle == null || svgOption.cancelled)
- {
- return;
- }
- }
-
- if (renderStyle.equalsIgnoreCase("lineart"))
- {
- g1.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
- SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
- g2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
- SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
- }
- printUnwrapped(aDimension.getWidth(), aDimension.getHeight(), 0, g1,
- g2);
- FileOutputStream out = new FileOutputStream(file);
-
- String titleSvgData = g1.getSVGDocument();
- String alignSvgData = g2.getSVGDocument();
- String jsonData = getJSONData();
- String htmlData = getHtml(titleSvgData, alignSvgData, jsonData);
-
- out.write(htmlData.getBytes());
- out.flush();
- out.close();
- if (!(System.getProperty("java.awt.headless") != null && System
- .getProperty("java.awt.headless").equals("true")))
+ if (outputFile == null)
{
- jalview.util.BrowserLauncher.openURL("file:///" + file);
+ outputFile = getOutputFile();
}
+ generatedFile = new File(outputFile);
+ } catch (NoFileSelectedException e)
+ {
+ setProgressMessage(MessageManager.formatMessage(
+ "status.cancelled_image_export_operation", "HTML"));
+ return;
} catch (Exception e)
{
+ setProgressMessage(MessageManager.formatMessage(
+ "info.error_creating_file", "HTML"));
e.printStackTrace();
+ return;
}
+ new Thread(this).start();
}
-
+
+
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)
throws PrinterException
{
- int idWidth = ap.getVisibleIdWidth(false);
- FontMetrics fm = ap.getFontMetrics(av.getFont());
- int scaleHeight = av.getCharHeight() + fm.getDescent();
-
- pg[0].setColor(Color.white);
- pg[0].fillRect(0, 0, pwidth, pheight);
- pg[0].setFont(av.getFont());
-
- // //////////////////////////////////
- // / How many sequences and residues can we fit on a printable page?
- int totalRes = (pwidth - idWidth) / av.getCharWidth();
- int totalSeq = (pheight - scaleHeight) / av.getCharHeight() - 1;
- int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1;
-
- // ///////////////////////////
- // / Only print these sequences and residues on this page
- int startRes;
-
- // ///////////////////////////
- // / Only print these sequences and residues on this page
- int endRes;
-
- // ///////////////////////////
- // / Only print these sequences and residues on this page
- int startSeq;
+ return ap.printUnwrapped(pwidth, pheight, pi, pg);
+ }
- // ///////////////////////////
- // / Only print these sequences and residues on this page
- int endSeq;
- startRes = (pi % pagesWide) * totalRes;
- endRes = (startRes + totalRes) - 1;
+ public int printWrapped(int pwidth, int pheight, int pi, Graphics... pg)
+ throws PrinterException
+ {
+ return ap.printWrappedAlignment(pwidth, pheight, pi, pg[0]);
+ }
- if (endRes > (av.getAlignment().getWidth() - 1))
+ private String getHtml(String titleSvg, String alignmentSvg,
+ String jsonData, boolean wrapped)
+ {
+ StringBuilder htmlSvg = new StringBuilder();
+ htmlSvg.append("\n");
+ if (jsonData != null)
{
- endRes = av.getAlignment().getWidth() - 1;
+ htmlSvg.append(" ");
+ htmlSvg.append("");
+ htmlSvg.append("
"
+ + jsonData + "
");
+ htmlSvg.append("
");
}
- startSeq = (pi / pagesWide) * totalSeq;
- endSeq = startSeq + totalSeq;
- if (endSeq > av.getAlignment().getHeight())
+ htmlSvg.append("\n");
+ if (!wrapped)
{
- pagesHigh += ap.getAnnotationPanel().adjustPanelHeight() + 3;
+ htmlSvg.append("");
+ htmlSvg.append("
\n");
+ htmlSvg.append("
\n");
+ htmlSvg.append(titleSvg);
+ htmlSvg.append("
");
+ htmlSvg.append("
\n\n\n\n");
+ htmlSvg.append("
");
+ htmlSvg.append(
+ "
")
+ .append(alignmentSvg).append("
").append("
");
+ htmlSvg.append("
");
+
+ htmlSvg.append("\n"
+ + "\n"
+ + "\n");
}
- pagesHigh /= pheight;
- if (pi >= (pagesWide * pagesHigh))
+ else
{
- return Printable.NO_SUCH_PAGE;
+ htmlSvg.append("\n")
+ .append(alignmentSvg).append("
");
+ htmlSvg.append("\n"
+ + "\n");
}
- // draw Scale
- pg[1].translate(0, 0);
- ap.getScalePanel().drawScale(pg[1], startRes, endRes, pwidth - idWidth,
- scaleHeight);
- pg[1].translate(-idWidth, scaleHeight);
+ // javascript for launching file in Jalview
+ htmlSvg.append("\n");
+ htmlSvg.append("");
+ return htmlSvg.toString();
+ }
- // draw annotation - need to offset for current scroll position
- pg[1].translate(0, offset);
- pg[1].translate(-idWidth - 3,
- (endSeq - startSeq) * av.getCharHeight() + 3);
- pg[1].translate(idWidth + 3, 0);
- ap.getAnnotationPanel().renderer.drawComponent(
- ap.getAnnotationPanel(), av, pg[1], -1, startRes, endRes + 1);
- pg[1].translate(0, -offset);
- }
+ @Override
+ public boolean isEmbedData()
+ {
+ return Boolean.valueOf(jalview.bin.Cache.getDefault(
+ "EXPORT_EMBBED_BIOJSON", "true"));
+ }
- return Printable.PAGE_EXISTS;
+ @Override
+ public boolean isLaunchInBrowserAfterExport()
+ {
+ return true;
}
-
- private String getJSONData()
+
+ @Override
+ public File getExportedFile()
{
- JSONFile jsonFile = new JSONFile();
- Alignment al = new Alignment(av.getAlignment().getSequencesArray());
- jsonFile.seqGroups = av.getAlignment().getGroups();
- // jsonFile.addAnnotations(al);
- jsonFile.setDisplayedFeatures(av.getFeaturesDisplayed());
- Desktop.setCurrentAlignFrame(av.getAlignPanel().alignFrame);
- for (SequenceI seq : av.getAlignment().getSequences())
- {
- jsonFile.seqs.add(seq);
- }
+ return generatedFile;
+ }
- // Add non auto calculated annotation to AlignFile
- for (AlignmentAnnotation annot : av.getAlignment()
- .getAlignmentAnnotation())
+ @Override
+ public void run()
+ {
+ try
{
- if (annot != null && !annot.autoCalculated)
+ 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())
{
- if (annot.label.equals("PDB.CATempFactor"))
+ HTMLOptions svgOption = new HTMLOptions();
+ renderStyle = svgOption.getValue();
+
+ if (renderStyle == null || svgOption.cancelled)
{
- continue;
+ setProgressMessage(MessageManager.formatMessage(
+ "status.cancelled_image_export_operation", "HTML"));
+ return;
}
- jsonFile.annotations.add(annot);
}
- }
- // jsonFile.setGlobalColorScheme(av.getGlobalColourScheme());
- String jsonString = jsonFile.print();
- System.out.println(">>>>>>>>>>> JSONFile : " + jsonString);
- return jsonString;
- }
+ 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);
+ }
- private String getHtml(String titleSvg, String alignmentSvg,
- String jsonData)
- {
- StringBuilder htmlSvg = new StringBuilder();
- htmlSvg.append("\n");
- if (jsonData != null)
+ 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)
{
- htmlSvg.append("");
+ e.printStackTrace();
+ setProgressMessage(MessageManager.formatMessage(
+ "info.error_creating_file", "HTML"));
}
- htmlSvg.append("\n");
- htmlSvg.append("");
- htmlSvg.append(
-"
");
- htmlSvg.append(
-"
")
- .append(titleSvg)
- .append("
")
- .append("
\n\n\n\n");
- htmlSvg.append(
-"
");
- htmlSvg.append(
- "
")
- .append(alignmentSvg)
-.append("
")
- .append("
");
- htmlSvg.append("
");
-
- htmlSvg.append("\n"
- + "\n"
- + "");
-
- return htmlSvg.toString();
}
}