1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.common.ui.client.widgets.pagetable;
17
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.Comparator;
21 import java.util.Iterator;
22 import java.util.List;
23
24 import org.kuali.student.common.dto.Idable;
25 import org.kuali.student.common.ui.client.widgets.searchtable.ResultRow;
26 import org.kuali.student.common.ui.client.widgets.searchtable.SearchColumnDefinition;
27
28 import com.google.gwt.gen2.table.client.AbstractColumnDefinition;
29 import com.google.gwt.gen2.table.client.CachedTableModel;
30 import com.google.gwt.gen2.table.client.MutableTableModel;
31 import com.google.gwt.gen2.table.client.TableModelHelper.Request;
32 import com.google.gwt.gen2.table.client.TableModelHelper.Response;
33 import com.google.gwt.gen2.table.client.TableModelHelper.SerializableResponse;
34
35
36
37
38
39
40
41
42
43 public class GenericTableModel<RowType> extends MutableTableModel<RowType> {
44 private List<RowType> rowDTOs = null;
45 private int fromIndex = 0;
46 private int lastSortedColumn = -1;
47 private int lastSortDirection = 1;
48
49
50 private List<AbstractColumnDefinition<ResultRow, ?>> columnDefs;
51
52
53
54
55 private boolean errorMode = false;
56
57
58
59 private boolean zeroMode = false;
60
61
62
63
64 public GenericTableModel() {
65 super();
66 }
67
68
69
70
71 public GenericTableModel(List<RowType>rows) {
72 super();
73 setRows(rows);
74 }
75
76
77
78
79 public CachedTableModel<RowType>createCachedTableModel(int pageSize,int pages) {
80 CachedTableModel<RowType>cachedTableModel = new CachedTableModel<RowType>(this);
81 cachedTableModel.setPreCachedRowCount(pageSize);
82 cachedTableModel.setPostCachedRowCount(pageSize);
83 cachedTableModel.setRowCount(pageSize * pages);
84 return cachedTableModel;
85 }
86
87
88
89
90
91 @Override
92 protected boolean onRowInserted(int beforeRow) {
93 return true;
94 }
95
96
97
98
99
100
101 @Override
102 protected boolean onRowRemoved(int row) {
103 return true;
104 }
105
106
107
108
109
110
111 @Override
112 protected boolean onSetRowValue(int row, RowType rowValue) {
113 return true;
114 }
115
116
117
118
119
120
121
122
123 @Override
124 public void requestRows(final Request request, final Callback<RowType> callback) {
125 if (errorMode) {
126
127 callback.onFailure(new Exception("An error has occured."));
128 } else if (zeroMode) {
129
130 List<RowType> selectedRows = new ArrayList<RowType>(0);
131 callback.onRowsReady(request, new SerializableResponse(selectedRows));
132 } else {
133 callback.onRowsReady(request, new Response<RowType>() {
134
135 @SuppressWarnings("unchecked")
136 @Override
137 public Iterator<RowType> getRowValues() {
138
139
140 final int sortColumn = request.getColumnSortList().getPrimaryColumn();
141 final int sortDirection = (request.getColumnSortList().isPrimaryAscending() ? 1 : -1);
142 if (sortColumn>=0 && (sortColumn != lastSortedColumn || sortDirection != lastSortDirection)) {
143 SearchColumnDefinition rowDef = (SearchColumnDefinition) columnDefs.get(sortColumn);
144 final String sortColumKey = rowDef.getColumnKey();
145 Collections.sort((List<ResultRow>)rowDTOs, new Comparator<ResultRow>() {
146
147 @Override
148 public int compare(ResultRow o1, ResultRow o2) {
149 return o1.getValue(sortColumKey).compareToIgnoreCase(o2.getValue(sortColumKey)) * sortDirection;
150 }});
151 lastSortedColumn = sortColumn;
152 lastSortDirection = sortDirection;
153 }
154 int numRows = request.getNumRows();
155
156 if((fromIndex + numRows) >= rowDTOs.size()) {
157 fromIndex = 0;
158 }
159 int toIndex = fromIndex + numRows;
160 List<RowType> selectedRows = new ArrayList<RowType>(numRows);
161 for(int i = fromIndex; i < toIndex; i++) {
162 RowType e = rowDTOs.get(i);
163 selectedRows.add(e);
164 }
165
166 fromIndex = toIndex;
167 return new SerializableResponse(selectedRows).getRowValues();
168 }
169 });
170 }
171 }
172
173
174
175
176 public void setRows(List<RowType> rows) {
177
178 List<Idable> ids = (List<Idable>)rows;
179 this.rowDTOs = rows;
180 }
181
182
183
184
185
186 @Override
187 public int getRowCount() {
188
189 return rowDTOs.size();
190 }
191
192
193
194 public boolean isErrorMode() {
195 return errorMode;
196 }
197
198
199
200 public void setErrorMode(boolean errorMode) {
201 this.errorMode = errorMode;
202 }
203
204
205
206 public boolean isZeroMode() {
207 return zeroMode;
208 }
209
210
211
212 public void setZeroMode(boolean zeroMode) {
213 this.zeroMode = zeroMode;
214 }
215
216 public void setColumnDefs(List<AbstractColumnDefinition<ResultRow, ?>> columnDefs) {
217 this.columnDefs = columnDefs;
218 }
219 }