001: package jimm.datavision.layout;
002:
003: import jimm.datavision.*;
004: import jimm.datavision.field.*;
005: import jimm.util.StringUtils;
006: import java.io.PrintWriter;
007: import java.awt.Color;
008: import javax.swing.ImageIcon;
009:
010: /**
011: * An HTML layout engine.
012: *
013: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
014: */
015: public class HTMLLE extends SortedLayoutEngine {
016:
017: protected int rowX;
018:
019: /**
020: * Constructor.
021: *
022: * @param out a print writer
023: */
024: public HTMLLE(PrintWriter out) {
025: super (out);
026: }
027:
028: protected void doStart() {
029: out.println("<html>");
030: out.println("<head>");
031: out.println("<!-- Generated by DataVision version "
032: + info.Version + " -->");
033: out.println("<!-- " + info.URL + " -->");
034: String title = report.getTitle();
035: if (title != null && title.length() > 0)
036: out.println("<title>" + report.getTitle() + "</title>");
037: out.println("</head>");
038: out.println("<body bgcolor=\"white\">");
039: }
040:
041: protected void doEnd() {
042: out.println("</body>");
043: out.println("</html>");
044: out.flush();
045: }
046:
047: protected void doOutputSection(Section s) {
048: out.println("<table width=\"" + pageWidth() + "pt\">");
049: out.println("<tr height=\"" + s.getOutputHeight() + "pt\">");
050: rowX = 0;
051:
052: super .doOutputSection(s);
053:
054: // Pad remaining page width
055: if (rowX < pageWidth())
056: out.println("<td width=\"" + (pageWidth() - rowX)
057: + "pt\"> </td>");
058:
059: out.println("</tr>");
060: out.println("</table>");
061: }
062:
063: protected void doOutputField(Field field) {
064: Format format = outputCellStart(field);
065:
066: // Style attribute courtesy of Brendon Price <Brendon.Price@sytec.co.nz>
067: out.print("<font style=\"");
068: if (format.getFontFamilyName() != null)
069: out.print("font-family: " + format.getFontFamilyName()
070: + "; ");
071: out.print("font-size: " + format.getSize() + "pt; ");
072: outputColor(format.getColor());
073:
074: // Border code courtesy of Khadiyd Idris <khad@linuxindo.com>
075: Border b = field.getBorderOrDefault();
076: String bcolor = "black";
077: if (b.getColor() != null) {
078: bcolor = "#" + Integer.toHexString(b.getColor().getRed())
079: + Integer.toHexString(b.getColor().getGreen())
080: + Integer.toHexString(b.getColor().getBlue());
081: }
082:
083: if (b.getTop() != null)
084: out.print("border-top: solid " + bcolor + " "
085: + b.getTop().getThickness() + "pt; ");
086: if (b.getLeft() != null)
087: out.print("border-left: solid " + bcolor + " "
088: + b.getLeft().getThickness() + "pt; ");
089: if (b.getBottom() != null)
090: out.print("border-bottom: solid " + bcolor + " "
091: + b.getBottom().getThickness() + "pt; ");
092: if (b.getRight() != null)
093: out.print("border-right: solid " + bcolor + " "
094: + b.getRight().getThickness() + "pt; ");
095:
096: out.print("\">");
097:
098: if (format.isBold())
099: out.print("<b>");
100: if (format.isItalic())
101: out.print("<i>");
102: if (format.isUnderline())
103: out.print("<u>");
104:
105: String str = field.toString();
106: if (str == null || str.length() == 0)
107: str = " ";
108:
109: // Fix courtesy of Brendon Price <Brendon.Price@sytec.co.nz>
110: if (" ".equals(str))
111: out.print(str);
112: else
113: out.print(StringUtils.newlinesToXHTMLBreaks(StringUtils
114: .escapeHTML(str)));
115:
116: if (format.isUnderline())
117: out.print("</u>");
118: if (format.isItalic())
119: out.print("</i>");
120: if (format.isBold())
121: out.print("</b>");
122: out.print("</font>");
123:
124: outputCellEnd();
125: }
126:
127: protected void doOutputImage(ImageField image) {
128: if (image == null || image.getImageURL() == null)
129: return;
130:
131: outputCellStart(image);
132:
133: ImageIcon icon = image.getImageIcon(); // For width and height
134: String url = image.getImageURL().toString();
135: if (url.startsWith("file:"))
136: url = url.substring(5);
137:
138: // Make an alt attribute from the URL
139: String alt = url;
140: int pos = alt.lastIndexOf("/");
141: if (pos != -1)
142: alt = alt.substring(pos + 1);
143:
144: out.print("<img src=\"" + StringUtils.escapeHTML(url)
145: + "\" alt=\"" + StringUtils.escapeHTML(alt)
146: + "\" width=\"" + icon.getIconWidth() + "\" height=\""
147: + icon.getIconHeight() + "\">");
148: outputCellEnd();
149: }
150:
151: protected Format outputCellStart(Field field) {
152: Format format = field.getFormat();
153: Rectangle bounds = field.getBounds();
154:
155: if (bounds.x > rowX)
156: out.println("<td width=\"" + (bounds.x - rowX)
157: + "pt\"> </td>");
158: rowX = (int) (bounds.x + bounds.width);
159:
160: String align = null;
161: switch (format.getAlign()) {
162: case Format.ALIGN_LEFT:
163: align = "left";
164: break;
165: case Format.ALIGN_CENTER:
166: align = "center";
167: break;
168: case Format.ALIGN_RIGHT:
169: align = "right";
170: break;
171: }
172:
173: out.print("<td align=\"" + align + "\" width=\""
174: + (int) bounds.width + "pt\" height=\""
175: + (int) field.getOutputHeight() + "pt\"");
176: if (!format.isWrap())
177: out.print(" nowrap");
178: out.print(">");
179:
180: return format;
181: }
182:
183: protected void outputCellEnd() {
184: out.println("</td>");
185: }
186:
187: protected void outputColor(Color c) {
188: if (c.equals(Color.black))
189: return;
190:
191: int[] rgb = new int[3];
192: rgb[0] = c.getRed();
193: rgb[1] = c.getGreen();
194: rgb[2] = c.getBlue();
195: out.print(" color: #");
196: for (int i = 0; i < 3; ++i) {
197: if (rgb[i] < 16)
198: out.print('0');
199: out.print(Integer.toHexString(rgb[i]));
200: }
201: }
202:
203: protected void doOutputLine(Line line) {
204: }
205:
206: }
|