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, ap.getAlignViewport(),
91 String bioJSON = new FormatAdapter(ap, exportData.getSettings())
92 .formatSequences(FileFormat.Json, exportData.getAlignment(),
93 exportData.getOmitHidden(),
94 exportData.getStartEndPostions(), ap.getAlignViewport()
95 .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");
201 public String getOutputFile() throws NoFileSelectedException
203 String selectedFile = null;
204 if (pIndicator != null && !isHeadless())
206 pIndicator.setProgressBar(MessageManager.formatMessage(
207 "status.waiting_for_user_to_select_output_file", "HTML"),
211 JalviewFileChooser jvFileChooser = new JalviewFileChooser("html",
213 jvFileChooser.setFileView(new JalviewFileView());
216 .setDialogTitle(MessageManager.getString("label.save_as_html"));
217 jvFileChooser.setToolTipText(MessageManager.getString("action.save"));
219 int fileChooserOpt = jvFileChooser.showSaveDialog(null);
220 if (fileChooserOpt == JalviewFileChooser.APPROVE_OPTION)
222 jalview.bin.Cache.setProperty("LAST_DIRECTORY",
223 jvFileChooser.getSelectedFile().getParent());
224 selectedFile = jvFileChooser.getSelectedFile().getPath();
228 throw new NoFileSelectedException("No file was selected.");
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 before a HTML file export. It MUST be called at the start of the
259 * exportHTML() method implementation.
261 protected void exportStarted()
263 pSessionId = System.currentTimeMillis();
267 * This method provides implementation of consistent behaviour which should
268 * occur after a HTML file export. It MUST be called at the end of the
269 * exportHTML() method implementation.
271 protected void exportCompleted()
273 if (isLaunchInBrowserAfterExport() && !isHeadless())
277 jalview.util.BrowserLauncher
278 .openURL("file:///" + getExportedFile());
279 } catch (IOException e)
287 * if this answers true then BioJSON data will be embedded to the exported
288 * HTML file otherwise it won't be embedded.
292 public abstract boolean isEmbedData();
295 * if this answers true then the generated HTML file is opened for viewing in
296 * a browser after its generation otherwise it won't be opened in a browser
300 public abstract boolean isLaunchInBrowserAfterExport();
303 * handle to the generated HTML file
307 public abstract File getExportedFile();
310 * This is the main method to handle the HTML generation.
313 * the file path of the generated HTML
315 public abstract void exportHTML(String outputFile);