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