1 16 17 package org.springframework.beans.support; 18 19 import java.io.Serializable ; 20 import java.util.ArrayList ; 21 import java.util.Date ; 22 import java.util.List ; 23 24 53 public class PagedListHolder implements Serializable { 54 55 public static final int DEFAULT_PAGE_SIZE = 10; 56 57 public static final int DEFAULT_MAX_LINKED_PAGES = 10; 58 59 60 private List source; 61 62 private Date refreshDate; 63 64 private SortDefinition sort; 65 66 private SortDefinition sortUsed; 67 68 private int pageSize = DEFAULT_PAGE_SIZE; 69 70 private int page = 0; 71 72 private boolean newPageSet; 73 74 private int maxLinkedPages = DEFAULT_MAX_LINKED_PAGES; 75 76 77 82 public PagedListHolder() { 83 this(new ArrayList (0)); 84 } 85 86 92 public PagedListHolder(List source) { 93 this(source, new MutableSortDefinition(true)); 94 } 95 96 101 public PagedListHolder(List source, SortDefinition sort) { 102 setSource(source); 103 setSort(sort); 104 } 105 106 107 110 public void setSource(List source) { 111 this.source = source; 112 this.refreshDate = new Date (); 113 this.sortUsed = null; 114 } 115 116 119 public List getSource() { 120 return source; 121 } 122 123 126 public Date getRefreshDate() { 127 return refreshDate; 128 } 129 130 135 public void setSort(SortDefinition sort) { 136 this.sort = sort; 137 } 138 139 142 public SortDefinition getSort() { 143 return sort; 144 } 145 146 151 public void setPageSize(int pageSize) { 152 if (pageSize != this.pageSize) { 153 this.pageSize = pageSize; 154 if (!this.newPageSet) { 155 this.page = 0; 156 } 157 } 158 } 159 160 163 public int getPageSize() { 164 return pageSize; 165 } 166 167 171 public void setPage(int page) { 172 this.page = page; 173 this.newPageSet = true; 174 } 175 176 180 public int getPage() { 181 this.newPageSet = false; 182 if (this.page >= getPageCount()) { 183 this.page = getPageCount() - 1; 184 } 185 return this.page; 186 } 187 188 191 public void setMaxLinkedPages(int maxLinkedPages) { 192 this.maxLinkedPages = maxLinkedPages; 193 } 194 195 198 public int getMaxLinkedPages() { 199 return maxLinkedPages; 200 } 201 202 203 206 public int getPageCount() { 207 float nrOfPages = (float) getSource().size() / getPageSize(); 208 return (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages); 209 } 210 211 214 public boolean isFirstPage() { 215 return getPage() == 0; 216 } 217 218 221 public boolean isLastPage() { 222 return getPage() == getPageCount() -1; 223 } 224 225 229 public void previousPage() { 230 if (!isFirstPage()) { 231 this.page--; 232 } 233 } 234 235 239 public void nextPage() { 240 if (!isLastPage()) { 241 this.page++; 242 } 243 } 244 245 248 public int getNrOfElements() { 249 return getSource().size(); 250 } 251 252 256 public int getFirstElementOnPage() { 257 return (getPageSize() * getPage()); 258 } 259 260 264 public int getLastElementOnPage() { 265 int endIndex = getPageSize() * (getPage() + 1); 266 return (endIndex > getSource().size() ? getSource().size() : endIndex) -1; 267 } 268 269 272 public List getPageList() { 273 return getSource().subList(getFirstElementOnPage(), getLastElementOnPage() +1); 274 } 275 276 279 public int getFirstLinkedPage() { 280 return Math.max(0, getPage() - (getMaxLinkedPages() /2)); 281 } 282 283 286 public int getLastLinkedPage() { 287 return Math.min(getFirstLinkedPage() + getMaxLinkedPages() -1, getPageCount() -1); 288 } 289 290 291 297 public void resort() { 298 SortDefinition sort = getSort(); 299 if (sort != null && !sort.equals(this.sortUsed)) { 300 this.sortUsed = copySortDefinition(sort); 301 doSort(getSource(), sort); 302 setPage(0); 303 } 304 } 305 306 318 protected SortDefinition copySortDefinition(SortDefinition sort) { 319 return new MutableSortDefinition(sort); 320 } 321 322 329 protected void doSort(List source, SortDefinition sort) { 330 PropertyComparator.sort(source, sort); 331 } 332 333 } 334 | Popular Tags |