Clover icon

Coverage Report

  1. Project Clover database Thu Nov 7 2024 10:11:34 GMT
  2. Package jalview.io

File HtmlSvgOutput.java

 

Coverage histogram

../../img/srcFileCovDistChart8.png
21% of files have more coverage

Code metrics

16
100
9
1
317
257
22
0.22
11.11
9
2.44

Classes

Class Line # Actions
HtmlSvgOutput 41 100 22
0.79279.2%
 

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