81cdd3c78fcdfe65c72bd83f66e84a24ea776276
[jalview.git] / src / jalview / io / HTMLOutput.java
1 package jalview.io;
2
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;
9
10 import java.io.BufferedReader;
11 import java.io.File;
12 import java.io.IOException;
13 import java.io.InputStreamReader;
14 import java.net.URL;
15 import java.util.Objects;
16
17 public abstract class HTMLOutput implements Runnable
18 {
19   protected AlignmentPanel ap;
20
21   protected long pSessionId;
22
23   protected IProgressIndicator pIndicator;
24
25   protected File generatedFile;
26
27   public HTMLOutput(AlignmentPanel ap)
28   {
29     if (ap != null)
30     {
31       this.ap = ap;
32       this.pIndicator = ap.alignFrame;
33     }
34   }
35
36   public String getBioJSONData()
37   {
38     return getBioJSONData(null);
39   }
40
41   public String getBioJSONData(AlignExportSettingI exportSettings)
42   {
43     if (!isEmbedData())
44     {
45       return null;
46     }
47     if (exportSettings == null)
48     {
49       exportSettings = new AlignExportSettingI()
50       {
51         @Override
52         public boolean isExportHiddenSequences()
53         {
54           return true;
55         }
56
57         @Override
58         public boolean isExportHiddenColumns()
59         {
60           return true;
61         }
62
63         @Override
64         public boolean isExportAnnotations()
65         {
66           return true;
67         }
68
69         @Override
70         public boolean isExportFeatures()
71         {
72           return true;
73         }
74
75         @Override
76         public boolean isExportGroups()
77         {
78           return true;
79         }
80
81         @Override
82         public boolean isCancelled()
83         {
84           return false;
85         }
86       };
87     }
88     AlignmentExportData exportData = jalview.gui.AlignFrame
89             .getAlignmentForExport(FileFormat.Json, ap.getAlignViewport(),
90                     exportSettings);
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());
96     return bioJSON;
97   }
98
99   /**
100    * Read a template file content as string
101    * 
102    * @param file
103    *          - the file to be read
104    * @return File content as String
105    * @throws IOException
106    */
107   public static String readFileAsString(File file) throws IOException
108   {
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();
115     if (url != null)
116     {
117       try
118       {
119         isReader = new InputStreamReader(url.openStream());
120         buffReader = new BufferedReader(isReader);
121         String line;
122         String lineSeparator = System.getProperty("line.separator");
123         while ((line = buffReader.readLine()) != null)
124         {
125           sb.append(line).append(lineSeparator);
126         }
127
128       } catch (Exception ex)
129       {
130         ex.printStackTrace();
131       } finally
132       {
133         if (isReader != null)
134         {
135           isReader.close();
136         }
137
138         if (buffReader != null)
139         {
140           buffReader.close();
141         }
142       }
143     }
144     return sb.toString();
145   }
146
147   public static String getImageMapHTML()
148   {
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"
156             + "  {\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"
161             + "    else\n" + "    {\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\">&nbsp;' + msg +\n"
176             + "    '&nbsp;</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");
198
199   }
200
201   public String getOutputFile() throws NoFileSelectedException
202   {
203     String selectedFile = null;
204     if (pIndicator != null && !isHeadless())
205     {
206       pIndicator.setProgressBar(MessageManager.formatMessage(
207               "status.waiting_for_user_to_select_output_file", "HTML"),
208               pSessionId);
209     }
210
211     JalviewFileChooser jvFileChooser = new JalviewFileChooser("html",
212             "HTML files");
213     jvFileChooser.setFileView(new JalviewFileView());
214
215     jvFileChooser
216             .setDialogTitle(MessageManager.getString("label.save_as_html"));
217     jvFileChooser.setToolTipText(MessageManager.getString("action.save"));
218
219     int fileChooserOpt = jvFileChooser.showSaveDialog(null);
220     if (fileChooserOpt == JalviewFileChooser.APPROVE_OPTION)
221     {
222       jalview.bin.Cache.setProperty("LAST_DIRECTORY",
223               jvFileChooser.getSelectedFile().getParent());
224       selectedFile = jvFileChooser.getSelectedFile().getPath();
225     }
226     else
227     {
228       throw new NoFileSelectedException("No file was selected.");
229     }
230     return selectedFile;
231   }
232
233   protected void setProgressMessage(String message)
234   {
235     if (pIndicator != null && !isHeadless())
236     {
237       pIndicator.setProgressBar(message, pSessionId);
238     }
239     else
240     {
241       System.out.println(message);
242     }
243   }
244
245   /**
246    * Answers true if HTML export is invoke in headless mode or false otherwise
247    * 
248    * @return
249    */
250   protected boolean isHeadless()
251   {
252     return System.getProperty("java.awt.headless") != null
253             && System.getProperty("java.awt.headless").equals("true");
254   }
255
256   /**
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.
260    */
261   protected void exportStarted()
262   {
263     pSessionId = System.currentTimeMillis();
264   }
265
266   /**
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.
270    */
271   protected void exportCompleted()
272   {
273     if (isLaunchInBrowserAfterExport() && !isHeadless())
274     {
275       try
276       {
277         jalview.util.BrowserLauncher
278                 .openURL("file:///" + getExportedFile());
279       } catch (IOException e)
280       {
281         e.printStackTrace();
282       }
283     }
284   }
285
286   /**
287    * if this answers true then BioJSON data will be embedded to the exported
288    * HTML file otherwise it won't be embedded.
289    * 
290    * @return
291    */
292   public abstract boolean isEmbedData();
293
294   /**
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
297    * 
298    * @return
299    */
300   public abstract boolean isLaunchInBrowserAfterExport();
301
302   /**
303    * handle to the generated HTML file
304    * 
305    * @return
306    */
307   public abstract File getExportedFile();
308
309   /**
310    * This is the main method to handle the HTML generation.
311    * 
312    * @param outputFile
313    *          the file path of the generated HTML
314    */
315   public abstract void exportHTML(String outputFile);
316 }