KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > displaytag > pagination > Pagination


1 /**
2  * Licensed under the Artistic License; you may not use this file
3  * except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://displaytag.sourceforge.net/license.html
7  *
8  * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11  */

12 package org.displaytag.pagination;
13
14 import java.text.MessageFormat JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.apache.commons.lang.builder.ToStringBuilder;
20 import org.apache.commons.lang.builder.ToStringStyle;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.displaytag.util.Href;
24
25
26 /**
27  * Helper class for generation of paging banners.
28  * @author Fabrizio Giustina
29  * @version $Revision: 922 $ ($Author: fgiust $)
30  */

31 public class Pagination
32 {
33
34     /**
35      * logger.
36      */

37     private static Log log = LogFactory.getLog(Pagination.class);
38
39     /**
40      * Base href for urls.
41      */

42     private Href href;
43
44     /**
45      * page parameter name.
46      */

47     private String JavaDoc pageParam;
48
49     /**
50      * first page.
51      */

52     private Integer JavaDoc firstPage;
53
54     /**
55      * last page.
56      */

57     private Integer JavaDoc lastPage;
58
59     /**
60      * previous page.
61      */

62     private Integer JavaDoc previousPage;
63
64     /**
65      * next page.
66      */

67     private Integer JavaDoc nextPage;
68
69     /**
70      * current page.
71      */

72     private Integer JavaDoc currentPage;
73
74     /**
75      * List containg NumberedPage objects.
76      * @see org.displaytag.pagination.NumberedPage
77      */

78     private List JavaDoc pages = new ArrayList JavaDoc();
79
80     /**
81      * Constructor for Pagination.
82      * @param baseHref Href used for links
83      * @param pageParameter name for the page parameter
84      */

85     public Pagination(Href baseHref, String JavaDoc pageParameter)
86     {
87         this.href = baseHref;
88         this.pageParam = pageParameter;
89     }
90
91     /**
92      * Adds a page.
93      * @param number int page number
94      * @param isSelected is the page selected?
95      */

96     public void addPage(int number, boolean isSelected)
97     {
98         if (log.isDebugEnabled())
99         {
100             log.debug("adding page " + number);
101         }
102         this.pages.add(new NumberedPage(number, isSelected));
103     }
104
105     /**
106      * first page selected?
107      * @return boolean
108      */

109     public boolean isFirst()
110     {
111         return this.firstPage == null;
112     }
113
114     /**
115      * last page selected?
116      * @return boolean
117      */

118     public boolean isLast()
119     {
120         return this.lastPage == null;
121     }
122
123     /**
124      * only one page?
125      * @return boolean
126      */

127     public boolean isOnePage()
128     {
129         return (this.pages == null) || this.pages.size() <= 1;
130     }
131
132     /**
133      * Gets the number of the first page.
134      * @return Integer number of the first page
135      */

136     public Integer JavaDoc getFirst()
137     {
138         return this.firstPage;
139     }
140
141     /**
142      * Sets the number of the first page.
143      * @param first Integer number of the first page
144      */

145     public void setFirst(Integer JavaDoc first)
146     {
147         this.firstPage = first;
148     }
149
150     /**
151      * Gets the number of the last page.
152      * @return Integer number of the last page
153      */

154     public Integer JavaDoc getLast()
155     {
156         return this.lastPage;
157     }
158
159     /**
160      * Sets the number of the last page.
161      * @param last Integer number of the last page
162      */

163     public void setLast(Integer JavaDoc last)
164     {
165         this.lastPage = last;
166     }
167
168     /**
169      * Gets the number of the previous page.
170      * @return Integer number of the previous page
171      */

172     public Integer JavaDoc getPrevious()
173     {
174         return this.previousPage;
175     }
176
177     /**
178      * Sets the number of the previous page.
179      * @param previous Integer number of the previous page
180      */

181     public void setPrevious(Integer JavaDoc previous)
182     {
183         this.previousPage = previous;
184     }
185
186     /**
187      * Gets the number of the next page.
188      * @return Integer number of the next page
189      */

190     public Integer JavaDoc getNext()
191     {
192         return this.nextPage;
193     }
194
195     /**
196      * Sets the number of the next page.
197      * @param next Integer number of the next page
198      */

199     public void setNext(Integer JavaDoc next)
200     {
201         this.nextPage = next;
202     }
203
204     /**
205      * Sets the number of the current page.
206      * @param current number of the current page
207      */

208     public void setCurrent(Integer JavaDoc current)
209     {
210         this.currentPage = current;
211     }
212
213     /**
214      * Returns the appropriate banner for the pagination.
215      * @param numberedPageFormat String to be used for a not selected page
216      * @param numberedPageSelectedFormat String to be used for a selected page
217      * @param numberedPageSeparator separator beetween pages
218      * @param fullBanner String basic banner
219      * @return String formatted banner whith pages
220      */

221     public String JavaDoc getFormattedBanner(String JavaDoc numberedPageFormat, String JavaDoc numberedPageSelectedFormat,
222         String JavaDoc numberedPageSeparator, String JavaDoc fullBanner)
223     {
224         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(100);
225
226         // numbered page list
227
Iterator JavaDoc pageIterator = this.pages.iterator();
228
229         while (pageIterator.hasNext())
230         {
231
232             // get NumberedPage from iterator
233
NumberedPage page = (NumberedPage) pageIterator.next();
234
235             Integer JavaDoc pageNumber = new Integer JavaDoc(page.getNumber());
236
237             String JavaDoc urlString = ((Href) this.href.clone()).addParameter(this.pageParam, pageNumber).toString();
238
239             // needed for MessageFormat : page number/url
240
Object JavaDoc[] pageObjects = {pageNumber, urlString};
241
242             // selected page need a different formatter
243
if (page.getSelected())
244             {
245                 buffer.append(MessageFormat.format(numberedPageSelectedFormat, pageObjects));
246             }
247             else
248             {
249                 buffer.append(MessageFormat.format(numberedPageFormat, pageObjects));
250             }
251
252             // next? add page separator
253
if (pageIterator.hasNext())
254             {
255                 buffer.append(numberedPageSeparator);
256             }
257         }
258
259         // String for numbered pages
260
String JavaDoc numberedPageString = buffer.toString();
261         // Object array
262
// {0} full String for numbered pages
263
// {1} first page url
264
// {2} previous page url
265
// {3} next page url
266
// {4} last page url
267
// {5} current page
268
// {6} total pages
269
Object JavaDoc[] pageObjects = {
270             numberedPageString,
271             ((Href) this.href.clone()).addParameter(this.pageParam, getFirst()),
272             ((Href) this.href.clone()).addParameter(this.pageParam, getPrevious()),
273             ((Href) this.href.clone()).addParameter(this.pageParam, getNext()),
274             ((Href) this.href.clone()).addParameter(this.pageParam, getLast()),
275             this.currentPage,
276             this.isLast() ? this.currentPage : this.lastPage}; // this.lastPage is null if the last page is displayed
277

278         // return the full banner
279
return MessageFormat.format(fullBanner, pageObjects);
280     }
281
282     /**
283      * @see java.lang.Object#toString()
284      */

285     public String JavaDoc toString()
286     {
287         return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) //
288
.append("firstPage", this.firstPage) //$NON-NLS-1$
289
.append("lastPage", this.lastPage) //$NON-NLS-1$
290
.append("currentPage", this.currentPage) //$NON-NLS-1$
291
.append("nextPage", this.nextPage) //$NON-NLS-1$
292
.append("previousPage", this.previousPage) //$NON-NLS-1$
293
.append("pages", this.pages) //$NON-NLS-1$
294
.append("href", this.href) //$NON-NLS-1$
295
.append("pageParam", this.pageParam) //$NON-NLS-1$
296
.toString();
297     }
298 }
Popular Tags