KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > layout > HTMLLE


1 package jimm.datavision.layout;
2 import jimm.datavision.*;
3 import jimm.datavision.field.*;
4 import jimm.util.StringUtils;
5 import java.io.PrintWriter JavaDoc;
6 import java.awt.Color JavaDoc;
7 import javax.swing.ImageIcon JavaDoc;
8
9 /**
10  * An HTML layout engine.
11  *
12  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
13  */

14 public class HTMLLE extends SortedLayoutEngine {
15
16 protected int rowX;
17
18 /**
19  * Constructor.
20  *
21  * @param out a print writer
22  */

23 public HTMLLE(PrintWriter JavaDoc out) {
24     super(out);
25 }
26
27 protected void doStart() {
28     out.println("<html>");
29     out.println("<head>");
30     out.println("<!-- Generated by DataVision version " + info.Version
31         + " -->");
32     out.println("<!-- " + info.URL + " -->");
33     String JavaDoc title = report.getTitle();
34     if (title != null && title.length() > 0)
35     out.println("<title>" + report.getTitle() + "</title>");
36     out.println("</head>");
37     out.println("<body bgcolor=\"white\">");
38 }
39
40 protected void doEnd() {
41     out.println("</body>");
42     out.println("</html>");
43     out.flush();
44 }
45
46 protected void doOutputSection(Section s) {
47     out.println("<table width=\"" + pageWidth() + "pt\">");
48     out.println("<tr height=\"" + s.getOutputHeight() + "pt\">");
49     rowX = 0;
50
51     super.doOutputSection(s);
52
53     // Pad remaining page width
54
if (rowX < pageWidth())
55     out.println("<td width=\"" + (pageWidth() - rowX)
56             + "pt\">&nbsp;</td>");
57
58     out.println("</tr>");
59     out.println("</table>");
60 }
61
62 protected void doOutputField(Field field) {
63     Format format = outputCellStart(field);
64
65     // Style attribute courtesy of Brendon Price <Brendon.Price@sytec.co.nz>
66
out.print("<font style=\"");
67     if (format.getFontFamilyName() != null)
68     out.print("font-family: " + format.getFontFamilyName() + "; ");
69     out.print("font-size: " + format.getSize() + "pt; ");
70     outputColor(format.getColor());
71     
72     // Border code courtesy of Khadiyd Idris <khad@linuxindo.com>
73
Border b = field.getBorderOrDefault();
74     String JavaDoc bcolor = "black";
75     if (b.getColor() != null) {
76     bcolor = "#" + Integer.toHexString(b.getColor().getRed())
77         + Integer.toHexString(b.getColor().getGreen())
78         + Integer.toHexString(b.getColor().getBlue());
79     }
80
81     if (b.getTop() != null)
82     out.print("border-top: solid " + bcolor + " "
83           + b.getTop().getThickness() + "pt; ");
84     if (b.getLeft() != null)
85     out.print("border-left: solid " + bcolor + " "
86           + b.getLeft().getThickness() + "pt; ");
87     if (b.getBottom() != null)
88     out.print("border-bottom: solid " + bcolor + " "
89           + b.getBottom().getThickness() + "pt; ");
90     if (b.getRight() != null)
91     out.print("border-right: solid " + bcolor + " "
92           + b.getRight().getThickness() + "pt; ");
93
94
95     out.print("\">");
96
97     if (format.isBold()) out.print("<b>");
98     if (format.isItalic()) out.print("<i>");
99     if (format.isUnderline()) out.print("<u>");
100
101     String JavaDoc str = field.toString();
102     if (str == null || str.length() == 0)
103     str = "&nbsp;";
104
105     // Fix courtesy of Brendon Price <Brendon.Price@sytec.co.nz>
106
if ("&nbsp;".equals(str))
107     out.print(str);
108     else
109     out.print(StringUtils.newlinesToXHTMLBreaks(StringUtils.escapeHTML(str)));
110
111     if (format.isUnderline()) out.print("</u>");
112     if (format.isItalic()) out.print("</i>");
113     if (format.isBold()) out.print("</b>");
114     out.print("</font>");
115
116     outputCellEnd();
117 }
118
119 protected void doOutputImage(ImageField image) {
120     if (image == null || image.getImageURL() == null)
121     return;
122
123     outputCellStart(image);
124
125     ImageIcon JavaDoc icon = image.getImageIcon(); // For width and height
126
String JavaDoc url = image.getImageURL().toString();
127     if (url.startsWith("file:"))
128     url = url.substring(5);
129
130     // Make an alt attribute from the URL
131
String JavaDoc alt = url;
132     int pos = alt.lastIndexOf("/");
133     if (pos != -1)
134     alt = alt.substring(pos + 1);
135
136     out.print("<img SRC=\"" + StringUtils.escapeHTML(url) + "\" alt=\""
137           + StringUtils.escapeHTML(alt)
138           + "\" width=\"" + icon.getIconWidth()
139           + "\" height=\"" + icon.getIconHeight()
140           + "\">");
141     outputCellEnd();
142 }
143
144 protected Format outputCellStart(Field field) {
145     Format format = field.getFormat();
146     Rectangle bounds = field.getBounds();
147
148     if (bounds.x > rowX)
149     out.println("<td width=\"" + (bounds.x - rowX) + "pt\">&nbsp;</td>");
150     rowX = (int)(bounds.x + bounds.width);
151
152     String JavaDoc align = null;
153     switch (format.getAlign()) {
154     case Format.ALIGN_LEFT: align = "left"; break;
155     case Format.ALIGN_CENTER: align = "center"; break;
156     case Format.ALIGN_RIGHT: align = "right"; break;
157     }
158
159     out.print("<td align=\"" + align + "\" width=\"" + (int)bounds.width
160           + "pt\" height=\"" + (int)field.getOutputHeight() + "pt\"");
161     if (!format.isWrap())
162     out.print(" nowrap");
163     out.print(">");
164
165     return format;
166 }
167
168 protected void outputCellEnd() {
169     out.println("</td>");
170 }
171
172 protected void outputColor(Color JavaDoc c) {
173     if (c.equals(Color.black))
174     return;
175
176     int[] rgb = new int[3];
177     rgb[0] = c.getRed();
178     rgb[1] = c.getGreen();
179     rgb[2] = c.getBlue();
180     out.print(" color: #");
181     for (int i = 0; i < 3; ++i) {
182     if (rgb[i] < 16) out.print('0');
183     out.print(Integer.toHexString(rgb[i]));
184     }
185 }
186
187
188 protected void doOutputLine(Line line) {}
189
190 }
191
192
Popular Tags