KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > table > tags > PageSizeTag


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.table.tags;
21
22 import java.net.MalformedURLException JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26
27 import javax.servlet.jsp.JspException JavaDoc;
28 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
29
30 import org.apache.struts.taglib.TagUtils;
31
32 import com.sslexplorer.core.CoreUtil;
33 import com.sslexplorer.core.tags.FormTag;
34 import com.sslexplorer.table.Pager;
35
36 public class PageSizeTag extends TagSupport JavaDoc {
37     protected String JavaDoc pagerProperty = null;
38     protected String JavaDoc pagerName = null;
39     protected String JavaDoc action = null;
40     protected String JavaDoc styleId = null;
41     protected String JavaDoc styleClass = null;
42     protected String JavaDoc style = null;
43     protected String JavaDoc forward;
44     protected String JavaDoc attributesName;
45     protected String JavaDoc attributesProperties;
46     protected String JavaDoc subForm = null;
47     
48     public final static String JavaDoc[] PAGE_SIZES = { "10", "20", "50", "100", "*" };
49
50     public String JavaDoc getPagerName() {
51         return (this.pagerName);
52     }
53
54     public void setAction(String JavaDoc action) {
55         this.action = action;
56     }
57
58     public String JavaDoc getAction() {
59         return action;
60     }
61
62     public String JavaDoc getForward() {
63         return forward;
64     }
65
66     public void setForward(String JavaDoc forward) {
67         this.forward = forward;
68     }
69
70     public void setPagerName(String JavaDoc pagerName) {
71         this.pagerName = pagerName;
72     }
73
74     public void setSubForm(String JavaDoc subForm) {
75         this.subForm = subForm;
76     }
77     
78     public String JavaDoc getSubForm() {
79         return subForm;
80     }
81     public String JavaDoc getPagerProperty() {
82         return (this.pagerProperty);
83     }
84
85     public void setPagerProperty(String JavaDoc pagerProperty) {
86         this.pagerProperty = pagerProperty;
87     }
88
89     public void setStyle(String JavaDoc style) {
90         this.style = style;
91     }
92
93     public String JavaDoc getStyle() {
94         return style;
95     }
96
97     public void setStyleClass(String JavaDoc styleClass) {
98         this.styleClass = styleClass;
99     }
100
101     public String JavaDoc getStyleClass() {
102         return styleClass;
103     }
104
105     public void setStyleId(String JavaDoc styleId) {
106         this.styleId = styleId;
107     }
108
109     public String JavaDoc getStyleId() {
110         return styleId;
111     }
112
113     public void release() {
114         super.release();
115         pagerName = null;
116         pagerProperty = null;
117         action = null;
118         styleId = null;
119         styleClass = null;
120         style = null;
121         forward = null;
122         attributesName = null;
123         attributesProperties = null;
124         subForm = null;
125
126     }
127     
128     public void setAttributesName(String JavaDoc attributesName) {
129         this.attributesName = attributesName;
130     }
131
132     public void setAttributesProperties(String JavaDoc attributesProperties) {
133         this.attributesProperties = attributesProperties;
134     }
135
136     public int doEndTag() throws JspException JavaDoc {
137         StringBuffer JavaDoc results = new StringBuffer JavaDoc();
138         Pager pager = (Pager) TagUtils.getInstance().lookup(pageContext, pagerName, pagerProperty, null);
139         if (pager != null && pager.getModel().getRowCount() >= Integer.parseInt(PAGE_SIZES[0])) {
140
141             // Start of table
142
results.append("<ul>");
143             for (int i = 0; i < PAGE_SIZES.length; i++) {
144                 if(PAGE_SIZES[i].equals("*") || pager.getModel().getRowCount() >= Integer.parseInt(PAGE_SIZES[i])) {
145                     results.append("<li><a HREF=\"");
146                     results.append(getPageLink(PAGE_SIZES[i].equals("*") ? 0 : Integer.parseInt(PAGE_SIZES[i]), pager));
147                     results.append("\"");
148                     results.append(makeStyles(false));
149                     results.append("><div class=\"pageSize\">");
150                     results.append(PAGE_SIZES[i]);
151                     results.append("</div></a></li>");
152                 }
153             }
154             results.append("</ul>");
155         }
156         else {
157             results.append("&nbsp;");
158         }
159
160         TagUtils.getInstance().write(pageContext, results.toString());
161         return (EVAL_PAGE);
162
163     }
164
165     protected String JavaDoc getPageLink(int pageSize, Pager pager) {
166         String JavaDoc url = null;
167         try {
168             Map JavaDoc params = new HashMap JavaDoc();
169             params.put("pageSize", String.valueOf(pageSize));
170             params.put("sortName", pager.getSortName());
171             params.put("sortReverse", String.valueOf(pager.getSortReverse()));
172             if(subForm!=null)
173                 params.put("subForm", subForm);
174             if (pageSize == 0 || pageSize > pager.getFilteredRowCount()) {
175                 params.put("startRow", "0");
176             }
177             else {
178                 params.put("startRow", String.valueOf(pager.getStartRow()));
179             }
180             if(attributesName != null) {
181                 StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(attributesProperties, ",");
182                 while (t.hasMoreTokens()) {
183                     String JavaDoc attr = t.nextToken();
184                     try {
185                         params.put(attr, TagUtils.getInstance().lookup(pageContext, attributesName, attr, null).toString());
186                     } catch (Exception JavaDoc e) {
187                     }
188                 }
189             }
190             url = TagUtils.getInstance().computeURLWithCharEncoding(pageContext, forward, null, "", action, null, params, null,
191                             false, false);
192         } catch (MalformedURLException JavaDoc e) {
193         }
194         return (url);
195     }
196
197     protected String JavaDoc makeStyles(boolean disabled) throws JspException JavaDoc {
198         StringBuffer JavaDoc results = new StringBuffer JavaDoc();
199         String JavaDoc s = getStyleClass();
200         if (s != null) {
201             results.append(" class=\"");
202             results.append(s);
203             results.append("\"");
204         }
205         s = getStyle();
206         if (s != null) {
207             results.append(" style=\"");
208             results.append(s);
209             results.append("\"");
210         }
211         s = getStyleId();
212         if (s != null) {
213             results.append(" id=\"");
214             results.append(s);
215             results.append("\"");
216         }
217         return results.toString();
218     }
219
220     /*
221      * (non-Javadoc)
222      *
223      * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
224      */

225     public int doStartTag() throws JspException JavaDoc {
226         // JDR change so that task processes return to the correct location
227
FormTag formTag = (FormTag) CoreUtil.getParentTagOfClass(FormTag.class, this);
228         if (formTag != null) {
229             subForm = formTag.getSubFormName();
230         }
231         else {
232             subForm = null;
233         }
234         return SKIP_BODY;
235     }
236
237 }
238
Popular Tags