apply gpl development license
[jalview.git] / src / jalview / io / HTMLOutput.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3  * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.io;
20
21 import java.io.*;
22
23 import java.awt.*;
24
25 import jalview.datamodel.*;
26 import jalview.gui.*;
27
28 public class HTMLOutput
29 {
30   AlignViewport av;
31
32   SequenceRenderer sr;
33
34   FeatureRenderer fr;
35
36   Color color;
37
38   public HTMLOutput(AlignmentPanel ap, SequenceRenderer sr,
39           FeatureRenderer fr1)
40   {
41     this.av = ap.av;
42     this.sr = sr;
43
44     fr = new FeatureRenderer(ap);
45     fr.transferSettings(fr1);
46
47     JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache
48             .getProperty("LAST_DIRECTORY"), new String[]
49     { "html" }, new String[]
50     { "HTML files" }, "HTML files");
51
52     chooser.setFileView(new JalviewFileView());
53     chooser.setDialogTitle("Save as HTML");
54     chooser.setToolTipText("Save");
55
56     int value = chooser.showSaveDialog(null);
57
58     if (value == JalviewFileChooser.APPROVE_OPTION)
59     {
60       String choice = chooser.getSelectedFile().getPath();
61       jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
62               .getSelectedFile().getParent());
63
64       try
65       {
66         PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(
67                 choice));
68         out.println("<HTML>");
69         out.println("<style type=\"text/css\">");
70         out.println("<!--");
71         out.print("td {font-family: \"" + av.getFont().getFamily()
72                 + "\", \"" + av.getFont().getName() + "\", mono; "
73                 + "font-size: " + av.getFont().getSize() + "px; ");
74
75         if (av.getFont().getStyle() == Font.BOLD)
76         {
77           out.print("font-weight: BOLD; ");
78         }
79
80         if (av.getFont().getStyle() == Font.ITALIC)
81         {
82           out.print("font-style: italic; ");
83         }
84
85         out.println("text-align: center; }");
86
87         out.println("-->");
88         out.println("</style>");
89         out.println("<BODY>");
90
91         if (av.getWrapAlignment())
92         {
93           drawWrappedAlignment(out);
94         }
95         else
96         {
97           drawUnwrappedAlignment(out);
98         }
99
100         out.println("\n</body>\n</html>");
101         out.close();
102         jalview.util.BrowserLauncher.openURL("file:///" + choice);
103       } catch (Exception ex)
104       {
105         ex.printStackTrace();
106       }
107     }
108   }
109
110   void drawUnwrappedAlignment(PrintWriter out)
111   {
112     out.println("<table border=\"1\"><tr><td>\n");
113     out
114             .println("<table border=\"0\"  cellpadding=\"0\" cellspacing=\"0\">\n");
115
116     // ////////////
117     SequenceI seq;
118     AlignmentI alignment = av.getAlignment();
119
120     // draws the top row, the measure rule
121     out.println("<tr><td colspan=\"6\"></td>");
122
123     int i = 0;
124
125     for (i = 10; i < (alignment.getWidth() - 10); i += 10)
126     {
127       out.println("<td colspan=\"9\">" + i + "<br>|</td><td></td>");
128     }
129
130     out.println("<td colspan=\"3\"></td><td colspan=\"3\">" + i
131             + "<br>|</td>");
132     out.println("</tr>");
133
134     for (i = 0; i < alignment.getHeight(); i++)
135     {
136       seq = alignment.getSequenceAt(i);
137
138       String id = seq.getDisplayId(av.getShowJVSuffix());
139
140       out.println("<tr><td nowrap>" + id + "&nbsp;&nbsp;</td>");
141
142       for (int res = 0; res < seq.getLength(); res++)
143       {
144         if (!jalview.util.Comparison.isGap(seq.getCharAt(res)))
145         {
146           color = sr.getResidueBoxColour(seq, res);
147
148           color = fr.findFeatureColour(color, seq, res);
149         }
150         else
151         {
152           color = Color.white;
153         }
154
155         if (color.getRGB() < -1)
156         {
157           out.println("<td bgcolor=\"#"
158                   + jalview.util.Format.getHexString(color) + "\">"
159                   + seq.getCharAt(res) + "</td>");
160         }
161         else
162         {
163           out.println("<td>" + seq.getCharAt(res) + "</td>");
164         }
165       }
166
167       out.println("</tr>");
168     }
169
170     // ////////////
171     out.println("</table>");
172     out.println("</td></tr></table>");
173   }
174
175   void drawWrappedAlignment(PrintWriter out)
176   {
177     // //////////////////////////////////
178     // / How many sequences and residues can we fit on a printable page?
179     AlignmentI al = av.getAlignment();
180     SequenceI seq;
181     String r;
182     String g;
183     String b;
184
185     out.println("<table border=\"1\"><tr><td>\n");
186     out
187             .println("<table border=\"0\"  cellpadding=\"0\" cellspacing=\"0\">\n");
188
189     for (int startRes = 0; startRes < al.getWidth(); startRes += av
190             .getWrappedWidth())
191     {
192       int endRes = startRes + av.getWrappedWidth();
193
194       if (endRes > al.getWidth())
195       {
196         endRes = al.getWidth();
197       }
198
199       if (av.getScaleAboveWrapped())
200       {
201         out.println("<tr>");
202
203         if (av.getScaleLeftWrapped())
204         {
205           out.println("<td colspan=\"7\">&nbsp;</td>");
206         }
207         else
208         {
209           out.println("<td colspan=\"6\">&nbsp;</td>");
210         }
211
212         for (int i = startRes + 10; i < endRes; i += 10)
213         {
214           out.println("<td colspan=\"9\">" + i + "<br>|</td><td></td>");
215         }
216
217         out.println("</tr>");
218       }
219
220       int startPos, endPos;
221       for (int s = 0; s < al.getHeight(); s++)
222       {
223         out.println("<tr>");
224         seq = al.getSequenceAt(s);
225
226         startPos = seq.findPosition(startRes);
227         endPos = seq.findPosition(endRes) - 1;
228
229         String id = seq.getDisplayId(av.getShowJVSuffix());
230
231         out.println("<td nowrap>" + id + "&nbsp;&nbsp;</td>");
232
233         if (av.getScaleLeftWrapped())
234         {
235           if (startPos > seq.getEnd() || endPos == 0)
236           {
237             out.println("<td nowrap>&nbsp;</td>");
238           }
239           else
240           {
241             out.println("<td nowrap>" + startPos + "&nbsp;&nbsp;</td>");
242           }
243         }
244
245         for (int res = startRes; res < endRes; res++)
246         {
247           if (!jalview.util.Comparison.isGap(seq.getCharAt(res)))
248           {
249             color = sr.getResidueBoxColour(seq, res);
250
251             color = fr.findFeatureColour(color, seq, res);
252           }
253           else
254           {
255             color = Color.white;
256           }
257
258           if (color.getRGB() < -1)
259           {
260             out.println("<td bgcolor=\"#"
261                     + jalview.util.Format.getHexString(color) + "\">"
262                     + seq.getCharAt(res) + "</td>");
263           }
264           else
265           {
266             out.println("<td>" + seq.getCharAt(res) + "</td>");
267           }
268
269         }
270
271         if (av.getScaleRightWrapped()
272                 && endRes < startRes + av.getWrappedWidth())
273         {
274           out.println("<td colspan=\""
275                   + (startRes + av.getWrappedWidth() - endRes) + "\">"
276                   + "&nbsp;&nbsp;</td>");
277         }
278
279         if (av.getScaleRightWrapped() && startPos < endPos)
280         {
281           out.println("<td nowrap>&nbsp;" + endPos + "&nbsp;&nbsp;</td>");
282         }
283
284         out.println("</tr>");
285       }
286
287       if (endRes < al.getWidth())
288       {
289         out.println("<tr><td height=\"5\"></td></tr>");
290       }
291     }
292
293     out.println("</table>");
294     out.println("</table>");
295   }
296
297   public static String getImageMapHTML()
298   {
299     return new String(
300             "<html>\n"
301                     + "<head>\n"
302                     + "<script language=\"JavaScript\">\n"
303                     + "var ns4 = document.layers;\n"
304                     + "var ns6 = document.getElementById && !document.all;\n"
305                     + "var ie4 = document.all;\n"
306                     + "offsetX = 0;\n"
307                     + "offsetY = 20;\n"
308                     + "var toolTipSTYLE=\"\";\n"
309                     + "function initToolTips()\n"
310                     + "{\n"
311                     + "  if(ns4||ns6||ie4)\n"
312                     + "  {\n"
313                     + "    if(ns4) toolTipSTYLE = document.toolTipLayer;\n"
314                     + "    else if(ns6) toolTipSTYLE = document.getElementById(\"toolTipLayer\").style;\n"
315                     + "    else if(ie4) toolTipSTYLE = document.all.toolTipLayer.style;\n"
316                     + "    if(ns4) document.captureEvents(Event.MOUSEMOVE);\n"
317                     + "    else\n"
318                     + "    {\n"
319                     + "      toolTipSTYLE.visibility = \"visible\";\n"
320                     + "      toolTipSTYLE.display = \"none\";\n"
321                     + "    }\n"
322                     + "    document.onmousemove = moveToMouseLoc;\n"
323                     + "  }\n"
324                     + "}\n"
325                     + "function toolTip(msg, fg, bg)\n"
326                     + "{\n"
327                     + "  if(toolTip.arguments.length < 1) // hide\n"
328                     + "  {\n"
329                     + "    if(ns4) toolTipSTYLE.visibility = \"hidden\";\n"
330                     + "    else toolTipSTYLE.display = \"none\";\n"
331                     + "  }\n"
332                     + "  else // show\n"
333                     + "  {\n"
334                     + "    if(!fg) fg = \"#555555\";\n"
335                     + "    if(!bg) bg = \"#FFFFFF\";\n"
336                     + "    var content =\n"
337                     + "    '<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" bgcolor=\"' + fg + '\"><td>' +\n"
338                     + "    '<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" bgcolor=\"' + bg + \n"
339                     + "    '\"><td align=\"center\"><font face=\"sans-serif\" color=\"' + fg +\n"
340                     + "    '\" size=\"-2\">&nbsp;' + msg +\n"
341                     + "    '&nbsp;</font></td></table></td></table>';\n"
342                     + "    if(ns4)\n"
343                     + "    {\n"
344                     + "      toolTipSTYLE.document.write(content);\n"
345                     + "      toolTipSTYLE.document.close();\n"
346                     + "      toolTipSTYLE.visibility = \"visible\";\n"
347                     + "    }\n"
348                     + "    if(ns6)\n"
349                     + "    {\n"
350                     + "      document.getElementById(\"toolTipLayer\").innerHTML = content;\n"
351                     + "      toolTipSTYLE.display='block'\n"
352                     + "    }\n"
353                     + "    if(ie4)\n"
354                     + "    {\n"
355                     + "      document.all(\"toolTipLayer\").innerHTML=content;\n"
356                     + "      toolTipSTYLE.display='block'\n"
357                     + "    }\n"
358                     + "  }\n"
359                     + "}\n"
360                     + "function moveToMouseLoc(e)\n"
361                     + "{\n"
362                     + "  if(ns4||ns6)\n"
363                     + "  {\n"
364                     + "    x = e.pageX;\n"
365                     + "    y = e.pageY;\n"
366                     + "  }\n"
367                     + "  else\n"
368                     + "  {\n"
369                     + "    x = event.x + document.body.scrollLeft;\n"
370                     + "    y = event.y + document.body.scrollTop;\n"
371                     + "  }\n"
372                     + "  toolTipSTYLE.left = x + offsetX;\n"
373                     + "  toolTipSTYLE.top = y + offsetY;\n"
374                     + "  return true;\n"
375                     + "}\n"
376                     + "</script>\n"
377                     + "</head>\n"
378                     + "<body>\n"
379                     + "<div id=\"toolTipLayer\" style=\"position:absolute; visibility: hidden\"></div>\n"
380                     + "<script language=\"JavaScript\"><!--\n"
381                     + "initToolTips(); //--></script>\n");
382
383   }
384 }