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