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 jalview.api.AlignExportSettingsI;
24 import jalview.bin.Cache;
25 import jalview.datamodel.AlignExportSettingsAdapter;
26 import jalview.datamodel.AlignmentExportData;
27 import jalview.gui.AlignmentPanel;
28 import jalview.gui.IProgressIndicator;
29 import jalview.util.MessageManager;
31 import java.io.BufferedReader;
33 import java.io.IOException;
34 import java.io.InputStreamReader;
36 import java.util.Objects;
38 public abstract class HTMLOutput implements Runnable
40 protected AlignmentPanel ap;
43 * key for progress or status messages
45 protected long pSessionId;
48 * (optional) place to write progress messages to
50 protected IProgressIndicator pIndicator;
52 protected File generatedFile;
54 String _bioJson = null;
56 private String description;
59 * Constructor given an alignment panel (which should not be null)
64 public HTMLOutput(AlignmentPanel ap, String desc)
67 this.pIndicator = ap.alignFrame;
68 this.description = desc;
69 this.pSessionId = System.currentTimeMillis();
73 * Gets the BioJSON data as a string, with lazy evaluation (first time called
74 * only). If the output format is configured not to embed BioJSON, returns
79 public String getBioJSONData()
87 AlignExportSettingsI options = new AlignExportSettingsAdapter(true);
88 AlignmentExportData exportData = ap.getAlignViewport()
89 .getAlignExportData(options);
90 _bioJson = new FormatAdapter(ap, options).formatSequences(
91 FileFormat.Json, exportData.getAlignment(),
92 exportData.getOmitHidden(), exportData.getStartEndPostions(),
93 ap.getAlignViewport().getAlignment().getHiddenColumns());
100 * Read a template file content as string
103 * - the file to be read
104 * @return File content as String
105 * @throws IOException
107 public static String readFileAsString(File file) throws IOException
109 InputStreamReader isReader = null;
110 BufferedReader buffReader = null;
111 StringBuilder sb = new StringBuilder();
112 Objects.requireNonNull(file, "File must not be null!");
113 @SuppressWarnings("deprecation")
114 URL url = file.toURL();
119 isReader = new InputStreamReader(url.openStream());
120 buffReader = new BufferedReader(isReader);
122 String lineSeparator = System.getProperty("line.separator");
123 while ((line = buffReader.readLine()) != null)
125 sb.append(line).append(lineSeparator);
128 } catch (Exception ex)
130 ex.printStackTrace();
133 if (isReader != null)
138 if (buffReader != null)
144 return sb.toString();
147 public static String getImageMapHTML()
149 return new String("<html>\n" + "<head>\n"
150 + "<script language=\"JavaScript\">\n"
151 + "var ns4 = document.layers;\n"
152 + "var ns6 = document.getElementById && !document.all;\n"
153 + "var ie4 = document.all;\n" + "offsetX = 0;\n"
154 + "offsetY = 20;\n" + "var toolTipSTYLE=\"\";\n"
155 + "function initToolTips()\n" + "{\n" + " if(ns4||ns6||ie4)\n"
157 + " if(ns4) toolTipSTYLE = document.toolTipLayer;\n"
158 + " else if(ns6) toolTipSTYLE = document.getElementById(\"toolTipLayer\").style;\n"
159 + " else if(ie4) toolTipSTYLE = document.all.toolTipLayer.style;\n"
160 + " if(ns4) document.captureEvents(Event.MOUSEMOVE);\n"
162 + " toolTipSTYLE.visibility = \"visible\";\n"
163 + " toolTipSTYLE.display = \"none\";\n" + " }\n"
164 + " document.onmousemove = moveToMouseLoc;\n" + " }\n"
165 + "}\n" + "function toolTip(msg, fg, bg)\n" + "{\n"
166 + " if(toolTip.arguments.length < 1) // hide\n" + " {\n"
167 + " if(ns4) toolTipSTYLE.visibility = \"hidden\";\n"
168 + " else toolTipSTYLE.display = \"none\";\n" + " }\n"
169 + " else // show\n" + " {\n"
170 + " if(!fg) fg = \"#555555\";\n"
171 + " if(!bg) bg = \"#FFFFFF\";\n" + " var content =\n"
172 + " '<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" bgcolor=\"' + fg + '\"><td>' +\n"
173 + " '<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" bgcolor=\"' + bg + \n"
174 + " '\"><td align=\"center\"><font face=\"sans-serif\" color=\"' + fg +\n"
175 + " '\" size=\"-2\"> ' + msg +\n"
176 + " ' </font></td></table></td></table>';\n"
177 + " if(ns4)\n" + " {\n"
178 + " toolTipSTYLE.document.write(content);\n"
179 + " toolTipSTYLE.document.close();\n"
180 + " toolTipSTYLE.visibility = \"visible\";\n" + " }\n"
181 + " if(ns6)\n" + " {\n"
182 + " document.getElementById(\"toolTipLayer\").innerHTML = content;\n"
183 + " toolTipSTYLE.display='block'\n" + " }\n"
184 + " if(ie4)\n" + " {\n"
185 + " document.all(\"toolTipLayer\").innerHTML=content;\n"
186 + " toolTipSTYLE.display='block'\n" + " }\n" + " }\n"
187 + "}\n" + "function moveToMouseLoc(e)\n" + "{\n"
188 + " if(ns4||ns6)\n" + " {\n" + " x = e.pageX;\n"
189 + " y = e.pageY;\n" + " }\n" + " else\n" + " {\n"
190 + " x = event.x + document.body.scrollLeft;\n"
191 + " y = event.y + document.body.scrollTop;\n" + " }\n"
192 + " toolTipSTYLE.left = x + offsetX;\n"
193 + " toolTipSTYLE.top = y + offsetY;\n" + " return true;\n"
194 + "}\n" + "</script>\n" + "</head>\n" + "<body>\n"
195 + "<div id=\"toolTipLayer\" style=\"position:absolute; visibility: hidden\"></div>\n"
196 + "<script language=\"JavaScript\"><!--\n"
197 + "initToolTips(); //--></script>\n");
202 * Prompts the user to choose an output file and returns the file path, or
207 public String getOutputFile()
209 String selectedFile = null;
211 // TODO: JAL-3048 generate html rendered view (requires SvgGraphics and/or
212 // Jalview HTML rendering system- probably not required for Jalview-JS)
213 JalviewFileChooser jvFileChooser = new JalviewFileChooser("html",
215 jvFileChooser.setFileView(new JalviewFileView());
218 .setDialogTitle(MessageManager.getString("label.save_as_html"));
219 jvFileChooser.setToolTipText(MessageManager.getString("action.save"));
221 int fileChooserOpt = jvFileChooser.showSaveDialog(null);
222 if (fileChooserOpt == JalviewFileChooser.APPROVE_OPTION)
224 Cache.setProperty("LAST_DIRECTORY",
225 jvFileChooser.getSelectedFile().getParent());
226 selectedFile = jvFileChooser.getSelectedFile().getPath();
232 protected void setProgressMessage(String message)
234 if (pIndicator != null && !isHeadless())
236 pIndicator.setProgressBar(message, pSessionId);
240 System.out.println(message);
245 * Answers true if HTML export is invoke in headless mode or false otherwise
249 protected boolean isHeadless()
251 return System.getProperty("java.awt.headless") != null
252 && System.getProperty("java.awt.headless").equals("true");
256 * This method provides implementation of consistent behaviour which should
257 * occur after a HTML file export. It MUST be called at the end of the
258 * exportHTML() method implementation.
260 protected void exportCompleted()
262 if (isLaunchInBrowserAfterExport() && !isHeadless())
266 jalview.util.BrowserLauncher
267 .openURL("file:///" + getExportedFile());
268 } catch (IOException e)
276 * if this answers true then BioJSON data will be embedded to the exported
277 * HTML file otherwise it won't be embedded.
281 public abstract boolean isEmbedData();
284 * if this answers true then the generated HTML file is opened for viewing in
285 * a browser after its generation otherwise it won't be opened in a browser
289 public abstract boolean isLaunchInBrowserAfterExport();
292 * handle to the generated HTML file
296 public File getExportedFile()
298 return generatedFile;
301 public void exportHTML(String outputFile)
303 setProgressMessage(MessageManager.formatMessage(
304 "status.exporting_alignment_as_x_file", getDescription()));
307 if (outputFile == null)
310 * prompt for output file
312 outputFile = getOutputFile();
313 if (outputFile == null)
315 setProgressMessage(MessageManager.formatMessage(
316 "status.cancelled_image_export_operation",
321 generatedFile = new File(outputFile);
322 } catch (Exception e)
324 setProgressMessage(MessageManager
325 .formatMessage("info.error_creating_file", getDescription()));
329 new Thread(this).start();
334 * Answers a short description of the image format suitable for display in
339 protected final String getDescription()