KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > utils > pagination > PaginatableList


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
// PaginatableList
14
//
15
//
16

17 package org.jahia.utils.pagination;
18
19
20 import java.sql.SQLException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23
24 //------------------------------------------------import com.workingdogs.village.*;
25

26 //------------------------------------------------import org.joist.database.*;
27

28 /**
29  * An abstract class describing a list which can be paginated.
30  *
31  * @author MJ
32  */

33 abstract public class PaginatableList extends ArrayList JavaDoc
34 {
35     protected int itemsPerPage;
36
37     /**
38      * Returns a sublist of this list, the contents determined by
39      * the page requested.
40      */

41     public List JavaDoc getPage(Integer JavaDoc page) throws SQLException JavaDoc
42     {
43         int start = firstItem(page);
44         int finish = lastItem(page);
45         return subList(start,finish);
46     }
47
48     /**
49      * Prints out an HTML navigation bar for the given page;
50      */

51     public String JavaDoc navigationBar(String JavaDoc linkText, Integer JavaDoc page)
52     throws SQLException JavaDoc
53     {
54         boolean prependDots = false;
55         boolean appendDots = false;
56         int pageNo = page.intValue();
57         int pages = numberOfPages();
58         int startPage = pageNo - 5;
59         if (startPage <= 0) startPage = 1;
60         if (startPage > 1) prependDots = true;
61         int endPage = pageNo + 4;
62         if (endPage < 10) endPage = 10;
63         if (endPage > pages) endPage = pages;
64         if (endPage < (pages-1)) appendDots = true;
65         
66         String JavaDoc extra = appendQueryString();
67         if (!extra.equals("")) {
68             if (linkText.lastIndexOf("?") == -1) {
69                 linkText += "?";
70             } else {
71                 linkText += "&";
72             }
73             linkText += extra;
74         }
75
76         StringBuffer JavaDoc bar = new StringBuffer JavaDoc();
77         if (startPage != endPage) {
78             if (pageNo != 1) {
79                 bar.append(makeLink(linkText, pageNo-1, "Prev"));
80             } else {
81                 bar.append("Prev");
82             }
83             bar.append(" | ");
84
85             if (prependDots) bar.append(" ... | ");
86
87             for (int i = startPage; i <= endPage; i++) {
88                 String JavaDoc linkedText = i + " " + makeGuide(new Integer JavaDoc(i));
89
90                 if (i == pageNo) {
91                     bar.append(linkedText);
92                 } else {
93                     bar.append(makeLink(linkText, i, linkedText));
94                 }
95                 if (i < endPage) {
96                     bar.append (" | ");
97                 }
98             }
99
100             bar.append(" | ");
101
102             if (prependDots) bar.append(" ... | ");
103
104             if (pageNo != endPage) {
105                 bar.append(makeLink(linkText, pageNo+1, "Next"));
106             } else {
107                 bar.append("Next");
108             }
109         }
110         return bar.toString();
111     }
112
113     /**
114      * Makes a link to the given page.
115      */

116     protected String JavaDoc makeLink(String JavaDoc linkText, int pageNo, String JavaDoc linkedText)
117     {
118         StringBuffer JavaDoc link = new StringBuffer JavaDoc();
119         link.append("<a HREF=\"").append(linkText);
120         if (linkText.lastIndexOf("?") == -1) {
121             link.append("?");
122         } else {
123             link.append("&");
124         }
125         link.append("page=").append(pageNo).append("\">").append(linkedText);
126         link.append("</a>");
127         return link.toString();
128     }
129
130     /**
131      * Makes a guide to the given page for use in the navbar. A
132      * list of Users, for instance, might print "(A-N)" for a page
133      * in which the first entry had a name beginning with A and the
134      * last entry had a name beginning with N.
135      */

136     public String JavaDoc makeGuide(Integer JavaDoc page) throws SQLException JavaDoc
137     {
138         return "";
139     }
140
141     /**
142      * Generate whatever text is neccessary to mantain this list's
143      * state in a link to another page. A list which had had a filter
144      * applied to it would need to return fields which would reinstate
145      * the filter.
146      */

147     public String JavaDoc appendQueryString()
148     {
149         return "";
150     }
151
152     /**
153      * Returns the number of items in the list.
154      */

155     public int numberOfItems() throws SQLException JavaDoc
156     {
157         return size();
158     }
159
160     /**
161      * Returns the number of pages the list will occupy.
162      */

163     public int numberOfPages() throws SQLException JavaDoc
164     {
165         int entries = numberOfItems();
166         int pages = entries / itemsPerPage;
167         int entriesOnExtraPage = entries % itemsPerPage;
168         if (entriesOnExtraPage != 0) pages++;
169         return pages;
170     }
171
172     /**
173      * Returns the maximum number of items that should be on any
174      * given page. If the list was created with 0 items per page,
175      * display the whole list.
176      */

177     public int itemsOnPage() throws SQLException JavaDoc
178     {
179         int itemsOnPage = itemsPerPage;
180         if (itemsOnPage == 0) {
181             itemsOnPage = numberOfItems();
182         }
183         return itemsOnPage;
184     }
185
186     /**
187      * Returns the index of the first item on the given page.
188      */

189     public int firstItem(Integer JavaDoc pageNo) throws SQLException JavaDoc
190     {
191         int itemsOnThisPage = itemsOnPage();
192         
193         int start = ((pageNo.intValue()-1) * itemsOnThisPage);
194         if (start >= numberOfItems()) {
195             start = 0;
196         }
197         return start;
198     }
199
200     /**
201      * Returns the index of the last item on the given page.
202      */

203     public int lastItem(Integer JavaDoc pageNo) throws SQLException JavaDoc
204     {
205         int finish = firstItem(pageNo) + itemsOnPage();
206         if (finish >= numberOfItems()) {
207             finish = numberOfItems();
208         }
209         return finish;
210     }
211
212 }
213
Popular Tags