KickJava   Java API By Example, From Geeks To Geeks.

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


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.taglib.core.util.TaglibConstants;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import javax.servlet.jsp.JspException JavaDoc;
23 import javax.servlet.jsp.JspTagException JavaDoc;
24 import javax.servlet.jsp.PageContext JavaDoc;
25 import javax.servlet.jsp.tagext.JspFragment JavaDoc;
26 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
27 import java.io.IOException JavaDoc;
28
29 /**
30  * <p>This tag iterates over all pages in pager and exports page number on each
31  * iteration.
32  * <br />
33  * This tag is only valid when nested within <em>pager</em> tag.</p>
34  * <p>
35  * Allowed attributes are:
36  * <ul>
37  * <li>
38  * <b>var</b> - name of variable to which page number will be saved on each
39  * iteration. If not specified, this is assigned to
40  * <code>TaglibConstants.PAGE_NUMBER_ATTR</code>.
41  * </li>
42  * </ul>
43  * </p>
44  * <p><a HREF="PagesIteratorTag.java.htm"><i>View Source</i></a></p>
45  *
46  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
47  * @version $Revision: 1.8 $ $Date: 2005/10/12 13:34:57 $
48  * @see PagerTag
49  * @see PageTag
50  * @see CurrentPageTag
51  * @jsp.tag name="pagesIterator"
52  * body-content="scriptless"
53  */

54 public class PagesIteratorTag extends SimpleTagSupport JavaDoc {
55
56     protected transient final Log log = LogFactory.getLog(PagesIteratorTag.class);
57
58     /**
59      * Page Context attribute name under which number of page must be saved on each iteration
60      */

61     protected String JavaDoc var;
62
63     /**
64      * Number of page on each iteration
65      */

66     protected Integer JavaDoc pageNumber = new Integer JavaDoc(1);
67
68     private int loopStart = 0;
69     private int loopFinish = 0;
70
71     /**
72      * Returns name of variable which will accept number of page
73      *
74      * @return name of variable
75      * @jsp.attribute required="false"
76      * rtexprvalue="true"
77      * type="java.lang.String"
78      * description="Page Context attribute name under which number of page must be saved on each iteration"
79      */

80     public String JavaDoc getVar() {
81         return var;
82     }
83
84     /**
85      * Sets name of variable which will accept number of page
86      *
87      * @param var name of variable to set
88      */

89     public void setVar(String JavaDoc var) {
90         this.var = var;
91     }
92
93     /**
94      * Returns current page number
95      *
96      * @return page number
97      */

98     public Integer JavaDoc getPageNumber() {
99         return pageNumber;
100     }
101
102     /**
103      * Sets current page number
104      *
105      * @param pageNumber page number to set
106      */

107     public void setPageNumber(Integer JavaDoc pageNumber) {
108         this.pageNumber = pageNumber;
109     }
110
111     /**
112      * Constructs an iterator for collection of page numbers and loops through the body once per element.
113      *
114      * @throws JspTagException if a JSP exception has occurred
115      * @throws java.io.IOException if there is an error when invoking body content
116      */

117     public void doTag() throws JspException JavaDoc, IOException JavaDoc {
118
119         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
120
121         // This tag is only valid when nested within 'pager' tag, so check this
122
PagerTag parentPagerTag = (PagerTag) findAncestorWithClass(this, PagerTag.class);
123
124         if ( parentPagerTag == null ) {
125             JspTagException JavaDoc e = new JspTagException JavaDoc("Parent tag is invalid! This tag is only valid when nested within 'pager' tag");
126             throw e;
127         }
128
129         GridTag parentGridTag = (GridTag) findAncestorWithClass(this, GridTag.class);
130
131         // Calculate loop start and loop finish
132
// loop start - page number to start iteration from
133
// loop finish - page number to finish iteration on
134

135         int pageCount = parentPagerTag.getPageCount().intValue();
136         int currentPageNumber = parentGridTag.getCurrentPageNumber().intValue();
137         int firstPageNumber = 1;
138         int lastPageNumber = parentPagerTag.getLastPageNumber().intValue();
139         int diff = (pageCount / 2);
140         loopStart = currentPageNumber - diff;
141         if ( loopStart < firstPageNumber ) {
142             loopStart = firstPageNumber;
143         }
144         loopFinish = loopStart + pageCount - 1;
145         if ( loopFinish > lastPageNumber ) {
146             loopFinish = lastPageNumber;
147             loopStart = loopFinish - pageCount + 1;
148             if ( loopStart < firstPageNumber ) {
149                 loopStart = firstPageNumber;
150             }
151         }
152
153         if ( var == null ) {
154             var = TaglibConstants.PAGE_NUMBER_ATTR;
155         }
156
157         while ( loopStart <= loopFinish ) {
158             // Export next page number and invoke body content into standard JSP out
159
pageNumber = new Integer JavaDoc(loopStart++);
160             pageContext.setAttribute(var, pageNumber);
161             JspFragment JavaDoc body = getJspBody();
162             if ( body != null ) {
163                 body.invoke(null);
164             }
165         }
166     }
167
168 }
169
Popular Tags