JAL-1432 updated copyright notices
[jalview.git] / src / jalview / io / HTMLOutput.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3  * Copyright (C) 2014 The Jalview Authors
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  * The Jalview Authors are detailed in the 'AUTHORS' file.
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(
48             jalview.bin.Cache.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.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.println("<table border=\"0\"  cellpadding=\"0\" cellspacing=\"0\">\n");
186
187     for (int startRes = 0; startRes < al.getWidth(); startRes += av
188             .getWrappedWidth())
189     {
190       int endRes = startRes + av.getWrappedWidth();
191
192       if (endRes > al.getWidth())
193       {
194         endRes = al.getWidth();
195       }
196
197       if (av.getScaleAboveWrapped())
198       {
199         out.println("<tr>");
200
201         if (av.getScaleLeftWrapped())
202         {
203           out.println("<td colspan=\"7\">&nbsp;</td>");
204         }
205         else
206         {
207           out.println("<td colspan=\"6\">&nbsp;</td>");
208         }
209
210         for (int i = startRes + 10; i < endRes; i += 10)
211         {
212           out.println("<td colspan=\"9\">" + i + "<br>|</td><td></td>");
213         }
214
215         out.println("</tr>");
216       }
217
218       int startPos, endPos;
219       for (int s = 0; s < al.getHeight(); s++)
220       {
221         out.println("<tr>");
222         seq = al.getSequenceAt(s);
223
224         startPos = seq.findPosition(startRes);
225         endPos = seq.findPosition(endRes) - 1;
226
227         String id = seq.getDisplayId(av.getShowJVSuffix());
228
229         out.println("<td nowrap>" + id + "&nbsp;&nbsp;</td>");
230
231         if (av.getScaleLeftWrapped())
232         {
233           if (startPos > seq.getEnd() || endPos == 0)
234           {
235             out.println("<td nowrap>&nbsp;</td>");
236           }
237           else
238           {
239             out.println("<td nowrap>" + startPos + "&nbsp;&nbsp;</td>");
240           }
241         }
242
243         for (int res = startRes; res < endRes; res++)
244         {
245           if (!jalview.util.Comparison.isGap(seq.getCharAt(res)))
246           {
247             color = sr.getResidueBoxColour(seq, res);
248
249             color = fr.findFeatureColour(color, seq, res);
250           }
251           else
252           {
253             color = Color.white;
254           }
255
256           if (color.getRGB() < -1)
257           {
258             out.println("<td bgcolor=\"#"
259                     + jalview.util.Format.getHexString(color) + "\">"
260                     + seq.getCharAt(res) + "</td>");
261           }
262           else
263           {
264             out.println("<td>" + seq.getCharAt(res) + "</td>");
265           }
266
267         }
268
269         if (av.getScaleRightWrapped()
270                 && endRes < startRes + av.getWrappedWidth())
271         {
272           out.println("<td colspan=\""
273                   + (startRes + av.getWrappedWidth() - endRes) + "\">"
274                   + "&nbsp;&nbsp;</td>");
275         }
276
277         if (av.getScaleRightWrapped() && startPos < endPos)
278         {
279           out.println("<td nowrap>&nbsp;" + endPos + "&nbsp;&nbsp;</td>");
280         }
281
282         out.println("</tr>");
283       }
284
285       if (endRes < al.getWidth())
286       {
287         out.println("<tr><td height=\"5\"></td></tr>");
288       }
289     }
290
291     out.println("</table>");
292     out.println("</table>");
293   }
294
295   public static String getImageMapHTML()
296   {
297     return new String(
298             "<html>\n"
299                     + "<head>\n"
300                     + "<script language=\"JavaScript\">\n"
301                     + "var ns4 = document.layers;\n"
302                     + "var ns6 = document.getElementById && !document.all;\n"
303                     + "var ie4 = document.all;\n"
304                     + "offsetX = 0;\n"
305                     + "offsetY = 20;\n"
306                     + "var toolTipSTYLE=\"\";\n"
307                     + "function initToolTips()\n"
308                     + "{\n"
309                     + "  if(ns4||ns6||ie4)\n"
310                     + "  {\n"
311                     + "    if(ns4) toolTipSTYLE = document.toolTipLayer;\n"
312                     + "    else if(ns6) toolTipSTYLE = document.getElementById(\"toolTipLayer\").style;\n"
313                     + "    else if(ie4) toolTipSTYLE = document.all.toolTipLayer.style;\n"
314                     + "    if(ns4) document.captureEvents(Event.MOUSEMOVE);\n"
315                     + "    else\n"
316                     + "    {\n"
317                     + "      toolTipSTYLE.visibility = \"visible\";\n"
318                     + "      toolTipSTYLE.display = \"none\";\n"
319                     + "    }\n"
320                     + "    document.onmousemove = moveToMouseLoc;\n"
321                     + "  }\n"
322                     + "}\n"
323                     + "function toolTip(msg, fg, bg)\n"
324                     + "{\n"
325                     + "  if(toolTip.arguments.length < 1) // hide\n"
326                     + "  {\n"
327                     + "    if(ns4) toolTipSTYLE.visibility = \"hidden\";\n"
328                     + "    else toolTipSTYLE.display = \"none\";\n"
329                     + "  }\n"
330                     + "  else // show\n"
331                     + "  {\n"
332                     + "    if(!fg) fg = \"#555555\";\n"
333                     + "    if(!bg) bg = \"#FFFFFF\";\n"
334                     + "    var content =\n"
335                     + "    '<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" bgcolor=\"' + fg + '\"><td>' +\n"
336                     + "    '<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" bgcolor=\"' + bg + \n"
337                     + "    '\"><td align=\"center\"><font face=\"sans-serif\" color=\"' + fg +\n"
338                     + "    '\" size=\"-2\">&nbsp;' + msg +\n"
339                     + "    '&nbsp;</font></td></table></td></table>';\n"
340                     + "    if(ns4)\n"
341                     + "    {\n"
342                     + "      toolTipSTYLE.document.write(content);\n"
343                     + "      toolTipSTYLE.document.close();\n"
344                     + "      toolTipSTYLE.visibility = \"visible\";\n"
345                     + "    }\n"
346                     + "    if(ns6)\n"
347                     + "    {\n"
348                     + "      document.getElementById(\"toolTipLayer\").innerHTML = content;\n"
349                     + "      toolTipSTYLE.display='block'\n"
350                     + "    }\n"
351                     + "    if(ie4)\n"
352                     + "    {\n"
353                     + "      document.all(\"toolTipLayer\").innerHTML=content;\n"
354                     + "      toolTipSTYLE.display='block'\n"
355                     + "    }\n"
356                     + "  }\n"
357                     + "}\n"
358                     + "function moveToMouseLoc(e)\n"
359                     + "{\n"
360                     + "  if(ns4||ns6)\n"
361                     + "  {\n"
362                     + "    x = e.pageX;\n"
363                     + "    y = e.pageY;\n"
364                     + "  }\n"
365                     + "  else\n"
366                     + "  {\n"
367                     + "    x = event.x + document.body.scrollLeft;\n"
368                     + "    y = event.y + document.body.scrollTop;\n"
369                     + "  }\n"
370                     + "  toolTipSTYLE.left = x + offsetX;\n"
371                     + "  toolTipSTYLE.top = y + offsetY;\n"
372                     + "  return true;\n"
373                     + "}\n"
374                     + "</script>\n"
375                     + "</head>\n"
376                     + "<body>\n"
377                     + "<div id=\"toolTipLayer\" style=\"position:absolute; visibility: hidden\"></div>\n"
378                     + "<script language=\"JavaScript\"><!--\n"
379                     + "initToolTips(); //--></script>\n");
380
381   }
382 }