1 12 package org.displaytag.pagination; 13 14 import java.text.MessageFormat ; 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.apache.commons.lang.builder.ToStringBuilder; 20 import org.apache.commons.lang.builder.ToStringStyle; 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 import org.displaytag.util.Href; 24 25 26 31 public class Pagination 32 { 33 34 37 private static Log log = LogFactory.getLog(Pagination.class); 38 39 42 private Href href; 43 44 47 private String pageParam; 48 49 52 private Integer firstPage; 53 54 57 private Integer lastPage; 58 59 62 private Integer previousPage; 63 64 67 private Integer nextPage; 68 69 72 private Integer currentPage; 73 74 78 private List pages = new ArrayList (); 79 80 85 public Pagination(Href baseHref, String pageParameter) 86 { 87 this.href = baseHref; 88 this.pageParam = pageParameter; 89 } 90 91 96 public void addPage(int number, boolean isSelected) 97 { 98 if (log.isDebugEnabled()) 99 { 100 log.debug("adding page " + number); 101 } 102 this.pages.add(new NumberedPage(number, isSelected)); 103 } 104 105 109 public boolean isFirst() 110 { 111 return this.firstPage == null; 112 } 113 114 118 public boolean isLast() 119 { 120 return this.lastPage == null; 121 } 122 123 127 public boolean isOnePage() 128 { 129 return (this.pages == null) || this.pages.size() <= 1; 130 } 131 132 136 public Integer getFirst() 137 { 138 return this.firstPage; 139 } 140 141 145 public void setFirst(Integer first) 146 { 147 this.firstPage = first; 148 } 149 150 154 public Integer getLast() 155 { 156 return this.lastPage; 157 } 158 159 163 public void setLast(Integer last) 164 { 165 this.lastPage = last; 166 } 167 168 172 public Integer getPrevious() 173 { 174 return this.previousPage; 175 } 176 177 181 public void setPrevious(Integer previous) 182 { 183 this.previousPage = previous; 184 } 185 186 190 public Integer getNext() 191 { 192 return this.nextPage; 193 } 194 195 199 public void setNext(Integer next) 200 { 201 this.nextPage = next; 202 } 203 204 208 public void setCurrent(Integer current) 209 { 210 this.currentPage = current; 211 } 212 213 221 public String getFormattedBanner(String numberedPageFormat, String numberedPageSelectedFormat, 222 String numberedPageSeparator, String fullBanner) 223 { 224 StringBuffer buffer = new StringBuffer (100); 225 226 Iterator pageIterator = this.pages.iterator(); 228 229 while (pageIterator.hasNext()) 230 { 231 232 NumberedPage page = (NumberedPage) pageIterator.next(); 234 235 Integer pageNumber = new Integer (page.getNumber()); 236 237 String urlString = ((Href) this.href.clone()).addParameter(this.pageParam, pageNumber).toString(); 238 239 Object [] pageObjects = {pageNumber, urlString}; 241 242 if (page.getSelected()) 244 { 245 buffer.append(MessageFormat.format(numberedPageSelectedFormat, pageObjects)); 246 } 247 else 248 { 249 buffer.append(MessageFormat.format(numberedPageFormat, pageObjects)); 250 } 251 252 if (pageIterator.hasNext()) 254 { 255 buffer.append(numberedPageSeparator); 256 } 257 } 258 259 String numberedPageString = buffer.toString(); 261 Object [] pageObjects = { 270 numberedPageString, 271 ((Href) this.href.clone()).addParameter(this.pageParam, getFirst()), 272 ((Href) this.href.clone()).addParameter(this.pageParam, getPrevious()), 273 ((Href) this.href.clone()).addParameter(this.pageParam, getNext()), 274 ((Href) this.href.clone()).addParameter(this.pageParam, getLast()), 275 this.currentPage, 276 this.isLast() ? this.currentPage : this.lastPage}; 278 return MessageFormat.format(fullBanner, pageObjects); 280 } 281 282 285 public String toString() 286 { 287 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) .append("firstPage", this.firstPage) .append("lastPage", this.lastPage) .append("currentPage", this.currentPage) .append("nextPage", this.nextPage) .append("previousPage", this.previousPage) .append("pages", this.pages) .append("href", this.href) .append("pageParam", this.pageParam) .toString(); 297 } 298 } | Popular Tags |