KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > displaytag > render > ItextTableWriter


1 /**
2  * Licensed under the Artistic License; you may not use this file
3  * except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://displaytag.sourceforge.net/license.html
7  *
8  * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11  */

12 package org.displaytag.render;
13
14 import java.awt.Color JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import org.apache.commons.lang.ObjectUtils;
18 import org.apache.commons.lang.StringUtils;
19 import org.displaytag.decorator.TableDecorator;
20 import org.displaytag.exception.DecoratorException;
21 import org.displaytag.exception.ObjectLookupException;
22 import org.displaytag.model.Column;
23 import org.displaytag.model.HeaderCell;
24 import org.displaytag.model.TableModel;
25
26 import com.lowagie.text.BadElementException;
27 import com.lowagie.text.Cell;
28 import com.lowagie.text.Chunk;
29 import com.lowagie.text.Document;
30 import com.lowagie.text.DocumentException;
31 import com.lowagie.text.Element;
32 import com.lowagie.text.Font;
33 import com.lowagie.text.FontFactory;
34 import com.lowagie.text.Paragraph;
35 import com.lowagie.text.Rectangle;
36 import com.lowagie.text.Table;
37
38
39 /**
40  * A table writer that formats table as and writes it to an iText document.
41  * @author Jorge L. Barroso
42  * @version $Id$
43  * @see org.displaytag.render.TableWriterTemplate
44  */

