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