updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / jalview / io / HTMLOutput.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
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   SequenceRenderer sr;
32   FeatureRenderer fr;
33   Color color;
34
35   public HTMLOutput(AlignViewport av, SequenceRenderer sr, FeatureRenderer fr1)
36   {
37     this.av = av;
38     this.sr = sr;
39
40     fr = new FeatureRenderer(av);
41     fr.transferSettings(fr1);
42
43     JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.
44         getProperty(
45             "LAST_DIRECTORY"), new String[]
46         {"html"},
47         new String[]
48         {"HTML files"}, "HTML files");
49
50     chooser.setFileView(new JalviewFileView());
51     chooser.setDialogTitle("Save as HTML");
52     chooser.setToolTipText("Save");
53
54     int value = chooser.showSaveDialog(null);
55
56     if (value == JalviewFileChooser.APPROVE_OPTION)
57     {
58       String choice = chooser.getSelectedFile().getPath();
59       jalview.bin.Cache.setProperty("LAST_DIRECTORY",
60                                     chooser.getSelectedFile().getParent());
61
62       try
63       {
64         PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(
65             choice));
66         out.println("<HTML>");
67         out.println("<style type=\"text/css\">");
68         out.println("<!--");
69         out.print("td {font-family: \"" + av.getFont().getFamily() +
70                   "\", \"" + av.getFont().getName() + "\", mono; " +
71                   "font-size: " + av.getFont().getSize() + "px; ");
72
73         if (av.getFont().getStyle() == Font.BOLD)
74         {
75           out.print("font-weight: BOLD; ");
76         }
77
78         if (av.getFont().getStyle() == Font.ITALIC)
79         {
80           out.print("font-style: italic; ");
81         }
82
83         out.println("text-align: center; }");
84
85         out.println("-->");
86         out.println("</style>");
87         out.println("<BODY>");
88
89         if (av.getWrapAlignment())
90         {
91           drawWrappedAlignment(out);
92         }
93         else
94         {
95           drawUnwrappedAlignment(out);
96         }
97
98         out.println("\n</body>\n</html>");
99         out.close();
100         jalview.util.BrowserLauncher.openURL("file:///" + choice);
101       }
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.println(
113         "<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 +
140                     "&nbsp;&nbsp;</td>");
141
142
143       for (int res = 0; res < seq.getLength(); res++)
144       {
145         if (!jalview.util.Comparison.isGap(seq.getCharAt(res)))
146         {
147           color = sr.getResidueBoxColour(seq, res);
148
149           color = fr.findFeatureColour(color, seq, res);
150         }
151         else
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(
187         "<table border=\"0\"  cellpadding=\"0\" cellspacing=\"0\">\n");
188
189     for (int startRes = 0; startRes < al.getWidth();
190          startRes += av.getWrappedWidth())
191     {
192       int endRes = startRes + av.getWrappedWidth();
193
194       if (endRes > al.getWidth())
195       {
196         endRes = al.getWidth();
197       }
198
199       if(av.getScaleAboveWrapped())
200       {
201         out.println("<tr>");
202
203         if (av.getScaleLeftWrapped())
204           out.println("<td colspan=\"7\">&nbsp;</td>");
205         else
206           out.println("<td colspan=\"6\">&nbsp;</td>");
207
208
209         for (int i = startRes + 10; i < endRes; i += 10)
210         {
211           out.println("<td colspan=\"9\">" + i + "<br>|</td><td></td>");
212         }
213
214         out.println("</tr>");
215       }
216
217       int startPos, endPos;
218       for (int s = 0; s < al.getHeight(); s++)
219       {
220         out.println("<tr>");
221         seq = al.getSequenceAt(s);
222
223         startPos = seq.findPosition( startRes );
224         endPos =   seq.findPosition( endRes )-1;
225
226         String id = seq.getDisplayId(av.getShowJVSuffix());
227
228         out.println("<td nowrap>" + id +
229                       "&nbsp;&nbsp;</td>");
230
231
232         if(av.getScaleLeftWrapped())
233         {
234           if(startPos > seq.getEnd() || endPos==0)
235             out.println("<td nowrap>&nbsp;</td>");
236           else
237             out.println("<td nowrap>" + startPos +
238                       "&nbsp;&nbsp;</td>");
239         }
240
241
242         for (int res = startRes; res < endRes; res++)
243         {
244           if (!jalview.util.Comparison.isGap(seq.getCharAt(res)))
245           {
246             color = sr.getResidueBoxColour(seq, res);
247
248             color = fr.findFeatureColour(color, seq, res);
249           }
250           else
251             color = Color.white;
252
253           if (color.getRGB() < -1)
254           {
255             r = Integer.toHexString(color.getRed());
256
257             if (r.length() < 2)
258             {
259               r = "0" + r;
260             }
261
262             g = Integer.toHexString(color.getGreen());
263
264             if (g.length() < 2)
265             {
266               g = "0" + g;
267             }
268
269             b = Integer.toHexString(color.getBlue());
270
271             if (b.length() < 2)
272             {
273               b = "0" + b;
274             }
275
276             out.println("<td bgcolor=\"#" + r + g + b + "\">" +
277                         seq.getCharAt(res) + "</td>");
278           }
279           else
280           {
281             out.println("<td>" + seq.getCharAt(res) + "</td>");
282           }
283
284
285         }
286
287         if(av.getScaleRightWrapped() &&
288            endRes < startRes + av.getWrappedWidth())
289         {
290          out.println("<td colspan=\""+ (startRes+av.getWrappedWidth()-endRes) +"\">"
291                      +"&nbsp;&nbsp;</td>");
292        }
293
294
295         if(av.getScaleRightWrapped() && startPos<endPos)
296         {
297           out.println("<td nowrap>&nbsp;" + endPos +
298                       "&nbsp;&nbsp;</td>");
299         }
300
301
302         out.println("</tr>");
303       }
304
305       if (endRes < al.getWidth())
306       {
307         out.println("<tr><td height=\"5\"></td></tr>");
308       }
309     }
310
311     out.println("</table>");
312     out.println("</table>");
313   }
314
315   public static String getImageMapHTML()
316   {
317     return new String(
318       "<html>\n"
319       +"<head>\n"
320       +"<script language=\"JavaScript\">\n"
321       +"var ns4 = document.layers;\n"
322       +"var ns6 = document.getElementById && !document.all;\n"
323       +"var ie4 = document.all;\n"
324       +"offsetX = 0;\n"
325       +"offsetY = 20;\n"
326       +"var toolTipSTYLE=\"\";\n"
327       +"function initToolTips()\n"
328       +"{\n"
329       +"  if(ns4||ns6||ie4)\n"
330       +"  {\n"
331       +"    if(ns4) toolTipSTYLE = document.toolTipLayer;\n"
332       +"    else if(ns6) toolTipSTYLE = document.getElementById(\"toolTipLayer\").style;\n"
333       +"    else if(ie4) toolTipSTYLE = document.all.toolTipLayer.style;\n"
334       +"    if(ns4) document.captureEvents(Event.MOUSEMOVE);\n"
335       +"    else\n"
336       +"    {\n"
337       +"      toolTipSTYLE.visibility = \"visible\";\n"
338       +"      toolTipSTYLE.display = \"none\";\n"
339       +"    }\n"
340       +"    document.onmousemove = moveToMouseLoc;\n"
341       +"  }\n"
342       +"}\n"
343       +"function toolTip(msg, fg, bg)\n"
344       +"{\n"
345       +"  if(toolTip.arguments.length < 1) // hide\n"
346       +"  {\n"
347       +"    if(ns4) toolTipSTYLE.visibility = \"hidden\";\n"
348       +"    else toolTipSTYLE.display = \"none\";\n"
349       +"  }\n"
350       +"  else // show\n"
351       +"  {\n"
352       +"    if(!fg) fg = \"#555555\";\n"
353       +"    if(!bg) bg = \"#FFFFFF\";\n"
354       +"    var content =\n"
355       +"    '<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" bgcolor=\"' + fg + '\"><td>' +\n"
356       +"    '<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" bgcolor=\"' + bg + \n"
357       +"    '\"><td align=\"center\"><font face=\"sans-serif\" color=\"' + fg +\n"
358       +"    '\" size=\"-2\">&nbsp;' + msg +\n"
359       +"    '&nbsp;</font></td></table></td></table>';\n"
360       +"    if(ns4)\n"
361       +"    {\n"
362       +"      toolTipSTYLE.document.write(content);\n"
363       +"      toolTipSTYLE.document.close();\n"
364       +"      toolTipSTYLE.visibility = \"visible\";\n"
365       +"    }\n"
366       +"    if(ns6)\n"
367       +"    {\n"
368       +"      document.getElementById(\"toolTipLayer\").innerHTML = content;\n"
369       +"      toolTipSTYLE.display='block'\n"
370       +"    }\n"
371       +"    if(ie4)\n"
372       +"    {\n"
373       +"      document.all(\"toolTipLayer\").innerHTML=content;\n"
374       +"      toolTipSTYLE.display='block'\n"
375       +"    }\n"
376       +"  }\n"
377       +"}\n"
378       +"function moveToMouseLoc(e)\n"
379       +"{\n"
380       +"  if(ns4||ns6)\n"
381       +"  {\n"
382       +"    x = e.pageX;\n"
383       +"    y = e.pageY;\n"
384       +"  }\n"
385       +"  else\n"
386       +"  {\n"
387       +"    x = event.x + document.body.scrollLeft;\n"
388       +"    y = event.y + document.body.scrollTop;\n"
389       +"  }\n"
390       +"  toolTipSTYLE.left = x + offsetX;\n"
391       +"  toolTipSTYLE.top = y + offsetY;\n"
392       +"  return true;\n"
393       +"}\n"
394       +"</script>\n"
395       +"</head>\n"
396       +"<body>\n"
397       +"<div id=\"toolTipLayer\" style=\"position:absolute; visibility: hidden\"></div>\n"
398       +"<script language=\"JavaScript\"><!--\n"
399       +"initToolTips(); //--></script>\n");
400
401   }
402 }