KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > website > generic > view > Pager


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.website.generic.view;
25
26 import java.io.UnsupportedEncodingException JavaDoc;
27 import java.net.URLEncoder JavaDoc;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30
31 import org.springframework.web.util.UrlPathHelper;
32
33 /**
34  *
35  */

36 public class Pager {
37
38     private int currentPage;
39
40     private int pages;
41
42     private PagerItem firstPage;
43
44     private PagerItem prevPage;
45
46     private PagerItem[] prevPages;
47
48     private PagerItem[] nextPages;
49
50     private PagerItem nextPage;
51
52     private PagerItem lastPage;
53
54     private boolean gapToFirstPage;
55
56     private boolean gapToLastPage;
57
58     private String JavaDoc pageParam;
59
60     private String JavaDoc linkPrefix;
61     
62     private String JavaDoc encodingScheme = "UTF-8";
63     
64     private boolean copyParameters = true;
65
66     private UrlPathHelper urlPathHelper = new UrlPathHelper();
67     
68     public Pager(int page, int pageSize, long itemCount) {
69         currentPage = page;
70         pages = (int) itemCount / pageSize + 1;
71         if (itemCount % pageSize == 0) {
72             pages--;
73         }
74     }
75
76     public void setCopyParameters(boolean copyParameters) {
77         this.copyParameters = copyParameters;
78     }
79
80     public void setEncodingScheme(String JavaDoc encodingScheme) {
81         this.encodingScheme = encodingScheme;
82     }
83
84     public void initialize(HttpServletRequest JavaDoc request, int padding,
85             String JavaDoc pageParam) {
86
87         this.pageParam = pageParam;
88         prepareLinkPrefix(request);
89
90         int start = currentPage - padding;
91         int end = currentPage + padding;
92
93         if (start < 0) {
94             end += -1 * start + 1;
95         }
96         if (end > pages) {
97             start -= (end - pages);
98         }
99         if (start < 1) {
100             start = 1;
101         }
102         if (end > pages) {
103             end = pages;
104         }
105
106         gapToFirstPage = start > 2;
107         gapToLastPage = end < pages - 1;
108
109         int prevCount = Math.max(currentPage - start, 0);
110         int nextCount = Math.max(end - currentPage, 0);
111
112         if (start > 1) {
113             firstPage = new PagerItem(linkPrefix, 1);
114         }
115         
116         prevPages = new PagerItem[prevCount];
117         for (int i = 0; i < prevCount; i++) {
118             prevPages[i] = new PagerItem(linkPrefix, start + i);
119         }
120         if (prevCount > 0) {
121             prevPage = prevPages[prevCount - 1];
122         }
123         
124         nextPages = new PagerItem[nextCount];
125         for (int i = 0; i < nextCount; i++) {
126             nextPages[i] = new PagerItem(linkPrefix, currentPage + i + 1);
127         }
128         if (nextCount > 0) {
129             nextPage = nextPages[0];
130         }
131         
132         if (end < pages) {
133             lastPage = new PagerItem(linkPrefix, pages);
134         }
135     }
136
137     private void prepareLinkPrefix(HttpServletRequest JavaDoc request) {
138         StringBuffer JavaDoc url = new StringBuffer JavaDoc(
139                 urlPathHelper.getOriginatingRequestUri(request));
140         
141         url.append('?');
142         if (copyParameters) {
143             String JavaDoc query = urlPathHelper.getOriginatingQueryString(request);
144             if (query != null) {
145                 int i = query.indexOf(pageParam);
146                 if (i != -1) {
147                     url.append(query.substring(0, i));
148                 }
149                 else {
150                     url.append(query).append('&');
151                 }
152             }
153         }
154         url.append(pageParam);
155         url.append('=');
156         linkPrefix = url.toString();
157     }
158
159     protected String JavaDoc urlEncode(String JavaDoc s) {
160         try {
161             return URLEncoder.encode(s, encodingScheme);
162         }
163         catch (UnsupportedEncodingException JavaDoc e) {
164             throw new RuntimeException JavaDoc(e);
165         }
166     }
167     
168     public int getCurrentPage() {
169         return currentPage;
170     }
171
172     public int getPages() {
173         return pages;
174     }
175
176     public PagerItem getFirstPage() {
177         return firstPage;
178     }
179
180     public boolean isGapToFirstPage() {
181         return gapToFirstPage;
182     }
183
184     public boolean isGapToLastPage() {
185         return gapToLastPage;
186     }
187
188     public PagerItem getLastPage() {
189         return lastPage;
190     }
191
192     public PagerItem[] getNextPages() {
193         return nextPages;
194     }
195
196     public PagerItem[] getPrevPages() {
197         return prevPages;
198     }
199
200     public PagerItem getNextPage() {
201         return nextPage;
202     }
203
204     public PagerItem getPrevPage() {
205         return prevPage;
206     }
207     
208     public class PagerItem {
209
210         private int number;
211
212         private String JavaDoc link;
213         
214         public PagerItem(String JavaDoc linkPrefix, int number) {
215             this.number = number;
216             this.link = linkPrefix + number;
217         }
218         
219         public int getNumber() {
220             return number;
221         }
222         
223         public String JavaDoc getLink() {
224             return link;
225         }
226     }
227 }
Popular Tags