1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.util.html;
35
36 import fr.paris.lutece.util.url.UrlItem;
37
38 import java.util.ArrayList;
39 import java.util.List;
40
41 import javax.servlet.http.HttpServletRequest;
42
43
44
45
46
47
48 public abstract class AbstractPaginator<E> implements IPaginator<E>
49 {
50
51 public static final String PARAMETER_PAGE_INDEX = "page_index";
52
53
54 public static final String PARAMETER_ITEMS_PER_PAGE = "items_per_page";
55 public static final String LABEL_FIRST = "|<";
56 public static final String LABEL_PREVIOUS = "<";
57 public static final String LABEL_NEXT = ">";
58 public static final String LABEL_LAST = ">|";
59 private static final long serialVersionUID = -2795417742672443863L;
60
61
62
63 protected String _strPageIndexParameterName;
64
65
66 protected int _nItemPerPage;
67
68
69 protected String _strBaseUrl;
70
71
72 protected List<E> _list;
73
74
75 protected int _nPageCurrent;
76
77
78 protected String _strItemsPerPageParameterName;
79
80
81 protected int _nItemsCount;
82
83
84
85
86 @Override
87 public int getPageCurrent( )
88 {
89 return _nPageCurrent;
90 }
91
92
93
94
95 @Override
96 public String getFirstPageLink( )
97 {
98 UrlItem url = new UrlItem( _strBaseUrl );
99 url.addParameter( _strPageIndexParameterName, "" + 1 );
100
101 return url.getUrl( );
102 }
103
104
105
106
107 @Override
108 public String getPreviousPageLink( )
109 {
110 int nPreviousIndex = _nPageCurrent - 1;
111 UrlItem url = new UrlItem( _strBaseUrl );
112 url.addParameter( _strPageIndexParameterName, "" + nPreviousIndex );
113
114 return url.getUrl( );
115 }
116
117
118
119
120 @Override
121 public String getLastPageLink( )
122 {
123 UrlItem url = new UrlItem( _strBaseUrl );
124 url.addParameter( _strPageIndexParameterName, "" + getPagesCount( ) );
125
126 return url.getUrl( );
127 }
128
129
130
131
132
133 @Override
134 public int getItemsPerPage( )
135 {
136 return _nItemPerPage;
137 }
138
139
140
141
142 @Override
143 public String getNextPageLink( )
144 {
145 int nNextIndex = _nPageCurrent + 1;
146 UrlItem url = new UrlItem( _strBaseUrl );
147 url.addParameter( _strPageIndexParameterName, "" + nNextIndex );
148
149 return url.getUrl( );
150 }
151
152
153
154
155 @Override
156 public List<PaginatorPage> getPagesLinks( )
157 {
158 List<PaginatorPage> list = new ArrayList<PaginatorPage>( );
159
160
161 int nNbLinkPages = getPagesCount( );
162
163
164 int nNbLinkPagesToDisplay = 10;
165
166
167 int nOffsetPrev = nNbLinkPagesToDisplay / 2;
168
169
170 int nOffsetNext = nNbLinkPagesToDisplay / 2;
171
172
173 if ( _nPageCurrent <= ( nNbLinkPagesToDisplay - nOffsetPrev ) )
174 {
175
176 nOffsetPrev = _nPageCurrent - 1;
177 nOffsetNext = nNbLinkPagesToDisplay - nOffsetPrev;
178 }
179 else if ( ( _nPageCurrent + nOffsetNext ) > nNbLinkPages )
180 {
181
182 nOffsetNext = nNbLinkPages - _nPageCurrent;
183 nOffsetPrev = nNbLinkPagesToDisplay - nOffsetNext;
184 }
185
186
187 int nMax = nNbLinkPages;
188
189
190 int nMin = 1;
191
192 int nTmpMin = _nPageCurrent - nOffsetPrev;
193 int nTmpMax = _nPageCurrent + nOffsetNext;
194
195 if ( nTmpMax < nMax )
196 {
197 nMax = nTmpMax;
198 }
199
200 if ( nTmpMin > 0 )
201 {
202 nMin = nTmpMin;
203 }
204
205 for ( int i = nMin; i <= nMax; i++ )
206 {
207 PaginatorPage page = new PaginatorPage( );
208 String strIndex = "" + i;
209 UrlItem url = new UrlItem( _strBaseUrl );
210 url.addParameter( _strPageIndexParameterName, strIndex );
211 page.setUrl( url.getUrl( ) );
212 page.setName( strIndex );
213 page.setIndex( i );
214 list.add( page );
215 }
216
217 return list;
218 }
219
220
221
222
223 @Override
224 public String getLabelFirst( )
225 {
226 return LABEL_FIRST;
227 }
228
229
230
231
232 @Override
233 public String getLabelPrevious( )
234 {
235 return LABEL_PREVIOUS;
236 }
237
238
239
240
241 @Override
242 public String getLabelNext( )
243 {
244 return LABEL_NEXT;
245 }
246
247
248
249
250 @Override
251 public String getLabelLast( )
252 {
253 return LABEL_LAST;
254 }
255
256
257
258
259 @Override
260 public String getLabelItemCount( )
261 {
262 return "";
263 }
264
265
266
267
268 @Override
269 public String getLabelItemCountPerPage( )
270 {
271 return "";
272 }
273
274
275
276
277 @Override
278 public String getItemsPerPageParameterName( )
279 {
280 return _strItemsPerPageParameterName;
281 }
282
283
284
285
286 @Override
287 public void setItemsPerPageParameterName( String strItemsPerPageParameterName )
288 {
289 _strItemsPerPageParameterName = strItemsPerPageParameterName;
290 }
291
292
293
294
295 @Override
296 public int getPagesCount( )
297 {
298 return ( ( _nItemsCount - 1 ) / _nItemPerPage ) + 1;
299 }
300
301
302
303
304 @Override
305 public int getRangeMin( )
306 {
307 return ( !_list.isEmpty( ) ) ? ( ( _nItemPerPage * ( _nPageCurrent - 1 ) ) + 1 ) : 0;
308 }
309
310
311
312
313 @Override
314 public int getRangeMax( )
315 {
316 return ( _nItemsCount < ( ( _nItemPerPage * _nPageCurrent ) - 1 ) ) ? _nItemsCount : ( _nItemPerPage * _nPageCurrent );
317 }
318
319
320
321
322 @Override
323 public int getItemsCount( )
324 {
325 return _nItemsCount;
326 }
327
328
329
330
331
332
333
334
335
336 public static int getItemsPerPage( HttpServletRequest request, String strParameter, int nCurrent, int nDefault )
337 {
338 int nItemsPerPage;
339 String strItemsPerPage = request.getParameter( strParameter );
340
341 if ( strItemsPerPage != null )
342 {
343 nItemsPerPage = Integer.parseInt( strItemsPerPage );
344 }
345 else
346 {
347 if ( nCurrent != 0 )
348 {
349 nItemsPerPage = nCurrent;
350 }
351 else
352 {
353 nItemsPerPage = nDefault;
354 }
355 }
356
357 return nItemsPerPage;
358 }
359
360
361
362
363
364
365
366
367 public static String getPageIndex( HttpServletRequest request, String strParameter, String strCurrentPageIndex )
368 {
369 String strPageIndex = request.getParameter( strParameter );
370 strPageIndex = ( strPageIndex != null ) ? strPageIndex : strCurrentPageIndex;
371
372 return strPageIndex;
373 }
374 }