KickJava   Java API By Example, From Geeks To Geeks.

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


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: string). This will generate a
32  * link for displaying string 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="StringFilterTag.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.10 $ $Date: 2005/09/21 13:46:01 $
47  * @jsp.tag name="stringFilter"
48  * body-content="scriptless"
49  * @see com.blandware.atleap.webapp.taglib.core.grid.GridTag
50  */

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

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