KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > vlib > components > Browser


1 // Copyright 2004 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.vlib.components;
16
17 import java.rmi.RemoteException JavaDoc;
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 /**
32  * Implements a paging browser for the results of a {@link IBookQuery}.
33  *
34  *
35  * @author Howard Lewis Ship
36  * @version $Id: Browser.java,v 1.10 2004/02/19 17:38:04 hlship Exp $
37  *
38  **/

39
40 public abstract class Browser extends AbstractComponent implements PageRenderListener
41 {
42     public abstract IBookQuery getQuery();
43
44     /**
45      * Default for the page size; the number of results viewed on each page.
46      *
47      **/

48
49     public static final int DEFAULT_PAGE_SIZE = 15;
50
51     /**
52      * The maximum number of items to display on a page.
53      *
54      **/

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 JavaDoc element);
67
68     public abstract String JavaDoc getElement();
69
70     public abstract void setValue(Object JavaDoc value);
71
72     public abstract IActionListener getListener();
73
74     public abstract Object JavaDoc[] getPageResults();
75
76     public abstract void setPageResults(Object JavaDoc[] pageResults);
77
78     /**
79      * Invoked by the container when the query (otherwise accessed via the query
80      * parameter) changes. Re-caches the number of results and sets the current page
81      * back to 1.
82      *
83      **/

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         // For 0 ... pageSize elements, its just one page.
99

100         int resultCount = getResultCount();
101
102         if (resultCount <= _pageSize)
103             return 1;
104
105         // We need the number of results divided by the results per page.
106

107         int result = resultCount / _pageSize;
108
109         // If there's any left-over, then we need an additional page.
110

111         if (resultCount % _pageSize > 0)
112             result++;
113
114         return result;
115     }
116
117     /**
118      * Invoked to change the displayed page number.
119      *
120      * @param page page to display, numbered from one. The currentPage property will be
121      * updated. The value is constrained to fit in the valid range of pages
122      * for the component.
123      *
124      **/

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 JavaDoc 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 JavaDoc[] books = getPageResults();
168         int count = Tapestry.size(books);
169         String JavaDoc 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 JavaDoc 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