001: package org.tigris.scarab.util;
002:
003: /* ================================================================
004: * Copyright (c) 2000-2002 CollabNet. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are
008: * met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowlegement: "This product includes
019: * software developed by Collab.Net <http://www.Collab.Net/>."
020: * Alternately, this acknowlegement may appear in the software itself, if
021: * and wherever such third-party acknowlegements normally appear.
022: *
023: * 4. The hosted project names must not be used to endorse or promote
024: * products derived from this software without prior written
025: * permission. For written permission, please contact info@collab.net.
026: *
027: * 5. Products derived from this software may not use the "Tigris" or
028: * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
029: * prior written permission of Collab.Net.
030: *
031: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
032: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
033: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
034: * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
035: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
036: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
037: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
038: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
039: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
040: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
041: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
042: *
043: * ====================================================================
044: *
045: * This software consists of voluntary contributions made by many
046: * individuals on behalf of Collab.Net.
047: */
048:
049: import java.util.List;
050:
051: /*
052: *
053: * @author <a href="mailto:tenersen@collab.net">Todd Enersen</a>
054: * @version $Id: ScarabPaginatedList.java 8532 2003-08-26 17:54:35Z elicia $
055: */
056: public class ScarabPaginatedList {
057: /**
058: * The total size of the list, not just the window which is
059: * the displayable region.
060: */
061: private int totalListSize;
062:
063: /**
064: * The number of items per page, including the current
065: * displayable region.
066: */
067: private int resultsPerPage;
068:
069: /**
070: * The current page number that is being displayed.
071: */
072: private int currentPageNumber;
073:
074: /**
075: * The current viewable list window, as a List
076: */
077: private List window;
078:
079: /**
080: * Constructor
081: */
082: public ScarabPaginatedList() {
083: window = null;
084: currentPageNumber = 0;
085: resultsPerPage = 0;
086: totalListSize = 0;
087: }
088:
089: /**
090: * Constructor which sets things up for a 'ready' list.
091: */
092: public ScarabPaginatedList(List l, int size, int pageNum,
093: int perPage) {
094: window = l;
095: totalListSize = size;
096: currentPageNumber = pageNum;
097: resultsPerPage = perPage;
098: }
099:
100: /**
101: * Method to return the size of the entire list, not
102: * just the part that is currently 'to be' displayed.
103: */
104: public int getTotalListSize() {
105: return totalListSize;
106: }
107:
108: /**
109: * Method to return the total number of pages in this list.
110: */
111: public int getNumberOfPages() {
112: int r = 0;
113:
114: if (resultsPerPage != 0) {
115: r = (int) Math.ceil((float) totalListSize / resultsPerPage);
116: }
117:
118: return r;
119: }
120:
121: /**
122: * Method to get the current page number.
123: */
124: public int getPageNumber() {
125: return currentPageNumber;
126: }
127:
128: /**
129: * Method to return the previous page number.
130: */
131: public int getPrevPageNumber() {
132: int r = getPageNumber() - 1;
133: if (r < 0) {
134: r = 0;
135: }
136: return r;
137: }
138:
139: /**
140: * Method to return the next page number
141: */
142: public int getNextPageNumber() {
143: int r = getPageNumber() + 1;
144: if (r > getNumberOfPages()) {
145: r = 0;
146: }
147: return r;
148: }
149:
150: /**
151: * Method to get the number of results per page.
152: */
153: public int getResultsPerPage() {
154: return resultsPerPage;
155: }
156:
157: /**
158: * Method to get the portion of the list that is currently
159: * viewable (inside the window)
160: */
161: public List getList() {
162: return window;
163: }
164:
165: /**
166: * Method to set the TotalListSize.
167: */
168: public void setTotalListSize(int size) {
169: totalListSize = size;
170: }
171:
172: /**
173: * Method to set the current page number.
174: */
175: public void setCurrentPageNumber(int pageNum) {
176: currentPageNumber = pageNum;
177: }
178:
179: /**
180: * Method to set the current list window.
181: */
182: public void setList(List list) {
183: window = list;
184: }
185:
186: }
|