45 public class ItextTableWriter extends TableWriterAdapter
46 {
47
48     /**
49      * iText representation of the table.
50      */

51     private Table table;
52
53     /**
54      * iText document to which the table is written.
55      */

56     private Document document;
57
58     /**
59      * The default font used in the document.
60      */

61     private Font defaultFont;
62
63     /**
64      * This table writer uses an iText table and document to do its work.
65      * @param table iText representation of the table.
66      * @param document iText document to which the table is written.
67      */

68     public ItextTableWriter(Table table, Document document)
69     {
70         this.table = table;
71         this.document = document;
72     }
73
74     /**
75      * Initialize the main info holder table, like the appropriate number of columns.
76      * @param model The table being represented as iText.
77      * @see org.displaytag.render.TableWriterTemplate#writeTableOpener(org.displaytag.model.TableModel)
78      */

79     protected void writeTableOpener(TableModel model)
80     {
81         this.table.setDefaultVerticalAlignment(Element.ALIGN_TOP);
82         this.table.setCellsFitPage(true);
83         this.table.setWidth(100);
84         this.table.setPadding(2);
85         this.table.setSpacing(0);
86         this.table.setBorder(Rectangle.NO_BORDER);
87         this.defaultFont = this.getTableFont();
88     }
89
90     /**
91      * Obtain the font used to render text in the table; Meant to be overriden if a different font is desired.
92      * @return The font used to render text in the table.
93      */

94     protected Font getTableFont()
95     {
96         return FontFactory.getFont(FontFactory.HELVETICA, 10, Font.NORMAL, new Color JavaDoc(0x00, 0x00, 0x00));
97     }
98
99     /**
100      * Write the table's caption to a iText document.
101      * @see org.displaytag.render.TableWriterTemplate#writeCaption(org.displaytag.model.TableModel)
102      */

103     protected void writeCaption(TableModel model) throws Exception JavaDoc
104     {
105         this.decorateCaption(model);
106     }
107
108     /**
109      * Writes the table caption according to a set style.
110      * @param model The table model containing the caption.
111      * @throws DocumentException If an error occurrs while decorating the caption.
112      */

113     private void decorateCaption(TableModel model) throws DocumentException
114     {
115         Paragraph caption = new Paragraph(new Chunk(model.getCaption(), this.getCaptionFont()));
116         caption.setAlignment(this.getCaptionHorizontalAlignment());
117         this.document.add(caption);
118     }
119
120     /**
121      * Obtain the caption font; Meant to be overriden if a different style is desired.
122      * @return The caption font.
123      */

124     protected Font getCaptionFont()
125     {
126         return FontFactory.getFont(FontFactory.HELVETICA, 17, Font.BOLD, new Color JavaDoc(0x00, 0x00, 0x00));
127     }
128
129     /**
130      * Obtain the caption horizontal alignment; Meant to be overriden if a different style is desired.
131      * @return The caption horizontal alignment.
132      */

133     protected int getCaptionHorizontalAlignment()
134     {
135         return Element.ALIGN_CENTER;
136     }
137
138     /**
139      * Write the table's header columns to an iText document.
140      * @see org.displaytag.render.TableWriterTemplate#writeTableHeader(org.displaytag.model.TableModel)
141      * @throws BadElementException if an error occurs while writing header.
142      */

143     protected void writeTableHeader(TableModel model) throws BadElementException
144     {
145         Iterator JavaDoc iterator = model.getHeaderCellList().iterator();
146
147         float[] widths = new float[model.getNumberOfColumns()];
148         for (int i = 0; iterator.hasNext(); i++)
149         {
150             HeaderCell headerCell = (HeaderCell) iterator.next();
151             widths[i] = this.getCellWidth(headerCell);
152
153             String JavaDoc columnHeader = headerCell.getTitle();
154
155             if (columnHeader == null)
156             {
157                 columnHeader = StringUtils.capitalize(headerCell.getBeanPropertyName());
158             }
159
160             Cell hdrCell = this.getHeaderCell(columnHeader);
161             this.table.addCell(hdrCell);
162         }
163         this.table.setWidths(widths);
164         this.table.endHeaders();
165     }
166
167     /**
168      * Returns the maximum size of all values in this column.
169      * @param headerCell Header cell for this column.
170      * @return The maximum size of all values in this column.
171      */

172     private float getCellWidth(HeaderCell headerCell)
173     {
174         int maxWidth = headerCell.getMaxLength();
175         return (maxWidth > 0) ? maxWidth : headerCell.getTitle().length();
176     }
177
178     /**
179      * @see org.displaytag.render.TableWriterTemplate#writePostBodyFooter(org.displaytag.model.TableModel)
180      * @throws DocumentException if an error occurs while writing post-body footer.
181      */

182     protected void writePostBodyFooter(TableModel model) throws DocumentException
183     {
184         Chunk cellContent = new Chunk(model.getFooter(), this.getFooterFont());
185         this.setFooterFontStyle(cellContent);
186         Cell cell = new Cell(cellContent);
187         cell.setLeading(8);
188         cell.setBackgroundColor(this.getFooterBackgroundColor());
189         cell.setHorizontalAlignment(this.getFooterHorizontalAlignment());
190         cell.setColspan(model.getNumberOfColumns());
191         table.addCell(cell);
192     }
193
194     /**
195      * Obtain the footer background color; Meant to be overriden if a different style is desired.
196      * @return The footer background color.
197      */

198     protected Color JavaDoc getFooterBackgroundColor()
199     {
200         return new Color JavaDoc(0xce, 0xcf, 0xce);
201     }
202
203     /**
204      * Obtain the footer horizontal alignment; Meant to be overriden if a different style is desired.
205      * @return The footer horizontal alignment.
206      */

207     protected int getFooterHorizontalAlignment()
208     {
209         return Element.ALIGN_LEFT;
210     }
211
212     /**
213      * Set the font style used to render the header text; Meant to be overridden if a different header style is desired.
214      * @param cellContent The header content whose font will be modified.
215      */

216     protected void setFooterFontStyle(Chunk cellContent)
217     {
218         this.setBoldStyle(cellContent, this.getFooterFontColor());
219     }
220
221     /**
222      * Obtain the footer font color; Meant to be overriden if a different style is desired.
223      * @return The footer font color.
224      */

225     protected Color JavaDoc getFooterFontColor()
226     {
227         return new Color JavaDoc(0x00, 0x00, 0x00);
228     }
229
230     /**
231      * Obtain the footer font; Meant to be overriden if a different style is desired.
232      * @return The footer font.
233      */

234     protected Font getFooterFont()
235     {
236         return FontFactory.getFont(FontFactory.HELVETICA, 10);
237     }
238
239     /**
240      * @see org.displaytag.render.TableWriterTemplate#writeDecoratedRowStart(org.displaytag.model.TableModel)
241      */

242     protected void writeDecoratedRowStart(TableModel model)
243     {
244         ItextDecorator decorator = (ItextDecorator) model.getTableDecorator();
245         decorator.setTable(this.table);
246         decorator.setFont(this.defaultFont);
247         ((TableDecorator) decorator).startRow();
248     }
249
250     /**
251      * @see org.displaytag.render.TableWriterTemplate#writeDecoratedRowFinish(org.displaytag.model.TableModel)
252      */

253     protected void writeDecoratedRowFinish(TableModel model) throws Exception JavaDoc
254     {
255         model.getTableDecorator().finishRow();
256     }
257
258     /**
259      * Write a column's opening structure to an iText document.
260      * @see org.displaytag.render.TableWriterTemplate#writeColumnOpener(org.displaytag.model.Column)
261      */

262     protected void writeColumnOpener(Column column) throws ObjectLookupException, DecoratorException
263     {
264         column.initialize(); // has side effect, setting its stringValue, which affects grouping logic.
265
}
266
267     /**
268      * Write a column's value to a iText document.
269      * @see org.displaytag.render.TableWriterTemplate#writeColumnValue(Object,org.displaytag.model.Column)
270      */

271     protected void writeColumnValue(Object JavaDoc value, Column column) throws BadElementException
272     {
273         this.table.addCell(getCell(value));
274     }
275
276     /**
277      * @see org.displaytag.render.TableWriterTemplate#writeDecoratedTableFinish(org.displaytag.model.TableModel)
278      */

279     protected void writeDecoratedTableFinish(TableModel model)
280     {
281         model.getTableDecorator().finish();
282     }
283
284     /**
285      * Returns a formatted cell for the given value.
286      * @param value cell value
287      * @return Cell
288      * @throws BadElementException if errors occurs while generating content.
289      */

290     private Cell getCell(Object JavaDoc value) throws BadElementException
291     {
292         Cell cell = new Cell(new Chunk(StringUtils.trimToEmpty(ObjectUtils.toString(value)), this.defaultFont));
293         cell.setVerticalAlignment(Element.ALIGN_TOP);
294         cell.setLeading(8);
295         return cell;
296     }
297
298     /**
299      * Obtain a header cell.
300      * @param value Cell content.
301      * @return A header cell with the given content.
302      * @throws BadElementException if errors occurs while generating content.
303      */

304     private Cell getHeaderCell(String JavaDoc value) throws BadElementException
305     {
306         Chunk cellContent = new Chunk(value, this.getHeaderFont());
307         setHeaderFontStyle(cellContent);
308         Cell cell = new Cell(cellContent);
309         cell.setLeading(8);
310         cell.setHeader(true);
311         cell.setHorizontalAlignment(this.getHeaderHorizontalAlignment());
312         cell.setBackgroundColor(this.getHeaderBackgroundColor());
313         return cell;
314     }
315
316     /**
317      * Obtain the font used to render the header text; Meant to be overridden if a different header font is desired.
318      * @return The font used to render the header text.
319      */

320     protected Font getHeaderFont()
321     {
322         return this.defaultFont;
323     }
324
325     /**
326      * Obtain the background color used to render the header; Meant to be overridden if a different header background
327      * color is desired.
328      * @return The backgrounc color used to render the header.
329      */

330     protected Color JavaDoc getHeaderBackgroundColor()
331     {
332         return new Color JavaDoc(0xee, 0xee, 0xee);
333     }
334
335     /**
336      * Set the font style used to render the header text; Meant to be overridden if a different header style is desired.
337      * @param cellContent The header content whose font will be modified.
338      */

339     protected void setHeaderFontStyle(Chunk cellContent)
340     {
341         setBoldStyle(cellContent, this.getHeaderFontColor());
342     }
343
344     /**
345      * Set the font color used to render the header text; Meant to be overridden if a different header style is desired.
346      * @return The font color used to render the header text.
347      */

348     protected Color JavaDoc getHeaderFontColor()
349     {
350         return new Color JavaDoc(0x00, 0x00, 0x00);
351     }
352
353     /**
354      * Obtain the horizontal alignment used to render header text; Meant to be overridden if a different alignment is
355      * desired.
356      * @return The horizontal alignment used to render header text;
357      */

358     protected int getHeaderHorizontalAlignment()
359     {
360         return Element.ALIGN_CENTER;
361     }
362
363     /**
364      * Makes chunk content bold.
365      * @param chunk The chunk whose content is to be rendered bold.
366      * @param color The font color desired.
367      */

368     private void setBoldStyle(Chunk chunk, Color JavaDoc color)
369     {
370         Font font = chunk.font();
371         chunk.setFont(FontFactory.getFont(font.getFamilyname(), font.size(), Font.BOLD, color));
372     }
373
374     /**
375      * An implementor of this interface decorates tables and columns appearing in iText documents.
376      * @author Jorge L. Barroso
377      * @version $Revision$ ($Author$)
378      */

379     public interface ItextDecorator
380     {
381
382         /**
383          * Set the iText table used to render a table model.
384          * @param table The iText table used to render a table model.
385          */

386         void setTable(Table table);
387
388         /**
389          * Set the font used to render a table's content.
390          * @param font The font used to render a table's content.
391          */

392         void setFont(Font font);
393     }
394
395 }
396
Popular Tags