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