KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jahia.deprecated.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.containers.JahiaContainerList;
12 import org.jahia.data.containers.JahiaContainerListPagination;
13 import org.jahia.deprecated.taglibs.container.ContainerListTag;
14
15 /**
16  * Lookup for the ContainerListPagination instance of the enclosing ContainerListTag and iterate through the list of paginated pages.
17  *
18  * <pre>
19  * <<next -->1 2 3<-- next>>
20  * </pre>
21  *
22  * @author NK
23  * @version 1.0
24  */

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

36     private int startPageIndex = 0;
37     private int stopPageIndex = 0;
38
39     /**
40      * Return the number of step per displayed page.
41      */

42     public int getNbStepPerPage(){
43         return this.nbStepPerPage;
44     }
45
46     /**
47      * Set the number of step per displayed page.
48      */

49     public void setNbStepPerPage( int nbStepPerPage ){
50         this.nbStepPerPage = nbStepPerPage;
51     }
52
53     /**
54      * Return the value of the status skipOnePageOnly.
55      */

56     public String JavaDoc skipOnePageOnly(){
57         return this.skipOnePageOnly;
58     }
59
60     /**
61      * Set the value of the status skipOnePageOnly ( true/false ).
62      */

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

72     public int getPageNumber(){
73         return this.pageNumber;
74     }
75
76     /**
77      * Return true if the pageIndex is equals to the current displayed page.
78      */

79     public boolean isCurrentPage(){
80         return isCurrentPage;
81     }
82
83     /**
84      * Return the stop page index.
85      */

86     public int getStopPageIndex(){
87         return this.stopPageIndex;
88     }
89
90     /**
91      * Return the start page index.
92      */

93     public int getStartPageIndex(){
94         return this.startPageIndex;
95     }
96
97     public int doStartTag() {
98         //JahiaConsole.println("CListPaginationTag.doStartTag", "Started");
99

100         // gets the enclosing tag ContainerListTag
101
ContainerListTag containerListTag = (ContainerListTag) findAncestorWithClass(this, ContainerListTag.class);
102         if (containerListTag != null && !containerListTag.isDeclarationPass()) {
103             JahiaContainerList cList = containerListTag.getContainerList();
104             if ( cList != null )
105             {
106                 cPagination = cList.getCtnListPagination();
107                 if ( cPagination != null && cPagination.isValid() )
108                 {
109                     if ( cPagination.getNbPages()==1 && this.skipOnePageOnly.equals("true") ){
110                         return SKIP_BODY;
111                     }
112                     if ( this.nbStepPerPage<=0 ){
113                         this.startPageIndex = 1;
114                         this.stopPageIndex = cPagination.getNbPages();
115                         this.pageNumber =1;
116                     } else {
117                         if ( cPagination.getCurrentPageIndex()<=this.nbStepPerPage ){
118                             this.startPageIndex = 1;
119                             this.stopPageIndex = this.nbStepPerPage;
120                         } else {
121                             if ( (cPagination.getCurrentPageIndex()%this.nbStepPerPage)==0 ){
122                                 this.startPageIndex = ( cPagination.getCurrentPageIndex() - this.nbStepPerPage ) + 1;
123                             } else {
124                                 this.startPageIndex = cPagination.getCurrentPageIndex() - ( cPagination.getCurrentPageIndex() % this.nbStepPerPage ) + 1;
125                             }
126                             this.stopPageIndex = this.startPageIndex + this.nbStepPerPage-1;
127                         }
128                     }
129                     iterator = cPagination.getPages().iterator();
130                     while (iterator.hasNext() && (this.pageNumber<this.startPageIndex-1) ){
131                         iterator.next();
132                         this.pageNumber +=1;
133                     }
134                     if ( iterator.hasNext() ){
135                         Integer JavaDoc I = (Integer JavaDoc)iterator.next();
136                         this.pageNumber = I.intValue();
137                         this.isCurrentPage = ( this.pageNumber == cPagination.getCurrentPageIndex() );
138                         return EVAL_BODY_BUFFERED;
139                     } else {
140                         return SKIP_BODY;
141                     }
142                 }
143             }
144         }
145         return SKIP_BODY;
146     }
147
148
149     public int doAfterBody() throws JspTagException JavaDoc {
150         BodyContent JavaDoc body = getBodyContent();
151         try {
152             body.writeOut(getPreviousOut());
153         } catch (IOException JavaDoc e) {
154             throw new JspTagException JavaDoc("CListPaginationTag.doAfterBody : " +
155                 e.getMessage());
156         }
157
158         // clear up so the next time the body content is empty
159
body.clearBody();
160         if (iterator.hasNext() && (this.pageNumber<this.stopPageIndex) )
161         {
162             Integer JavaDoc I = (Integer JavaDoc)iterator.next();
163             this.pageNumber = I.intValue();
164             this.isCurrentPage = ( this.pageNumber == cPagination.getCurrentPageIndex() );
165             return EVAL_BODY_BUFFERED;
166        }
167        return SKIP_BODY;
168     }
169
170     public int doEndTag() throws JspException JavaDoc {
171         // let's reinitialize the tag variables to allow tag object reuse in
172
// pooling.
173
cPagination = null;
174         pageNumber = 0;
175         isCurrentPage = false;
176         Iterator JavaDoc iterator = null;
177
178         nbStepPerPage = -1; // by default, display all page step.
179
skipOnePageOnly = "true"; // by default skip displaying pagination if there is only one page
180

181         startPageIndex = 0;
182         stopPageIndex = 0;
183         return EVAL_PAGE;
184     }
185
186
187 }
Popular Tags