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