Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.io

File HtmlSvgOutput.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
56% of files have more coverage

Code metrics

18
114
12
1
358
295
27
0.24
9.5
12
2.25

Classes

Class Line # Actions
HtmlSvgOutput 43 114 27 144
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

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  0 toggle public HtmlSvgOutput(AlignmentPanel ap)
46    {
47  0 super(ap);
48    }
49   
 
50  0 toggle @Override
51    public void exportHTML(String outputFile)
52    {
53  0 exportStarted();
54  0 try
55    {
56  0 if (outputFile == null)
57    {
58  0 outputFile = getOutputFile();
59  0 if (outputFile == null)
60    {
61  0 setProgressMessage(MessageManager.formatMessage(
62    "status.cancelled_image_export_operation", "HTML"));
63  0 return;
64    }
65    }
66  0 generatedFile = new File(outputFile);
67    } catch (Exception e)
68    {
69  0 setProgressMessage(MessageManager
70    .formatMessage("info.error_creating_file", "HTML"));
71  0 e.printStackTrace();
72  0 return;
73    }
74  0 new Thread(this).start();
75    }
76   
 
77  0 toggle public int printUnwrapped(int pwidth, int pheight, int pi,
78    Graphics idGraphics, Graphics alignmentGraphics)
79    throws PrinterException
80    {
81  0 return ap.printUnwrapped(pwidth, pheight, pi, idGraphics,
82    alignmentGraphics);
83    }
84   
 
85  0 toggle public int printWrapped(int pwidth, int pheight, int pi, Graphics... pg)
86    throws PrinterException
87    {
88  0 return ap.printWrappedAlignment(pwidth, pheight, pi, pg[0]);
89    }
90   
 
