KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > displaytag > decorator > TableDecorator


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.decorator;
13
14 import javax.servlet.jsp.PageContext JavaDoc;
15
16 import org.displaytag.exception.ObjectLookupException;
17 import org.displaytag.model.TableModel;
18 import org.displaytag.render.TableWriterTemplate;
19 import org.displaytag.util.LookupUtil;
20 import org.displaytag.util.TagConstants;
21
22
23 /**
24  * @author epesh
25  * @author Fabrizio Giustina
26  * @version $Revision: 971 $ ($Author: fgiust $)
27  */

28 public abstract class TableDecorator extends Decorator
29 {
30
31     /**
32      * object representing the current row.
33      */

34     private Object JavaDoc currentRowObject;
35
36     /**
37      * index in displayed list.
38      */

39     private int viewIndex = -1;
40
41     /**
42      * index in original list.
43      */

44     private int listIndex = -1;
45
46     /**
47      * The associated table model.
48      */

49     protected TableModel tableModel;
50
51     /**
52      * Return the index in the displayed list.
53      * @return int index in the displayed list
54      */

55     public final int getViewIndex()
56     {
57         return this.viewIndex;
58     }
59
60     /**
61      * Return the index in the full list (view index + offset). Note that the index returned if from the <strong>sorted</strong>
62      * list, and not from the original one.
63      * @return int index in the full list
64      */

65     public final int getListIndex()
66     {
67         return this.listIndex;
68     }
69
70     /**
71      * Get the object representing the current row.
72      * @return Object
73      */

74     public final Object JavaDoc getCurrentRowObject()
75     {
76         return this.currentRowObject;
77     }
78
79     /**
80      * Initialize the TableTecorator instance.
81      * @param context PageContext
82      * @param decorated decorated object (usually a list)
83      * @param tableModel the tableModel
84      */

85     public void init(PageContext JavaDoc context, Object JavaDoc decorated, TableModel tableModel)
86     {
87         this.tableModel = tableModel;
88         this.init(context, decorated);
89     }
90
91     /**
92      * Initialize the current row. Note this method is also called when sorting a table using a property supplied by the
93      * table decorator, so the method could be called multiple times during rendering. When used to initialize sorting
94      * the method is always called with 0, 0 as currentViewIndex and currentListIndex.
95      * @param rowObject object representing the current row
96      * @param currentViewIndex int index in the displayed list
97      * @param currentListIndex int index in the original list
98      */

99     public final void initRow(Object JavaDoc rowObject, int currentViewIndex, int currentListIndex)
100     {
101         this.currentRowObject = rowObject;
102         this.viewIndex = currentViewIndex;
103         this.listIndex = currentListIndex;
104     }
105
106     /**
107      * Called at the beginning of a row. Can be subclassed to provide specific data at the beginning of a row
108      * @return null in the default implementation
109      */

110     public String JavaDoc startRow()
111     {
112         return null;
113     }
114
115     /**
116      * Called at the end of a row. Can be subclassed to provide specific data at the end of a row
117      * @return null in the default implementation
118      */

119     public String JavaDoc finishRow()
120     {
121         return null;
122     }
123
124     /**
125      * Called at the end of evaluation. Can be subclassed to eventully clean up data. Always remember to also call
126      * super.finish()!
127      */

128     public void finish()
129     {
130         this.currentRowObject = null;
131         super.finish();
132     }
133
134     /**
135      * Call back to add an additional row class to the current row.
136      * @return CSS class attribute value for the current row
137      * @since 1.1
138      */

139     public String JavaDoc addRowClass()
140     {
141         return null;
142     }
143
144     /**
145      * Call back to allow setting an "id" attribute on a row.
146      * @return HTML id attribute value for the current row
147      * @since 1.1
148      */

149     public String JavaDoc addRowId()
150     {
151         return null;
152     }
153
154     /**
155      * Indicates that we are begining a new group.
156      * @param value of the current cell
157      * @param group number of the current column
158      */

159     public void startOfGroup(String JavaDoc value, int group)
160     {
161     }
162
163     /**
164      * Called at the end of a group. Can be subclassed to provide specific data at the end of a row.
165      * @param value of the current cell
166      * @param groupThatHasEnded number of the current column
167      */

168     public void endOfGroup(String JavaDoc value, int groupThatHasEnded)
169     {
170     }
171
172     /**
173      * What value should I display in this cell? The default value for grouped columns is to not display any value if
174      * the cellValue has not changed on an interior iteration. Only invoked for columns that are grouped.
175      * @param cellValue
176      * @param groupingStatus
177      * @return the value to display
178      */

179     public String JavaDoc displayGroupedValue(String JavaDoc cellValue, short groupingStatus)
180     {
181         if (groupingStatus == TableWriterTemplate.GROUP_END || groupingStatus == TableWriterTemplate.GROUP_NO_CHANGE)
182         {
183             return TagConstants.EMPTY_STRING;
184         }
185         else
186         {
187             return cellValue;
188         }
189     }
190
191     public boolean isLastRow()
192     {
193         return getListIndex() == this.tableModel.getRowListPage().size() - 1;
194     }
195
196     /**
197      * Shortcut for evaluating properties in the current row object. Can be useful for implementing anonymous decorators
198      * in jsp pages without having to know/import the decorated object Class.
199      * @param propertyName property to lookup in current row object. Can also be a nested or indexed property.
200      * @since 1.1
201      */

202     protected Object JavaDoc evaluate(String JavaDoc propertyName)
203     {
204         try
205         {
206             return LookupUtil.getBeanProperty(getCurrentRowObject(), propertyName);
207         }
208         catch (ObjectLookupException e)
209         {
210             return null;
211         }
212     }
213
214 }
Popular Tags