KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import javax.servlet.jsp.PageContext JavaDoc;
19
20 import org.apache.commons.beanutils.PropertyUtils;
21 import org.apache.commons.lang.BooleanUtils;
22 import org.displaytag.model.TableModel;
23
24
25 /**
26  * <p>
27  * This class provides some basic functionality for all objects which serve as decorators for the objects in the List
28  * being displayed.
29  * <p>
30  * <p>
31  * Decorator should never be subclassed directly. Use TableDecorator instead
32  * </p>
33  * @author mraible
34  * @author Fabrizio Giustina
35  * @version $Revision: 934 $ ($Author: fgiust $)
36  */

37 abstract class Decorator
38 {
39
40     /**
41      * Char used to separate class name and property in the cache key.
42      */

43     private static final char CLASS_PROPERTY_SEPARATOR = '#';
44
45     /**
46      * property info cache contains classname#propertyname Strings as keys and Booleans as values.
47      */

48     private static Map JavaDoc propertyMap = new HashMap JavaDoc();
49
50     /**
51      * page context.
52      */

53     private PageContext JavaDoc pageContext;
54
55     /**
56      * decorated object. Usually a List
57      */

58     private Object JavaDoc decoratedObject;
59
60     /**
61      * The table model.
62      * @since 1.1
63      */

64     protected TableModel tableModel;
65
66     /**
67      * Initialize the TableTecorator instance.
68      * @param context PageContext
69      * @param decorated decorated object (usually a list)
70      * @deprecated use #init(PageContext, Object, TableModel)
71      * @see #init(PageContext, Object, TableModel)
72      */

73     public void init(PageContext JavaDoc context, Object JavaDoc decorated)
74     {
75         this.pageContext = context;
76         this.decoratedObject = decorated;
77     }
78
79     /**
80      * Initialize the TableTecorator instance.
81      * @param context PageContext
82      * @param decorated decorated object (usually a list)
83      * @param tableModel table model
84      */

85     public void init(PageContext JavaDoc context, Object JavaDoc decorated, TableModel tableModel)
86     {
87         // temporary used for backward (source) compatibility
88
init(pageContext, decorated);
89         // this.pageContext = context;
90
// this.decoratedObject = decorated;
91
this.tableModel = tableModel;
92     }
93
94     /**
95      * returns the page context.
96      * @return PageContext
97      */

98     public PageContext JavaDoc getPageContext()
99     {
100         return this.pageContext;
101     }
102
103     /**
104      * returns the decorated object.
105      * @return Object
106      */

107     public Object JavaDoc getDecoratedObject()
108     {
109         return this.decoratedObject;
110     }
111
112     /**
113      * Called at the end of evaluation to clean up instance variable. A subclass of Decorator can override this method
114      * but should always call super.finish() before return
115      */

116     public void finish()
117     {
118         this.pageContext = null;
119         this.decoratedObject = null;
120     }
121
122     /**
123      * Check if a getter exists for a given property. Uses cached info if property has already been requested. This
124      * method only check for a simple property, if pPropertyName contains multiple tokens only the first part is
125      * evaluated
126      * @param propertyName name of the property to check
127      * @return boolean true if the decorator has a getter for the given property
128      */

129     public boolean hasGetterFor(String JavaDoc propertyName)
130     {
131         String JavaDoc simpleProperty = propertyName;
132
133         // get the simple (not nested) bean property
134
int indexOfDot = simpleProperty.indexOf('.');
135         if (indexOfDot > 0)
136         {
137             simpleProperty = simpleProperty.substring(0, indexOfDot);
138         }
139
140         Boolean JavaDoc cachedResult = (Boolean JavaDoc) propertyMap.get(getClass().getName()
141             + CLASS_PROPERTY_SEPARATOR
142             + simpleProperty);
143
144         if (cachedResult != null)
145         {
146             return cachedResult.booleanValue();
147         }
148
149         // not already cached... check
150
boolean hasGetter = searchGetterFor(propertyName);
151
152         // save in cache
153
propertyMap.put(getClass().getName() + CLASS_PROPERTY_SEPARATOR + simpleProperty, BooleanUtils
154             .toBooleanObject(hasGetter));
155
156         // and return
157
return hasGetter;
158
159     }
160
161     /**
162      * Looks for a getter for the given property using introspection.
163      * @param propertyName name of the property to check
164      * @return boolean true if the decorator has a getter for the given property
165      */

166     public boolean searchGetterFor(String JavaDoc propertyName)
167     {
168
169         Class JavaDoc type = null;
170
171         try
172         {
173             // using getPropertyType instead of isReadable since isReadable doesn't support mapped properties.
174
// Note that this method usually returns null if a property is not found and doesn't throw any exception
175
// also for non existent properties
176
type = PropertyUtils.getPropertyType(this, propertyName);
177         }
178         catch (IllegalAccessException JavaDoc e)
179         {
180             // ignore
181
}
182         catch (InvocationTargetException JavaDoc e)
183         {
184             // ignore
185
}
186         catch (NoSuchMethodException JavaDoc e)
187         {
188             // ignore
189
}
190
191         return type != null;
192
193     }
194
195 }
Popular Tags