3fb3cf2e916598bb3a280471861fa1db50e7c32d
[jalview.git] / src / jalview / io / HtmlSvgOutput.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.io;
22
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;
29
30 import java.awt.Graphics;
31 import java.awt.print.PrinterException;
32 import java.io.File;
33 import java.io.FileOutputStream;
34 import java.io.IOException;
35
36 import org.jfree.graphics2d.svg.SVGGraphics2D;
37 import org.jfree.graphics2d.svg.SVGHints;
38
39 public class HtmlSvgOutput extends HTMLOutput
40 {
41
42
43   public HtmlSvgOutput(AlignmentPanel ap)
44   {
45     super(ap);
46   }
47
48   @Override
49   public void exportHTML(String outputFile)
50   {
51     exportStarted();
52     try
53     {
54       if (outputFile == null)
55       {
56         outputFile = getOutputFile();
57       }
58       generatedFile = new File(outputFile);
59     } catch (NoFileSelectedException e)
60     {
61       setProgressMessage(MessageManager.formatMessage(
62               "status.cancelled_image_export_operation", "HTML"));
63       return;
64     } catch (Exception e)
65     {
66       setProgressMessage(MessageManager.formatMessage(
67               "info.error_creating_file", "HTML"));
68       e.printStackTrace();
69       return;
70     }
71     new Thread(this).start();
72   }
73
74   public int printUnwrapped(int pwidth, int pheight, int pi,
75           Graphics idGraphics, Graphics alignmentGraphics)
76           throws PrinterException
77   {
78     return ap.printUnwrapped(pwidth, pheight, pi, idGraphics,
79             alignmentGraphics);
80   }
81
82   public int printWrapped(int pwidth, int pheight, int pi, Graphics... pg)
83           throws PrinterException
84   {
85     return ap.printWrappedAlignment(pwidth, pheight, pi, pg[0]);
86   }
87
88   private String getHtml(String titleSvg, String alignmentSvg,
89           String jsonData, boolean wrapped)
90   {
91     StringBuilder htmlSvg = new StringBuilder();
92     htmlSvg.append("<html>\n");
93     if (jsonData != null)
94     {
95       htmlSvg.append("<button onclick=\"javascipt:openJalviewUsingCurrentUrl();\">Launch in Jalview</button> &nbsp;");
96       htmlSvg.append("<input type=\"submit\" value=\"View raw BioJSON Data\" onclick=\"jQuery.facebox({ div:'#seqData' }); return false;\" />");
97       htmlSvg.append("<div style=\"display: none;\" name=\"seqData\" id=\"seqData\" >"
98               + jsonData + "</div>");
99       htmlSvg.append("<br/>&nbsp;");
100     }
101     htmlSvg.append("\n<style type=\"text/css\"> "
102             + "div.parent{ width:100%;<!-- overflow: auto; -->}\n"
103             + "div.titlex{ width:11%; float: left; }\n"
104             + "div.align{ width:89%; float: right; }\n"
105             + "div.main-container{ border: 2px solid blue; border: 2px solid blue; width: 99%;   min-height: 99%; }\n"
106             + ".sub-category-container {overflow-y: scroll; overflow-x: hidden; width: 100%; height: 100%;}\n"
107             + "object {pointer-events: none;}");
108     if (jsonData != null)
109     {
110       // facebox style sheet for displaying raw BioJSON data
111       htmlSvg.append("#facebox { position: absolute;  top: 0;   left: 0; z-index: 100; text-align: left; }\n"
112               + "#facebox .popup{ position:relative; border:3px solid rgba(0,0,0,0); -webkit-border-radius:5px;"
113               + "-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);"
114               + "box-shadow:0 0 18px rgba(0,0,0,0.4); }\n"
115               + "#facebox .content { display:table; width: 98%; padding: 10px; background: #fff; -webkit-border-radius:4px; -moz-border-radius:4px;"
116               + " border-radius:4px; }\n"
117               + "#facebox .content > p:first-child{ margin-top:0; }\n"
118               + "#facebox .content > p:last-child{ margin-bottom:0; }\n"
119               + "#facebox .close{ position:absolute; top:5px; right:5px; padding:2px; background:#fff; }\n"
120               + "#facebox .close img{ opacity:0.3; }\n"
121               + "#facebox .close:hover img{ opacity:1.0; }\n"
122               + "#facebox .loading { text-align: center; }\n"
123               + "#facebox .image { text-align: center;}\n"
124               + "#facebox img { border: 0;  margin: 0; }\n"
125               + "#facebox_overlay { position: fixed; top: 0px; left: 0px; height:100%; width:100%; }\n"
126               + ".facebox_hide { z-index:-100; }\n"
127               + ".facebox_overlayBG { background-color: #000;  z-index: 99;  }");
128     }
129     htmlSvg.append("</style>");
130     if (!wrapped)
131     {
132     htmlSvg.append("<div class=\"main-container\" \n>");
133     htmlSvg.append("<div class=\"titlex\">\n");
134     htmlSvg.append("<div class=\"sub-category-container\"> \n");
135     htmlSvg.append(titleSvg);
136     htmlSvg.append("</div>");
137     htmlSvg.append("</div>\n\n<!-- ========================================================================================== -->\n\n");
138     htmlSvg.append("<div class=\"align\" >");
139     htmlSvg.append(
140             "<div class=\"sub-category-container\"> <div style=\"overflow-x: scroll;\">")
141             .append(alignmentSvg).append("</div></div>").append("</div>");
142     htmlSvg.append("</div>");
143
144     htmlSvg.append("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js\"></script>\n"
145             + "<script language=\"JavaScript\" type=\"text/javascript\"  src=\"http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js\"></script>\n"
146             + "<script>\n"
147             + "var subCatContainer = $(\".sub-category-container\");\n"
148             + "subCatContainer.scroll(\nfunction() {\n"
149             + "subCatContainer.scrollTop($(this).scrollTop());\n});\n");
150
151     htmlSvg.append("</script>\n");
152     }
153     else
154     {
155       htmlSvg.append("<div>\n")
156               .append(alignmentSvg).append("</div>");
157       htmlSvg.append("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js\"></script>\n"
158               + "<script language=\"JavaScript\" type=\"text/javascript\"  src=\"http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js\"></script>\n");
159     }
160
161     // javascript for launching file in Jalview
162     htmlSvg.append("<script language=\"JavaScript\">\n");
163     htmlSvg.append("function openJalviewUsingCurrentUrl(){\n");
164     htmlSvg.append("    var json = JSON.parse(document.getElementById(\"seqData\").innerHTML);\n");
165     htmlSvg.append("    var jalviewVersion = json['appSettings'].version;\n");
166     htmlSvg.append("    var url = json['appSettings'].webStartUrl;\n");
167     htmlSvg.append("    var myForm = document.createElement(\"form\");\n\n");
168     htmlSvg.append("    var heap = document.createElement(\"input\");\n");
169     htmlSvg.append("    heap.setAttribute(\"name\", \"jvm-max-heap\") ;\n");
170     htmlSvg.append("    heap.setAttribute(\"value\", \"2G\");\n\n");
171     htmlSvg.append("    var target = document.createElement(\"input\");\n");
172     htmlSvg.append("    target.setAttribute(\"name\", \"open\");\n");
173     htmlSvg.append("    target.setAttribute(\"value\", document.URL);\n\n");
174     htmlSvg.append("    var jvVersion = document.createElement(\"input\");\n");
175     htmlSvg.append("    jvVersion.setAttribute(\"name\", \"version\") ;\n");
176     htmlSvg.append("    jvVersion.setAttribute(\"value\", jalviewVersion);\n\n");
177     htmlSvg.append("    myForm.action = url;\n");
178     htmlSvg.append("    myForm.appendChild(heap);\n");
179     htmlSvg.append("    myForm.appendChild(target);\n");
180     htmlSvg.append("    myForm.appendChild(jvVersion);\n");
181     htmlSvg.append("    document.body.appendChild(myForm);\n");
182     htmlSvg.append("    myForm.submit() ;\n");
183     htmlSvg.append("    document.body.removeChild(myForm);\n");
184     htmlSvg.append("}\n");
185
186     if (jsonData != null)
187     {
188       // JQuery FaceBox for displaying raw BioJSON data");
189       File faceBoxJsFile = new File("examples/javascript/facebox-1.3.js");
190       try
191       {
192         htmlSvg.append(HTMLOutput.readFileAsString(faceBoxJsFile));
193       } catch (IOException e)
194       {
195         e.printStackTrace();
196       }
197     }
198
199     htmlSvg.append("</script>\n");
200     htmlSvg.append("</html>");
201     return htmlSvg.toString();
202   }
203
204   @Override
205   public boolean isEmbedData()
206   {
207     return Boolean.valueOf(jalview.bin.Cache.getDefault(
208             "EXPORT_EMBBED_BIOJSON", "true"));
209   }
210
211   @Override
212   public boolean isLaunchInBrowserAfterExport()
213   {
214     return true;
215   }
216
217   @Override
218   public File getExportedFile()
219   {
220     return generatedFile;
221   }
222
223   @Override
224   public void run()
225   {
226     try
227     {
228       setProgressMessage(null);
229       setProgressMessage(MessageManager.formatMessage(
230               "status.exporting_alignment_as_x_file", "HTML"));
231       AlignmentDimension aDimension = ap.getAlignmentDimension();
232       SVGGraphics2D idPanelGraphics = new SVGGraphics2D(
233               aDimension.getWidth(), aDimension.getHeight());
234       SVGGraphics2D alignPanelGraphics = new SVGGraphics2D(
235               aDimension.getWidth(), aDimension.getHeight());
236
237       String renderStyle = jalview.bin.Cache.getDefault("HTML_RENDERING",
238               "Prompt each time");
239
240       // If we need to prompt, and if the GUI is visible then
241       // Prompt for rendering style
242       if (renderStyle.equalsIgnoreCase("Prompt each time") && !isHeadless())
243       {
244         HTMLOptions svgOption = new HTMLOptions();
245         renderStyle = svgOption.getValue();
246
247         if (renderStyle == null || svgOption.cancelled)
248         {
249           setProgressMessage(MessageManager.formatMessage(
250                   "status.cancelled_image_export_operation", "HTML"));
251           return;
252         }
253       }
254
255       if (renderStyle.equalsIgnoreCase("Lineart"))
256       {
257         idPanelGraphics.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
258                 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
259         alignPanelGraphics.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
260                 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
261       }
262       if (ap.av.getWrapAlignment())
263       {
264         printWrapped(aDimension.getWidth(), aDimension.getHeight(), 0,
265                 alignPanelGraphics);
266       }
267       else
268       {
269         printUnwrapped(aDimension.getWidth(), aDimension.getHeight(), 0,
270                 idPanelGraphics, alignPanelGraphics);
271       }
272
273       String idPanelSvgData = idPanelGraphics.getSVGDocument();
274       String alignPanelSvgData = alignPanelGraphics.getSVGDocument();
275       String jsonData = getBioJSONData();
276       String htmlData = getHtml(idPanelSvgData, alignPanelSvgData,
277               jsonData, ap.av.getWrapAlignment());
278       FileOutputStream out = new FileOutputStream(generatedFile);
279       out.write(htmlData.getBytes());
280       out.flush();
281       out.close();
282       setProgressMessage(MessageManager.formatMessage(
283               "status.export_complete", "HTML"));
284       exportCompleted();
285     } catch (OutOfMemoryError err)
286     {
287       System.out.println("########################\n" + "OUT OF MEMORY "
288               + generatedFile + "\n" + "########################");
289       new OOMWarning("Creating Image for " + generatedFile, err);
290     } catch (Exception e)
291     {
292       e.printStackTrace();
293       setProgressMessage(MessageManager.formatMessage(
294               "info.error_creating_file", "HTML"));
295     }
296   }
297 }