View Javadoc

1   /*
2    * Copyright 2006 Robert Hanson <iamroberthanson AT gmail.com>
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.gwtwidgets.client.ui.pagination;
18  
19  import java.util.List;
20  
21  import com.google.gwt.user.client.DOM;
22  import com.google.gwt.user.client.rpc.AsyncCallback;
23  import com.google.gwt.user.client.ui.FlexTable;
24  import com.google.gwt.user.client.ui.Widget;
25  
26  /***
27   * Pagination is a wrapper for FlexTable that allows easy support for paging
28   * through data.
29   * 
30   * NOTE: In case your wondering, Pagination takes a FlexTable as a parameter to
31   * abstract its logic from the view. Pagination (the term in general) is more of
32   * a logic implementation than a UI control, it controls a table, hence the
33   * current implementation. You can keep your UI construction in a completely
34   * separate class to keep the MVC paradigm architecture.
35   * 
36   * h3>CSS Style Rules</h3>
37   * <ul class="css">
38   * <li>.oddRow { }</li>
39   * <li>.evenRow { }</li>
40   * <li>.oddCell { }</li>
41   * <li>.evenCell { }</li>
42   * </ul>
43   * 
44   * @author Joe Toth (joetoth@gmail.com)
45   * 
46   */
47  abstract public class PaginationBehavior {
48  
49  	PaginationParameters parameters;
50  
51  	Results results;
52  
53  	int rowCount;
54  
55  	int resultsPerPage;
56  
57  	int page;
58  
59  	FlexTable table;
60  
61  	public PaginationBehavior(FlexTable table, int resultsPerPage) {
62  		table.clear();
63  		this.table = table;
64  		this.resultsPerPage = resultsPerPage;
65  	}
66  
67  	public void showPage(int page, String parameter, boolean isAscending) {
68  		this.page = page;
69  		this.parameters = new PaginationParameters();
70  		this.parameters.setAscending(isAscending);
71  		this.parameters.setMaxResults(resultsPerPage);
72  		this.parameters.setParameter(parameter);
73  
74  		int firstResults = 0;
75  		if (page > 1) {
76  			firstResults = (page - 1) * resultsPerPage;
77  		}
78  		this.parameters.setOffset(firstResults);
79  
80  		getDataProvider().update(this.parameters, new AsyncCallback() {
81  
82  			public void onSuccess(Object result) {
83  
84  				if (!(result instanceof Results)) {
85  					throw new RuntimeException(
86  							"result from DataProvider must be of type Results");
87  				}
88  
89  				int rowCount = table.getRowCount();
90  				for (int i = 0; i < rowCount; i++) {
91  					table.removeRow(0);
92  				}
93  
94  				results = (Results) result;
95  
96  				List resultsList = results.getList();
97  
98  				rowCount = resultsList.size();
99  				if (rowCount > resultsPerPage) {
100 					rowCount = resultsPerPage;
101 				}
102 
103 				for (int i = 0; i < rowCount; i++) {
104 					getRowRenderer().populateRow(PaginationBehavior.this, i,
105 							resultsList.get(i));
106 
107 					// Set style of row
108 					DOM.setAttribute(
109 							getTable().getRowFormatter().getElement(i),
110 							"className", (i % 2 == 1 ? "evenRow" : "oddRow"));
111 				}
112 
113 				onUpdateSuccess(result);
114 			}
115 
116 			public void onFailure(Throwable caught) {
117 				onUpdateFailure(caught);
118 			}
119 
120 		});
121 	}
122 
123 	public void setCell(int row, int column, Widget widget) {
124 		table.setWidget(row, column, widget);
125 
126 		// Set style of cell
127 		DOM.setAttribute(getTable().getCellFormatter().getElement(row, column),
128 				"className", (row % 2 == 1 ? "evenCell" : "oddCell"));
129 	}
130 
131 	protected void onUpdateSuccess(Object result) {
132 
133 	}
134 
135 	protected void onUpdateFailure(Throwable caught) {
136 		throw new RuntimeException(caught);
137 	}
138 
139 	abstract protected RowRenderer getRowRenderer();
140 
141 	abstract protected DataProvider getDataProvider();
142 
143 	public FlexTable getTable() {
144 		return table;
145 	}
146 
147 	public int getRowCount() {
148 		return rowCount;
149 	}
150 
151 	public int getResultsPerPage() {
152 		return resultsPerPage;
153 	}
154 
155 	public PaginationParameters getParameters() {
156 		return parameters;
157 	}
158 
159 	public int getPage() {
160 		return page;
161 	}
162 
163 	public Results getResults() {
164 		return results;
165 	}
166 
167 }