KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > servlet > http > jsp > tagext > iteration > ComputePageTag


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.servlet.http.jsp.tagext.iteration;
8
9 import javax.servlet.jsp.JspException JavaDoc;
10 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
11
12 import org.apache.struts.util.RequestUtils;
13
14 /**
15  * Description of the Class
16  *
17  * @author Laurent Etiemble
18  * @version $Revision: 1.4 $
19  * @todo Javadoc to complete
20  * @jsp:tag name="computePage"
21  * body-content="empty"
22  */

23 public class ComputePageTag extends TagSupport JavaDoc
24 {
25    /** Description of the Field */
26    protected String JavaDoc current = null;
27    /** Description of the Field */
28    protected String JavaDoc id = null;
29    /** Description of the Field */
30    protected String JavaDoc length = null;
31    /** Description of the Field */
32    protected String JavaDoc offset = null;
33    /** Description of the Field */
34    protected String JavaDoc scope = null;
35    /** Description of the Field */
36    protected String JavaDoc size = null;
37
38
39    /**
40     * Description of the Method
41     *
42     * @return Description of the Returned Value
43     * @exception JspException Description of Exception
44     */

45    public int doStartTag()
46       throws JspException JavaDoc
47    {
48       int currentPage = ((Integer JavaDoc) RequestUtils.lookup(pageContext, current, scope)).intValue();
49       int contentSize = ((Integer JavaDoc) RequestUtils.lookup(pageContext, size, scope)).intValue();
50       int offsetValue = Integer.parseInt(offset);
51       int pageLength = Integer.parseInt(length);
52
53       // Compute the last page
54
int lastPage = (contentSize / pageLength);
55       if ((contentSize % pageLength) > 0)
56       {
57          lastPage++;
58       }
59       if (lastPage == 0)
60       {
61          lastPage = 1;
62       }
63       if (currentPage < 1)
64       {
65          currentPage = 1;
66       }
67       if (currentPage > lastPage)
68       {
69          currentPage = 1;
70       }
71
72       // Apply the offset
73
currentPage = currentPage + offsetValue;
74
75       int index = (currentPage - 1) * pageLength;
76
77       pageContext.setAttribute(current, new Integer JavaDoc(currentPage));
78       pageContext.setAttribute(id, new Integer JavaDoc(index));
79
80       // Continue processing this page
81
return (SKIP_BODY);
82    }
83
84
85    /**
86     * Returns the start.
87     *
88     * @return String
89     * @jsp:attribute name="current"
90     * required="true"
91     * rtexprvalue="true"
92     */

93    public String JavaDoc getCurrent()
94    {
95       return current;
96    }
97
98
99    /**
100     * Returns the id.
101     *
102     * @return String
103     * @jsp:attribute name="id"
104     * required="true"
105     * rtexprvalue="true"
106     */

107    public String JavaDoc getId()
108    {
109       return id;
110    }
111
112
113    /**
114     * Returns the page.
115     *
116     * @return String
117     * @jsp:attribute name="length"
118     * required="true"
119     * rtexprvalue="true"
120     */

121    public String JavaDoc getLength()
122    {
123       return length;
124    }
125
126
127    /**
128     * Returns the offset.
129     *
130     * @return String
131     * @jsp:attribute name="offset"
132     * required="true"
133     * rtexprvalue="true"
134     */

135    public String JavaDoc getOffset()
136    {
137       return offset;
138    }
139
140
141    /**
142     * Getter for the scope attribute
143     *
144     * @return The value of scope attribute
145     * @jsp:attribute name="scope"
146     * required="false"
147     * rtexprvalue="true"
148     */

149    public String JavaDoc getScope()
150    {
151       return (this.scope);
152    }
153
154
155    /**
156     * Returns the length.
157     *
158     * @return String
159     * @jsp:attribute name="size"
160     * required="true"
161     * rtexprvalue="true"
162     */

163    public String JavaDoc getSize()
164    {
165       return size;
166    }
167
168
169    /** Release all allocated resources. */
170    public void release()
171    {
172       super.release();
173       size = null;
174       current = null;
175    }
176
177
178    /**
179     * Sets the current.
180     *
181     * @param current The current to set
182     */

183    public void setCurrent(String JavaDoc current)
184    {
185       this.current = current;
186    }
187
188
189    /**
190     * Sets the id.
191     *
192     * @param id The id to set
193     */

194    public void setId(String JavaDoc id)
195    {
196       this.id = id;
197    }
198
199
200    /**
201     * Sets the length.
202     *
203     * @param length The length to set
204     */

205    public void setLength(String JavaDoc length)
206    {
207       this.length = length;
208    }
209
210
211    /**
212     * Sets the offset.
213     *
214     * @param offset The offset to set
215     */

216    public void setOffset(String JavaDoc offset)
217    {
218       this.offset = offset;
219    }
220
221
222    /**
223     * Sets the scope.
224     *
225     * @param scope The scope to set
226     */

227    public void setScope(String JavaDoc scope)
228    {
229       this.scope = scope;
230    }
231
232
233    /**
234     * Sets the size.
235     *
236     * @param size The size to set
237     */

238    public void setSize(String JavaDoc size)
239    {
240       this.size = size;
241    }
242 }
243
244
Popular Tags