KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > data > containers > JahiaContainerListPagination


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12

13 //
14
// JahiaContainerListPagination
15
// NK 05.05.2002
16
//
17
//
18

19 package org.jahia.data.containers;
20
21
22 import java.util.Properties JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import org.jahia.exceptions.JahiaException;
26 import org.jahia.params.ParamBean;
27
28
29 /**
30  * <p>Handle containers list pagination</p>
31  *
32  * @author Khue Nguyen <a HREF="mailto:khue@jahia.org">khue@jahia.org</a>
33  * @version 1.0
34  */

35
36 public class JahiaContainerListPagination
37 {
38
39     private static final String JavaDoc CLASS_NAME = JahiaContainerListPagination.class.getName();
40
41     /** The total number of containers. **/
42     private int size = 0;
43     /** The number of items ( containers ) to display per page. **/
44     private int windowSize = -1;
45     /** The current windowOffset. **/
46     private int windowOffset = -1;
47     /** The current page index ( the currently displayed page ). **/
48     private int currentPageIndex = 0;
49     /** The vector of pages indexes **/
50     private Vector JavaDoc pages = new Vector JavaDoc();
51     /** The index of the first displayed item. **/
52     private int firstItemIndex = 0;
53     /** The index of the last displayed item. **/
54     private int lastItemIndex = 0;
55     /** Is the status of this instance valid or not **/
56     private boolean isValid = false;
57
58
59     //-------------------------------------------------------------------------
60
/**
61      * Constructor
62      *
63      * @param int size, the total number of items.
64      * @param int windowSize, the number of items ( containers ) to display per page.
65      * @param int windowOffset, the current windowOffset.
66      */

67     public JahiaContainerListPagination( int size,
68                                           int windowSize ,
69                                           int windowOffset )
70     {
71         this.size = size;
72         this.windowSize = windowSize;
73         this.windowOffset = windowOffset;
74         this.paginate();
75     }
76
77     //-------------------------------------------------------------------------
78
/**
79      * Constructor
80      *
81      * windowSize and windowOffset are computed from request parameters :
82      * <pre>
83      * "ctnscroll_" + containerListName
84      * </pre>
85      *
86      * Or if not present, loaded from container list scrolling properties :
87      * <pre>
88      * "windowSize" and "windowOffset"
89      * </pre>
90      *
91      * size is retrieved from theContainerList.getFullSize()
92      *
93      *
94      * @param JahiaConainterList cList, the container list.
95      * @param ParamBean jParams, the param bean.
96      * @param int newWindowSize, the new window size option, set to -1 to deactivate.
97      */

98     public JahiaContainerListPagination( JahiaContainerList theContainerList,
99                                           ParamBean jParams,
100                                           int newWindowSize )
101     throws JahiaException {
102
103         //JahiaConsole.println(CLASS_NAME+".Constructor(ctnlist,jparams,newWindowSize)","Started for ctnlist [" + theContainerList.getID() + "] - " + theContainerList.getDefinition().getName());
104

105         if ( theContainerList == null )
106         {
107             return;
108         }
109
110         // we now check in we are in a scrollable container list, and retrieve
111
// only the values of the containers that must be displayed.
112
int windowSize = -1;
113         int windowOffset = -1;
114
115         JahiaContainerDefinition ctnDef = theContainerList.getDefinition();
116         Properties JavaDoc ctnDefProps = ctnDef.getProperties();
117         int defWindowSize = -1;
118         int defWindowOffset = -1;
119         if (ctnDefProps.size() > 0) {
120             try {
121                 if (ctnDefProps.getProperty("windowSize") != null) {
122                     defWindowSize = Integer.parseInt(ctnDefProps.getProperty("windowSize"));
123                     defWindowOffset = 0;
124                 }
125                 if (ctnDefProps.getProperty("windowOffset") != null) {
126                     defWindowOffset = Integer.parseInt(ctnDefProps.getProperty("windowOffset"));
127                 }
128             } catch (NumberFormatException JavaDoc nfe) {
129             }
130         }
131
132         //JahiaConsole.println(CLASS_NAME+".Constructor","defWindowSize : " + defWindowSize);
133
//JahiaConsole.println(CLASS_NAME+".Constructor","defWindowOffset : " + defWindowOffset);
134
if ( (defWindowSize >= 1) &&
135              (defWindowOffset >= 0) ) {
136             windowSize = defWindowSize;
137             windowOffset = defWindowOffset;
138         }
139
140         //JahiaConsole.println(CLASS_NAME+".Constructor","windowSize : " + windowSize);
141
//JahiaConsole.println(CLASS_NAME+".Constructor","windowOffset : " + windowOffset);
142

143         if (jParams != null) {
144             String JavaDoc containerListName = theContainerList.getDefinition().getName();
145             String JavaDoc scrollStr = jParams.getParameter("ctnscroll_" + containerListName);
146             if (scrollStr != null) {
147                 // we have an window scrolling code for this container list.
148

149                 /*
150                 JahiaConsole.println(CLASS_NAME+".Constructor",
151                                      "Found window parameters for container list " +
152                                      containerListName);
153                 */

154                 int separatorPos = scrollStr.indexOf("_");
155                 if ((separatorPos != -1) &&
156                     (separatorPos + 1 < scrollStr.length())) {
157                     // seperator is present, looks like a valid parameter. We
158
// must still test if we have two integer values.
159
String JavaDoc windowSizeStr = scrollStr.substring(0, separatorPos);
160                     String JavaDoc windowOffsetStr = scrollStr.substring(separatorPos + 1, scrollStr.length());
161
162                     try {
163                         windowSize = Integer.parseInt(windowSizeStr);
164                         windowOffset = Integer.parseInt(windowOffsetStr);
165                         if ((windowSize < -1) || (windowOffset < -1) ) {
166                             // little sanity check to avoid simple hacks.
167
windowSize = -1;
168                             windowOffset = -1;
169                         }
170                     } catch (NumberFormatException JavaDoc nfe) {
171                         windowSize = -1;
172                         windowOffset = -1;
173                     }
174                 }
175             }
176
177             // Check for customized window size
178
String JavaDoc custWindowSize = jParams.getParameter(containerListName + "_windowsize");
179             try {
180                 if (custWindowSize != null) {
181                   int val = Integer.parseInt(custWindowSize);
182                   if ( val > 0 ){
183                       windowSize = val;
184                   }
185                 }
186             } catch ( Throwable JavaDoc t ){
187             }
188         }
189
190         // set the new window size if needed
191
if ( newWindowSize > 0 ){
192             windowSize = newWindowSize;
193         }
194
195         if ( windowOffset > (theContainerList.getFullSize()-1) )
196         {
197             windowOffset = 0; // Perhaps the number of container has changed ( i.e : some ctn has been deleted ),
198
// so display the first page
199
}
200
201         if ( windowSize == 0 || ( windowOffset % windowSize ) != 0)
202         {
203             windowOffset = 0;
204         }
205
206         this.size = theContainerList.getFullSize();
207         this.windowSize = windowSize;
208         this.windowOffset = windowOffset;
209
210         //JahiaConsole.println(CLASS_NAME+".Constructor","size : " + this.size +
211
// " windowSize : " + windowSize +
212
// " windowOffset : " + windowOffset);
213
this.paginate();
214     }
215
216     //-------------------------------------------------------------------------
217
/**
218      * Return the total number of items ( containers ).
219      *
220      * @return int the total number of itms.
221      */

222     public int getSize()
223     {
224         return this.size;
225     }
226
227     //-------------------------------------------------------------------------
228
/**
229      * Return the windowSize , the number of items per page.
230      *
231      * @return int windowSize, the number of items per page
232      */

233     public int getWindowSize()
234     {
235         return this.windowSize;
236     }
237
238     //-------------------------------------------------------------------------
239
/**
240      * Return the windowOffset , the current window Offset.
241      *
242      * @return int windowOffset, the current window offset.
243      */

244     public int getWindowOffset()
245     {
246         return this.windowOffset;
247     }
248
249     //-------------------------------------------------------------------------
250
/**
251      * Return the valid status.
252      *
253      * @return boolean true if valid, false on error.
254      */

255     public boolean isValid()
256     {
257         return this.isValid;
258     }
259
260     //-------------------------------------------------------------------------
261
/**
262      * Return the current page Index, ( the index of the displayed page ).
263      * Starting from 1
264      *
265      * @return int current pageIndex.
266      */

267     public int getCurrentPageIndex()
268     {
269         return this.currentPageIndex;
270     }
271
272     //-------------------------------------------------------------------------
273
/**
274      * Return the index of the first item listed in the current displayed page.
275      * Starting from 0
276      *
277      * @return int index of the first item listed in the current displayed page.
278      */

279     public int getFirstItemIndex()
280     {
281         return this.firstItemIndex;
282     }
283
284     //-------------------------------------------------------------------------
285
/**
286      * Return the index of the last item listed in the current displayed page.
287      * Starting from 0
288      *
289      * @return int index of the last item listed in the current displayed page.
290      */

291     public int getLastItemIndex()
292     {
293         return this.lastItemIndex;
294     }
295
296     //-------------------------------------------------------------------------
297
/**
298      * Return the vector of paginated pages as index numbers starting from 1 to n.
299      *
300      * 1,2,3,4..
301      *
302      * @return Vector the vector of paginated pages as index numbers ( Integer ).
303      */

304     public Vector JavaDoc getPages()
305     {
306         return this.pages;
307     }
308
309     //-------------------------------------------------------------------------
310
/**
311      * Return the number of pages.
312      *
313      * @return int the number of pages.
314      */

315     public int getNbPages()
316     {
317         if ( this.pages == null ){
318             return 0;
319         }
320         return this.pages.size();
321     }
322
323     //-------------------------------------------------------------------------
324
/**
325      * Return the windowOffset for a given page index ( page index start from 1 to n).
326      * The computed value is equals to : (pageIndex-1) * windowSize
327      *
328      * @param int the page index must be > 0.
329      * @return int the windowOffset for a given page index, -1 on error.
330      */

331     public int getWindowOffset(int pageIndex)
332     {
333         if ( !isValid() || (pageIndex<=0) ){
334             return -1;
335         }
336         try {
337             int val = (pageIndex-1) * this.windowSize ;
338             if ( val <= this.size ){
339                 return val;
340             }
341         } catch ( Throwable JavaDoc t ) {
342             t.printStackTrace();
343         }
344         return -1;
345     }
346
347     //-------------------------------------------------------------------------
348
/**
349      * Return the scrolling value ( i.e : '5_10' ) for a given page index ( page index start from 1 to n).
350      *
351      * <pre>
352      * Scrolling value format is : windowSize + "_" + windowOffset
353      * </pre>
354      *
355      * @param int the page index must be > 0.
356      * @return String the scrolling value for the given page index, null on error.
357      */

358     public String JavaDoc getScrollingValue(int pageIndex)
359     {
360         int windowOffset = getWindowOffset(pageIndex);
361         if ( pageIndex <0 ){
362             return null;
363         }
364         return ( String.valueOf(getWindowSize()) + "_" + String.valueOf(windowOffset) );
365     }
366
367     //-------------------------------------------------------------------------
368
/**
369      * Perform pagination tasks.
370      *
371      */

372     private void paginate()
373     {
374         //JahiaConsole.println(CLASS_NAME+".paginate","Started");
375

376         try {
377
378             if ( ( this.windowSize <= 0 ) || ( this.windowOffset < 0 ) || ( ( this.windowOffset % this.windowSize ) != 0) )
379             {
380                 return; // not valid value.
381
}
382
383             // Compute current page index.
384
this.currentPageIndex = ( this.windowOffset / this.windowSize ) + 1;
385
386             //JahiaConsole.println(CLASS_NAME+".paginate","Current page index : " + this.currentPageIndex);
387

388             // Compute the first and last item indexes
389
if ( this.currentPageIndex == 1 )
390             {
391                 this.firstItemIndex = 0;
392             } else {
393                 this.firstItemIndex = ( this.currentPageIndex * this.windowSize ) - this.windowSize;
394             }
395             this.lastItemIndex = ( this.currentPageIndex * this.windowSize ) -1;
396             if ( this.lastItemIndex > this.size-1 )
397             {
398                 this.lastItemIndex = this.size-1;
399             }
400
401             //JahiaConsole.println(CLASS_NAME+".paginate","firtsItemIndex : " + this.firstItemIndex);
402
//JahiaConsole.println(CLASS_NAME+".paginate","lastItemIndex : " + this.lastItemIndex);
403

404             // Compute the vector of page indexes
405
if ( this.size>0 )
406             {
407                 if ( this.size <= this.windowSize )
408                 {
409                     this.pages.add( new Integer JavaDoc(1) );
410                 } else {
411                     int nbPages = ( this.size / this.windowSize );
412                     for ( int i=0 ; i<nbPages; i++ )
413                     {
414                         this.pages.add( new Integer JavaDoc(i+1) );
415                     }
416                     if ( (this.size % this.windowSize) != 0 )
417                     {
418                         int newPageIndex = this.pages.size() + 1;
419                         this.pages.add( new Integer JavaDoc(newPageIndex) );
420                     }
421                 }
422             }
423
424             // everything seems ok
425
this.isValid = true;
426
427         } catch ( Throwable JavaDoc t ) {
428             t.printStackTrace();
429         }
430     }
431
432 }
433
Popular Tags