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.util.MessageManager;
39 public abstract class HTMLOutput implements Runnable
41 protected AlignmentPanel ap;
44 * key for progress or status messages
46 protected long pSessionId;
49 * (optional) place to write progress messages to
51 protected IProgressIndicator pIndicator;
53 protected File generatedFile;
55 String _bioJson = null;
57 private String description;
60 * Constructor given an alignment panel (which should not be null)
65 public HTMLOutput(AlignmentPanel ap, String desc)
68 this.pIndicator = ap.alignFrame;
69 this.description = desc;
70 this.pSessionId = System.currentTimeMillis();
74 * Gets the BioJSON data as a string, with lazy evaluation (first time called
75 * only). If the output format is configured not to embed BioJSON, returns
80 public String getBioJSONData()
88 AlignExportSettingsI options = new AlignExportSettingsAdapter(true);
89 AlignmentExportData exportData = ap.getAlignViewport()
90 .getAlignExportData(options);
91 _bioJson = new FormatAdapter(ap, options).formatSequences(
92 FileFormat.Json, exportData.getAlignment(),
93 exportData.getOmitHidden(), exportData.getStartEndPostions(),
94 ap.getAlignViewport().getAlignment().getHiddenColumns());
101 * Read a template file content as string
104 * - the file to be read
105 * @return File content as String
106 * @throws IOException
108 public static String readFileAsString(File file) throws IOException
110 InputStreamReader isReader = null;
111 BufferedReader buffReader = null;
112 StringBuilder sb = new StringBuilder();
113 Objects.requireNonNull(file, "File must not be null!");
114 @SuppressWarnings("deprecation")
115 URL url = file.toURL();
120 isReader = new InputStreamReader(url.openStream());
121 buffReader = new BufferedReader(isReader);
123 String lineSeparator = System.getProperty("line.separator");
124 while ((line = buffReader.readLine()) != null)
126 sb.append(line).append(lineSeparator);
129 } catch (Exception ex)
131 ex.printStackTrace();
134 if (isReader != null)
139 if (buffReader != null)
145 return sb.toString();
148 public static String getImageMapHTML()
150 return new String("<html>\n" + "<head>\n"
151 + "<script language=\"JavaScript\">\n"
152 + "var ns4 = document.layers;\n"
153 + "var ns6 = document.getElementById && !document.all;\n"
154 + "var ie4 = document.all;\n" + "offsetX = 0;\n"
155 + "offsetY = 20;\n" + "var toolTipSTYLE=\"\";\n"
156 + "function initToolTips()\n" + "{\n" + " if(ns4||ns6||ie4)\n"
158 + " if(ns4) toolTipSTYLE = document.toolTipLayer;\n"
159 + " else if(ns6) toolTipSTYLE = document.getElementById(\"toolTipLayer\").style;\n"
160 + " else if(ie4) toolTipSTYLE = document.all.toolTipLayer.style;\n"
161 + " if(ns4) document.captureEvents(Event.MOUSEMOVE);\n"
163 + " toolTipSTYLE.visibility = \"visible\";\n"
164 + " toolTipSTYLE.display = \"none\";\n" + " }\n"
165 + " document.onmousemove = moveToMouseLoc;\n" + " }\n"
166 + "}\n" + "function toolTip(msg, fg, bg)\n" + "{\n"
167 + " if(toolTip.arguments.length < 1) // hide\n" + " {\n"
168 + " if(ns4) toolTipSTYLE.visibility = \"hidden\";\n"
169 + " else toolTipSTYLE.display = \"none\";\n" + " }\n"
170 + " else // show\n" + " {\n"
171 + " if(!fg) fg = \"#555555\";\n"
172 + " if(!bg) bg = \"#FFFFFF\";\n" + " var content =\n"
173 + " '<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" bgcolor=\"' + fg + '\"><td>' +\n"
174 + " '<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" bgcolor=\"' + bg + \n"
175 + " '\"><td align=\"center\"><font face=\"sans-serif\" color=\"' + fg +\n"
176 + " '\" size=\"-2\"> ' + msg +\n"
177 + " ' </font></td></table></td></table>';\n"
178 + " if(ns4)\n" + " {\n"
179 + " toolTipSTYLE.document.write(content);\n"
180 + " toolTipSTYLE.document.close();\n"
181 + " toolTipSTYLE.visibility = \"visible\";\n" + " }\n"
182 + " if(ns6)\n" + " {\n"
183 + " document.getElementById(\"toolTipLayer\").innerHTML = content;\n"
184 + " toolTipSTYLE.display='block'\n" + " }\n"
185 + " if(ie4)\n" + " {\n"
186 + " document.all(\"toolTipLayer\").innerHTML=content;\n"
187 + " toolTipSTYLE.display='block'\n" + " }\n" + " }\n"
188 + "}\n" + "function moveToMouseLoc(e)\n" + "{\n"
189 + " if(ns4||ns6)\n" + " {\n" + " x = e.pageX;\n"
190 + " y = e.pageY;\n" + " }\n" + " else\n" + " {\n"
191 + " x = event.x + document.body.scrollLeft;\n"
192 + " y = event.y + document.body.scrollTop;\n" + " }\n"
193 + " toolTipSTYLE.left = x + offsetX;\n"
194 + " toolTipSTYLE.top = y + offsetY;\n" + " return true;\n"
195 + "}\n" + "</script>\n" + "</head>\n" + "<body>\n"
196 + "<div id=\"toolTipLayer\" style=\"position:absolute; visibility: hidden\"></div>\n"
197 + "<script language=\"JavaScript\"><!--\n"
198 + "initToolTips(); //--></script>\n");
203 * Prompts the user to choose an output file and returns the file path, or
208 public String getOutputFile()
210 String selectedFile = null;
212 // TODO: JAL-3048 generate html rendered view (requires SvgGraphics and/or
213 // Jalview HTML rendering system- probably not required for Jalview-JS)
214 JalviewFileChooser jvFileChooser = new JalviewFileChooser("html",
216 jvFileChooser.setFileView(new JalviewFileView());
219 .setDialogTitle(MessageManager.getString("label.save_as_html"));
220 jvFileChooser.setToolTipText(MessageManager.getString("action.save"));
222 int fileChooserOpt = jvFileChooser.showSaveDialog(null);
223 if (fileChooserOpt == JalviewFileChooser.APPROVE_OPTION)
225 Cache.setProperty("LAST_DIRECTORY",
226 jvFileChooser.getSelectedFile().getParent());
227 selectedFile = jvFileChooser.getSelectedFile().getPath();
233 protected void setProgressMessage(String message)
235 if (pIndicator != null && !isHeadless())
237 pIndicator.setProgressBar(message, pSessionId);
241 System.out.println(message);
246 * Answers true if HTML export is invoke in headless mode or false otherwise
250 protected boolean isHeadless()
252 return System.getProperty("java.awt.headless") != null
253 && System.getProperty("java.awt.headless").equals("true");
257 * This method provides implementation of consistent behaviour which should
258 * occur after a HTML file export. It MUST be called at the end of the
259 * exportHTML() method implementation.
261 protected void exportCompleted()
263 if (isLaunchInBrowserAfterExport() && !isHeadless())
269 jalview.util.BrowserLauncher.openURL("file:///" + getExportedFile());
271 } catch (IOException e)
280 * if this answers true then BioJSON data will be embedded to the exported
281 * HTML file otherwise it won't be embedded.
285 public abstract boolean isEmbedData();
288 * if this answers true then the generated HTML file is opened for viewing in
289 * a browser after its generation otherwise it won't be opened in a browser
293 public abstract boolean isLaunchInBrowserAfterExport();
296 * handle to the generated HTML file
300 public File getExportedFile()
302 return generatedFile;
305 public void exportHTML(String outputFile)
307 exportHTML(outputFile, null);
310 public void exportHTML(String outputFile, String renderer)
312 setProgressMessage(MessageManager.formatMessage(
313 "status.exporting_alignment_as_x_file", getDescription()));
316 if (outputFile == null)
319 * prompt for output file
321 outputFile = getOutputFile();
322 if (outputFile == null)
324 setProgressMessage(MessageManager.formatMessage(
325 "status.cancelled_image_export_operation",
330 generatedFile = new File(outputFile);
331 } catch (Exception e)
333 setProgressMessage(MessageManager
334 .formatMessage("info.error_creating_file", getDescription()));
338 if (Jalview.isHeadlessMode())
344 new Thread(this).start();
350 * Answers a short description of the image format suitable for display in
355 protected final String getDescription()
360 // used to pass an option such as render to run
361 public abstract void run(String string);