KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > grid > SetFilterTag


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.taglib.core.grid;
17
18 import com.blandware.atleap.webapp.taglib.core.grid.util.SetFilter;
19 import com.blandware.atleap.webapp.util.core.RequestUtil;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.apache.struts.taglib.TagUtils;
23
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.JspTagException JavaDoc;
26 import javax.servlet.jsp.PageContext JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.StringWriter JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Map JavaDoc;
31
32
33 /**
34  * <p>Tag for marking column as filterable (Type: set). This will generate a
35  * link for displaying set filter window.
36  * <br />
37  * This tag is only valid when nested within <em>grid</em> tag.
38  * </p>
39  * <p>
40  * Allowed attributes are:
41  * <ul>
42  * <li>
43  * <b>beanId</b> - identifier of Spring bean to use to get list of available
44  * elements for set filter
45  * </li>
46  * <li>
47  * <b>method</b> - method of specified bean to call to get list of available
48  * elements for set filter. This method must return instance of
49  * <code>java.util.Collection</code> and must have no arguments. All elements in
50  * returned list will be converted to Strings while iterating them on the page.
51  * </li>
52  * <li>
53  * <b>collection</b> - collection of available elements if beanId is not
54  * specified. Only instances of <code>java.util.Collection</code> and
55  * <code>java.lang.Object[]</code> are supported.
56  * </li>
57  * <li>
58  * <b>label</b> - property of each bean in collection to get label to display.
59  * If not specified, String representation of bean will be used as label.
60  * </li>
61  * <li>
62  * <b>value</b> - property of each bean in collection to get the value to send
63  * to action. If not specified, String representation of bean will be used as
64  * value.
65  * </li>
66  * </ul>
67  * </p>
68  * <p>
69  * Some attributes are inherited from {@link BaseFilterTag}.
70  * </p>
71  * <p>
72  * This tag must either be contained in <em>column</em> tag or have
73  * <b>fieldName</b> specified.
74  * </p>
75  * <p>
76  * Either <b>collection</b> or both <b>beanId</b> and <b>method</b> must be specified.
77  * If <b>beanId</b> and <b>method</b> are specified, collection, over which
78  * iteration will be done, will be obtained invoking <b>method</b> on bean,
79  * which ID is <b>beanId</b>. Else, <b>collection</b> will be used as such
80  * collection.
81  * </p>
82  * <p>
83  * If <b>label</b> is specified, then <b>value</b> must be specified too.
84  * </p>
85  * <p><a HREF="SetFilterTag.java.htm"><i>View Source</i></a></p>
86  *
87  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
88  * @version $Revision: 1.12 $ $Date: 2005/10/12 13:34:57 $
89  * @jsp.tag name="setFilter"
90  * body-content="scriptless"
91  * @see com.blandware.atleap.webapp.taglib.core.grid.GridTag
92  */

