3 import jalview.api.AlignExportSettingI;
4 import jalview.bin.Cache;
5 import jalview.datamodel.AlignmentExportData;
6 import jalview.exceptions.NoFileSelectedException;
7 import jalview.gui.AlignmentPanel;
8 import jalview.gui.IProgressIndicator;
9 import jalview.util.MessageManager;
11 import java.io.BufferedReader;
13 import java.io.IOException;
14 import java.io.InputStreamReader;
16 import java.util.Objects;
18 public abstract class HTMLOutput implements Runnable
20 protected AlignmentPanel ap;
22 protected long pSessionId;
24 protected IProgressIndicator pIndicator;
26 protected File generatedFile;
28 public HTMLOutput(AlignmentPanel ap)
33 this.pIndicator = ap.alignFrame;
37 public String getBioJSONData()
39 return getBioJSONData(null);
42 public String getBioJSONData(AlignExportSettingI exportSettings)
48 if (exportSettings == null)
50 exportSettings = new AlignExportSettingI()
53 public boolean isExportHiddenSequences()
59 public boolean isExportHiddenColumns()
65 public boolean isExportAnnotations()
71 public boolean isExportFeatures()
77 public boolean isExportGroups()
83 public boolean isCancelled()
89 AlignmentExportData exportData = jalview.gui.AlignFrame
90 .getAlignmentForExport(FileFormat.Json,
91 ap.getAlignViewport(), exportSettings);
92 String bioJSON = new FormatAdapter(ap, exportData.getSettings())
93 .formatSequences(FileFormat.Json, exportData.getAlignment(),
94 exportData.getOmitHidden(), exportData
95 .getStartEndPostions(), ap.getAlignViewport()
96 .getColumnSelection());
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()
153 + "<script language=\"JavaScript\">\n"
154 + "var ns4 = document.layers;\n"
155 + "var ns6 = document.getElementById && !document.all;\n"
156 + "var ie4 = document.all;\n"
159 + "var toolTipSTYLE=\"\";\n"
160 + "function initToolTips()\n"
162 + " if(ns4||ns6||ie4)\n"
164 + " if(ns4) toolTipSTYLE = document.toolTipLayer;\n"
165 + " else if(ns6) toolTipSTYLE = document.getElementById(\"toolTipLayer\").style;\n"
166 + " else if(ie4) toolTipSTYLE = document.all.toolTipLayer.style;\n"
167 + " if(ns4) document.captureEvents(Event.MOUSEMOVE);\n"
170 + " toolTipSTYLE.visibility = \"visible\";\n"
171 + " toolTipSTYLE.display = \"none\";\n"
173 + " document.onmousemove = moveToMouseLoc;\n"
176 + "function toolTip(msg, fg, bg)\n"
178 + " if(toolTip.arguments.length < 1) // hide\n"
180 + " if(ns4) toolTipSTYLE.visibility = \"hidden\";\n"
181 + " else toolTipSTYLE.display = \"none\";\n"
185 + " if(!fg) fg = \"#555555\";\n"
186 + " if(!bg) bg = \"#FFFFFF\";\n"
188 + " '<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" bgcolor=\"' + fg + '\"><td>' +\n"
189 + " '<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" bgcolor=\"' + bg + \n"
190 + " '\"><td align=\"center\"><font face=\"sans-serif\" color=\"' + fg +\n"
191 + " '\" size=\"-2\"> ' + msg +\n"
192 + " ' </font></td></table></td></table>';\n"
195 + " toolTipSTYLE.document.write(content);\n"
196 + " toolTipSTYLE.document.close();\n"
197 + " toolTipSTYLE.visibility = \"visible\";\n"
201 + " document.getElementById(\"toolTipLayer\").innerHTML = content;\n"
202 + " toolTipSTYLE.display='block'\n"
206 + " document.all(\"toolTipLayer\").innerHTML=content;\n"
207 + " toolTipSTYLE.display='block'\n"
211 + "function moveToMouseLoc(e)\n"
220 + " x = event.x + document.body.scrollLeft;\n"
221 + " y = event.y + document.body.scrollTop;\n"
223 + " toolTipSTYLE.left = x + offsetX;\n"
224 + " toolTipSTYLE.top = y + offsetY;\n"
230 + "<div id=\"toolTipLayer\" style=\"position:absolute; visibility: hidden\"></div>\n"
231 + "<script language=\"JavaScript\"><!--\n"
232 + "initToolTips(); //--></script>\n");
236 public String getOutputFile() throws NoFileSelectedException
238 String selectedFile = null;
239 if (pIndicator != null && !isHeadless())
241 pIndicator.setProgressBar(MessageManager.formatMessage(
242 "status.waiting_for_user_to_select_output_file", "HTML"),
246 JalviewFileChooser jvFileChooser = new JalviewFileChooser(
247 Cache.getProperty("LAST_DIRECTORY"), "html", "HTML files");
248 jvFileChooser.setFileView(new JalviewFileView());
250 jvFileChooser.setDialogTitle(MessageManager
251 .getString("label.save_as_html"));
252 jvFileChooser.setToolTipText(MessageManager.getString("action.save"));
254 int fileChooserOpt = jvFileChooser.showSaveDialog(null);
255 if (fileChooserOpt == JalviewFileChooser.APPROVE_OPTION)
257 jalview.bin.Cache.setProperty("LAST_DIRECTORY", jvFileChooser
258 .getSelectedFile().getParent());
259 selectedFile = jvFileChooser.getSelectedFile().getPath();
263 throw new NoFileSelectedException("No file was selected.");
268 protected void setProgressMessage(String message)
270 if (pIndicator != null && !isHeadless())
272 pIndicator.setProgressBar(message, pSessionId);
276 System.out.println(message);
281 * Answers true if HTML export is invoke in headless mode or false otherwise
285 protected boolean isHeadless()
287 return System.getProperty("java.awt.headless") != null
288 && System.getProperty("java.awt.headless").equals("true");
292 * This method provides implementation of consistent behaviour which should
293 * occur before a HTML file export. It MUST be called at the start of the
294 * exportHTML() method implementation.
296 protected void exportStarted()
298 pSessionId = System.currentTimeMillis();
302 * This method provides implementation of consistent behaviour which should
303 * occur after a HTML file export. It MUST be called at the end of the
304 * exportHTML() method implementation.
306 protected void exportCompleted()
308 if (isLaunchInBrowserAfterExport() && !isHeadless())
312 jalview.util.BrowserLauncher
313 .openURL("file:///" + getExportedFile());
314 } catch (IOException e)
322 * if this answers true then BioJSON data will be embedded to the exported
323 * HTML file otherwise it won't be embedded.
327 public abstract boolean isEmbedData();
330 * if this answers true then the generated HTML file is opened for viewing in
331 * a browser after its generation otherwise it won't be opened in a browser
335 public abstract boolean isLaunchInBrowserAfterExport();
338 * handle to the generated HTML file
342 public abstract File getExportedFile();
345 * This is the main method to handle the HTML generation.
348 * the file path of the generated HTML
350 public abstract void exportHTML(String outputFile);