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