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.displaytag.properties.TableProperties; 20 import org.displaytag.util.Href; 21 22 23 28 public class PaginatedListSmartListHelper extends SmartListHelper 29 { 30 31 private PaginatedList paginatedList; 32 33 private TableProperties properties; 34 35 private int pageCount; 36 37 public PaginatedListSmartListHelper(PaginatedList paginatedList, TableProperties tableProperties) 38 { 39 super(); 40 this.paginatedList = paginatedList; 41 this.properties = tableProperties; 42 this.pageCount = computePageCount(); 43 } 44 45 private int computePageCount() 46 { 47 int pageCount = paginatedList.getFullListSize() / Math.max(1, paginatedList.getObjectsPerPage()); 48 if ((paginatedList.getFullListSize() % paginatedList.getObjectsPerPage()) > 0) 49 { 50 pageCount++; 51 } 52 return pageCount; 53 } 54 55 public int getFirstIndexForCurrentPage() 56 { 57 return getFirstIndexForPage(paginatedList.getPageNumber()); 58 } 59 60 protected int getFirstIndexForPage(int pageNumber) 61 { 62 if (pageNumber > pageCount) 63 { 64 pageNumber = pageCount; 65 } 66 67 return ((pageNumber - 1) * paginatedList.getObjectsPerPage()); 68 } 69 70 protected int getLastIndexForCurrentPage() 71 { 72 return getLastIndexForPage(paginatedList.getPageNumber()); 73 } 74 75 protected int getLastIndexForPage(int pageNumber) 76 { 77 if (pageNumber > pageCount) 78 { 79 pageNumber = pageCount; 80 } 81 82 int result = getFirstIndexForPage(pageNumber) + paginatedList.getObjectsPerPage() - 1; 83 if (result >= paginatedList.getFullListSize()) 84 { 85 result = paginatedList.getFullListSize() - 1; 86 } 87 return result; 88 } 89 90 public List getListForCurrentPage() 91 { 92 return paginatedList.getList(); 93 } 94 95 protected List getListForPage(int pageNumber) 96 { 97 if ((pageNumber) == paginatedList.getPageNumber()) 98 { 99 return getListForCurrentPage(); 100 } 101 else 102 { 103 return null; 104 } 105 } 106 107 public String getPageNavigationBar(Href baseHref, String pageParameter) 108 { 109 110 int groupSize = this.properties.getPagingGroupSize(); 111 int startPage; 112 int endPage; 113 114 Pagination pagination = new Pagination(baseHref, pageParameter); 115 pagination.setCurrent(new Integer (paginatedList.getPageNumber())); 116 117 if (pageCount == 0) 119 { 120 pagination.addPage(1, true); 121 } 122 123 startPage = Math.max(Math.min(paginatedList.getPageNumber() - groupSize / 2, pageCount - (groupSize - 1)), 1); 127 endPage = Math.min(startPage + groupSize - 1, pageCount); 128 129 if (paginatedList.getPageNumber() != 1) 130 { 131 pagination.setFirst(new Integer (1)); 132 pagination.setPrevious(new Integer (paginatedList.getPageNumber() - 1)); 133 } 134 135 for (int j = startPage; j <= endPage; j++) 136 { 137 pagination.addPage(j, (j == paginatedList.getPageNumber())); 138 } 139 140 if (paginatedList.getPageNumber() != pageCount) 141 { 142 pagination.setNext(new Integer (paginatedList.getPageNumber() + 1)); 143 pagination.setLast(new Integer (pageCount)); 144 } 145 146 String bannerFormat; 148 149 if (pagination.isOnePage()) 150 { 151 bannerFormat = this.properties.getPagingBannerOnePage(); 152 } 153 else if (pagination.isFirst()) 154 { 155 bannerFormat = this.properties.getPagingBannerFirst(); 156 } 157 else if (pagination.isLast()) 158 { 159 bannerFormat = this.properties.getPagingBannerLast(); 160 } 161 else 162 { 163 bannerFormat = this.properties.getPagingBannerFull(); 164 } 165 166 return pagination.getFormattedBanner(this.properties.getPagingPageLink(), this.properties 167 .getPagingPageSelected(), this.properties.getPagingPageSeparator(), bannerFormat); 168 } 169 170 public String getSearchResultsSummary() 171 { 172 173 Object [] objs; 174 String message; 175 176 if (this.paginatedList.getFullListSize() == 0) 177 { 178 objs = new Object []{this.properties.getPagingItemsName()}; 179 message = this.properties.getPagingFoundNoItems(); 180 } 181 else if (this.paginatedList.getFullListSize() == 1) 182 { 183 objs = new Object []{this.properties.getPagingItemName()}; 184 message = this.properties.getPagingFoundOneItem(); 185 } 186 else if (pageCount == 1) 187 { 188 objs = new Object []{ 189 new Integer (this.paginatedList.getFullListSize()), 190 this.properties.getPagingItemsName(), 191 this.properties.getPagingItemsName()}; 192 message = this.properties.getPagingFoundAllItems(); 193 } 194 else 195 { 196 objs = new Object []{ 197 new Integer (this.paginatedList.getFullListSize()), 198 this.properties.getPagingItemsName(), 199 new Integer (getFirstIndexForCurrentPage() + 1), 200 new Integer (getLastIndexForCurrentPage() + 1), 201 new Integer (this.paginatedList.getPageNumber()), 202 new Integer (pageCount)}; 203 message = this.properties.getPagingFoundSomeItems(); 204 } 205 206 return MessageFormat.format(message, objs); 207 } 208 209 212 public String toString() 213 { 214 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) .append("paginatedList", this.paginatedList) .append("properties", this.properties) .toString(); 218 } 219 } | Popular Tags |