KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
16
17 import org.apache.commons.lang.builder.ToStringBuilder;
18 import org.apache.commons.lang.builder.ToStringStyle;
19 import org.displaytag.properties.TableProperties;
20 import org.displaytag.util.Href;
21
22
23 /**
24  * An implementation of SmartListHelper used for externally sorted and paginated lists. It duplicates nearly all of its
25  * superclass, so these two classes should be refactored
26  * @author JBN
27  */

28 public class PaginatedListSmartListHelper extends SmartListHelper
29 {
30
31     private PaginatedList paginatedList;
32
33     private TableProperties properties;
34
35     private int pageCount;
36
37     public PaginatedListSmartListHelper(PaginatedList paginatedList, TableProperties tableProperties)
38     {
39         super();
40         this.paginatedList = paginatedList;
41         this.properties = tableProperties;
42         this.pageCount = computePageCount();
43     }
44
45     private int computePageCount()
46     {
47         int pageCount = paginatedList.getFullListSize() / Math.max(1, paginatedList.getObjectsPerPage());
48         if ((paginatedList.getFullListSize() % paginatedList.getObjectsPerPage()) > 0)
49         {
50             pageCount++;
51         }
52         return pageCount;
53     }
54
55     public int getFirstIndexForCurrentPage()
56     {
57         return getFirstIndexForPage(paginatedList.getPageNumber());
58     }
59
60     protected int getFirstIndexForPage(int pageNumber)
61     {
62         if (pageNumber > pageCount)
63         {
64             pageNumber = pageCount;
65         }
66
67         return ((pageNumber - 1) * paginatedList.getObjectsPerPage());
68     }
69
70     protected int getLastIndexForCurrentPage()
71     {
72         return getLastIndexForPage(paginatedList.getPageNumber());
73     }
74
75     protected int getLastIndexForPage(int pageNumber)
76     {
77         if (pageNumber > pageCount)
78         {
79             pageNumber = pageCount;
80         }
81
82         int result = getFirstIndexForPage(pageNumber) + paginatedList.getObjectsPerPage() - 1;
83         if (result >= paginatedList.getFullListSize())
84         {
85             result = paginatedList.getFullListSize() - 1;
86         }
87         return result;
88     }
89
90     public List JavaDoc getListForCurrentPage()
91     {
92         return paginatedList.getList();
93     }
94
95     protected List JavaDoc getListForPage(int pageNumber)
96     {
97         if ((pageNumber) == paginatedList.getPageNumber())
98         {
99             return getListForCurrentPage();
100         }
101         else
102         {
103             return null;
104         }
105     }
106
107     public String JavaDoc getPageNavigationBar(Href baseHref, String JavaDoc pageParameter)
108     {
109
110         int groupSize = this.properties.getPagingGroupSize();
111         int startPage;
112         int endPage;
113
114         Pagination pagination = new Pagination(baseHref, pageParameter);
115         pagination.setCurrent(new Integer JavaDoc(paginatedList.getPageNumber()));
116
117         // if no items are found still add pagination?
118
if (pageCount == 0)
119         {
120             pagination.addPage(1, true);
121         }
122
123         // center the selected page, but only if there are {groupSize} pages
124
// available after it, and check that the
125
// result is not < 1
126
startPage = Math.max(Math.min(paginatedList.getPageNumber() - groupSize / 2, pageCount - (groupSize - 1)), 1);
127         endPage = Math.min(startPage + groupSize - 1, pageCount);
128
129         if (paginatedList.getPageNumber() != 1)
130         {
131             pagination.setFirst(new Integer JavaDoc(1));
132             pagination.setPrevious(new Integer JavaDoc(paginatedList.getPageNumber() - 1));
133         }
134
135         for (int j = startPage; j <= endPage; j++)
136         {
137             pagination.addPage(j, (j == paginatedList.getPageNumber()));
138         }
139
140         if (paginatedList.getPageNumber() != pageCount)
141         {
142             pagination.setNext(new Integer JavaDoc(paginatedList.getPageNumber() + 1));
143             pagination.setLast(new Integer JavaDoc(pageCount));
144         }
145
146         // format for previous/next banner
147
String JavaDoc bannerFormat;
148
149         if (pagination.isOnePage())
150         {
151             bannerFormat = this.properties.getPagingBannerOnePage();
152         }
153         else if (pagination.isFirst())
154         {
155             bannerFormat = this.properties.getPagingBannerFirst();
156         }
157         else if (pagination.isLast())
158         {
159             bannerFormat = this.properties.getPagingBannerLast();
160         }
161         else
162         {
163             bannerFormat = this.properties.getPagingBannerFull();
164         }
165
166         return pagination.getFormattedBanner(this.properties.getPagingPageLink(), this.properties
167             .getPagingPageSelected(), this.properties.getPagingPageSeparator(), bannerFormat);
168     }
169
170     public String JavaDoc getSearchResultsSummary()
171     {
172
173         Object JavaDoc[] objs;
174         String JavaDoc message;
175
176         if (this.paginatedList.getFullListSize() == 0)
177         {
178             objs = new Object JavaDoc[]{this.properties.getPagingItemsName()};
179             message = this.properties.getPagingFoundNoItems();
180         }
181         else if (this.paginatedList.getFullListSize() == 1)
182         {
183             objs = new Object JavaDoc[]{this.properties.getPagingItemName()};
184             message = this.properties.getPagingFoundOneItem();
185         }
186         else if (pageCount == 1)
187         {
188             objs = new Object JavaDoc[]{
189                 new Integer JavaDoc(this.paginatedList.getFullListSize()),
190                 this.properties.getPagingItemsName(),
191                 this.properties.getPagingItemsName()};
192             message = this.properties.getPagingFoundAllItems();
193         }
194         else
195         {
196             objs = new Object JavaDoc[]{
197                 new Integer JavaDoc(this.paginatedList.getFullListSize()),
198                 this.properties.getPagingItemsName(),
199                 new Integer JavaDoc(getFirstIndexForCurrentPage() + 1),
200                 new Integer JavaDoc(getLastIndexForCurrentPage() + 1),
201                 new Integer JavaDoc(this.paginatedList.getPageNumber()),
202                 new Integer JavaDoc(pageCount)};
203             message = this.properties.getPagingFoundSomeItems();
204         }
205
206         return MessageFormat.format(message, objs);
207     }
208
209     /**
210      * @see java.lang.Object#toString()
211      */

212     public String JavaDoc toString()
213     {
214         return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) //
215
.append("paginatedList", this.paginatedList) //$NON-NLS-1$
216
.append("properties", this.properties) //$NON-NLS-1$
217
.toString();
218     }
219 }
Popular Tags