Jalview 2.6 source licence
[jalview.git] / src / jalview / io / HTMLOutput.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3  * Copyright (C) 2010 J Procter, AM Waterhouse, 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(jalview.bin.Cache
47             .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
113             .println("<table border=\"0\"  cellpadding=\"0\" cellspacing=\"0\">\n");
114
115     // ////////////
116     SequenceI seq;
117     AlignmentI alignment = av.getAlignment();
118
119     // draws the top row, the measure rule
120     out.println("<tr><td colspan=\"6\"></td>");
121
122     int i = 0;
123
124     for (i = 10; i < (alignment.getWidth() - 10); i += 10)
125     {
126       out.println("<td colspan=\"9\">" + i + "<br>|</td><td></td>");
127     }
128
129     out.println("<td colspan=\"3\"></td><td colspan=\"3\">" + i
130             + "<br>|</td>");
131     out.println("</tr>");
132
133     for (i = 0; i < alignment.getHeight(); i++)
134     {
135       seq = alignment.getSequenceAt(i);
136
137       String id = seq.getDisplayId(av.getShowJVSuffix());
138
139       out.println("<tr><td nowrap>" + id + "&nbsp;&nbsp;</td>");
140
141       for (int res = 0; res < seq.getLength(); res++)
142       {
143         if (!jalview.util.Comparison.isGap(seq.getCharAt(res)))
144         {
145           color = sr.getResidueBoxColour(seq, res);
146
147           color = fr.findFeatureColour(color, seq, res);
148         }
149         else
150         {
151           color = Color.white;
152         }
153
154         if (color.getRGB() < -1)
155         {
156           out.println("<td bgcolor=\"#"
157                   + jalview.util.Format.getHexString(color) + "\">"
158                   + seq.getCharAt(res) + "</td>");
159         }
160         else
161         {
162           out.println("<td>" + seq.getCharAt(res) + "</td>");
163         }
164       }
165
166       out.println("</tr>");
167     }
168
169     // ////////////
170     out.println("</table>");
171     out.println("</td></tr></table>");
172   }
173
174   void drawWrappedAlignment(PrintWriter out)
175   {
176     // //////////////////////////////////
177     // / How many sequences and residues can we fit on a printable page?
178     AlignmentI al = av.getAlignment();
179     SequenceI seq;
180     String r;
181     String g;
182     String b;
183
184     out.println("<table border=\"1\"><tr><td>\n");
185     out
186             .println("<table border=\"0\"  cellpadding=\"0\" cellspacing=\"0\">\n");
187
188     for (int startRes = 0; startRes < al.getWidth(); startRes += av
189             .getWrappedWidth())
190     {
191       int endRes = startRes + av.getWrappedWidth();
192
193       if (endRes > al.getWidth())
194       {
195         endRes = al.getWidth();
196       }
197
198       if (av.getScaleAboveWrapped())
199       {
200         out.println("<tr>");
201
202         if (av.getScaleLeftWrapped())
203         {
204           out.println("<td colspan=\"7\">&nbsp;</td>");
205         }
206         else
207         {
208           out.println("<td colspan=\"6\">&nbsp;</td>");
209         }
210
211         for (int i = startRes + 10; i < endRes; i += 10)
212         {
213           out.println("<td colspan=\"9\">" + i + "<br>|</td><td></td>");
214         }
215
216         out.println("</tr>");
217       }
218
219       int startPos, endPos;
220       for (int s = 0; s < al.getHeight(); s++)
221       {
222         out.println("<tr>");
223         seq = al.getSequenceAt(s);
224
225         startPos = seq.findPosition(startRes);
226         endPos = seq.findPosition(endRes) - 1;
227
228         String id = seq.getDisplayId(av.getShowJVSuffix());
229
230         out.println("<td nowrap>" + id + "&nbsp;&nbsp;</td>");
231
232         if (av.getScaleLeftWrapped())
233         {
234           if (startPos > seq.getEnd() || endPos == 0)
235           {
236             out.println("<td nowrap>&nbsp;</td>");
237           }
238           else
239           {
240             out.println("<td nowrap>" + startPos + "&nbsp;&nbsp;</td>");
241           }
242         }
243
244         for (int res = startRes; res < endRes; res++)
245         {
246           if (!jalview.util.Comparison.isGap(seq.getCharAt(res)))
247           {
248             color = sr.getResidueBoxColour(seq, res);
249
250             color = fr.findFeatureColour(color, seq, res);
251           }
252           else
253           {
254             color = Color.white;
255           }
256
257           if (color.getRGB() < -1)
258           {
259             out.println("<td bgcolor=\"#"
260                     + jalview.util.Format.getHexString(color) + "\">"
261                     + seq.getCharAt(res) + "</td>");
262           }
263           else
264           {
265             out.println("<td>" + seq.getCharAt(res) + "</td>");
266           }
267
268         }
269
270         if (av.getScaleRightWrapped()
271                 && endRes < startRes + av.getWrappedWidth())
272         {
273           out.println("<td colspan=\""
274                   + (startRes + av.getWrappedWidth() - endRes) + "\">"
275                   + "&nbsp;&nbsp;</td>");
276         }
277
278         if (av.getScaleRightWrapped() && startPos < endPos)
279         {
280           out.println("<td nowrap>&nbsp;" + endPos + "&nbsp;&nbsp;</td>");
281         }
282
283         out.println("</tr>");
284       }
285
286       if (endRes < al.getWidth())
287       {
288         out.println("<tr><td height=\"5\"></td></tr>");
289       }
290     }
291
292     out.println("</table>");
293     out.println("</table>");
294   }
295
296   public static String getImageMapHTML()
297   {
298     return new String(
299             "<html>\n"
300                     + "<head>\n"
301                     + "<script language=\"JavaScript\">\n"
302                     + "var ns4 = document.layers;\n"
303                     + "var ns6 = document.getElementById && !document.all;\n"
304                     + "var ie4 = document.all;\n"
305                     + "offsetX = 0;\n"
306                     + "offsetY = 20;\n"
307                     + "var toolTipSTYLE=\"\";\n"
308                     + "function initToolTips()\n"
309                     + "{\n"
310                     + "  if(ns4||ns6||ie4)\n"
311                     + "  {\n"
312                     + "    if(ns4) toolTipSTYLE = document.toolTipLayer;\n"
313                     + "    else if(ns6) toolTipSTYLE = document.getElementById(\"toolTipLayer\").style;\n"
314                     + "    else if(ie4) toolTipSTYLE = document.all.toolTipLayer.style;\n"
315                     + "    if(ns4) document.captureEvents(Event.MOUSEMOVE);\n"
316                     + "    else\n"
317                     + "    {\n"
318                     + "      toolTipSTYLE.visibility = \"visible\";\n"
319                     + "      toolTipSTYLE.display = \"none\";\n"
320                     + "    }\n"
321                     + "    document.onmousemove = moveToMouseLoc;\n"
322                     + "  }\n"
323                     + "}\n"
324                     + "function toolTip(msg, fg, bg)\n"
325                     + "{\n"
326                     + "  if(toolTip.arguments.length < 1) // hide\n"
327                     + "  {\n"
328                     + "    if(ns4) toolTipSTYLE.visibility = \"hidden\";\n"
329                     + "    else toolTipSTYLE.display = \"none\";\n"
330                     + "  }\n"
331                     + "  else // show\n"
332                     + "  {\n"
333                     + "    if(!fg) fg = \"#555555\";\n"
334                     + "    if(!bg) bg = \"#FFFFFF\";\n"
335                     + "    var content =\n"
336                     + "    '<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" bgcolor=\"' + fg + '\"><td>' +\n"
337                     + "    '<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" bgcolor=\"' + bg + \n"
338                     + "    '\"><td align=\"center\"><font face=\"sans-serif\" color=\"' + fg +\n"
339                     + "    '\" size=\"-2\">&nbsp;' + msg +\n"
340                     + "    '&nbsp;</font></td></table></td></table>';\n"
341                     + "    if(ns4)\n"
342                     + "    {\n"
343                     + "      toolTipSTYLE.document.write(content);\n"
344                     + "      toolTipSTYLE.document.close();\n"
345                     + "      toolTipSTYLE.visibility = \"visible\";\n"
346                     + "    }\n"
347                     + "    if(ns6)\n"
348                     + "    {\n"
349                     + "      document.getElementById(\"toolTipLayer\").innerHTML = content;\n"
350                     + "      toolTipSTYLE.display='block'\n"
351                     + "    }\n"
352                     + "    if(ie4)\n"
353                     + "    {\n"
354                     + "      document.all(\"toolTipLayer\").innerHTML=content;\n"
355                     + "      toolTipSTYLE.display='block'\n"
356                     + "    }\n"
357                     + "  }\n"
358                     + "}\n"
359                     + "function moveToMouseLoc(e)\n"
360                     + "{\n"
361                     + "  if(ns4||ns6)\n"
362                     + "  {\n"
363                     + "    x = e.pageX;\n"
364                     + "    y = e.pageY;\n"
365                     + "  }\n"
366                     + "  else\n"
367                     + "  {\n"
368                     + "    x = event.x + document.body.scrollLeft;\n"
369                     + "    y = event.y + document.body.scrollTop;\n"
370                     + "  }\n"
371                     + "  toolTipSTYLE.left = x + offsetX;\n"
372                     + "  toolTipSTYLE.top = y + offsetY;\n"
373                     + "  return true;\n"
374                     + "}\n"
375                     + "</script>\n"
376                     + "</head>\n"
377                     + "<body>\n"
378                     + "<div id=\"toolTipLayer\" style=\"position:absolute; visibility: hidden\"></div>\n"
379                     + "<script language=\"JavaScript\"><!--\n"
380                     + "initToolTips(); //--></script>\n");
381
382   }
383 }