1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.gwtwidgets.client.ui.pagination;
18
19 import com.google.gwt.user.client.ui.ClickListener;
20 import com.google.gwt.user.client.ui.FlexTable;
21 import com.google.gwt.user.client.ui.Hyperlink;
22 import com.google.gwt.user.client.ui.Widget;
23
24 /***
25 * Default Pagination Behavior
26 *
27 * <h3>CSS Style Rules</h3>
28 * <ul class="css">
29 * <li>.oddRow { }</li>
30 * <li>.evenRow { }</li>
31 * <li>.oddCell { }</li>
32 * <li>.evenCell { }</li>
33 * <li>.headerRow { }</li>
34 * <li>.headerCell { }</li>
35 * <li>.pagingRow { }</li>
36 * <li>.noPreviousPageCell { }</li>
37 * <li>.previousPageCell { }</li>
38 * <li>.pageCell { }</li>
39 * <li>.currentPageCell { }</li>
40 * <li>.noNextPageCell { }</li>
41 * <li>.nextPageCell { }</li>
42 * </ul>
43 *
44 * @author Joe Toth (joetoth@gmail.com)
45 *
46 */
47 abstract public class DefaultPaginationBehavior {
48
49 private FlexTable[] pagingControls;
50
51 private SortablePaginationBehavior pagination;
52
53 private String nextPageText = "Next »";
54
55 private String previousPageText = "« Previous";
56
57 private int maxPageLinks = 11;
58
59 public DefaultPaginationBehavior(FlexTable pagingControl,
60 FlexTable resultsTable, int resultsPerPage) {
61 this(new FlexTable[] { pagingControl }, resultsTable, resultsPerPage);
62 }
63
64 public DefaultPaginationBehavior(FlexTable[] pagingControls,
65 FlexTable resultsTable, int resultsPerPage) {
66 this.pagingControls = pagingControls;
67 pagination = new SortablePaginationBehavior(resultsTable,
68 resultsPerPage) {
69
70 protected RowRenderer getRowRenderer() {
71 return DefaultPaginationBehavior.this.getRowRenderer();
72 }
73
74 protected DataProvider getDataProvider() {
75 return DefaultPaginationBehavior.this.getDataProvider();
76 }
77
78 protected Column[] getColumns() {
79 return DefaultPaginationBehavior.this.getColumns();
80 }
81
82 protected void onUpdateSuccess(Object result) {
83 updatePagingControls();
84 DefaultPaginationBehavior.this.onUpdateSuccess(result);
85 }
86
87 protected void onUpdateFailure(Throwable caught) {
88 DefaultPaginationBehavior.this.onUpdateFailure(caught);
89 }
90
91 };
92
93 updatePagingControls();
94 }
95
96 private void updatePagingControls() {
97 for (int i = 0; i < pagingControls.length; i++) {
98 if (pagingControls[i].getRowCount() > 0) {
99 pagingControls[i].removeRow(0);
100 }
101 pagingControls[i].insertRow(0);
102
103 if (pagination.getResults() != null) {
104
105 int totalPages = pagination.getResults().getSize()
106 / pagination.getResultsPerPage();
107 if (pagination.getResults().getSize()
108 % pagination.getResultsPerPage() != 0) {
109 totalPages++;
110 }
111
112
113 Hyperlink previousPageLink = new Hyperlink(previousPageText,
114 "previousPage");
115 pagingControls[i].setWidget(0, 0, previousPageLink);
116 if (pagination.getPage() == 1
117 || pagination.getResults().getSize() == 0) {
118 pagingControls[i].getCellFormatter().addStyleName(0, 0,
119 "noPreviousPageCell");
120 } else {
121 previousPageLink.addClickListener(new ClickListener() {
122 public void onClick(Widget sender) {
123 pagination.showPage(pagination.getPage() - 1);
124 }
125 });
126 pagingControls[i].getCellFormatter().addStyleName(0, 0,
127 "previousPageCell");
128 }
129
130
131 int currentPageForCell = 1;
132 int totalCells = totalPages;
133 if (totalPages > maxPageLinks) {
134 totalCells = maxPageLinks;
135
136 if (pagination.getPage() > maxPageLinks / 2) {
137 currentPageForCell = pagination.getPage()
138 - (maxPageLinks / 2);
139 if (pagination.getPage() >= totalPages
140 - (maxPageLinks / 2)) {
141 currentPageForCell = totalPages
142 - (maxPageLinks - 1);
143 }
144 }
145 }
146
147 int cellNumber = 1;
148 for (; cellNumber <= totalCells; cellNumber++) {
149 Hyperlink pageLink = new Hyperlink(currentPageForCell + "",
150 "page");
151 pagingControls[i].setWidget(0, cellNumber, pageLink);
152 final int tmp = currentPageForCell;
153 if (pagination.getPage() != currentPageForCell) {
154 pageLink.addClickListener(new ClickListener() {
155 public void onClick(Widget sender) {
156 pagination.showPage(tmp);
157 }
158 });
159 pagingControls[i].getCellFormatter().addStyleName(0,
160 cellNumber, "pageCell");
161 } else {
162 pagingControls[i].getCellFormatter().addStyleName(0,
163 cellNumber, "currentPageCell");
164 }
165 currentPageForCell++;
166 }
167
168
169 Hyperlink nextPageLink = new Hyperlink(nextPageText, "nextPage");
170 pagingControls[i].setWidget(0, cellNumber, nextPageLink);
171 if (pagination.getPage() == totalPages
172 || pagination.getResults().getSize() == 0) {
173 pagingControls[i].getCellFormatter().addStyleName(0,
174 cellNumber, "noNextPageCell");
175 } else {
176 nextPageLink.addClickListener(new ClickListener() {
177 public void onClick(Widget sender) {
178 pagination.showPage(pagination.getPage() + 1);
179 }
180 });
181
182 pagingControls[i].getCellFormatter().addStyleName(0,
183 cellNumber, "nextPageCell");
184 }
185
186 }
187 }
188 }
189
190 public void setCell(int row, int column, Widget widget) {
191 pagination.setCell(row, column, widget);
192 }
193
194 public void showPage(int pageNumber, String orderByProperty,
195 boolean isAscending) {
196 pagination.showPage(pageNumber, orderByProperty, isAscending);
197 }
198
199 abstract protected DataProvider getDataProvider();
200
201 abstract protected RowRenderer getRowRenderer();
202
203 abstract protected Column[] getColumns();
204
205 protected void onUpdateSuccess(Object result) {
206
207 }
208
209 protected void onUpdateFailure(Throwable caught) {
210 throw new RuntimeException(caught);
211 }
212
213 public int getRowCount() {
214 return pagination.getRowCount();
215 }
216
217 public PaginationParameters getParameters() {
218 return pagination.getParameters();
219 }
220
221 public int getResultsPerPage() {
222 return pagination.getResultsPerPage();
223 }
224
225 public int getPage() {
226 return pagination.getPage();
227 }
228
229 public Results getResults() {
230 return pagination.getResults();
231 }
232
233 public String getNextPageText() {
234 return nextPageText;
235 }
236
237 public void setNextPageText(String nextPageText) {
238 this.nextPageText = nextPageText;
239 }
240
241 public String getPreviousPageText() {
242 return previousPageText;
243 }
244
245 public void setPreviousPageText(String previousPageText) {
246 this.previousPageText = previousPageText;
247 }
248
249 public int getMaxPageLinks() {
250 return maxPageLinks;
251 }
252
253 public void setMaxPageLinks(int maxPageLinks) {
254 this.maxPageLinks = maxPageLinks;
255 }
256
257 }