KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20
21 import javax.servlet.jsp.JspException JavaDoc;
22 import javax.servlet.jsp.JspTagException JavaDoc;
23 import javax.servlet.jsp.PageContext JavaDoc;
24 import javax.servlet.jsp.tagext.JspFragment JavaDoc;
25 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 /**
29  * <p>Tag that renders to pager. If there are no pages to show or only one page
30  * exists, body of this tag will not be rendered. The main function of this tag
31  * is exactly to determine whether we need to render some controls (links) that
32  * are called pager because they help navigate through data pages.
33  * <br />
34  * This tag is only valid when nested within <em>grid</em> tag.
35  * </p>
36  * <p>
37  * Allowed attributes are:
38  * <ul>
39  * <li>
40  * <b>pageCount</b> - number of links to pages to display. Default is 10.
41  * </li>
42  * </ul>
43  * </p>
44  * <p>
45  * Here's an example:
46  * <pre>
47  * &lt;atleap:pager pageCount=&quot;10&quot;&gt;
48  * &lt;td colspan=&quot;${3 + additionalCells}&quot; align=&quot;center&quot;&gt;
49  * &lt;atleap:message key=&quot;core.grid.pager.title&quot; /&gt;&amp;nbsp;
50  * &lt;atleap:firstPage&gt;&lt;img SRC=&quot;&lt;atleap:rewriteUrl HREF=&quot;/images/core/first.gif&quot; /&gt;&quot; border=&quot;0&quot; alt=&quot;&lt;atleap:message key=&quot;core.grid.pager.first&quot;/&gt;&quot; title=&quot;&lt;atleap:message key=&quot;core.grid.pager.first&quot;/&gt;&quot; /&gt;&lt;/atleap:firstPage&gt;
51  * &lt;atleap:prevPage&gt;&lt;img SRC=&quot;&lt;atleap:rewriteUrl HREF=&quot;/images/core/prev.gif&quot; /&gt;&quot; border=&quot;0&quot; alt=&quot;&lt;atleap:message key=&quot;core.grid.pager.prev&quot;/&gt;&quot; title=&quot;&lt;atleap:message key=&quot;core.grid.pager.prev&quot;/&gt;&quot; /&gt;&lt;/atleap:prevPage&gt;
52  * &lt;atleap:pagesIterator&gt;
53  * &lt;atleap:page&gt;${pageNumber}&amp;nbsp;&lt;/atleap:page&gt;
54  * &lt;atleap:currentPage&gt;&lt;b&gt;${pageNumber}&lt;/b&gt;&amp;nbsp;&lt;/atleap:currentPage&gt;
55  * &lt;/atleap:pagesIterator&gt;
56  * &lt;atleap:nextPage&gt;&lt;img SRC=&quot;&lt;atleap:rewriteUrl HREF=&quot;/images/core/next.gif&quot; /&gt;&quot; border=&quot;0&quot; alt=&quot;&lt;atleap:message key=&quot;core.grid.pager.next&quot;/&gt;&quot; title=&quot;&lt;atleap:message key=&quot;core.grid.pager.next&quot;/&gt;&quot; /&gt;&lt;/atleap:nextPage&gt;
57  * &lt;atleap:lastPage&gt;&lt;img SRC=&quot;&lt;atleap:rewriteUrl HREF=&quot;/images/core/last.gif&quot; /&gt;&quot; border=&quot;0&quot; alt=&quot;&lt;atleap:message key=&quot;core.grid.pager.last&quot;/&gt;&quot; title=&quot;&lt;atleap:message key=&quot;core.grid.pager.last&quot;/&gt;&quot; /&gt;&lt;/atleap:lastPage&gt;
58  * &lt;/td&gt;
59  * &lt;/atleap:pager&gt;
60  * </pre>
61  * </p>
62  * <p><a HREF="PagerTag.java.htm"><i>View Source</i></a></p>
63  *
64  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
65  * @version $Revision: 1.9 $ $Date: 2006/03/17 09:04:49 $
66  * @see PagesIteratorTag
67  * @see PageTag
68  * @see CurrentPageTag
69  * @jsp.tag name="pager"
70  * body-content="scriptless"
71  * @see com.blandware.atleap.webapp.taglib.core.grid.GridTag
72  */

73 public class PagerTag extends SimpleTagSupport JavaDoc {
74
75     protected transient final Log log = LogFactory.getLog(PagerTag.class);
76
77     /**
78      * Number of page links to display
79      */

80     protected Integer JavaDoc pageCount = null;
81
82     /**
83      * Number of last page
84      */

85     protected Integer JavaDoc lastPageNumber = null;
86
87     /**
88      * Returns number of page links to display
89      *
90      * @return number of page links to display
91      * @jsp.attribute required="false"
92      * rtexprvalue="true"
93      * type="java.lang.Integer"
94      * description="Number of page links to display"
95      */

96     public Integer JavaDoc getPageCount() {
97         return pageCount;
98     }
99
100     /**
101      * Sets number of page links to display
102      *
103      * @param pageCount number of page links to display to set
104      */

105     public void setPageCount(Integer JavaDoc pageCount) {
106         this.pageCount = pageCount;
107     }
108
109     /**
110      * Returns number of last page
111      *
112      * @return number of last page
113      */

114     protected Integer JavaDoc getLastPageNumber() {
115         return this.lastPageNumber;
116     }
117
118     /**
119      * Processes the tag
120      *
121      * @throws JspException
122      * @throws IOException
123      */

124     public void doTag() throws JspException JavaDoc, IOException JavaDoc {
125
126         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
127
128         // This tag is only valid when nested within 'grid' tag, so check this
129
GridTag parentGridTag = (GridTag) findAncestorWithClass(this, GridTag.class);
130
131         if ( parentGridTag == null ) {
132             JspTagException JavaDoc e = new JspTagException JavaDoc("Parent tag is invalid! This tag is only valid when nested within 'grid' tag");
133             throw e;
134         }
135
136         // Set page count (number of links to pages to show) and calculate number of last page
137

138         if ( pageCount == null ) {
139             pageCount = new Integer JavaDoc(10);
140         }
141
142         int total = parentGridTag.getGrid().getTotal().intValue();
143         int pageSize = parentGridTag.getPageSize().intValue();
144         if (pageSize <= 0) {
145             throw new IllegalArgumentException JavaDoc("Page size must be positive");
146         }
147
148         if ( total > pageSize ) {
149
150             int last = total / pageSize;
151             if ( total % pageSize > 0 ) {
152                 last += 1;
153             }
154
155             if ( last <= 1 ) {
156                 last = 1;
157             }
158
159             lastPageNumber = new Integer JavaDoc(last);
160
161             JspFragment JavaDoc body = getJspBody();
162             if ( body != null ) {
163                 body.invoke(null);
164             }
165         }
166     }
167
168 }
169
Popular Tags