1 15 package org.apache.tapestry.vlib.components; 16 17 import java.rmi.RemoteException ; 18 19 import org.apache.tapestry.AbstractComponent; 20 import org.apache.tapestry.IActionListener; 21 import org.apache.tapestry.IMarkupWriter; 22 import org.apache.tapestry.IPage; 23 import org.apache.tapestry.IRequestCycle; 24 import org.apache.tapestry.Tapestry; 25 import org.apache.tapestry.event.PageEvent; 26 import org.apache.tapestry.event.PageRenderListener; 27 import org.apache.tapestry.vlib.VirtualLibraryEngine; 28 import org.apache.tapestry.vlib.ejb.Book; 29 import org.apache.tapestry.vlib.ejb.IBookQuery; 30 31 39 40 public abstract class Browser extends AbstractComponent implements PageRenderListener 41 { 42 public abstract IBookQuery getQuery(); 43 44 48 49 public static final int DEFAULT_PAGE_SIZE = 15; 50 51 55 56 private int _pageSize = DEFAULT_PAGE_SIZE; 57 58 public abstract int getResultCount(); 59 60 public abstract void setResultCount(int resultCount); 61 62 public abstract int getCurrentPage(); 63 64 public abstract void setCurrentPage(int currentPage); 65 66 public abstract void setElement(String element); 67 68 public abstract String getElement(); 69 70 public abstract void setValue(Object value); 71 72 public abstract IActionListener getListener(); 73 74 public abstract Object [] getPageResults(); 75 76 public abstract void setPageResults(Object [] pageResults); 77 78 84 85 public void initializeForResultCount(int resultCount) 86 { 87 setResultCount(resultCount); 88 setCurrentPage(1); 89 setPageCount(computePageCount()); 90 } 91 92 public abstract int getPageCount(); 93 94 public abstract void setPageCount(int pageCount); 95 96 private int computePageCount() 97 { 98 100 int resultCount = getResultCount(); 101 102 if (resultCount <= _pageSize) 103 return 1; 104 105 107 int result = resultCount / _pageSize; 108 109 111 if (resultCount % _pageSize > 0) 112 result++; 113 114 return result; 115 } 116 117 125 126 public void jump(int page) 127 { 128 if (page < 2) 129 { 130 setCurrentPage(1); 131 return; 132 } 133 134 int pageCount = getPageCount(); 135 if (page > getPageCount()) 136 { 137 setCurrentPage(pageCount); 138 return; 139 } 140 141 setCurrentPage(page); 142 } 143 144 public boolean getDisableBack() 145 { 146 return getCurrentPage() <= 1; 147 } 148 149 public boolean getDisableNext() 150 { 151 return getCurrentPage() >= getPageCount(); 152 } 153 154 public String getRange() 155 { 156 int currentPage = getCurrentPage(); 157 int resultCount = getResultCount(); 158 159 int low = (currentPage - 1) * _pageSize + 1; 160 int high = Math.min(currentPage * _pageSize, resultCount); 161 162 return low + " - " + high; 163 } 164 165 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) 166 { 167 Object [] books = getPageResults(); 168 int count = Tapestry.size(books); 169 String element = getElement(); 170 171 for (int i = 0; i < count; i++) 172 { 173 setValue(books[i]); 174 175 if (element != null) 176 { 177 writer.begin(element); 178 renderInformalParameters(writer, cycle); 179 } 180 181 renderBody(writer, cycle); 182 183 if (element != null) 184 writer.end(); 185 } 186 } 187 188 protected void finishLoad() 189 { 190 setElement("tr"); 191 } 192 193 public void pageBeginRender(PageEvent event) 194 { 195 int resultCount = getResultCount(); 196 int currentPage = getCurrentPage(); 197 198 int low = (currentPage - 1) * _pageSize; 199 int high = Math.min(currentPage * _pageSize, resultCount) - 1; 200 201 if (low > high) 202 return; 203 204 Book[] pageResults = null; 205 206 int i = 0; 207 while (true) 208 { 209 210 try 211 { 212 pageResults = getQuery().get(low, high - low + 1); 213 214 break; 215 } 216 catch (RemoteException ex) 217 { 218 IPage page = getPage(); 219 220 if (i++ == 0) 221 getListener().actionTriggered(this, page.getRequestCycle()); 222 else 223 { 224 VirtualLibraryEngine vengine = (VirtualLibraryEngine) page.getEngine(); 225 vengine.rmiFailure("Unable to retrieve query results.", ex, i); 226 } 227 228 } 229 } 230 231 setPageResults(pageResults); 232 } 233 234 } | Popular Tags |