93 public class SetFilterTag extends BaseFilterTag {
94
95     protected transient final Log log = LogFactory.getLog(SetFilterTag.class);
96
97     /**
98      * Identifier of Spring bean to use to get list of available elements for set filter.
99      */

100     protected String JavaDoc beanId;
101
102     /**
103      * Method of specified bean to call to get list of available elements for set filter.
104      * This method must return instance of <code>java.util.List</code> and
105      * must have no arguments. All elements in returned list will be converted to Strings
106      * while iterating them on the page.
107      *
108      * @see java.util.List
109      */

110     protected String JavaDoc method;
111
112     /**
113      * Collection of available elements if beanId not specified.
114      * Only instances of <code>java.util.Collection</code> and
115      * <code>java.lang.Object[]</code> are supported.
116      */

117     protected Object JavaDoc collection;
118
119     /**
120      * Property of each bean in collection to get label to display
121      */

122     protected String JavaDoc label;
123
124     /**
125      * Property of each bean in collection to get the value to send to action
126      */

127     protected String JavaDoc value;
128
129     /**
130      * Returns ID of bean that will supply the universe set
131      *
132      * @return bean ID
133      * @see #beanId
134      * @jsp.attribute required="false"
135      * rtexprvalue="true"
136      * type="java.lang.String"
137      * description="Identifier of Spring bean to use to get list for set filter"
138      */

139     public String JavaDoc getBeanId() {
140         return beanId;
141     }
142
143     /**
144      * Sets ID of bean that will supply the universe set
145      *
146      * @param beanId bean ID to set
147      * @see #beanId
148      */

149     public void setBeanId(String JavaDoc beanId) {
150         this.beanId = beanId;
151     }
152
153     /**
154      * Returns name of method of bean that will be used to obtain the universe set
155      *
156      * @return method name
157      * @see #method
158      * @jsp.attribute required="false"
159      * rtexprvalue="true"
160      * type="java.lang.String"
161      * description="Method of specified bean to call to get list of available elements for set filter"
162      */

163     public String JavaDoc getMethod() {
164         return method;
165     }
166
167     /**
168      * Sets name of method of bean that will be used to obtain the universe set
169      *
170      * @param method method name to set
171      * @see #method
172      */

173     public void setMethod(String JavaDoc method) {
174         this.method = method;
175     }
176
177     /**
178      * Returns collection of elements (used as universe set if bean ID is not specified)
179      *
180      * @return collection
181      * @see #collection
182      * @jsp.attribute required="false"
183      * rtexprvalue="true"
184      * type="java.lang.Object"
185      * description="Collection of available elements to display if beanId not specified"
186      */

187     public Object JavaDoc getCollection() {
188         return collection;
189     }
190
191     /**
192      * Sets collection of elements (used as universe set if bean ID is not specified)
193      *
194      * @param collection collection to set
195      * @see #collection
196      */

197     public void setCollection(Object JavaDoc collection) {
198         this.collection = collection;
199     }
200
201     /**
202      * Returns name of property of each bean that will be used to get label
203      *
204      * @return label property name
205      * @see #label
206      * @jsp.attribute required="false"
207      * rtexprvalue="true"
208      * type="java.lang.String"
209      * description="Property of each bean in collection to get label to display"
210      */

211     public String JavaDoc getLabel() {
212         return label;
213     }
214
215     /**
216      * Sets name of property of each bean that will be used to get label
217      *
218      * @param label label property name to set
219      * @see #label
220      */

221     public void setLabel(String JavaDoc label) {
222         this.label = label;
223     }
224
225     /**
226      * Returns name of property of each bean that will be used to get value
227      *
228      * @return value propery name
229      * @see #value
230      * @jsp.attribute required="false"
231      * rtexprvalue="true"
232      * type="java.lang.String"
233      * description="Property of each bean in collection to get the value to send to action"
234      */

235     public String JavaDoc getValue() {
236         return value;
237     }
238
239     /**
240      * Sets name of property of each bean that will be used to get value
241      *
242      * @param value value propery name to set
243      * @see #value
244      */

245     public void setValue(String JavaDoc value) {
246         this.value = value;
247     }
248
249     /**
250      * Processes the tag
251      *
252      * @throws JspException
253      * @throws IOException
254      */

255     public void doTag() throws JspException JavaDoc, IOException JavaDoc {
256
257         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
258
259         GridTag parentGridTag = (GridTag) findAncestorWithClass(this, GridTag.class);
260         if ( parentGridTag == null ) {
261             JspTagException JavaDoc e = new JspTagException JavaDoc("Parent tag is invalid! This tag is only valid when nested within 'grid' tag");
262             throw e;
263         }
264
265         // Get field name and field key from parent 'column' tag
266
ColumnTag parentColumnTag = (ColumnTag) findAncestorWithClass(this, ColumnTag.class);
267         if ( parentColumnTag != null ) {
268             if ( fieldName == null || fieldName.length() == 0 ) {
269                 fieldName = parentColumnTag.getFieldName();
270             }
271
272             if ( fieldKey == null || fieldKey.length() == 0 ) {
273                 fieldKey = parentColumnTag.getFieldKey();
274             }
275         }
276
277         if ( fieldName == null || "".equals(fieldName) ) {
278             // Error: there is no ancestor 'column' tag and no fieldName attribute is specified
279
String JavaDoc errorMessage = "There is no ancestor 'column' tag and no fieldName attribute is specified!";
280             if ( log.isErrorEnabled() ) {
281                 log.error(errorMessage);
282             }
283             JspTagException JavaDoc e = new JspTagException JavaDoc(errorMessage);
284             throw e;
285         }
286
287         // check beanId, method and collection attributes
288
if ( (beanId == null || method == null) && collection == null ) {
289             // error - either collection or beanId with method attributes must be specified
290
String JavaDoc errorMessage = "Either collection or beanId with method attributes must be specified";
291             JspTagException JavaDoc e = new JspTagException JavaDoc(errorMessage);
292             throw e;
293         }
294
295         if ( label != null && label.length() > 0 && (value == null || value.length() == 0) ) {
296             // error - either value or label and value properties must be specified, or both omitted
297
String JavaDoc errorMessage = "Either value or label and value properties must be specified, or both omitted";
298             JspTagException JavaDoc e = new JspTagException JavaDoc(errorMessage);
299             throw e;
300         }
301
302         String JavaDoc pageUrl = RequestUtil.getPageUrl(parentGridTag);
303
304         String JavaDoc scriptCall = "showSetFilterWindow(\'" + fieldName + "\', \'" + parentGridTag.getName() +
305                 "\', \'" + pageUrl + "\'";
306
307         if ( fieldKey == null ) {
308             fieldKey = "";
309         }
310
311         scriptCall += ", \'" + fieldKey + "\'";
312
313         if ( beanId == null || method == null ) {
314             beanId = "";
315             method = "";
316             // collection is specified - it is guaranteed above, so check its class
317
if ( !(collection instanceof Map JavaDoc) && !(collection instanceof Collection JavaDoc) && !(collection instanceof Object JavaDoc[]) ) {
318                 String JavaDoc errorMessage = "Only instances of java.util.Collection, java.util.Map and java.lang.Object[] are supported";
319                 JspTagException JavaDoc e = new JspTagException JavaDoc(errorMessage);
320                 throw e;
321             }
322
323             pageContext.getSession().setAttribute(SetFilter.AVAILABLE_ELEMENTS, collection);
324         }
325
326         scriptCall += ", \'" + beanId + "\', \'" + method + "\'";
327
328         if ( label == null ) {
329             label = "";
330         }
331
332         scriptCall += ", \'" + label + "\'";
333
334         if ( value == null ) {
335             value = "";
336         }
337
338         scriptCall += ", \'" + value + "\'";
339
340         if ( rowIterators == null ) {
341             rowIterators = "";
342         }
343
344         scriptCall += ", \'" + rowIterators + "\'";
345
346         scriptCall += ");";
347
348         if ( onclick != null && onclick.length() != 0 ) {
349             int k = onclick.indexOf("return false");
350             if ( k != -1 ) {
351                 onclick = onclick.substring(0, k);
352             }
353         } else {
354             onclick = new String JavaDoc();
355         }
356         onclick += scriptCall + " return false;";
357         StringWriter JavaDoc sw = new StringWriter JavaDoc();
358         StringBuffer JavaDoc sb = sw.getBuffer();
359         sb.append("<a HREF=\"javascript:void(0)\"").append(prepareAttributes()).append(">");
360         getJspBody().invoke(sw);
361         sb.append("</a>");
362         TagUtils.getInstance().write(pageContext, sw.toString());
363     }
364
365 }
366
Popular Tags