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