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