|
@@ -15,10 +15,10 @@ |
15 |
15 |
|
*/ |
16 |
16 |
|
package com.google.gwt.gen2.table.client; |
17 |
17 |
|
|
18 |
|
- |
import java.util.*; |
19 |
|
- |
|
20 |
18 |
|
import com.google.gwt.gen2.table.client.TableModelHelper.*; |
21 |
19 |
|
|
|
20 |
+ |
import java.util.*; |
|
21 |
+ |
|
22 |
22 |
|
/** |
23 |
23 |
|
* <p> |
24 |
24 |
|
* A {@link MutableTableModel} that wraps another {@link MutableTableModel} and |
|
@@ -56,14 +56,12 @@ |
56 |
56 |
|
* |
57 |
57 |
|
* @param <RowType> the data type of the row values |
58 |
58 |
|
*/ |
59 |
|
- |
public class CachedTableModel<RowType> extends MutableTableModel<RowType> |
60 |
|
- |
{ |
|
59 |
+ |
public class CachedTableModel<RowType> extends MutableTableModel<RowType> { |
61 |
60 |
|
/** |
62 |
61 |
|
* An intermediate callback that adds the response from the inner table model |
63 |
62 |
|
* into the cache, then responds to the original request. |
64 |
63 |
|
*/ |
65 |
|
- |
private class CacheCallback implements Callback<RowType> |
66 |
|
- |
{ |
|
64 |
+ |
private class CacheCallback implements Callback<RowType> { |
67 |
65 |
|
/** |
68 |
66 |
|
* The actual callback from the original requester. |
69 |
67 |
|
*/ |
|
@@ -92,30 +90,24 @@ |
92 |
90 |
|
* @param startRow The actual first requested row |
93 |
91 |
|
* @param numRows The actual number of requested rows |
94 |
92 |
|
*/ |
95 |
|
- |
public CacheCallback( Request request, Callback<RowType> callback, int startRow, int numRows ) |
96 |
|
- |
{ |
|
93 |
+ |
public CacheCallback( Request request, Callback<RowType> callback, int startRow, int numRows ) { |
97 |
94 |
|
actualRequest = request; |
98 |
95 |
|
actualCallback = callback; |
99 |
96 |
|
actualStartRow = startRow; |
100 |
97 |
|
actualNumRows = numRows; |
101 |
98 |
|
} |
102 |
99 |
|
|
103 |
|
- |
public void onFailure( Throwable caught ) |
104 |
|
- |
{ |
|
100 |
+ |
public void onFailure( Throwable caught ) { |
105 |
101 |
|
actualCallback.onFailure( caught ); |
106 |
102 |
|
} |
107 |
103 |
|
|
108 |
|
- |
public void onRowsReady( Request request, Response<RowType> response ) |
109 |
|
- |
{ |
|
104 |
+ |
public void onRowsReady( Request request, Response<RowType> response ) { |
110 |
105 |
|
// Save the response data into the cache |
111 |
|
- |
if ( response != null ) |
112 |
|
- |
{ |
|
106 |
+ |
if ( response != null ) { |
113 |
107 |
|
Iterator<RowType> rowValues = response.getRowValues(); |
114 |
|
- |
if ( rowValues != null ) |
115 |
|
- |
{ |
|
108 |
+ |
if ( rowValues != null ) { |
116 |
109 |
|
int curRow = request.getStartRow(); |
117 |
|
- |
while ( rowValues.hasNext() ) |
118 |
|
- |
{ |
|
110 |
+ |
while ( rowValues.hasNext() ) { |
119 |
111 |
|
rowValuesMap.put( new Integer( curRow ), rowValues.next() ); |
120 |
112 |
|
curRow++; |
121 |
113 |
|
} |
|
@@ -130,8 +122,7 @@ |
130 |
122 |
|
/** |
131 |
123 |
|
* An {@link Iterator} that iterates over the cached rows of data. |
132 |
124 |
|
*/ |
133 |
|
- |
private class CacheIterator implements Iterator<RowType> |
134 |
|
- |
{ |
|
125 |
+ |
private class CacheIterator implements Iterator<RowType> { |
135 |
126 |
|
/** |
136 |
127 |
|
* The current row that will be returned on the next call to next. |
137 |
128 |
|
*/ |
|
@@ -148,21 +139,17 @@ |
148 |
139 |
|
* @param firstRow the first row of data |
149 |
140 |
|
* @param lastRow the last row of data |
150 |
141 |
|
*/ |
151 |
|
- |
public CacheIterator( int firstRow, int lastRow ) |
152 |
|
- |
{ |
|
142 |
+ |
public CacheIterator( int firstRow, int lastRow ) { |
153 |
143 |
|
this.curRow = firstRow - 1; |
154 |
144 |
|
this.lastRow = lastRow; |
155 |
145 |
|
} |
156 |
146 |
|
|
157 |
|
- |
public boolean hasNext() |
158 |
|
- |
{ |
|
147 |
+ |
public boolean hasNext() { |
159 |
148 |
|
return curRow < lastRow && rowValuesMap.containsKey( new Integer( curRow + 1 ) ); |
160 |
149 |
|
} |
161 |
150 |
|
|
162 |
|
- |
public RowType next() |
163 |
|
- |
{ |
164 |
|
- |
if ( !hasNext() ) |
165 |
|
- |
{ |
|
151 |
+ |
public RowType next() { |
|
152 |
+ |
if ( !hasNext() ) { |
166 |
153 |
|
throw new NoSuchElementException(); |
167 |
154 |
|
} |
168 |
155 |
|
|
|
@@ -170,8 +157,7 @@ |
170 |
157 |
|
return rowValuesMap.get( new Integer( curRow ) ); |
171 |
158 |
|
} |
172 |
159 |
|
|
173 |
|
- |
public void remove() |
174 |
|
- |
{ |
|
160 |
+ |
public void remove() { |
175 |
161 |
|
throw new UnsupportedOperationException( "Remove not supported." ); |
176 |
162 |
|
} |
177 |
163 |
|
} |
|
@@ -179,21 +165,18 @@ |
179 |
165 |
|
/** |
180 |
166 |
|
* A Response that comes directly from the cache. |
181 |
167 |
|
*/ |
182 |
|
- |
private class CacheResponse extends Response<RowType> |
183 |
|
- |
{ |
|
168 |
+ |
private class CacheResponse extends Response<RowType> { |
184 |
169 |
|
/** |
185 |
170 |
|
* An iterator over the row values. |
186 |
171 |
|
*/ |
187 |
172 |
|
private CacheIterator it; |
188 |
173 |
|
|
189 |
|
- |
public CacheResponse( int firstRow, int lastRow ) |
190 |
|
- |
{ |
|
174 |
+ |
public CacheResponse( int firstRow, int lastRow ) { |
191 |
175 |
|
it = new CacheIterator( firstRow, lastRow ); |
192 |
176 |
|
} |
193 |
177 |
|
|
194 |
178 |
|
@Override |
195 |
|
- |
public Iterator<RowType> getRowValues() |
196 |
|
- |
{ |
|
179 |
+ |
public Iterator<RowType> getRowValues() { |
197 |
180 |
|
return it; |
198 |
181 |
|
} |
199 |
182 |
|
} |
|
@@ -228,56 +211,46 @@ |
228 |
211 |
|
* |
229 |
212 |
|
* @param tableModel the underlying {@link MutableTableModel} |
230 |
213 |
|
*/ |
231 |
|
- |
public CachedTableModel( MutableTableModel<RowType> tableModel ) |
232 |
|
- |
{ |
|
214 |
+ |
public CachedTableModel( MutableTableModel<RowType> tableModel ) { |
233 |
215 |
|
this.tableModel = tableModel; |
234 |
216 |
|
} |
235 |
217 |
|
|
236 |
218 |
|
/** |
237 |
219 |
|
* Clear all data from the cache. |
238 |
220 |
|
*/ |
239 |
|
- |
public void clearCache() |
240 |
|
- |
{ |
|
221 |
+ |
public void clearCache() { |
241 |
222 |
|
rowValuesMap.clear(); |
242 |
223 |
|
} |
243 |
224 |
|
|
244 |
225 |
|
/** |
245 |
226 |
|
* @return the number of rows to cache after the requested rows |
246 |
227 |
|
*/ |
247 |
|
- |
public int getPostCachedRowCount() |
248 |
|
- |
{ |
|
228 |
+ |
public int getPostCachedRowCount() { |
249 |
229 |
|
return postCacheRows; |
250 |
230 |
|
} |
251 |
231 |
|
|
252 |
232 |
|
/** |
253 |
233 |
|
* @return the number of rows to cache before the requested rows |
254 |
234 |
|
*/ |
255 |
|
- |
public int getPreCachedRowCount() |
256 |
|
- |
{ |
|
235 |
+ |
public int getPreCachedRowCount() { |
257 |
236 |
|
return preCacheRows; |
258 |
237 |
|
} |
259 |
238 |
|
|
260 |
239 |
|
@Override |
261 |
|
- |
public int getRowCount() |
262 |
|
- |
{ |
|
240 |
+ |
public int getRowCount() { |
263 |
241 |
|
return tableModel.getRowCount(); |
264 |
242 |
|
} |
265 |
243 |
|
|
266 |
244 |
|
@Override |
267 |
|
- |
public void requestRows( Request request, Callback<RowType> callback ) |
268 |
|
- |
{ |
|
245 |
+ |
public void requestRows( Request request, Callback<RowType> callback ) { |
269 |
246 |
|
// Clear the cache if the sort order has changed |
270 |
247 |
|
ColumnSortList sortList = request.getColumnSortList(); |
271 |
|
- |
if ( sortList == null ) |
272 |
|
- |
{ |
273 |
|
- |
if ( lastSortList != null ) |
274 |
|
- |
{ |
|
248 |
+ |
if ( sortList == null ) { |
|
249 |
+ |
if ( lastSortList != null ) { |
275 |
250 |
|
clearCache(); |
276 |
251 |
|
lastSortList = null; |
277 |
252 |
|
} |
278 |
|
- |
} |
279 |
|
- |
else if ( !sortList.equals( lastSortList ) ) |
280 |
|
- |
{ |
|
253 |
+ |
} else if ( !sortList.equals( lastSortList ) ) { |
281 |
254 |
|
clearCache(); |
282 |
255 |
|
lastSortList = sortList.copy(); |
283 |
256 |
|
} |
|
@@ -287,23 +260,19 @@ |
287 |
260 |
|
int numRows = request.getNumRows(); |
288 |
261 |
|
int lastRow = startRow + numRows - 1; |
289 |
262 |
|
int totalNumRows = getRowCount(); |
290 |
|
- |
if ( totalNumRows != UNKNOWN_ROW_COUNT ) |
291 |
|
- |
{ |
|
263 |
+ |
if ( totalNumRows != UNKNOWN_ROW_COUNT ) { |
292 |
264 |
|
lastRow = Math.min( lastRow, totalNumRows - 1 ); |
293 |
265 |
|
} |
294 |
266 |
|
boolean fullyCached = true; |
295 |
|
- |
for ( int row = startRow; row <= lastRow; row++ ) |
296 |
|
- |
{ |
297 |
|
- |
if ( !rowValuesMap.containsKey( new Integer( row ) ) ) |
298 |
|
- |
{ |
|
267 |
+ |
for ( int row = startRow; row <= lastRow; row++ ) { |
|
268 |
+ |
if ( !rowValuesMap.containsKey( new Integer( row ) ) ) { |
299 |
269 |
|
fullyCached = false; |
300 |
270 |
|
break; |
301 |
271 |
|
} |
302 |
272 |
|
} |
303 |
273 |
|
|
304 |
274 |
|
// Return the fully cached data |
305 |
|
- |
if ( fullyCached ) |
306 |
|
- |
{ |
|
275 |
+ |
if ( fullyCached ) { |
307 |
276 |
|
callback.onRowsReady( request, new CacheResponse( startRow, lastRow ) ); |
308 |
277 |
|
return; |
309 |
278 |
|
} |
|
@@ -313,35 +282,26 @@ |
313 |
282 |
|
int uncachedLastRow = lastRow + postCacheRows; |
314 |
283 |
|
|
315 |
284 |
|
// Check the upper bounds against the total number of rows |
316 |
|
- |
if ( totalNumRows != UNKNOWN_ROW_COUNT ) |
317 |
|
- |
{ |
|
285 |
+ |
if ( totalNumRows != UNKNOWN_ROW_COUNT ) { |
318 |
286 |
|
lastRow = Math.min( totalNumRows - 1, lastRow ); |
319 |
287 |
|
uncachedLastRow = Math.min( totalNumRows - 1, uncachedLastRow ); |
320 |
288 |
|
} |
321 |
289 |
|
|
322 |
290 |
|
// Skip past any data already retrieved starting at the first row |
323 |
|
- |
for ( int row = uncachedFirstRow; row <= lastRow; row++ ) |
324 |
|
- |
{ |
325 |
|
- |
if ( rowValuesMap.containsKey( new Integer( row ) ) ) |
326 |
|
- |
{ |
|
291 |
+ |
for ( int row = uncachedFirstRow; row <= lastRow; row++ ) { |
|
292 |
+ |
if ( rowValuesMap.containsKey( new Integer( row ) ) ) { |
327 |
293 |
|
uncachedFirstRow++; |
328 |
|
- |
} |
329 |
|
- |
else |
330 |
|
- |
{ |
|
294 |
+ |
} else { |
331 |
295 |
|
// Need to request the remaining rows |
332 |
296 |
|
break; |
333 |
297 |
|
} |
334 |
298 |
|
} |
335 |
299 |
|
|
336 |
300 |
|
// Skip past any data already retrieved from the last row |
337 |
|
- |
for ( int row = uncachedLastRow; row >= startRow; row-- ) |
338 |
|
- |
{ |
339 |
|
- |
if ( rowValuesMap.containsKey( new Integer( row ) ) ) |
340 |
|
- |
{ |
|
301 |
+ |
for ( int row = uncachedLastRow; row >= startRow; row-- ) { |
|
302 |
+ |
if ( rowValuesMap.containsKey( new Integer( row ) ) ) { |
341 |
303 |
|
uncachedLastRow--; |
342 |
|
- |
} |
343 |
|
- |
else |
344 |
|
- |
{ |
|
304 |
+ |
} else { |
345 |
305 |
|
// Need to request the remaining rows |
346 |
306 |
|
break; |
347 |
307 |
|
} |
|
@@ -358,8 +318,7 @@ |
358 |
318 |
|
* |
359 |
319 |
|
* @param postCacheRows the number of rows to post cache |
360 |
320 |
|
*/ |
361 |
|
- |
public void setPostCachedRowCount( int postCacheRows ) |
362 |
|
- |
{ |
|
321 |
+ |
public void setPostCachedRowCount( int postCacheRows ) { |
363 |
322 |
|
this.postCacheRows = postCacheRows; |
364 |
323 |
|
} |
365 |
324 |
|
|
|
@@ -368,35 +327,30 @@ |
368 |
327 |
|
* |
369 |
328 |
|
* @param preCacheRows the number of rows to pre cache |
370 |
329 |
|
*/ |
371 |
|
- |
public void setPreCachedRowCount( int preCacheRows ) |
372 |
|
- |
{ |
|
330 |
+ |
public void setPreCachedRowCount( int preCacheRows ) { |
373 |
331 |
|
this.preCacheRows = preCacheRows; |
374 |
332 |
|
} |
375 |
333 |
|
|
376 |
334 |
|
@Override |
377 |
|
- |
public void setRowCount( int rowCount ) |
378 |
|
- |
{ |
|
335 |
+ |
public void setRowCount( int rowCount ) { |
379 |
336 |
|
tableModel.setRowCount( rowCount ); |
380 |
337 |
|
super.setRowCount( rowCount ); |
381 |
338 |
|
} |
382 |
339 |
|
|
383 |
340 |
|
@Override |
384 |
|
- |
protected boolean onRowInserted( int beforeRow ) |
385 |
|
- |
{ |
|
341 |
+ |
protected boolean onRowInserted( int beforeRow ) { |
386 |
342 |
|
clearCache(); |
387 |
343 |
|
return tableModel.onRowInserted( beforeRow ); |
388 |
344 |
|
} |
389 |
345 |
|
|
390 |
346 |
|
@Override |
391 |
|
- |
protected boolean onRowRemoved( int row ) |
392 |
|
- |
{ |
|
347 |
+ |
protected boolean onRowRemoved( int row ) { |
393 |
348 |
|
clearCache(); |
394 |
349 |
|
return tableModel.onRowRemoved( row ); |
395 |
350 |
|
} |
396 |
351 |
|
|
397 |
352 |
|
@Override |
398 |
|
- |
protected boolean onSetRowValue( int row, RowType rowValue ) |
399 |
|
- |
{ |
|
353 |
+ |
protected boolean onSetRowValue( int row, RowType rowValue ) { |
400 |
354 |
|
rowValuesMap.put( new Integer( row ), rowValue ); |
401 |
355 |
|
return tableModel.onSetRowValue( row, rowValue ); |
402 |
356 |
|
} |