KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mdarad > framework > util > struts > action > ListActionForm


1 /*
2     Mdarad-Toolobox is a collection of tools for Architected RAD
3     (Rapid Application Development) based on an MDA approach.
4     The toolbox contains frameworks and generators for many environments
5     (JAVA, J2EE, Hibernate, .NET, C++, etc.) which allow to generate
6     applications from a design Model
7     Copyright (C) 2004-2005 Elapse Technologies Inc.
8
9     This library is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public
11     License as published by the Free Software Foundation; either
12     version 2.1 of the License, or (at your option) any later version.
13
14     This library is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17     General Public License for more details.
18
19     You should have received a copy of the GNU General Public
20     License along with this library; if not, write to the Free Software
21     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */

23 package org.mdarad.framework.util.struts.action;
24
25 import java.util.Collection JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31
32 /**
33  * This class is used to list results from
34  * a collection.
35  *
36  * @author Philippe Brouillette
37  * @version 1.0
38  */

39 public abstract class ListActionForm extends MdaradActionForm {
40     
41     /**
42      * Constant that specifies the maximum number of
43      * pages shown on the form
44      */

45     private static int MAX_PAGES_SHOWN = 10;
46     
47     /**
48      * Constant that controls the maximum number
49      * of results by page
50      */

51     private static int MAX_NB_HITS = 30;
52     
53     /**
54      * Property that contains the collection of
55      * object to be listed
56      */

57     private Collection JavaDoc collection;
58     
59     /**
60      * Method that returns the collection of
61      * objects to be listed
62      * @return
63      */

64     public Collection JavaDoc getCollection() {
65
66         // get the starting index
67
int startingIndex = getStartingIndex().intValue();
68         
69         // create subset for the page
70
List JavaDoc pageResults = new Vector JavaDoc(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     /**
80      * Set the collection of objects to be listed
81      * @param collection the collection object
82      */

83     public void setCollection(Collection JavaDoc collection) {
84         this.collection = collection;
85     }
86     
87     
88     /**
89      * Property that holds the current page
90      * of object listed. The first page is 0
91      */

92     private int currentPage = 0;
93     
94     public Integer JavaDoc getCurrentPage() {
95         return new Integer JavaDoc(currentPage);
96     }
97     
98     public void setCurrentPage(Integer JavaDoc currentPage) {
99         if (currentPage != null) {
100             this.currentPage = currentPage.intValue();
101         }
102     }
103
104     /**
105      * Method that returns the starting index of
106      * the list currently shown
107      * @return <code>Integer</code> that holds the
108      * starting index
109      */

110     public Integer JavaDoc getStartingIndex() {
111         return new Integer JavaDoc(currentPage * numberOfHits);
112     }
113
114     /**
115      * Method that returns the ending index of
116      * the list currently shown
117      * @return <code>Integer</code> that holds the
118      * ending index
119      */

120     public Integer JavaDoc getEndingIndex() {
121         return new Integer JavaDoc(getStartingIndex().intValue() + numberOfHits);
122     }
123     
124     /**
125      * Attribute that contains the number of records
126      * per page
127      */

128     private int numberOfHits = MAX_NB_HITS;
129     
130     /**
131      * Getter for the numberOfHits property
132      * @return the maximum number of hits per page
133      */

134     public Integer JavaDoc getNumberOfHits() {
135         return new Integer JavaDoc(numberOfHits);
136     }
137     
138     /**
139      * Setter for the numberOfHits property
140      * @param numberOfHits the value to be set
141      */

142     public void setNumberOfHits(Integer JavaDoc numberOfHits) {
143         this.numberOfHits = numberOfHits.intValue();
144     }
145     
146
147     /**
148      * Return the page lists links
149      * It returns links for the x next pages
150      * and the x previous pages.
151      * @return
152      */

153     public Collection JavaDoc getPageList() {
154         Collection JavaDoc pageList = new Vector JavaDoc();
155         int size = collection.size();
156         int nbPages = size / numberOfHits;
157         
158         int middlePage = currentPage;
159         
160         // returns the page selectors only if there is more
161
// than one page
162
if (nbPages > 1) {
163             // add a "next" keyword if the current page
164
// is more than the half of page shown
165
if (middlePage > MAX_PAGES_SHOWN/2) {
166                 pageList.add(new PageSelectorDescriptor("< Previous", middlePage - MAX_PAGES_SHOWN/2));
167             }
168             
169             // add a page for remaining records
170
if (size % numberOfHits > 0) {
171                 nbPages++;
172             }
173             
174             int startingPage = middlePage - MAX_PAGES_SHOWN/2;
175             // starting page cannot be under 0
176
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     /**
203      * Method that initialize the search list form.
204      * @param request
205      * @param response
206      */

207     public void initialize(HttpServletRequest JavaDoc request,
208                                  HttpServletResponse JavaDoc response) {
209         // do nothing...
210
}
211 }
212
Popular Tags