JAL-1503 update version in GPL header
[jalview.git] / src / jalview / io / HTMLOutput.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
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 import jalview.util.MessageManager;
28
29 public class HTMLOutput
30 {
31   AlignViewport av;
32
33   SequenceRenderer sr;
34
35   FeatureRenderer fr;
36
37   Color color;
38
39   public HTMLOutput(AlignmentPanel ap, SequenceRenderer sr,
40           FeatureRenderer fr1)
41   {
42     this.av = ap.av;
43     this.sr = sr;
44
45     fr = new FeatureRenderer(ap);
46     fr.transferSettings(fr1);
47
48     JalviewFileChooser chooser = new JalviewFileChooser(
49             jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
50             { "html" }, new String[]
51             { "HTML files" }, "HTML files");
52
53     chooser.setFileView(new JalviewFileView());
54     chooser.setDialogTitle("Save as HTML");
55     chooser.setToolTipText(MessageManager.getString("action.save"));
56
57     int value = chooser.showSaveDialog(null);
58
59     if (value == JalviewFileChooser.APPROVE_OPTION)
60     {
61       String choice = chooser.getSelectedFile().getPath();
62       jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
63               .getSelectedFile().getParent());
64
65       try
66       {
67         PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(
68                 choice));
69         out.println("<HTML>");
70         out.println("<style type=\"text/css\">");
71         out.println("<!--");
72         out.print("td {font-family: \"" + av.getFont().getFamily()
73                 + "\", \"" + av.getFont().getName() + "\", mono; "
74                 + "font-size: " + av.getFont().getSize() + "px; ");
75
76         if (av.getFont().getStyle() == Font.BOLD)
77         {
78           out.print("font-weight: BOLD; ");
79         }
80
81         if (av.getFont().getStyle() == Font.ITALIC)
82         {
83           out.print("font-style: italic; ");
84         }
85
86         out.println("text-align: center; }");
87
88         out.println("-->");
89         out.println("</style>");
90         out.println("<BODY>");
91
92         if (av.getWrapAlignment())
93         {
94           drawWrappedAlignment(out);
95         }
96         else
97         {
98           drawUnwrappedAlignment(out);
99         }
100
101         out.println("\n</body>\n</html>");
102         out.close();
103         jalview.util.BrowserLauncher.openURL("file:///" + choice);
104       } catch (Exception ex)
105       {
106         ex.printStackTrace();
107       }
108     }
109   }
110
111   void drawUnwrappedAlignment(PrintWriter out)
112   {
113     out.println("<table border=\"1\"><tr><td>\n");
114     out.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.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 }