KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > mlw > vlh > web > tag > FilterListTag


1 /**
2  * Copyright (c) 2003 held jointly by the individual authors.
3  *
4  * This library is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; with out even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  *
18  * > http://www.gnu.org/copyleft/lesser.html
19  * > http://www.opensource.org/licenses/lgpl-license.php
20  */

21 package net.mlw.vlh.web.tag;
22
23 import java.text.MessageFormat JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30 import javax.servlet.jsp.JspException JavaDoc;
31 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
32 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
33
34 import net.mlw.vlh.ValueListInfo;
35 import net.mlw.vlh.web.util.JspUtils;
36
37 /**
38  *
39  * @author Matthew L. Wilson
40  * @version $Revision: 1.11 $ $Date: 2005/11/23 15:00:33 $
41  */

42 public class FilterListTag extends BodyTagSupport JavaDoc
43 {
44
45    private static final String JavaDoc DEFAULT_TOKEN = "|";
46
47    private String JavaDoc token = DEFAULT_TOKEN;
48
49    private String JavaDoc key;
50
51    private String JavaDoc format;
52
53    private Map JavaDoc parameters;
54
55    private ValueListSpaceTag rootTag;
56
57    /**
58     * @see javax.servlet.jsp.tagext.Tag#doStartTag()
59     */

60    public int doStartTag() throws JspException JavaDoc
61    {
62       rootTag = (ValueListSpaceTag) JspUtils.getParent(this, ValueListSpaceTag.class);
63       parameters = new HashMap JavaDoc(rootTag.getTableInfo().getParameters());
64       return super.doStartTag();
65    }
66
67    /**
68     * @see javax.servlet.jsp.tagext.IterationTag#doAfterBody()
69     */

70    public int doAfterBody() throws JspException JavaDoc
71    {
72       BodyContent JavaDoc body = getBodyContent();
73       if (body != null)
74       {
75          List JavaDoc ignoreList = new ArrayList JavaDoc();
76          ignoreList.add(getKey());
77
78          TableInfo tableInfo = rootTag.getTableInfo();
79
80          //Remove the page. Bug [1047622]
81
parameters.remove(ValueListInfo.PAGING_PAGE + tableInfo.getId());
82
83          String JavaDoc content = body.getString().trim();
84          String JavaDoc valueInRequest = pageContext.getRequest().getParameter(getKey() + tableInfo.getId());
85          JspUtils.writePrevious(pageContext, "<table class=\"filters\" width=\"100%\"><tr>");
86          for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(content, getToken()); st.hasMoreTokens();)
87          {
88             String JavaDoc token = st.nextToken();
89             String JavaDoc value = (format == null) ? token : MessageFormat.format(format, new String JavaDoc[]
90             { token });
91
92             boolean selected = value.equals(valueInRequest);
93
94             parameters.put(getKey() + tableInfo.getId(), value);
95
96             JspUtils.writePrevious(pageContext, (selected ? "<th>" : "<td>") + "<a HREF=\"" + tableInfo.getUrl()
97                   + rootTag.getConfig().getLinkEncoder().encode(pageContext, parameters) + "\">");
98             JspUtils.writePrevious(pageContext, token);
99             JspUtils.writePrevious(pageContext, "</a>" + (selected ? "</th>" : "</td>"));
100          }
101          JspUtils.writePrevious(pageContext, "</tr></table>");
102
103       }
104       return SKIP_BODY;
105    }
106
107    /**
108     * @see javax.servlet.jsp.tagext.Tag#doEndTag()
109     */

110    public int doEndTag() throws JspException JavaDoc
111    {
112       int result = super.doEndTag();
113       reset();
114       return result;
115    }
116
117    /**
118     * @return Returns the key.
119     */

120    public String JavaDoc getKey()
121    {
122       return key;
123    }
124
125    /**
126     * @param key
127     * The key to set.
128     */

129    public void setKey(String JavaDoc key)
130    {
131       this.key = key;
132    }
133
134    /**
135     * @return Returns the token.
136     */

137    public String JavaDoc getToken()
138    {
139       return token;
140    }
141
142    /**
143     * @param token
144     * The token to set.
145     */

146    public void setToken(String JavaDoc token)
147    {
148       this.token = token;
149    }
150
151    /**
152     * @return Returns the format.
153     */

154    public String JavaDoc getFormat()
155    {
156       return format;
157    }
158
159    /**
160     * @param format
161     * The format to set.
162     */

163    public void setFormat(String JavaDoc format)
164    {
165       this.format = format;
166    }
167
168    private void reset()
169    {
170       this.rootTag = null;
171       this.format = null;
172       this.key = null;
173       this.parameters = null;
174       this.token = DEFAULT_TOKEN;
175    }
176
177    /**
178     * Called on a Tag handler to release state.
179     * The page compiler guarantees that JSP page implementation
180     * objects will invoke this method on all tag handlers,
181     * but there may be multiple invocations on doStartTag and doEndTag in between.
182     *
183     * @see javax.servlet.jsp.tagext.Tag#release()
184     */

185    public void release()
186    {
187       super.release();
188       reset();
189    }
190 }
Popular Tags