KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > taglibs > pagination > CListPaginationTag


1 package org.jahia.taglibs.pagination;
2
3 import java.io.IOException JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 import javax.servlet.jsp.JspException JavaDoc;
7 import javax.servlet.jsp.JspTagException JavaDoc;
8 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
9 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
10
11 import org.jahia.data.beans.ContainerListBean;
12 import org.jahia.data.beans.PaginationBean;
13 import org.jahia.data.containers.JahiaContainerList;
14 import org.jahia.data.containers.JahiaContainerListPagination;
15 import org.jahia.taglibs.container.ContainerListTag;
16
17 /**
18  * Lookup for the ContainerListPagination instance of the enclosing
19  * ContainerListTag and iterate through the list of paginated pages.
20  *
21  * <pre>
22  * <<next -->1 2 3<-- next>>
23  * </pre>
24  *
25  * @author NK
26  * @version 1.0
27  */

28
29 public class CListPaginationTag extends BodyTagSupport JavaDoc {
30
31     private JahiaContainerListPagination cPagination = null;
32     private int pageNumber = 0;
33     private boolean isCurrentPage = false;
34     private Iterator JavaDoc iterator;
35
36     private int nbStepPerPage = -1; // by default, display all page step.
37
private String JavaDoc skipOnePageOnly = "true"; // by default skip displaying pagination if there is only one page
38

39     private int startPageIndex = 0;
40     private int stopPageIndex = 0;
41     private String JavaDoc name = null;
42
43     /**
44      * Return the number of step per displayed page.
45      */

46     public int getNbStepPerPage () {
47         return this.nbStepPerPage;
48     }
49
50     /**
51      * Set the number of step per displayed page.
52      */

53     public void setNbStepPerPage (int aNbStepPerPage) {
54         this.nbStepPerPage = aNbStepPerPage;
55     }
56
57     /**
58      * Return the value of the status skipOnePageOnly.
59      */

60     public String JavaDoc skipOnePageOnly () {
61         return this.skipOnePageOnly;
62     }
63
64     /**
65      * Set the value of the status skipOnePageOnly ( true/false ).
66      */

67     public void setSkipOnePageOnly (String JavaDoc val) {
68         if (val != null) {
69             this.skipOnePageOnly = val.toLowerCase();
70         }
71     }
72
73     /**
74      * The next iterated page number from the list of paginated pages.
75      */

76     public int getPageNumber () {
77         return this.pageNumber;
78     }
79
80     /**
81      * Return true if the pageIndex is equals to the current displayed page.
82      */

83     public boolean isCurrentPage () {
84         return isCurrentPage;
85     }
86
87     /**
88      * Return the stop page index.
89      */

90     public int getStopPageIndex () {
91         return this.stopPageIndex;
92     }
93
94     /**
95      * Return the start page index.
96      */

97     public int getStartPageIndex () {
98         return this.startPageIndex;
99     }
100
101     public String JavaDoc getName () {
102         return name;
103     }
104
105     public void setName (String JavaDoc aName) {
106         this.name = aName;
107     }
108
109     public int doStartTag () {
110
111         JahiaContainerList cList = null;
112         if (getName() != null) {
113             ContainerListBean containerListBean = (ContainerListBean)
114                                                   pageContext.findAttribute(
115                 getName());
116             cList = containerListBean.getJahiaContainerList();
117         } else {
118             // gets the enclosing tag ContainerListTag
119
ContainerListTag containerListTag = (ContainerListTag)
120                                                 findAncestorWithClass(this,
121                 ContainerListTag.class);
122             if (containerListTag != null) {
123                 cList = containerListTag.getContainerList();
124             }
125         }
126         if (cList != null) {
127             cPagination = cList.getCtnListPagination();
128             if (cPagination != null && cPagination.isValid()) {
129                 if (cPagination.getNbPages() == 1 &&
130                     this.skipOnePageOnly.equals("true")) {
131                     return SKIP_BODY;
132                 }
133                 if (this.nbStepPerPage <= 0) {
134                     this.startPageIndex = 1;
135                     this.stopPageIndex = cPagination.getNbPages();
136                     this.pageNumber = 1;
137                 } else {
138                     if (cPagination.getCurrentPageIndex() <= this.nbStepPerPage) {
139                         this.startPageIndex = 1;
140                         this.stopPageIndex = this.nbStepPerPage;
141                     } else {
142                         if ( (cPagination.getCurrentPageIndex() %
143                               this.nbStepPerPage) == 0) {
144                             this.startPageIndex = (cPagination.
145                                 getCurrentPageIndex() - this.nbStepPerPage) + 1;
146                         } else {
147                             this.startPageIndex = cPagination.
148                                                   getCurrentPageIndex() -
149                                                   (cPagination.
150                                 getCurrentPageIndex() % this.nbStepPerPage) + 1;
151                         }
152                         this.stopPageIndex = this.startPageIndex +
153                                              this.nbStepPerPage - 1;
154                     }
155                 }
156                 iterator = cPagination.getPages().iterator();
157                 while (iterator.hasNext() &&
158                        (this.pageNumber < this.startPageIndex - 1)) {
159                     iterator.next();
160                     this.pageNumber += 1;
161                 }
162                 if (iterator.hasNext()) {
163                     Integer JavaDoc I = (Integer JavaDoc) iterator.next();
164                     this.pageNumber = I.intValue();
165                     this.isCurrentPage = (this.pageNumber ==
166                                           cPagination.getCurrentPageIndex());
167                     if (getId() != null) {
168                         if (getPaginationBean() != null) {
169                             pageContext.setAttribute(getId(), getPaginationBean());
170                         } else {
171                             pageContext.removeAttribute(getId());
172                         }
173                     }
174                     return EVAL_BODY_BUFFERED;
175                 } else {
176                     return SKIP_BODY;
177                 }
178             }
179         }
180         return SKIP_BODY;
181     }
182
183     public int doAfterBody ()
184         throws JspTagException JavaDoc {
185         BodyContent JavaDoc body = getBodyContent();
186         try {
187             body.writeOut(getPreviousOut());
188         } catch (IOException JavaDoc e) {
189             throw new JspTagException JavaDoc("CListPaginationTag.doAfterBody : " +
190                                       e.getMessage());
191         }
192
193         // clear up so the next time the body content is empty
194
body.clearBody();
195         if (iterator.hasNext() && (this.pageNumber < this.stopPageIndex)) {
196             Integer JavaDoc I = (Integer JavaDoc) iterator.next();
197             this.pageNumber = I.intValue();
198             this.isCurrentPage = (this.pageNumber ==
199                                   cPagination.getCurrentPageIndex());
200             if (getId() != null) {
201                 pageContext.setAttribute(getId(), getPaginationBean());
202             }
203             return EVAL_BODY_BUFFERED;
204         }
205         return SKIP_BODY;
206     }
207
208     public int doEndTag ()
209         throws JspException JavaDoc {
210         // let's reinitialize the tag variables to allow tag object reuse in
211
// pooling.
212
cPagination = null;
213         pageNumber = 0;
214         isCurrentPage = false;
215
216         nbStepPerPage = -1; // by default, display all page step.
217
skipOnePageOnly = "true"; // by default skip displaying pagination if there is only one page
218

219         startPageIndex = 0;
220         stopPageIndex = 0;
221
222         name = null;
223         return EVAL_PAGE;
224     }
225     
226     public PaginationBean getPaginationBean() {
227
228         return new PaginationBean(this.pageNumber, this.isCurrentPage);
229     }
230
231 }
Popular Tags