KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.core.RequestUtil;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.struts.taglib.TagUtils;
22
23 import javax.servlet.jsp.JspException JavaDoc;
24 import javax.servlet.jsp.JspTagException JavaDoc;
25 import javax.servlet.jsp.PageContext JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.StringWriter JavaDoc;
28
29
30 /**
31  * <p>Tag for marking column as filterable (Type: number). This will generate a
32  * link for displaying number filter window.
33  * <br />
34  * This tag is only valid when nested within <em>grid</em> tag
35  * </p>
36  * <p>
37  * Some attributes are inherited from {@link BaseFilterTag}.
38  * </p>
39  * <p>
40  * This tag must either be contained in <em>column</em> tag or have
41  * <b>fieldName</b> specified.
42  * </p>
43  * <p><a HREF="NumberFilterTag.java.htm"><i>View Source</i></a></p>
44  *
45  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
46  * @version $Revision: 1.11 $ $Date: 2005/09/21 13:45:59 $
47  * @jsp.tag name="numberFilter"
48  * body-content="scriptless"
49  * @see com.blandware.atleap.webapp.taglib.core.grid.GridTag
50  */

51 public class NumberFilterTag extends BaseFilterTag {
52     protected transient final Log log = LogFactory.getLog(NumberFilterTag.class);
53
54     /**
55      * Processes the tag
56      *
57      * @throws JspException
58      * @throws IOException
59      */

60     public void doTag() throws JspException JavaDoc, IOException JavaDoc {
61
62         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
63
64         // This tag is only valid when nested within 'grid' tag, so check this
65
GridTag parentGridTag = (GridTag) findAncestorWithClass(this, GridTag.class);
66
67         if ( parentGridTag == null ) {
68             JspTagException JavaDoc e = new JspTagException JavaDoc("Parent tag is invalid! This tag is only valid when nested within 'grid' tag");
69             throw e;
70         }
71
72         // Get field name and field key from parent 'column' tag
73
ColumnTag parentColumnTag = (ColumnTag) findAncestorWithClass(this, ColumnTag.class);
74         if ( parentColumnTag != null ) {
75             if ( fieldName == null || fieldName.length() == 0 ) {
76                 fieldName = parentColumnTag.getFieldName();
77             }
78
79             if ( fieldKey == null || fieldKey.length() == 0 ) {
80                 fieldKey = parentColumnTag.getFieldKey();
81             }
82         }
83
84         if ( fieldName == null || "".equals(fieldName) ) {
85             // Error: there is no ancestor 'column' tag and no fieldName attribute is specified
86
String JavaDoc errorMessage = "There is no ancestor 'column' tag and no fieldName attribute is specified!";
87             if ( log.isErrorEnabled() ) {
88                 log.error(errorMessage);
89             }
90             JspTagException JavaDoc e = new JspTagException JavaDoc(errorMessage);
91             throw e;
92         }
93
94
95         // Get page URL and create hyperlink to show filter window
96

97         String JavaDoc pageUrl = RequestUtil.getPageUrl(parentGridTag);
98
99         String JavaDoc scriptCall = "showNumberFilterWindow(\'" + fieldName + "\', \'" + parentGridTag.getName() +
100                 "\', \'" + pageUrl + "\'";
101
102         if ( fieldKey == null ) {
103             fieldKey = "";
104         }
105         scriptCall += ", \'" + fieldKey + "\'";
106
107         if ( rowIterators == null ) {
108             rowIterators = "";
109         }
110         scriptCall += ", \'" + rowIterators + "\'";
111
112
113         scriptCall += ");";
114
115         if ( onclick != null && onclick.length() != 0 ) {
116             int k = onclick.indexOf("return false");
117             if ( k != -1 ) {
118                 onclick = onclick.substring(0, k);
119             }
120         } else {
121             onclick = new String JavaDoc();
122         }
123         onclick += scriptCall + " return false;";
124         StringWriter JavaDoc sw = new StringWriter JavaDoc();
125         StringBuffer JavaDoc sb = sw.getBuffer();
126         sb.append("<a HREF=\"javascript:void(0)\"").append(prepareAttributes()).append(">");
127         getJspBody().invoke(sw);
128         sb.append("</a>");
129         TagUtils.getInstance().write(pageContext, sw.toString());
130     }
131
132 }
133
Popular Tags