91  0 toggle String getHtml(String titleSvg, String alignmentSvg,
92    String jsonData, boolean wrapped)
93    {
94  0 StringBuilder htmlSvg = new StringBuilder();
95  0 htmlSvg.append("<html>\n");
96  0 if (jsonData != null)
97    {
98  0 htmlSvg.append(
99    "<button onclick=\"javascipt:openJalviewUsingCurrentUrl();\">Launch in Jalview</button> &nbsp;");
100  0 htmlSvg.append(
101    "<input type=\"submit\" value=\"View raw BioJSON Data\" onclick=\"jQuery.facebox({ div:'#seqData' }); return false;\" />");
102  0 htmlSvg.append(
103    "<div style=\"display: none;\" name=\"seqData\" id=\"seqData\" >"
104    + jsonData + "</div>");
105  0 htmlSvg.append("<br/>&nbsp;");
106    }
107  0 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  0 if (jsonData != null)
115    {
116    // facebox style sheet for displaying raw BioJSON data
117  0 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  0 htmlSvg.append("</style>");
137  0 if (!wrapped)
138    {
139  0 htmlSvg.append("<div class=\"main-container\" \n>");
140  0 htmlSvg.append("<div class=\"titlex\">\n");
141  0 htmlSvg.append("<div class=\"sub-category-container\"> \n");
142  0 htmlSvg.append(titleSvg);
143  0 htmlSvg.append("</div>");
144  0 htmlSvg.append(
145    "</div>\n\n<!-- ========================================================================================== -->\n\n");
146  0 htmlSvg.append("<div class=\"align\" >");
147  0 htmlSvg.append(
148    "<div class=\"sub-category-container\"> <div style=\"overflow-x: scroll;\">")
149    .append(alignmentSvg).append("</div></div>").append("</div>");
150  0 htmlSvg.append("</div>");
151   
152  0 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  0 htmlSvg.append("</script>\n");
161    }
162    else
163    {
164  0 htmlSvg.append("<div>\n").append(alignmentSvg).append("</div>");
165  0 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  0 htmlSvg.append("<script language=\"JavaScript\">\n");
172  0 htmlSvg.append("function openJalviewUsingCurrentUrl(){\n");
173  0 htmlSvg.append(
174    " var json = JSON.parse(document.getElementById(\"seqData\").innerHTML);\n");
175  0 htmlSvg.append(
176    " var jalviewVersion = json['appSettings'].version;\n");
177  0 htmlSvg.append(" var url = json['appSettings'].webStartUrl;\n");
178  0 htmlSvg.append(
179    " var myForm = document.createElement(\"form\");\n\n");
180  0 htmlSvg.append(" var heap = document.createElement(\"input\");\n");
181  0 htmlSvg.append(" heap.setAttribute(\"name\", \"jvm-max-heap\") ;\n");
182  0 htmlSvg.append(" heap.setAttribute(\"value\", \"2G\");\n\n");
183  0 htmlSvg.append(" var target = document.createElement(\"input\");\n");
184  0 htmlSvg.append(" target.setAttribute(\"name\", \"open\");\n");
185  0 htmlSvg.append(" target.setAttribute(\"value\", document.URL);\n\n");
186  0 htmlSvg.append(
187    " var jvVersion = document.createElement(\"input\");\n");
188  0 htmlSvg.append(" jvVersion.setAttribute(\"name\", \"version\") ;\n");
189  0 htmlSvg.append(
190    " jvVersion.setAttribute(\"value\", jalviewVersion);\n\n");
191  0 htmlSvg.append(" myForm.action = url;\n");
192  0 htmlSvg.append(" myForm.appendChild(heap);\n");
193  0 htmlSvg.append(" myForm.appendChild(target);\n");
194  0 htmlSvg.append(" myForm.appendChild(jvVersion);\n");
195  0 htmlSvg.append(" document.body.appendChild(myForm);\n");
196  0 htmlSvg.append(" myForm.submit() ;\n");
197  0 htmlSvg.append(" document.body.removeChild(myForm);\n");
198  0 htmlSvg.append("}\n");
199   
200  0 if (jsonData != null)
201    {
202    // JQuery FaceBox for displaying raw BioJSON data");
203  0 File faceBoxJsFile = new File("examples/javascript/facebox-1.3.js");
204  0 try
205    {
206  0 htmlSvg.append(HTMLOutput.readFileAsString(faceBoxJsFile));
207    } catch (IOException e)
208    {
209  0 e.printStackTrace();
210    }
211    }
212   
213  0 htmlSvg.append("</script>\n");
214  0 htmlSvg.append("</html>");
215  0 return htmlSvg.toString();
216    }
217   
 
218  0 toggle @Override
219    public boolean isEmbedData()
220    {
221  0 return Boolean.valueOf(
222    jalview.bin.Cache.getDefault("EXPORT_EMBBED_BIOJSON", "true"));
223    }
224   
 
225  0 toggle @Override
226    public boolean isLaunchInBrowserAfterExport()
227    {
228  0 return true;
229    }
230   
 
231  0 toggle @Override
232    public void run()
233    {
234  0 try
235    {
236  0 setProgressMessage(null);
237  0 setProgressMessage(MessageManager.formatMessage(
238    "status.exporting_alignment_as_x_file", getDescription()));
239   
240  0 String renderStyle = Cache.getDefault("HTML_RENDERING",
241    "Prompt each time");
242  0 AtomicBoolean textOption = new AtomicBoolean(
243    !"Lineart".equals(renderStyle));
244   
245    /*
246    * configure the action to run on OK in the dialog
247    */
248  0 RunResponse okAction = new RunResponse(JOptionPane.OK_OPTION)
249    {
 
250  0 toggle @Override
251    public void run()
252    {
253  0 doOutput(textOption.get());
254    }
255    };
256   
257    /*
258    * Prompt for character rendering style if preference is not set
259    */
260  0 if (renderStyle.equalsIgnoreCase("Prompt each time") && !isHeadless())
261    {
262  0 LineartOptions svgOption = new LineartOptions("HTML", textOption);
263  0 svgOption.setResponseAction(new RunResponse(JOptionPane.NO_OPTION)
264    {
 
265  0 toggle @Override
266    public void run()
267    {
268  0 setProgressMessage(MessageManager.formatMessage(
269    "status.cancelled_image_export_operation",
270    getDescription()));
271    }
272    });
273  0 svgOption.setResponseAction(okAction);
274  0 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  0 doOutput(textOption.get());
283    }
284    } catch (OutOfMemoryError err)
285    {
286  0 System.out.println("########################\n" + "OUT OF MEMORY "
287    + generatedFile + "\n" + "########################");
288  0 new OOMWarning("Creating Image for " + generatedFile, err);
289    } catch (Exception e)
290    {
291  0 e.printStackTrace();
292  0 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  0 toggle protected void doOutput(boolean textCharacters)
305    {
306  0 try
307    {
308  0 AlignmentDimension aDimension = ap.getAlignmentDimension();
309  0 SVGGraphics2D idPanelGraphics = new SVGGraphics2D(
310    aDimension.getWidth(), aDimension.getHeight());
311  0 SVGGraphics2D alignPanelGraphics = new SVGGraphics2D(
312    aDimension.getWidth(), aDimension.getHeight());
313  0 if (!textCharacters) // Lineart selected
314    {
315  0 idPanelGraphics.setRenderingHint(
316    SVGHints.KEY_DRAW_STRING_TYPE,
317    SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
318  0 alignPanelGraphics.setRenderingHint(
319    SVGHints.KEY_DRAW_STRING_TYPE,
320    SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
321    }
322  0 if (ap.av.getWrapAlignment())
323    {
324  0 printWrapped(aDimension.getWidth(), aDimension.getHeight(), 0,
325    alignPanelGraphics);
326    }
327    else
328    {
329  0 printUnwrapped(aDimension.getWidth(), aDimension.getHeight(),
330    0, idPanelGraphics, alignPanelGraphics);
331    }
332   
333  0 String idPanelSvgData = idPanelGraphics.getSVGDocument();
334  0 String alignPanelSvgData = alignPanelGraphics.getSVGDocument();
335  0 String jsonData = getBioJSONData();
336  0 String htmlData = getHtml(idPanelSvgData, alignPanelSvgData,
337    jsonData, ap.av.getWrapAlignment());
338  0 FileOutputStream out = new FileOutputStream(generatedFile);
339  0 out.write(htmlData.getBytes());
340  0 out.flush();
341  0 out.close();
342  0 setProgressMessage(MessageManager
343    .formatMessage("status.export_complete", getDescription()));
344  0 exportCompleted();
345    } catch (Exception e)
346    {
347  0 e.printStackTrace();
348  0 setProgressMessage(MessageManager
349    .formatMessage("info.error_creating_file", getDescription()));
350    }
351    }
352   
 
353  0 toggle @Override
354    protected String getDescription()
355    {
356  0 return "HTML";
357    }
358    }