1 23 package org.mdarad.framework.util.struts.action; 24 25 import java.util.Collection ; 26 import java.util.List ; 27 import java.util.Vector ; 28 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 32 39 public abstract class ListActionForm extends MdaradActionForm { 40 41 45 private static int MAX_PAGES_SHOWN = 10; 46 47 51 private static int MAX_NB_HITS = 30; 52 53 57 private Collection collection; 58 59 64 public Collection getCollection() { 65 66 int startingIndex = getStartingIndex().intValue(); 68 69 List pageResults = new Vector (collection); 71 int endIndex = startingIndex + numberOfHits; 72 if (endIndex > pageResults.size()) { 73 endIndex = pageResults.size(); 74 } 75 return pageResults.subList(startingIndex, 76 endIndex); 77 } 78 79 83 public void setCollection(Collection collection) { 84 this.collection = collection; 85 } 86 87 88 92 private int currentPage = 0; 93 94 public Integer getCurrentPage() { 95 return new Integer (currentPage); 96 } 97 98 public void setCurrentPage(Integer currentPage) { 99 if (currentPage != null) { 100 this.currentPage = currentPage.intValue(); 101 } 102 } 103 104 110 public Integer getStartingIndex() { 111 return new Integer (currentPage * numberOfHits); 112 } 113 114 120 public Integer getEndingIndex() { 121 return new Integer (getStartingIndex().intValue() + numberOfHits); 122 } 123 124 128 private int numberOfHits = MAX_NB_HITS; 129 130 134 public Integer getNumberOfHits() { 135 return new Integer (numberOfHits); 136 } 137 138 142 public void setNumberOfHits(Integer numberOfHits) { 143 this.numberOfHits = numberOfHits.intValue(); 144 } 145 146 147 153 public Collection getPageList() { 154 Collection pageList = new Vector (); 155 int size = collection.size(); 156 int nbPages = size / numberOfHits; 157 158 int middlePage = currentPage; 159 160 if (nbPages > 1) { 163 if (middlePage > MAX_PAGES_SHOWN/2) { 166 pageList.add(new PageSelectorDescriptor("< Previous", middlePage - MAX_PAGES_SHOWN/2)); 167 } 168 169 if (size % numberOfHits > 0) { 171 nbPages++; 172 } 173 174 int startingPage = middlePage - MAX_PAGES_SHOWN/2; 175 if (startingPage < 0) { 177 startingPage = 0; 178 } 179 180 int endingPage = startingPage + MAX_PAGES_SHOWN; 181 if (endingPage > (startingPage + nbPages)) { 182 endingPage = startingPage + nbPages; 183 } 184 185 PageSelectorDescriptor pageDescriptor = null; 186 for (int i = startingPage; i < endingPage; i++) { 187 188 int startingIndex = i * numberOfHits; 189 int endingIndex = startingIndex + numberOfHits; 190 if (i < MAX_PAGES_SHOWN) { 191 pageDescriptor = new PageSelectorDescriptor("[" + startingIndex + "-" + endingIndex + "]", i); 192 } 193 else { 194 pageDescriptor = new PageSelectorDescriptor("Next >", i); 195 } 196 pageList.add(pageDescriptor); 197 } 198 } 199 return pageList; 200 } 201 202 207 public void initialize(HttpServletRequest request, 208 HttpServletResponse response) { 209 } 211 } 212 | Popular Tags |