3 import jalview.api.AlignExportSettingI;
4 import jalview.datamodel.AlignmentExportData;
5 import jalview.exceptions.NoFileSelectedException;
6 import jalview.gui.AlignmentPanel;
7 import jalview.gui.IProgressIndicator;
8 import jalview.util.MessageManager;
10 import java.io.BufferedReader;
12 import java.io.IOException;
13 import java.io.InputStreamReader;
15 import java.util.Objects;
17 public abstract class HTMLOutput implements Runnable
19 protected AlignmentPanel ap;
21 protected long pSessionId;
23 protected IProgressIndicator pIndicator;
25 protected File generatedFile;
27 public HTMLOutput(AlignmentPanel ap)
32 this.pIndicator = ap.alignFrame;
36 public String getBioJSONData()
38 return getBioJSONData(null);
41 public String getBioJSONData(AlignExportSettingI exportSettings)
47 if (exportSettings == null)
49 exportSettings = new AlignExportSettingI()
52 public boolean isExportHiddenSequences()
58 public boolean isExportHiddenColumns()
64 public boolean isExportAnnotations()
70 public boolean isExportFeatures()
76 public boolean isExportGroups()
82 public boolean isCancelled()
88 AlignmentExportData exportData = jalview.gui.AlignFrame
89 .getAlignmentForExport(FileFormat.Json,
90 ap.getAlignViewport(), exportSettings);
91 String bioJSON = new FormatAdapter(ap, exportData.getSettings())
92 .formatSequences(FileFormat.Json, exportData.getAlignment(),
93 exportData.getOmitHidden(), exportData
94 .getStartEndPostions(), ap.getAlignViewport()
95 .getColumnSelection());
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()
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"
158 + "var toolTipSTYLE=\"\";\n"
159 + "function initToolTips()\n"
161 + " if(ns4||ns6||ie4)\n"
163 + " if(ns4) toolTipSTYLE = document.toolTipLayer;\n"
164 + " else if(ns6) toolTipSTYLE = document.getElementById(\"toolTipLayer\").style;\n"
165 + " else if(ie4) toolTipSTYLE = document.all.toolTipLayer.style;\n"
166 + " if(ns4) document.captureEvents(Event.MOUSEMOVE);\n"
169 + " toolTipSTYLE.visibility = \"visible\";\n"
170 + " toolTipSTYLE.display = \"none\";\n"
172 + " document.onmousemove = moveToMouseLoc;\n"
175 + "function toolTip(msg, fg, bg)\n"
177 + " if(toolTip.arguments.length < 1) // hide\n"
179 + " if(ns4) toolTipSTYLE.visibility = \"hidden\";\n"
180 + " else toolTipSTYLE.display = \"none\";\n"
184 + " if(!fg) fg = \"#555555\";\n"
185 + " if(!bg) bg = \"#FFFFFF\";\n"
187 + " '<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" bgcolor=\"' + fg + '\"><td>' +\n"
188 + " '<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" bgcolor=\"' + bg + \n"
189 + " '\"><td align=\"center\"><font face=\"sans-serif\" color=\"' + fg +\n"
190 + " '\" size=\"-2\"> ' + msg +\n"
191 + " ' </font></td></table></td></table>';\n"
194 + " toolTipSTYLE.document.write(content);\n"
195 + " toolTipSTYLE.document.close();\n"
196 + " toolTipSTYLE.visibility = \"visible\";\n"
200 + " document.getElementById(\"toolTipLayer\").innerHTML = content;\n"
201 + " toolTipSTYLE.display='block'\n"
205 + " document.all(\"toolTipLayer\").innerHTML=content;\n"
206 + " toolTipSTYLE.display='block'\n"
210 + "function moveToMouseLoc(e)\n"
219 + " x = event.x + document.body.scrollLeft;\n"
220 + " y = event.y + document.body.scrollTop;\n"
222 + " toolTipSTYLE.left = x + offsetX;\n"
223 + " toolTipSTYLE.top = y + offsetY;\n"
229 + "<div id=\"toolTipLayer\" style=\"position:absolute; visibility: hidden\"></div>\n"
230 + "<script language=\"JavaScript\"><!--\n"
231 + "initToolTips(); //--></script>\n");
235 public String getOutputFile() throws NoFileSelectedException
237 String selectedFile = null;
238 if (pIndicator != null && !isHeadless())
240 pIndicator.setProgressBar(MessageManager.formatMessage(
241 "status.waiting_for_user_to_select_output_file", "HTML"),
245 JalviewFileChooser jvFileChooser = new JalviewFileChooser(
246 jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
247 new String[] { "html" }, new String[] { "HTML files" },
249 jvFileChooser.setFileView(new JalviewFileView());
251 jvFileChooser.setDialogTitle(MessageManager
252 .getString("label.save_as_html"));
253 jvFileChooser.setToolTipText(MessageManager.getString("action.save"));
255 int fileChooserOpt = jvFileChooser.showSaveDialog(null);
256 if (fileChooserOpt == JalviewFileChooser.APPROVE_OPTION)
258 jalview.bin.Cache.setProperty("LAST_DIRECTORY", jvFileChooser
259 .getSelectedFile().getParent());
260 selectedFile = jvFileChooser.getSelectedFile().getPath();
264 throw new NoFileSelectedException("No file was selected.");
269 protected void setProgressMessage(String message)
271 if (pIndicator != null && !isHeadless())
273 pIndicator.setProgressBar(message, pSessionId);
277 System.out.println(message);
282 * Answers true if HTML export is invoke in headless mode or false otherwise
286 protected boolean isHeadless()
288 return System.getProperty("java.awt.headless") != null
289 && System.getProperty("java.awt.headless").equals("true");
293 * This method provides implementation of consistent behaviour which should
294 * occur before a HTML file export. It MUST be called at the start of the
295 * exportHTML() method implementation.
297 protected void exportStarted()
299 pSessionId = System.currentTimeMillis();
303 * This method provides implementation of consistent behaviour which should
304 * occur after a HTML file export. It MUST be called at the end of the
305 * exportHTML() method implementation.
307 protected void exportCompleted()
309 if (isLaunchInBrowserAfterExport() && !isHeadless())
313 jalview.util.BrowserLauncher
314 .openURL("file:///" + getExportedFile());
315 } catch (IOException e)
323 * if this answers true then BioJSON data will be embedded to the exported
324 * HTML file otherwise it won't be embedded.
328 public abstract boolean isEmbedData();
331 * if this answers true then the generated HTML file is opened for viewing in
332 * a browser after its generation otherwise it won't be opened in a browser
336 public abstract boolean isLaunchInBrowserAfterExport();
339 * handle to the generated HTML file
343 public abstract File getExportedFile();
346 * This is the main method to handle the HTML generation.
349 * the file path of the generated HTML
351 public abstract void exportHTML(String outputFile);