2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import java.io.BufferedReader;
25 import java.io.IOException;
26 import java.io.InputStreamReader;
28 import java.util.Objects;
30 import jalview.api.AlignExportSettingsI;
31 import jalview.bin.Cache;
32 import jalview.bin.Jalview;
33 import jalview.datamodel.AlignExportSettingsAdapter;
34 import jalview.datamodel.AlignmentExportData;
35 import jalview.gui.AlignmentPanel;
36 import jalview.gui.IProgressIndicator;
37 import jalview.io.exceptions.ImageOutputException;
38 import jalview.util.MessageManager;
40 public abstract class HTMLOutput implements Runnable
42 protected AlignmentPanel ap;
45 * key for progress or status messages
47 protected long pSessionId;
50 * (optional) place to write progress messages to
52 protected IProgressIndicator pIndicator;
54 protected File generatedFile;
56 String _bioJson = null;
58 private String description;
61 * Constructor given an alignment panel (which should not be null)
66 public HTMLOutput(AlignmentPanel ap, String desc)
69 this.pIndicator = ap.alignFrame;
70 this.description = desc;
71 this.pSessionId = System.currentTimeMillis();
75 * Gets the BioJSON data as a string, with lazy evaluation (first time called
76 * only). If the output format is configured not to embed BioJSON, returns
81 public String getBioJSONData()
89 AlignExportSettingsI options = new AlignExportSettingsAdapter(true);
90 AlignmentExportData exportData = ap.getAlignViewport()
91 .getAlignExportData(options);
92 _bioJson = new FormatAdapter(ap, options).formatSequences(
93 FileFormat.Json, exportData.getAlignment(),
94 exportData.getOmitHidden(), exportData.getStartEndPostions(),
95 ap.getAlignViewport().getAlignment().getHiddenColumns());
102 * Read a template file content as string
105 * - the file to be read
106 * @return File content as String
107 * @throws IOException
109 public static String readFileAsString(File file) throws IOException
111 InputStreamReader isReader = null;
112 BufferedReader buffReader = null;
113 StringBuilder sb = new StringBuilder();
114 Objects.requireNonNull(file, "File must not be null!");
115 @SuppressWarnings("deprecation")
116 URL url = file.toURL();
121 isReader = new InputStreamReader(url.openStream());
122 buffReader = new BufferedReader(isReader);
124 String lineSeparator = System.getProperty("line.separator");
125 while ((line = buffReader.readLine()) != null)
127 sb.append(line).append(lineSeparator);
130 } catch (Exception ex)
132 ex.printStackTrace();
135 if (isReader != null)
140 if (buffReader != null)
146 return sb.toString();
149 public static String getImageMapHTML()
151 return new String("<html>\n" + "<head>\n"
152 + "<script language=\"JavaScript\">\n"
153 + "var ns4 = document.layers;\n"
154 + "var ns6 = document.getElementById && !document.all;\n"
155 + "var ie4 = document.all;\n" + "offsetX = 0;\n"
156 + "offsetY = 20;\n" + "var toolTipSTYLE=\"\";\n"
157 + "function initToolTips()\n" + "{\n" + " if(ns4||ns6||ie4)\n"
159 + " if(ns4) toolTipSTYLE = document.toolTipLayer;\n"
160 + " else if(ns6) toolTipSTYLE = document.getElementById(\"toolTipLayer\").style;\n"
161 + " else if(ie4) toolTipSTYLE = document.all.toolTipLayer.style;\n"
162 + " if(ns4) document.captureEvents(Event.MOUSEMOVE);\n"
164 + " toolTipSTYLE.visibility = \"visible\";\n"
165 + " toolTipSTYLE.display = \"none\";\n" + " }\n"
166 + " document.onmousemove = moveToMouseLoc;\n" + " }\n"
167 + "}\n" + "function toolTip(msg, fg, bg)\n" + "{\n"
168 + " if(toolTip.arguments.length < 1) // hide\n" + " {\n"
169 + " if(ns4) toolTipSTYLE.visibility = \"hidden\";\n"
170 + " else toolTipSTYLE.display = \"none\";\n" + " }\n"
171 + " else // show\n" + " {\n"
172 + " if(!fg) fg = \"#555555\";\n"
173 + " if(!bg) bg = \"#FFFFFF\";\n" + " var content =\n"
174 + " '<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" bgcolor=\"' + fg + '\"><td>' +\n"
175 + " '<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" bgcolor=\"' + bg + \n"
176 + " '\"><td align=\"center\"><font face=\"sans-serif\" color=\"' + fg +\n"
177 + " '\" size=\"-2\"> ' + msg +\n"
178 + " ' </font></td></table></td></table>';\n"
179 + " if(ns4)\n" + " {\n"
180 + " toolTipSTYLE.document.write(content);\n"
181 + " toolTipSTYLE.document.close();\n"
182 + " toolTipSTYLE.visibility = \"visible\";\n" + " }\n"
183 + " if(ns6)\n" + " {\n"
184 + " document.getElementById(\"toolTipLayer\").innerHTML = content;\n"
185 + " toolTipSTYLE.display='block'\n" + " }\n"
186 + " if(ie4)\n" + " {\n"
187 + " document.all(\"toolTipLayer\").innerHTML=content;\n"
188 + " toolTipSTYLE.display='block'\n" + " }\n" + " }\n"
189 + "}\n" + "function moveToMouseLoc(e)\n" + "{\n"
190 + " if(ns4||ns6)\n" + " {\n" + " x = e.pageX;\n"
191 + " y = e.pageY;\n" + " }\n" + " else\n" + " {\n"
192 + " x = event.x + document.body.scrollLeft;\n"
193 + " y = event.y + document.body.scrollTop;\n" + " }\n"
194 + " toolTipSTYLE.left = x + offsetX;\n"
195 + " toolTipSTYLE.top = y + offsetY;\n" + " return true;\n"
196 + "}\n" + "</script>\n" + "</head>\n" + "<body>\n"
197 + "<div id=\"toolTipLayer\" style=\"position:absolute; visibility: hidden\"></div>\n"
198 + "<script language=\"JavaScript\"><!--\n"
199 + "initToolTips(); //--></script>\n");
204 * Prompts the user to choose an output file and returns the file path, or
209 public String getOutputFile()
211 String selectedFile = null;
213 // TODO: JAL-3048 generate html rendered view (requires SvgGraphics and/or
214 // Jalview HTML rendering system- probably not required for Jalview-JS)
215 JalviewFileChooser jvFileChooser = new JalviewFileChooser("html",
217 jvFileChooser.setFileView(new JalviewFileView());
220 .setDialogTitle(MessageManager.getString("label.save_as_html"));
221 jvFileChooser.setToolTipText(MessageManager.getString("action.save"));
223 int fileChooserOpt = jvFileChooser.showSaveDialog(null);
224 if (fileChooserOpt == JalviewFileChooser.APPROVE_OPTION)
226 Cache.setProperty("LAST_DIRECTORY",
227 jvFileChooser.getSelectedFile().getParent());
228 selectedFile = jvFileChooser.getSelectedFile().getPath();
234 protected void setProgressMessage(String message)
236 if (pIndicator != null && !isHeadless())
238 pIndicator.setProgressBar(message, pSessionId);
242 jalview.bin.Console.outPrintln(message);
247 * Answers true if HTML export is invoke in headless mode or false otherwise
251 protected boolean isHeadless()
253 return System.getProperty("java.awt.headless") != null
254 && System.getProperty("java.awt.headless").equals("true");
258 * This method provides implementation of consistent behaviour which should
259 * occur after a HTML file export. It MUST be called at the end of the
260 * exportHTML() method implementation.
262 protected void exportCompleted()
264 if (isLaunchInBrowserAfterExport() && !isHeadless())
270 jalview.util.BrowserLauncher.openURL("file:///" + getExportedFile());
272 } catch (IOException e)
281 * if this answers true then BioJSON data will be embedded to the exported
282 * HTML file otherwise it won't be embedded.
286 public abstract boolean isEmbedData();
289 * if this answers true then the generated HTML file is opened for viewing in
290 * a browser after its generation otherwise it won't be opened in a browser
294 public abstract boolean isLaunchInBrowserAfterExport();
297 * handle to the generated HTML file
301 public File getExportedFile()
303 return generatedFile;
306 public void exportHTML(String outputFile) throws ImageOutputException
308 exportHTML(outputFile, null);
311 public void exportHTML(String outputFile, String renderer)
312 throws ImageOutputException
314 setProgressMessage(MessageManager.formatMessage(
315 "status.exporting_alignment_as_x_file", getDescription()));
318 if (outputFile == null)
321 * prompt for output file
323 outputFile = getOutputFile();
324 if (outputFile == null)
326 setProgressMessage(MessageManager.formatMessage(
327 "status.cancelled_image_export_operation",
332 generatedFile = new File(outputFile);
333 } catch (Exception e)
335 setProgressMessage(MessageManager
336 .formatMessage("info.error_creating_file", getDescription()));
340 if (Jalview.isHeadlessMode())
346 new Thread(this).start();
352 * Answers a short description of the image format suitable for display in
357 protected final String getDescription()
362 // used to pass an option such as render to run
363 public abstract void run(String string) throws ImageOutputException;