1 24 package org.riotfamily.website.generic.view; 25 26 import java.io.UnsupportedEncodingException ; 27 import java.net.URLEncoder ; 28 29 import javax.servlet.http.HttpServletRequest ; 30 31 import org.springframework.web.util.UrlPathHelper; 32 33 36 public class Pager { 37 38 private int currentPage; 39 40 private int pages; 41 42 private PagerItem firstPage; 43 44 private PagerItem prevPage; 45 46 private PagerItem[] prevPages; 47 48 private PagerItem[] nextPages; 49 50 private PagerItem nextPage; 51 52 private PagerItem lastPage; 53 54 private boolean gapToFirstPage; 55 56 private boolean gapToLastPage; 57 58 private String pageParam; 59 60 private String linkPrefix; 61 62 private String encodingScheme = "UTF-8"; 63 64 private boolean copyParameters = true; 65 66 private UrlPathHelper urlPathHelper = new UrlPathHelper(); 67 68 public Pager(int page, int pageSize, long itemCount) { 69 currentPage = page; 70 pages = (int) itemCount / pageSize + 1; 71 if (itemCount % pageSize == 0) { 72 pages--; 73 } 74 } 75 76 public void setCopyParameters(boolean copyParameters) { 77 this.copyParameters = copyParameters; 78 } 79 80 public void setEncodingScheme(String encodingScheme) { 81 this.encodingScheme = encodingScheme; 82 } 83 84 public void initialize(HttpServletRequest request, int padding, 85 String pageParam) { 86 87 this.pageParam = pageParam; 88 prepareLinkPrefix(request); 89 90 int start = currentPage - padding; 91 int end = currentPage + padding; 92 93 if (start < 0) { 94 end += -1 * start + 1; 95 } 96 if (end > pages) { 97 start -= (end - pages); 98 } 99 if (start < 1) { 100 start = 1; 101 } 102 if (end > pages) { 103 end = pages; 104 } 105 106 gapToFirstPage = start > 2; 107 gapToLastPage = end < pages - 1; 108 109 int prevCount = Math.max(currentPage - start, 0); 110 int nextCount = Math.max(end - currentPage, 0); 111 112 if (start > 1) { 113 firstPage = new PagerItem(linkPrefix, 1); 114 } 115 116 prevPages = new PagerItem[prevCount]; 117 for (int i = 0; i < prevCount; i++) { 118 prevPages[i] = new PagerItem(linkPrefix, start + i); 119 } 120 if (prevCount > 0) { 121 prevPage = prevPages[prevCount - 1]; 122 } 123 124 nextPages = new PagerItem[nextCount]; 125 for (int i = 0; i < nextCount; i++) { 126 nextPages[i] = new PagerItem(linkPrefix, currentPage + i + 1); 127 } 128 if (nextCount > 0) { 129 nextPage = nextPages[0]; 130 } 131 132 if (end < pages) { 133 lastPage = new PagerItem(linkPrefix, pages); 134 } 135 } 136 137 private void prepareLinkPrefix(HttpServletRequest request) { 138 StringBuffer url = new StringBuffer ( 139 urlPathHelper.getOriginatingRequestUri(request)); 140 141 url.append('?'); 142 if (copyParameters) { 143 String query = urlPathHelper.getOriginatingQueryString(request); 144 if (query != null) { 145 int i = query.indexOf(pageParam); 146 if (i != -1) { 147 url.append(query.substring(0, i)); 148 } 149 else { 150 url.append(query).append('&'); 151 } 152 } 153 } 154 url.append(pageParam); 155 url.append('='); 156 linkPrefix = url.toString(); 157 } 158 159 protected String urlEncode(String s) { 160 try { 161 return URLEncoder.encode(s, encodingScheme); 162 } 163 catch (UnsupportedEncodingException e) { 164 throw new RuntimeException (e); 165 } 166 } 167 168 public int getCurrentPage() { 169 return currentPage; 170 } 171 172 public int getPages() { 173 return pages; 174 } 175 176 public PagerItem getFirstPage() { 177 return firstPage; 178 } 179 180 public boolean isGapToFirstPage() { 181 return gapToFirstPage; 182 } 183 184 public boolean isGapToLastPage() { 185 return gapToLastPage; 186 } 187 188 public PagerItem getLastPage() { 189 return lastPage; 190 } 191 192 public PagerItem[] getNextPages() { 193 return nextPages; 194 } 195 196 public PagerItem[] getPrevPages() { 197 return prevPages; 198 } 199 200 public PagerItem getNextPage() { 201 return nextPage; 202 } 203 204 public PagerItem getPrevPage() { 205 return prevPage; 206 } 207 208 public class PagerItem { 209 210 private int number; 211 212 private String link; 213 214 public PagerItem(String linkPrefix, int number) { 215 this.number = number; 216 this.link = linkPrefix + number; 217 } 218 219 public int getNumber() { 220 return number; 221 } 222 223 public String getLink() { 224 return link; 225 } 226 } 227 } | Popular Tags |