KickJava   Java API By Example, From Geeks To Geeks.

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


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.BaseHandlerTag;
19 import com.blandware.atleap.webapp.util.core.RequestUtil;
20 import org.apache.struts.taglib.TagUtils;
21
22 import javax.servlet.jsp.JspException JavaDoc;
23 import javax.servlet.jsp.JspTagException JavaDoc;
24 import javax.servlet.jsp.PageContext JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.StringWriter JavaDoc;
27 import java.util.Map JavaDoc;
28
29
30 /**
31  * <p>Generates a link for clearing a filter with given name or all filters on
32  * a grid. Body of this tag will be rendered (as link text) if there is a filter
33  * in current grid associated with field with specified name, or if there is at
34  * least one filter if "all" attribute set to "true".
35  * <br />
36  * This tag is only valid when nested within <em>grid</em> tag.
37  * </p>
38  * <p>
39  * Allowed attributes are:
40  * <ul>
41  * <li>
42  * <b>fieldName</b> - name of field for which to remove filtering
43  * </li>
44  * <li>
45  * <b>all</b> - if 'true', all filters for current grid will be cleared
46  * </li>
47  * </ul>
48  * </p>
49  * <p>
50  * At least one of these attributes must be specified. <b>fieldName</b> has a
51  * precedence (that is, if <code>fieldName="someField"</code> and
52  * <code>all="true"</code>, only field with name 'someField' will be cleared
53  * when user clicks generated link).
54  * </p>
55  * <p><a HREF="ClearFilterTag.java.htm"><i>View Source</i></a></p>
56  * <p/>
57  *
58  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
59  * @version $Revision: 1.9 $ $Date: 2005/09/21 13:45:58 $
60  * @jsp.tag name="clearFilter"
61  * body-content="scriptless"
62  */

63 public class ClearFilterTag extends BaseHandlerTag {
64
65     // ~ Instance variables
66

67     /**
68      * Field name to remove filter from
69      */

70     protected String JavaDoc fieldName = null;
71
72     /**
73      * Indicates whether or not all filters must be removed from grid
74      */

75     protected Boolean JavaDoc all = null;
76
77
78     /**
79      * Returns field name
80      *
81      * @return field name
82      * @jsp.attribute required="false"
83      * rtexprvalue="true"
84      * type="java.lang.String"
85      * description="Field name to remove filter from"
86      */

87     public String JavaDoc getFieldName() {
88         return fieldName;
89     }
90
91     /**
92      * Sets field name
93      *
94      * @param fieldName field name to set
95      */

96     public void setFieldName(String JavaDoc fieldName) {
97         this.fieldName = fieldName;
98     }
99
100     /**
101      * Returns whether or not all filters must be removed from grid
102      *
103      * @return whether all filters must be removed from grid
104      * @jsp.attribute required="false"
105      * rtexprvalue="true"
106      * type="java.lang.Boolean"
107      * description="Indicates whether or not all filters must be removed from grid"
108      */

109     public Boolean JavaDoc getAll() {
110         return all;
111     }
112
113     /**
114      * Sets whether or not all filters must be removed from grid
115      *
116      * @param all whether all filters must be removed from grid
117      */

118     public void setAll(Boolean JavaDoc all) {
119         this.all = all;
120     }
121
122     /**
123      * Processes the tag
124      *
125      * @throws JspException
126      * @throws IOException
127      */

128     public void doTag() throws JspException JavaDoc, IOException JavaDoc {
129
130         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
131
132         GridTag parentGridTag = (GridTag) findAncestorWithClass(this, GridTag.class);
133
134         if ( parentGridTag == null ) {
135             JspTagException JavaDoc e = new JspTagException JavaDoc("Parent tag is invalid! This tag is only valid when nested within 'grid' tag");
136             throw e;
137         }
138
139         if ( fieldName == null && all == null ) {
140             throw new JspTagException JavaDoc("Either 'fieldName' or 'all' attributes must be specified!");
141         }
142
143
144         if ( fieldName != null || all.equals(Boolean.TRUE) ) {
145
146             // Get request URL and parameter map from parent grid tag.
147
// Put needed request parameters in this map and attach it to page URL
148

149             String JavaDoc pageUrl = parentGridTag.getPageUrl();
150             Map JavaDoc parameterMap = RequestUtil.prepareParameterMap(parentGridTag.getParameterMap());
151
152             parameterMap.put("gridName", parentGridTag.getName());
153             pageUrl += "?" + RequestUtil.createQueryStringFromMap(parameterMap, "&");
154
155             if ( fieldName != null ) {
156                 parameterMap.put("clearFilter", fieldName);
157             } else {
158                 parameterMap.put("clearAllFilters", "true");
159             }
160
161             StringWriter JavaDoc sw = new StringWriter JavaDoc();
162             StringBuffer JavaDoc sb = sw.getBuffer();
163             sb.append("<a HREF=\"").append(pageUrl).append("\"").append(prepareAttributes()).append(">");
164             getJspBody().invoke(sw);
165             sb.append("</a>");
166             TagUtils.getInstance().write(pageContext, sw.toString());
167         }
168
169     }
170
171 }
172
Popular Tags