1 12 package org.displaytag.pagination; 13 14 import java.text.MessageFormat ; 15 import java.util.List ; 16 17 import org.apache.commons.lang.builder.ToStringBuilder; 18 import org.apache.commons.lang.builder.ToStringStyle; 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.displaytag.Messages; 22 import org.displaytag.properties.TableProperties; 23 import org.displaytag.util.Href; 24 25 26 37 public class SmartListHelper 38 { 39 40 43 private static Log log = LogFactory.getLog(SmartListHelper.class); 44 45 48 private List fullList; 49 50 53 private int fullListSize; 54 55 58 private int pageSize; 59 60 63 private int pageCount; 64 65 68 private boolean partialList; 69 70 73 private int currentPage; 74 75 78 private TableProperties properties; 79 80 88 public SmartListHelper( 89 List list, 90 int fullSize, 91 int itemsInPage, 92 TableProperties tableProperties, 93 boolean partialList) 94 { 95 if (log.isDebugEnabled()) 96 { 97 log.debug(Messages.getString("SmartListHelper.debug.instantiated", new Object []{new Integer (list.size()), new Integer (itemsInPage), new Integer (fullSize)})); 99 } 100 101 this.properties = tableProperties; 102 this.pageSize = itemsInPage; 103 this.fullList = list; 104 this.fullListSize = fullSize; 105 this.pageCount = computedPageCount(); 106 this.currentPage = 1; 107 this.partialList = partialList; 108 } 109 110 114 protected SmartListHelper() 115 { 116 } 117 118 123 protected int computedPageCount() 124 { 125 int size = this.fullListSize; 126 int div = size / this.pageSize; 127 int result = (size % this.pageSize == 0) ? div : div + 1; 128 129 return result; 130 } 131 132 137 public int getFirstIndexForCurrentPage() 138 { 139 return getFirstIndexForPage(this.currentPage); 140 } 141 142 147 protected int getLastIndexForCurrentPage() 148 { 149 150 return getLastIndexForPage(this.currentPage); 151 } 152 153 158 protected int getFirstIndexForPage(int pageNumber) 159 { 160 if (this.partialList) 161 { 162 return 0; 163 } 164 else 165 { 166 return (pageNumber - 1) * this.pageSize; 167 } 168 } 169 170 175 protected int getLastIndexForPage(int pageNumber) 176 { 177 if (this.partialList) 178 { 179 return Math.min(this.pageSize - 1, this.fullList.size() - 1); 181 } 182 else 183 { 184 int firstIndex = getFirstIndexForPage(pageNumber); 185 int pageIndex = this.pageSize - 1; 186 int lastIndex = this.fullListSize - 1; 187 188 return Math.min(firstIndex + pageIndex, lastIndex); 189 } 190 } 191 192 197 public List getListForCurrentPage() 198 { 199 200 return getListForPage(this.currentPage); 201 } 202 203 209 protected List getListForPage(int pageNumber) 210 { 211 if (log.isDebugEnabled()) 212 { 213 log.debug(Messages.getString("SmartListHelper.debug.sublist", new Object []{new Integer (pageNumber)})); 215 } 216 217 int firstIndex = getFirstIndexForPage(pageNumber); 218 int lastIndex = getLastIndexForPage(pageNumber); 219 return this.fullList.subList(firstIndex, lastIndex + 1); 220 } 221 222 226 public void setCurrentPage(int pageNumber) 227 { 228 if (log.isDebugEnabled()) 229 { 230 log.debug(Messages.getString("SmartListHelper.debug.currentpage", new Object []{new Integer (pageNumber), new Integer (this.pageCount)})); 232 } 233 234 if (pageNumber < 1) 235 { 236 this.currentPage = 1; 239 } 240 else if (pageNumber != 1 && pageNumber > this.pageCount) 241 { 242 this.currentPage = this.pageCount; 244 } 245 else 246 { 247 this.currentPage = pageNumber; 248 } 249 } 250 251 257 public String getSearchResultsSummary() 258 { 259 260 Object [] objs; 261 String message; 262 263 if (this.fullListSize == 0) 264 { 265 objs = new Object []{this.properties.getPagingItemsName()}; 266 message = this.properties.getPagingFoundNoItems(); 267 268 } 269 else if (this.fullListSize == 1) 270 { 271 objs = new Object []{this.properties.getPagingItemName()}; 272 message = this.properties.getPagingFoundOneItem(); 273 } 274 else if (computedPageCount() == 1) 275 { 276 objs = new Object []{ 277 new Integer (this.fullListSize), 278 this.properties.getPagingItemsName(), 279 this.properties.getPagingItemsName()}; 280 message = this.properties.getPagingFoundAllItems(); 281 } 282 else 283 { 284 objs = new Object []{ 285 new Integer (this.fullListSize), 286 this.properties.getPagingItemsName(), 287 new Integer (getFirstIndexForCurrentPage() + 1), 288 new Integer (getLastIndexForCurrentPage() + 1), 289 new Integer (this.currentPage), 290 new Integer (this.pageCount)}; 291 message = this.properties.getPagingFoundSomeItems(); 292 } 293 294 return MessageFormat.format(message, objs); 295 } 296 297 304 public String getPageNavigationBar(Href baseHref, String pageParameter) 305 { 306 307 int groupSize = this.properties.getPagingGroupSize(); 308 int startPage; 309 int endPage; 310 311 Pagination pagination = new Pagination(baseHref, pageParameter); 312 pagination.setCurrent(new Integer (this.currentPage)); 313 314 if (this.pageCount == 0) 316 { 317 pagination.addPage(1, true); 318 } 319 320 startPage = Math.max(Math.min(this.currentPage - groupSize / 2, this.pageCount - (groupSize - 1)), 1); 323 endPage = Math.min(startPage + groupSize - 1, this.pageCount); 324 325 if (log.isDebugEnabled()) 326 { 327 log.debug("Displaying pages from " + startPage + " to " + endPage); 328 } 329 330 if (this.currentPage != 1) 331 { 332 pagination.setFirst(new Integer (1)); 333 pagination.setPrevious(new Integer (this.currentPage - 1)); 334 } 335 336 for (int j = startPage; j <= endPage; j++) 337 { 338 if (log.isDebugEnabled()) 339 { 340 log.debug("adding page " + j); } 342 pagination.addPage(j, (j == this.currentPage)); 343 } 344 345 if (this.currentPage != this.pageCount) 346 { 347 pagination.setNext(new Integer (this.currentPage + 1)); 348 pagination.setLast(new Integer (this.pageCount)); 349 } 350 351 String bannerFormat; 353 354 if (pagination.isOnePage()) 355 { 356 bannerFormat = this.properties.getPagingBannerOnePage(); 357 } 358 else if (pagination.isFirst()) 359 { 360 bannerFormat = this.properties.getPagingBannerFirst(); 361 } 362 else if (pagination.isLast()) 363 { 364 bannerFormat = this.properties.getPagingBannerLast(); 365 } 366 else 367 { 368 bannerFormat = this.properties.getPagingBannerFull(); 369 } 370 371 return pagination.getFormattedBanner(this.properties.getPagingPageLink(), this.properties 372 .getPagingPageSelected(), this.properties.getPagingPageSeparator(), bannerFormat); 373 } 374 375 378 public String toString() 379 { 380 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) .append("fullList", this.fullList) .append("fullListSize", this.fullListSize) .append("pageSize", this.pageSize) .append("pageCount", this.pageCount) .append("properties", this.properties) .append("currentPage", this.currentPage) .append("partialList", this.partialList) .toString(); 389 } 390 } | Popular Tags |