af6e2554703b141d87316e08c541167fb7ee3b58
[jalview.git] / src / jalview / io / HTMLOutput.java
1 package jalview.io;\r
2 \r
3 import jalview.gui.*;\r
4 import jalview.datamodel.*;\r
5 import jalview.schemes.*;\r
6 import java.awt.*;\r
7 import javax.swing.*;\r
8 import java.io.*;\r
9 import java.util.*;\r
10 \r
11 \r
12 public class HTMLOutput\r
13 {\r
14 \r
15 \r
16   public HTMLOutput(AlignViewport av)\r
17   {\r
18     SequenceRenderer sr = new SequenceRenderer(av);\r
19     Color color;\r
20     JFileChooser chooser = new JFileChooser(jalview.bin.Cache.LAST_DIRECTORY);\r
21     chooser.setDialogTitle("Save as HTML");\r
22     chooser.setToolTipText("Save");\r
23     int value = chooser.showSaveDialog(null);\r
24     if(value == JFileChooser.APPROVE_OPTION)\r
25     {\r
26       String choice =  chooser.getSelectedFile().getPath();\r
27       jalview.bin.Cache.LAST_DIRECTORY = choice;\r
28       try{\r
29         PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(choice));\r
30         out.println("<HTML>");\r
31         out.println("<style type=\"text/css\">");\r
32         out.println("<!--");\r
33         out.print("td {font-family: \""+av.getFont().getFamily()+"\", \""+av.getFont().getName()+ "\", mono; "\r
34                    +"font-size: "+av.getFont().getSize()+"px; ");\r
35 \r
36 \r
37         if (av.getFont().getStyle() == Font.BOLD)\r
38            out.print("font-weight: BOLD; ");\r
39 \r
40         if (av.getFont().getStyle() == Font.ITALIC)\r
41            out.print("font-style: italic; ");\r
42 \r
43         out.println("text-align: center; }");\r
44 \r
45         out.println("-->");\r
46         out.println("</style>");\r
47         out.println("<BODY>");\r
48         out.println("<table border=\"1\"><tr><td>\n");\r
49         out.println("<table border=\"0\"  cellpadding=\"0\" cellspacing=\"0\">\n");\r
50 \r
51 \r
52         //////////////\r
53         SequenceGroup group;\r
54         SequenceI seq;\r
55         ColourSchemeI cs;\r
56         AlignmentI alignment = av.getAlignment();\r
57         String r,g,b;\r
58         for (int i = 0; i < alignment.getHeight(); i++)\r
59         {\r
60           seq = alignment.getSequenceAt(i);\r
61           group = alignment.findGroup( seq );\r
62           out.println("<tr><td nowrap>"+seq.getDisplayId()+"&nbsp;&nbsp;</td>");\r
63           for (int res = 0; res < seq.getLength(); res++)\r
64           {\r
65             if(group!=null && group.getStartRes()<=res && group.getEndRes()>=res)\r
66               cs = group.cs;\r
67             else\r
68               cs = av.getGlobalColourScheme();\r
69 \r
70 \r
71             color = sr.getResidueBoxColour(cs, seq, res);\r
72 \r
73             if(color.getRGB()<-1)\r
74             {\r
75               r = Integer.toHexString(color.getRed());\r
76               if(r.length()<2)\r
77                 r = "0"+r;\r
78               g = Integer.toHexString(color.getGreen());\r
79               if(g.length()<2)\r
80                 g = "0"+g;\r
81               b = Integer.toHexString(color.getBlue());\r
82               if(b.length()<2)\r
83                 b = "0"+b;\r
84               out.println("<td bgcolor=\"#"+ r + g + b + "\">"+seq.getCharAt(res)+"</td>");\r
85             }\r
86             else\r
87               out.println("<td>"+seq.getCharAt(res)+"</td>");\r
88 \r
89           }\r
90 \r
91           out.println("</tr>");\r
92         }\r
93         //////////////\r
94         out.println("</table>");\r
95         out.println("</td></tr></table>\n</body>\n</html>");\r
96 \r
97         out.close();\r
98 \r
99         jalview.util.BrowserLauncher.openURL( "file:///"+choice );\r
100       }\r
101       catch(Exception ex){ex.printStackTrace();}\r
102     }\r
103   }\r
104 \r
105 \r
106 }\r