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: * A TABLE free DIV/CSS HTML layout engine.
012: *
013: * @author David Bennett, <a href="mailto:dbennett@bensoft.com">dbennett@bensoft.com</a>
014: *
015: * Adapted from Jim Menard's HTMLLE.java
016: *
017: */
018: public class CSSHTMLLE extends SortedLayoutEngine {
019:
020: /**
021: * Constructor.
022: *
023: * @param out a print writer
024: */
025: public CSSHTMLLE(PrintWriter out) {
026: super (out);
027: }
028:
029: protected void doStart() {
030: out.println("<html>");
031: out.println("<head>");
032: out.println("<!-- Generated by DataVision version "
033: + info.Version + " -->");
034: out.println("<!-- " + info.URL + " -->");
035: String title = report.getTitle();
036: if (title != null && title.length() > 0)
037: out.println("<title>" + report.getTitle() + "</title>");
038: out.println("</head>");
039: out.println("<body bgcolor=\"white\">");
040: }
041:
042: protected void doEnd() {
043: out.println("</body>");
044: out.println("</html>");
045: out.flush();
046: }
047:
048: protected void doOutputSection(Section s) {
049: out.print("<div style=\"position:relative;top:0;left:0;");
050: out.println("width:" + pageWidth() + "pt;height:"
051: + s.getOutputHeight() + "pt;overflow:visible;\">");
052: super .doOutputSection(s);
053: out.println("</div><br clear=\"left\"/>");
054: }
055:
056: protected void doOutputField(Field field) {
057: Format format = outputCellStart(field);
058: // Style attribute courtesy of Brendon Price <Brendon.Price@sytec.co.nz>
059: out.print("<span style=\"");
060: if (format.getFontFamilyName() != null)
061: out.print("font-family: " + format.getFontFamilyName()
062: + "; ");
063: out.print("font-size: " + format.getSize() + "pt; ");
064: outputColor(format.getColor());
065:
066: // Border code courtesy of Khadiyd Idris <khad@linuxindo.com>
067: Border b = field.getBorderOrDefault();
068: String bcolor = "black";
069: if (b.getColor() != null) {
070: bcolor = "#" + Integer.toHexString(b.getColor().getRed())
071: + Integer.toHexString(b.getColor().getGreen())
072: + Integer.toHexString(b.getColor().getBlue());
073: }
074:
075: if (b.getTop() != null)
076: out.print("border-top: solid " + bcolor + " "
077: + b.getTop().getThickness() + "pt; ");
078: if (b.getLeft() != null)
079: out.print("border-left: solid " + bcolor + " "
080: + b.getLeft().getThickness() + "pt; ");
081: if (b.getBottom() != null)
082: out.print("border-bottom: solid " + bcolor + " "
083: + b.getBottom().getThickness() + "pt; ");
084: if (b.getRight() != null)
085: out.print("border-right: solid " + bcolor + " "
086: + b.getRight().getThickness() + "pt; ");
087:
088: out.print("\">");
089:
090: if (format.isBold())
091: out.print("<b>");
092: if (format.isItalic())
093: out.print("<i>");
094: if (format.isUnderline())
095: out.print("<u>");
096:
097: String str = field.toString();
098: if (str == null || str.length() == 0)
099: str = " ";
100:
101: // Fix courtesy of Brendon Price <Brendon.Price@sytec.co.nz>
102: if (" ".equals(str))
103: out.print(str);
104: else
105: out.print(StringUtils.newlinesToXHTMLBreaks(StringUtils
106: .escapeHTML(str)));
107:
108: if (format.isUnderline())
109: out.print("</u>");
110: if (format.isItalic())
111: out.print("</i>");
112: if (format.isBold())
113: out.print("</b>");
114: out.print("</span>");
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 icon = image.getImageIcon(); // For width and height
126: String 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 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)
137: + "\" alt=\"" + StringUtils.escapeHTML(alt)
138: + "\" width=\"" + icon.getIconWidth() + "\" height=\""
139: + icon.getIconHeight() + "\">");
140: outputCellEnd();
141: }
142:
143: protected Format outputCellStart(Field field) {
144: Format format = field.getFormat();
145: Rectangle bounds = field.getBounds();
146:
147: String align = null;
148: switch (format.getAlign()) {
149: case Format.ALIGN_LEFT:
150: align = "left";
151: break;
152: case Format.ALIGN_CENTER:
153: align = "center";
154: break;
155: case Format.ALIGN_RIGHT:
156: align = "right";
157: break;
158: }
159:
160: out.print("<div style=\"position:absolute;width:"
161: + (int) bounds.width + "pt;");
162: out.print("height:" + (int) field.getOutputHeight()
163: + "pt;text-align:" + align + ";");
164: out.print("left:" + (int) bounds.x + "pt;top:" + (int) bounds.y
165: + "pt;");
166: if (!format.isWrap())
167: out.print("white-space:nowrap;");
168: out.println("\">");
169:
170: return format;
171: }
172:
173: protected void outputCellEnd() {
174: out.println("</div>");
175: }
176:
177: protected void outputColor(Color c) {
178: if (c.equals(Color.black))
179: return;
180:
181: int[] rgb = new int[3];
182: rgb[0] = c.getRed();
183: rgb[1] = c.getGreen();
184: rgb[2] = c.getBlue();
185: out.print(" color: #");
186: for (int i = 0; i < 3; ++i) {
187: if (rgb[i] < 16)
188: out.print('0');
189: out.print(Integer.toHexString(rgb[i]));
190: }
191: }
192:
193: protected void doOutputLine(Line line) {
194: }
195:
196: }
|