KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > table > PagedTableModel


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.table;
14
15 /**
16  * a TableModel decorator that divides the table model into pages
17  */

18 class PagedTableModel extends TableModelDecorator {
19   private int pageSize = 10;
20   private int currentPage;
21   private boolean showAll = false;
22
23   public PagedTableModel() {
24   }
25
26   public PagedTableModel(TableModel model) {
27     super(model);
28   }
29
30   public int getRowCount() {
31     if (showAll)
32       return super.getRowCount();
33     validate();
34     int offs = currentPage * pageSize;
35     int rows = super.getRowCount();
36     if (offs + pageSize > rows)
37       return rows - offs;
38     return pageSize;
39   }
40
41   public TableRow getRow(int rowIndex) {
42     if (showAll)
43       return super.getRow(rowIndex);
44     validate();
45     return super.getRow(rowIndex + currentPage * pageSize);
46   }
47
48   public void setPageSize(int newPageSize) {
49     pageSize = newPageSize;
50     if (pageSize < 1)
51       pageSize = 1;
52   }
53
54   public int getPageSize() {
55     return pageSize;
56   }
57
58   public void setCurrentPage(int newCurrentPage) {
59     currentPage = newCurrentPage;
60   }
61
62   public int getCurrentPage() {
63     return currentPage;
64   }
65
66   public int getPageCount() {
67     int rc = super.getRowCount();
68     int pc = rc / pageSize;
69     if ((rc % pageSize) != 0)
70       pc += 1;
71     return pc;
72   }
73
74   void validate() {
75     int pageCount = getPageCount();
76     if (currentPage >= pageCount)
77       currentPage = pageCount - 1;
78     if (currentPage < 0)
79       currentPage = 0;
80   }
81
82   public void setShowAll(boolean newShowAll) {
83     showAll = newShowAll;
84   }
85
86   public boolean isShowAll() {
87     return showAll;
88   }
89
90   public void tableModelChanged(TableModelChangeEvent event) {
91     currentPage = 0;
92     validate();
93   }
94
95 }
Popular Tags