2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.exceptions.NoFileSelectedException;
24 import jalview.gui.AlignmentPanel;
25 import jalview.gui.HTMLOptions;
26 import jalview.gui.OOMWarning;
27 import jalview.math.AlignmentDimension;
28 import jalview.util.MessageManager;
30 import java.awt.Graphics;
31 import java.awt.print.PrinterException;
33 import java.io.FileOutputStream;
34 import java.io.IOException;
36 import org.jfree.graphics2d.svg.SVGGraphics2D;
37 import org.jfree.graphics2d.svg.SVGHints;
39 public class HtmlSvgOutput extends HTMLOutput
43 public HtmlSvgOutput(AlignmentPanel ap)
49 public void exportHTML(String outputFile)
54 if (outputFile == null)
56 outputFile = getOutputFile();
58 generatedFile = new File(outputFile);
59 } catch (NoFileSelectedException e)
61 setProgressMessage(MessageManager.formatMessage(
62 "status.cancelled_image_export_operation", "HTML"));
66 setProgressMessage(MessageManager.formatMessage(
67 "info.error_creating_file", "HTML"));
71 new Thread(this).start();
75 static JalviewFileChooser getHTMLChooser()
77 return new jalview.io.JalviewFileChooser(
78 jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
79 new String[] { "html" },
80 new String[] { "Hypertext Markup Language" },
81 "Hypertext Markup Language");
84 public int printUnwrapped(int pwidth, int pheight, int pi, Graphics... pg)
85 throws PrinterException
87 return ap.printUnwrapped(pwidth, pheight, pi, pg);
90 public int printWrapped(int pwidth, int pheight, int pi, Graphics... pg)
91 throws PrinterException
93 return ap.printWrappedAlignment(pwidth, pheight, pi, pg[0]);
96 private String getHtml(String titleSvg, String alignmentSvg,
97 String jsonData, boolean wrapped)
99 StringBuilder htmlSvg = new StringBuilder();
100 htmlSvg.append("<html>\n");
101 if (jsonData != null)
103 htmlSvg.append("<button onclick=\"javascipt:openJalviewUsingCurrentUrl();\">Launch in Jalview</button> ");
104 htmlSvg.append("<input type=\"submit\" value=\"View raw BioJSON Data\" onclick=\"jQuery.facebox({ div:'#seqData' }); return false;\" />");
105 htmlSvg.append("<div style=\"display: none;\" name=\"seqData\" id=\"seqData\" >"
106 + jsonData + "</div>");
107 htmlSvg.append("<br/> ");
109 htmlSvg.append("\n<style type=\"text/css\"> "
110 + "div.parent{ width:100%;<!-- overflow: auto; -->}\n"
111 + "div.titlex{ width:11%; float: left; }\n"
112 + "div.align{ width:89%; float: right; }\n"
113 + "div.main-container{ border: 2px solid blue; border: 2px solid blue; width: 99%; min-height: 99%; }\n"
114 + ".sub-category-container {overflow-y: scroll; overflow-x: hidden; width: 100%; height: 100%;}\n"
115 + "object {pointer-events: none;}");
116 if (jsonData != null)
118 // facebox style sheet for displaying raw BioJSON data
119 htmlSvg.append("#facebox { position: absolute; top: 0; left: 0; z-index: 100; text-align: left; }\n"
120 + "#facebox .popup{ position:relative; border:3px solid rgba(0,0,0,0); -webkit-border-radius:5px;"
121 + "-moz-border-radius:5px; border-radius:5px; -webkit-box-shadow:0 0 18px rgba(0,0,0,0.4); -moz-box-shadow:0 0 18px rgba(0,0,0,0.4);"
122 + "box-shadow:0 0 18px rgba(0,0,0,0.4); }\n"
123 + "#facebox .content { display:table; width: 98%; padding: 10px; background: #fff; -webkit-border-radius:4px; -moz-border-radius:4px;"
124 + " border-radius:4px; }\n"
125 + "#facebox .content > p:first-child{ margin-top:0; }\n"
126 + "#facebox .content > p:last-child{ margin-bottom:0; }\n"
127 + "#facebox .close{ position:absolute; top:5px; right:5px; padding:2px; background:#fff; }\n"
128 + "#facebox .close img{ opacity:0.3; }\n"
129 + "#facebox .close:hover img{ opacity:1.0; }\n"
130 + "#facebox .loading { text-align: center; }\n"
131 + "#facebox .image { text-align: center;}\n"
132 + "#facebox img { border: 0; margin: 0; }\n"
133 + "#facebox_overlay { position: fixed; top: 0px; left: 0px; height:100%; width:100%; }\n"
134 + ".facebox_hide { z-index:-100; }\n"
135 + ".facebox_overlayBG { background-color: #000; z-index: 99; }");
137 htmlSvg.append("</style>");
140 htmlSvg.append("<div class=\"main-container\" \n>");
141 htmlSvg.append("<div class=\"titlex\">\n");
142 htmlSvg.append("<div class=\"sub-category-container\"> \n");
143 htmlSvg.append(titleSvg);
144 htmlSvg.append("</div>");
145 htmlSvg.append("</div>\n\n<!-- ========================================================================================== -->\n\n");
146 htmlSvg.append("<div class=\"align\" >");
148 "<div class=\"sub-category-container\"> <div style=\"overflow-x: scroll;\">")
149 .append(alignmentSvg).append("</div></div>").append("</div>");
150 htmlSvg.append("</div>");
152 htmlSvg.append("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js\"></script>\n"
153 + "<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js\"></script>\n"
155 + "var subCatContainer = $(\".sub-category-container\");\n"
156 + "subCatContainer.scroll(\nfunction() {\n"
157 + "subCatContainer.scrollTop($(this).scrollTop());\n});\n");
159 htmlSvg.append("</script>\n");
163 htmlSvg.append("<div>\n")
164 .append(alignmentSvg).append("</div>");
165 htmlSvg.append("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js\"></script>\n"
166 + "<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js\"></script>\n");
169 // javascript for launching file in Jalview
170 htmlSvg.append("<script language=\"JavaScript\">\n");
171 htmlSvg.append("function openJalviewUsingCurrentUrl(){\n");
172 htmlSvg.append(" var json = JSON.parse(document.getElementById(\"seqData\").innerHTML);\n");
173 htmlSvg.append(" var jalviewVersion = json['appSettings'].version;\n");
174 htmlSvg.append(" var url = json['appSettings'].webStartUrl;\n");
175 htmlSvg.append(" var myForm = document.createElement(\"form\");\n\n");
176 htmlSvg.append(" var heap = document.createElement(\"input\");\n");
177 htmlSvg.append(" heap.setAttribute(\"name\", \"jvm-max-heap\") ;\n");
178 htmlSvg.append(" heap.setAttribute(\"value\", \"2G\");\n\n");
179 htmlSvg.append(" var target = document.createElement(\"input\");\n");
180 htmlSvg.append(" target.setAttribute(\"name\", \"open\");\n");
181 htmlSvg.append(" target.setAttribute(\"value\", document.URL);\n\n");
182 htmlSvg.append(" var jvVersion = document.createElement(\"input\");\n");
183 htmlSvg.append(" jvVersion.setAttribute(\"name\", \"version\") ;\n");
184 htmlSvg.append(" jvVersion.setAttribute(\"value\", jalviewVersion);\n\n");
185 htmlSvg.append(" myForm.action = url;\n");
186 htmlSvg.append(" myForm.appendChild(heap);\n");
187 htmlSvg.append(" myForm.appendChild(target);\n");
188 htmlSvg.append(" myForm.appendChild(jvVersion);\n");
189 htmlSvg.append(" document.body.appendChild(myForm);\n");
190 htmlSvg.append(" myForm.submit() ;\n");
191 htmlSvg.append(" document.body.removeChild(myForm);\n");
192 htmlSvg.append("}\n");
194 if (jsonData != null)
196 // JQuery FaceBox for displaying raw BioJSON data");
197 File faceBoxJsFile = new File("examples/javascript/facebox-1.3.js");
200 htmlSvg.append(HTMLOutput.readFileAsString(faceBoxJsFile));
201 } catch (IOException e)
207 htmlSvg.append("</script>\n");
208 htmlSvg.append("</html>");
209 return htmlSvg.toString();
213 public boolean isEmbedData()
215 return Boolean.valueOf(jalview.bin.Cache.getDefault(
216 "EXPORT_EMBBED_BIOJSON", "true"));
220 public boolean isLaunchInBrowserAfterExport()
226 public File getExportedFile()
228 return generatedFile;
236 setProgressMessage(null);
237 setProgressMessage(MessageManager.formatMessage(
238 "status.exporting_alignment_as_x_file", "HTML"));
239 AlignmentDimension aDimension = ap.getAlignmentDimension();
240 SVGGraphics2D idPanelGraphics = new SVGGraphics2D(
241 aDimension.getWidth(), aDimension.getHeight());
242 SVGGraphics2D alignPanelGraphics = new SVGGraphics2D(
243 aDimension.getWidth(), aDimension.getHeight());
245 String renderStyle = jalview.bin.Cache.getDefault("HTML_RENDERING",
248 // If we need to prompt, and if the GUI is visible then
249 // Prompt for rendering style
250 if (renderStyle.equalsIgnoreCase("Prompt each time") && !isHeadless())
252 HTMLOptions svgOption = new HTMLOptions();
253 renderStyle = svgOption.getValue();
255 if (renderStyle == null || svgOption.cancelled)
257 setProgressMessage(MessageManager.formatMessage(
258 "status.cancelled_image_export_operation", "HTML"));
263 if (renderStyle.equalsIgnoreCase("Lineart"))
265 idPanelGraphics.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
266 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
267 alignPanelGraphics.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
268 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
270 if (ap.av.getWrapAlignment())
272 printWrapped(aDimension.getWidth(), aDimension.getHeight(), 0,
277 printUnwrapped(aDimension.getWidth(), aDimension.getHeight(), 0,
278 idPanelGraphics, alignPanelGraphics);
281 String idPanelSvgData = idPanelGraphics.getSVGDocument();
282 String alignPanelSvgData = alignPanelGraphics.getSVGDocument();
283 String jsonData = getBioJSONData();
284 String htmlData = getHtml(idPanelSvgData, alignPanelSvgData,
285 jsonData, ap.av.getWrapAlignment());
286 FileOutputStream out = new FileOutputStream(generatedFile);
287 out.write(htmlData.getBytes());
290 setProgressMessage(MessageManager.formatMessage(
291 "status.export_complete", "HTML"));
293 } catch (OutOfMemoryError err)
295 System.out.println("########################\n" + "OUT OF MEMORY "
296 + generatedFile + "\n" + "########################");
297 new OOMWarning("Creating Image for " + generatedFile, err);
298 } catch (Exception e)
301 setProgressMessage(MessageManager.formatMessage(
302 "info.error_creating_file", "HTML"));