1 13 19 package org.jahia.data.containers; 20 21 22 import java.util.Properties ; 23 import java.util.Vector ; 24 25 import org.jahia.exceptions.JahiaException; 26 import org.jahia.params.ParamBean; 27 28 29 35 36 public class JahiaContainerListPagination 37 { 38 39 private static final String CLASS_NAME = JahiaContainerListPagination.class.getName(); 40 41 42 private int size = 0; 43 44 private int windowSize = -1; 45 46 private int windowOffset = -1; 47 48 private int currentPageIndex = 0; 49 50 private Vector pages = new Vector (); 51 52 private int firstItemIndex = 0; 53 54 private int lastItemIndex = 0; 55 56 private boolean isValid = false; 57 58 59 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 98 public JahiaContainerListPagination( JahiaContainerList theContainerList, 99 ParamBean jParams, 100 int newWindowSize ) 101 throws JahiaException { 102 103 105 if ( theContainerList == null ) 106 { 107 return; 108 } 109 110 int windowSize = -1; 113 int windowOffset = -1; 114 115 JahiaContainerDefinition ctnDef = theContainerList.getDefinition(); 116 Properties 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 nfe) { 129 } 130 } 131 132 if ( (defWindowSize >= 1) && 135 (defWindowOffset >= 0) ) { 136 windowSize = defWindowSize; 137 windowOffset = defWindowOffset; 138 } 139 140 143 if (jParams != null) { 144 String containerListName = theContainerList.getDefinition().getName(); 145 String scrollStr = jParams.getParameter("ctnscroll_" + containerListName); 146 if (scrollStr != null) { 147 149 154 int separatorPos = scrollStr.indexOf("_"); 155 if ((separatorPos != -1) && 156 (separatorPos + 1 < scrollStr.length())) { 157 String windowSizeStr = scrollStr.substring(0, separatorPos); 160 String 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 windowSize = -1; 168 windowOffset = -1; 169 } 170 } catch (NumberFormatException nfe) { 171 windowSize = -1; 172 windowOffset = -1; 173 } 174 } 175 } 176 177 String 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 t ){ 187 } 188 } 189 190 if ( newWindowSize > 0 ){ 192 windowSize = newWindowSize; 193 } 194 195 if ( windowOffset > (theContainerList.getFullSize()-1) ) 196 { 197 windowOffset = 0; } 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 this.paginate(); 214 } 215 216 222 public int getSize() 223 { 224 return this.size; 225 } 226 227 233 public int getWindowSize() 234 { 235 return this.windowSize; 236 } 237 238 244 public int getWindowOffset() 245 { 246 return this.windowOffset; 247 } 248 249 255 public boolean isValid() 256 { 257 return this.isValid; 258 } 259 260 267 public int getCurrentPageIndex() 268 { 269 return this.currentPageIndex; 270 } 271 272 279 public int getFirstItemIndex() 280 { 281 return this.firstItemIndex; 282 } 283 284 291 public int getLastItemIndex() 292 { 293 return this.lastItemIndex; 294 } 295 296 304 public Vector getPages() 305 { 306 return this.pages; 307 } 308 309 315 public int getNbPages() 316 { 317 if ( this.pages == null ){ 318 return 0; 319 } 320 return this.pages.size(); 321 } 322 323 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 t ) { 342 t.printStackTrace(); 343 } 344 return -1; 345 } 346 347 358 public String 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 372 private void paginate() 373 { 374 376 try { 377 378 if ( ( this.windowSize <= 0 ) || ( this.windowOffset < 0 ) || ( ( this.windowOffset % this.windowSize ) != 0) ) 379 { 380 return; } 382 383 this.currentPageIndex = ( this.windowOffset / this.windowSize ) + 1; 385 386 388 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 404 if ( this.size>0 ) 406 { 407 if ( this.size <= this.windowSize ) 408 { 409 this.pages.add( new Integer (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 (i+1) ); 415 } 416 if ( (this.size % this.windowSize) != 0 ) 417 { 418 int newPageIndex = this.pages.size() + 1; 419 this.pages.add( new Integer (newPageIndex) ); 420 } 421 } 422 } 423 424 this.isValid = true; 426 427 } catch ( Throwable t ) { 428 t.printStackTrace(); 429 } 430 } 431 432 } 433 | Popular Tags |