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