KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > displaytag > sample > decorators > ItextTotalWrapper


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
13 package org.displaytag.sample.decorators;
14
15 import com.lowagie.text.BadElementException;
16 import com.lowagie.text.Cell;
17 import com.lowagie.text.Chunk;
18 import com.lowagie.text.Element;
19 import com.lowagie.text.Font;
20 import com.lowagie.text.FontFactory;
21 import com.lowagie.text.Rectangle;
22 import com.lowagie.text.Table;
23
24
25 /**
26  * Same idea implemented in ItextTableWriter applied to decorators.
27  * @see org.displaytag.render.ItextTableWriter
28  * @author Jorge L. Barroso
29  * @version $Revision$ ($Author$)
30  */

31 public class ItextTotalWrapper extends TotalWrapperTemplate
32     implements
33     org.displaytag.render.ItextTableWriter.ItextDecorator
34 {
35
36     /**
37      * The iText table in which the totals are rendered.
38      */

39     private Table table;
40
41     /**
42      * The iText font used to render the totals.
43      */

44     private Font font;
45
46     /**
47      * Set the table required to render the totals line.
48      * @param table The table required to render the totals line.
49      * @see org.displaytag.decorator.itext.DecoratesItext#setTable(com.lowagie.text.Table)
50      */

51     public void setTable(Table table)
52     {
53         this.table = table;
54     }
55
56     /**
57      * Set the font required to render the totals line.
58      * @param font The font required to render the totals line.
59      * @see org.displaytag.decorator.itext.DecoratesItext#setFont(com.lowagie.text.Font)
60      */

61     public void setFont(Font font)
62     {
63         this.font = font;
64     }
65
66     /**
67      * Writes cell border at bottom of cell.
68      */

69     public String JavaDoc startRow()
70     {
71         this.table.setDefaultCellBorder(Rectangle.BOTTOM);
72         return null;
73     }
74
75     /**
76      * Writes the city total line.
77      * @param city City name.
78      * @param total City total.
79      */

80     protected void writeCityTotal(String JavaDoc city, double total)
81     {
82         this.writeTotal(city, total);
83     }
84
85     /**
86      * Writes the table grand total
87      * @param total Table grand total
88      */

89     protected void writeGrandTotal(double total)
90     {
91         this.writeTotal("Grand", total);
92     }
93
94     /**
95      * Writes a total line.
96      * @param value Total message.
97      * @param total Total number.
98      */

99     private void writeTotal(String JavaDoc value, double total)
100     {
101         if (assertRequiredState())
102         {
103             try
104             {
105                 this.font = FontFactory.getFont(this.font.getFamilyname(), this.font.size(), Font.BOLD, this.font
106                     .color());
107                 table.addCell(this.getCell(""));
108                 table.addCell(this.getCell(""));
109                 table.addCell(this.getCell("-------------"));
110                 table.addCell(this.getCell(""));
111                 // new row
112
table.addCell(this.getCell(""));
113                 table.addCell(this.getCell(value + " Total:"));
114                 table.addCell(this.getCell(total + ""));
115                 table.addCell(this.getCell(""));
116             }
117             catch (BadElementException e)
118             {
119             }
120         }
121     }
122
123     /**
124      * Obtain a cell with the given value.
125      * @param value Value to display in the cell.
126      * @return A cell with the given value.
127      * @throws BadElementException if an error occurs while generating the cell.
128      */

129     private Cell getCell(String JavaDoc value) throws BadElementException
130     {
131         Cell cell = new Cell(new Chunk(value, this.font));
132         cell.setLeading(8);
133         cell.setHorizontalAlignment(Element.ALIGN_LEFT);
134         return cell;
135     }
136
137     /**
138      * Asserts that the table and font properties needed have been set by the client.
139      * @return true if the required properties are not null; false otherwise.
140      */

141     private boolean assertRequiredState()
142     {
143         return this.table != null && this.font != null;
144     }
145 }
146
Popular Tags