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