KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > table > Pager


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.table;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Comparator JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.regex.Matcher JavaDoc;
28 import java.util.regex.Pattern JavaDoc;
29
30 import com.sslexplorer.boot.Util;
31
32 /**
33  * Provides an additional layer over a
34  * {@link com.sslexplorer.table.TableItemModel} that allows the items within the
35  * model to be displayed a page at a time.
36  * <p>
37  * The pager also deals with the sorting of the displayed items as well as
38  * filtering, leaving the contents of the model alone.
39  *
40  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
41  */

42 public class Pager {
43
44     private TableItemModel model;
45     private int pageSize;
46     private int startRow;
47     private String JavaDoc sortName;
48     private boolean sortReverse;
49     private List JavaDoc filteredList;
50     private boolean sorts = true;
51
52     /**
53      * Constructor
54      *
55      * @param model table model to page
56      */

57     public Pager(TableItemModel model) {
58         super();
59         this.model = model;
60         pageSize = 10;
61         startRow = 0;
62         filteredList = null;
63     }
64
65     /**
66      * Get whether this pager sorts
67      *
68      * @return sorts
69      */

70     public boolean isSorts() {
71         return sorts;
72     }
73
74     /**
75      * Set whether this pager sorts
76      *
77      * @param sorts pager sorts
78      */

79     public void setSorts(boolean sorts) {
80         this.sorts = sorts;
81     }
82
83     /**
84      * Get the number of items
85      *
86      * @return the number of rows after the filter has been applied
87      */

88     public int getFilteredRowCount() {
89         return filteredList == null ? 0 : filteredList.size();
90     }
91
92     /**
93      * Get an item given its filtered index
94      *
95      * @param row filtered row
96      * @return the item at the row
97      */

98     public TableItem getFilteredItem(int row) {
99         return (TableItem) filteredList.get(row);
100     }
101
102     /**
103      * Get if there are no rows after the filter has been applied.
104      *
105      * @return empty table
106      */

107     public boolean getEmpty() {
108         return getFilteredRowCount() == 0;
109     }
110
111     /**
112      * Set the number of items on each page
113      *
114      * @param pageSize items per page
115      */

116     public void setPageSize(int pageSize) {
117         this.pageSize = pageSize;
118     }
119
120     /**
121      * Get the number of items on each page
122      *
123      * @return number of items on each page
124      */

125     public int getPageSize() {
126         return pageSize;
127     }
128
129     /**
130      * Create an {@link Iterator} for all the items on the current page.
131      *
132      * @return iterator of items for the current page
133      */

134     public Iterator JavaDoc getPageItems() {
135         return new ItemIterator(startRow);
136     }
137
138     /**
139      * Get if there are any more pages after the current one
140      *
141      * @return more pages available
142      */

143     public boolean getHasNextPage() {
144         boolean hasNextPage = (startRow + calcPageSize()) < getFilteredRowCount();
145         return hasNextPage;
146     }
147
148     /**
149      * Get if there are any pages before the current one
150      *
151      * @return previous pages available
152      */

153     public boolean getHasPreviousPage() {
154         boolean hasNextPage = (startRow - calcPageSize()) >= 0;
155         return hasNextPage;
156     }
157
158     /**
159      * Get the index the current page starts at
160      *
161      * @return start index of current page
162      */

163     public int getStartRow() {
164         return startRow;
165     }
166
167     /**
168      * Set the index the current page starts at
169      *
170      * @param startRow start row
171      */

172     public void setStartRow(int startRow) {
173         this.startRow = startRow;
174     }
175
176     /**
177      * Move on to the next page
178      */

179     public void nextPage() {
180         if (!getHasNextPage()) {
181             throw new IllegalArgumentException JavaDoc("No more pages.");
182         }
183         setStartRow(startRow + calcPageSize());
184     }
185
186     /**
187      * Move back to the previous page
188      */

189     public void previousPage() {
190         if (!getHasPreviousPage()) {
191             throw new IllegalArgumentException JavaDoc("No more pages.");
192         }
193         setStartRow(startRow - calcPageSize());
194     }
195
196     /**
197      * Get the model this pager overlays.
198      *
199      * @return model
200      */

201     public TableItemModel getModel() {
202         return model;
203     }
204
205     /**
206      * Get the name of the field that is currently being used for sorting
207      *
208      * @return name of sort field
209      */

210     public String JavaDoc getSortName() {
211         return sortName;
212     }
213
214     /**
215      * Get whether the pager is currently sorting in reverse
216      *
217      * @return sort in reverse
218      */

219     public boolean getSortReverse() {
220         return sortReverse;
221     }
222
223     /**
224      * Set the name of the field that is currenting being used for sorting. You
225      * must call {@link #rebuild(String)} to do the actual filter / sort.
226      *
227      * @param sortName name of sort field
228      */

229     public void setSortName(String JavaDoc sortName) {
230         this.sortName = sortName;
231     }
232
233     /**
234      * Set whether the current sort should be in reverse order. You must call
235      * {@link #rebuild(String)} to do the actual filter / sort.
236      *
237      * @param sortReverse sort in reverse order
238      */

239     public void setSortReverse(boolean sortReverse) {
240         this.sortReverse = sortReverse;
241     }
242
243     /**
244      * Rebuild the pager using the specified filter text. All items will also be
245      * re-sorted based on the current criteria.
246      *
247      * @param filterText
248      */

249     public void rebuild(String JavaDoc filterText) {
250         if (filterText == null || filterText.equals("") || filterText.equals("*")) {
251             filteredList = new ArrayList JavaDoc(model.getItems());
252         } else {
253             filteredList = new ArrayList JavaDoc();
254             for (Iterator JavaDoc i = model.getItems().iterator(); i.hasNext();) {
255                 TableItem ti = (TableItem) i.next();
256                 if (matchedFilter(ti, filterText)) {
257                     filteredList.add(ti);
258                 }
259             }
260         }
261         if (isSorts()) {
262             if (sortName == null || sortName.equals("")) {
263                 sortName = model.getId() + "." + model.getColumnName(0);
264             }
265             if (sortName != null && !sortName.equals("")) {
266                 for (int i = model.getColumnCount() - 1; i >= 0; i--) {
267                     if (model.getColumnName(i).equals(sortName)) {
268                         Collections.sort(filteredList, new TableItemComparator(i));
269                         return;
270                     }
271                 }
272             }
273         }
274     }
275
276     /**
277      * Move to the first page
278      */

279     public void firstPage() {
280         setStartRow(0);
281     }
282
283     /**
284      * Move to the last page
285      */

286     public void lastPage() {
287         int lastPageRow = getFilteredRowCount() - calcPageSize();
288         setStartRow(lastPageRow < 0 ? 0 : lastPageRow);
289     }
290
291     /**
292      * Calculate the actual page size taking into account if the 'all' page size
293      * is set (in which case the total filtered rows will be returned)
294      *
295      * @return page size
296      */

297     public int calcPageSize() {
298         return (pageSize == 0 ? getFilteredRowCount() : pageSize);
299     }
300
301     boolean matchedFilter(TableItem item, String JavaDoc filterText) {
302         if (filterText == null || filterText.equals("")) {
303             return true;
304         } else {
305             if (filterText.startsWith("!")) {
306                 filterText = filterText.substring(1);
307                 Pattern JavaDoc p = Pattern.compile(Util.parseSimplePatternToRegExp(filterText), Pattern.CASE_INSENSITIVE);
308                 for (int i = 0; i < model.getColumnCount(); i++) {
309                     Object JavaDoc cv = item.getColumnValue(i);
310                     String JavaDoc val = cv == null ? "" : cv.toString();
311                     Matcher JavaDoc matcher = p.matcher(val);
312                     if (matcher.matches()) {
313                         return false;
314                     }
315                 }
316             } else {
317                 Pattern JavaDoc p = Pattern.compile(Util.parseSimplePatternToRegExp(filterText), Pattern.CASE_INSENSITIVE);
318                 for (int i = 0; i < model.getColumnCount(); i++) {
319                     Object JavaDoc cv = item.getColumnValue(i);
320                     String JavaDoc val = cv == null ? "" : cv.toString();
321                     Matcher JavaDoc matcher = p.matcher(val);
322                     if (matcher.matches()) {
323                         return true;
324                     }
325                 }
326             }
327         }
328         return false;
329     }
330
331     class TableItemComparator implements Comparator JavaDoc {
332
333         int col;
334
335         TableItemComparator(int col) {
336             this.col = col;
337         }
338
339         public int compare(Object JavaDoc arg0, Object JavaDoc arg1) {
340             return (sortReverse ? -1 : 1)
341                 * ((Comparable JavaDoc) ((TableItem) arg0).getColumnValue(col)).compareTo((Comparable JavaDoc) ((TableItem) arg1).getColumnValue(col));
342         }
343     }
344
345     class ItemIterator implements Iterator JavaDoc {
346
347         int idx;
348         int end;
349
350         ItemIterator(int idx) {
351             this.idx = idx;
352             this.end = idx + calcPageSize();
353         }
354
355         public boolean hasNext() {
356             return idx < getFilteredRowCount() && idx < end;
357         }
358
359         public Object JavaDoc next() {
360             return filteredList.get(idx++);
361         }
362
363         public void remove() {
364         }
365
366     }
367 }
368
Popular Tags