|
@@ -15,121 +15,139 @@ |
15 |
15 |
|
*/ |
16 |
16 |
|
package com.google.gwt.gen2.table.client; |
17 |
17 |
|
|
18 |
|
- |
import com.google.gwt.gen2.table.client.TableModelHelper.Request; |
19 |
|
- |
import com.google.gwt.gen2.table.client.TableModelHelper.Response; |
|
18 |
+ |
import java.util.*; |
20 |
19 |
|
|
21 |
|
- |
import java.util.Iterator; |
22 |
|
- |
import java.util.List; |
23 |
|
- |
import java.util.NoSuchElementException; |
|
20 |
+ |
import com.google.gwt.gen2.table.client.TableModelHelper.*; |
24 |
21 |
|
|
25 |
22 |
|
/** |
26 |
23 |
|
* A {@link ClientTableModel} that uses a 2D {@link List} of Objects as its |
27 |
24 |
|
* source of data. |
28 |
25 |
|
*/ |
29 |
|
- |
public class ListTableModel extends MutableTableModel<List<Object>> { |
30 |
|
- |
/** |
31 |
|
- |
* An {@link Iterator} over the requested rows. |
32 |
|
- |
*/ |
33 |
|
- |
private class RowIterator implements Iterator<List<Object>> { |
34 |
|
- |
private int curRow; |
35 |
|
- |
private int lastRow; |
36 |
|
- |
|
37 |
|
- |
public RowIterator(Request request) { |
38 |
|
- |
curRow = request.getStartRow() - 1; |
39 |
|
- |
lastRow = Math.min(rowValues.size() - 1, curRow + request.getNumRows()); |
40 |
|
- |
} |
41 |
|
- |
|
42 |
|
- |
public boolean hasNext() { |
43 |
|
- |
return curRow < lastRow; |
44 |
|
- |
} |
45 |
|
- |
|
46 |
|
- |
public List<Object> next() { |
47 |
|
- |
if (!hasNext()) { |
48 |
|
- |
throw new NoSuchElementException(); |
49 |
|
- |
} |
50 |
|
- |
|
51 |
|
- |
curRow++; |
52 |
|
- |
return rowValues.get(new Integer(curRow)); |
53 |
|
- |
} |
54 |
|
- |
|
55 |
|
- |
public void remove() { |
56 |
|
- |
throw new UnsupportedOperationException(); |
57 |
|
- |
} |
58 |
|
- |
} |
59 |
|
- |
|
60 |
|
- |
/** |
61 |
|
- |
* The values associated with each cell. |
62 |
|
- |
*/ |
63 |
|
- |
private List<List<Object>> rowValues; |
64 |
|
- |
|
65 |
|
- |
/** |
66 |
|
- |
* Construct a new {@link ListTableModel}. |
67 |
|
- |
* |
68 |
|
- |
* @param rows the data that this model feeds from |
69 |
|
- |
*/ |
70 |
|
- |
public ListTableModel(List<List<Object>> rows) { |
71 |
|
- |
this.rowValues = rows; |
72 |
|
- |
setRowCount(rows.size()); |
73 |
|
- |
} |
74 |
|
- |
|
75 |
|
- |
@Override |
76 |
|
- |
public void requestRows(Request request, Callback<List<Object>> callback) { |
77 |
|
- |
final RowIterator it = new RowIterator(request); |
78 |
|
- |
Response<List<Object>> response = new Response<List<Object>>() { |
79 |
|
- |
@Override |
80 |
|
- |
public Iterator<List<Object>> getRowValues() { |
81 |
|
- |
return it; |
82 |
|
- |
} |
83 |
|
- |
}; |
84 |
|
- |
callback.onRowsReady(request, response); |
85 |
|
- |
} |
86 |
|
- |
|
87 |
|
- |
@Override |
88 |
|
- |
protected boolean onRowInserted(int beforeRow) { |
89 |
|
- |
if (beforeRow < rowValues.size()) { |
90 |
|
- |
rowValues.add(beforeRow, null); |
91 |
|
- |
} |
92 |
|
- |
return true; |
93 |
|
- |
} |
94 |
|
- |
|
95 |
|
- |
@Override |
96 |
|
- |
protected boolean onRowRemoved(int row) { |
97 |
|
- |
if (row < rowValues.size()) { |
98 |
|
- |
rowValues.remove(row); |
99 |
|
- |
} |
100 |
|
- |
return true; |
101 |
|
- |
} |
102 |
|
- |
|
103 |
|
- |
@Override |
104 |
|
- |
protected boolean onSetRowValue(int row, List<Object> rowValue) { |
105 |
|
- |
// Expand to fit row |
106 |
|
- |
for (int i = rowValues.size(); i <= row; i++) { |
107 |
|
- |
rowValues.add(null); |
108 |
|
- |
} |
109 |
|
- |
|
110 |
|
- |
// Set the new row value |
111 |
|
- |
rowValues.set(row, rowValue); |
112 |
|
- |
return true; |
113 |
|
- |
} |
114 |
|
- |
|
115 |
|
- |
/** |
116 |
|
- |
* Get the value at a given cell. This is used for testing. |
117 |
|
- |
* |
118 |
|
- |
* @param rowIndex the index of the row |
119 |
|
- |
* @param cellIndex the index of the cell |
120 |
|
- |
* @return the cell value, or null if it does not exist |
121 |
|
- |
*/ |
122 |
|
- |
Object getCellValue(int rowIndex, int cellIndex) { |
123 |
|
- |
// Row does not exist |
124 |
|
- |
if (rowIndex >= rowValues.size()) { |
125 |
|
- |
return null; |
126 |
|
- |
} |
127 |
|
- |
|
128 |
|
- |
// Get the cell value from the row |
129 |
|
- |
List<Object> rowList = rowValues.get(rowIndex); |
130 |
|
- |
if (rowList != null && rowList.size() > cellIndex) { |
131 |
|
- |
return rowList.get(cellIndex); |
|
26 |
+ |
public class ListTableModel extends MutableTableModel<List<Object>> |
|
27 |
+ |
{ |
|
28 |
+ |
/** |
|
29 |
+ |
* An {@link Iterator} over the requested rows. |
|
30 |
+ |
*/ |
|
31 |
+ |
private class RowIterator implements Iterator<List<Object>> |
|
32 |
+ |
{ |
|
33 |
+ |
private int curRow; |
|
34 |
+ |
private int lastRow; |
|
35 |
+ |
|
|
36 |
+ |
public RowIterator( Request request ) |
|
37 |
+ |
{ |
|
38 |
+ |
curRow = request.getStartRow() - 1; |
|
39 |
+ |
lastRow = Math.min( rowValues.size() - 1, curRow + request.getNumRows() ); |
|
40 |
+ |
} |
|
41 |
+ |
|
|
42 |
+ |
public boolean hasNext() |
|
43 |
+ |
{ |
|
44 |
+ |
return curRow < lastRow; |
|
45 |
+ |
} |
|
46 |
+ |
|
|
47 |
+ |
public List<Object> next() |
|
48 |
+ |
{ |
|
49 |
+ |
if ( !hasNext() ) |
|
50 |
+ |
{ |
|
51 |
+ |
throw new NoSuchElementException(); |
|
52 |
+ |
} |
|
53 |
+ |
|
|
54 |
+ |
curRow++; |
|
55 |
+ |
return rowValues.get( new Integer( curRow ) ); |
|
56 |
+ |
} |
|
57 |
+ |
|
|
58 |
+ |
public void remove() |
|
59 |
+ |
{ |
|
60 |
+ |
throw new UnsupportedOperationException(); |
|
61 |
+ |
} |
|
62 |
+ |
} |
|
63 |
+ |
|
|
64 |
+ |
/** |
|
65 |
+ |
* The values associated with each cell. |
|
66 |
+ |
*/ |
|
67 |
+ |
private List<List<Object>> rowValues; |
|
68 |
+ |
|
|
69 |
+ |
/** |
|
70 |
+ |
* Construct a new {@link ListTableModel}. |
|
71 |
+ |
* |
|
72 |
+ |
* @param rows the data that this model feeds from |
|
73 |
+ |
*/ |
|
74 |
+ |
public ListTableModel( List<List<Object>> rows ) |
|
75 |
+ |
{ |
|
76 |
+ |
this.rowValues = rows; |
|
77 |
+ |
setRowCount( rows.size() ); |
|
78 |
+ |
} |
|
79 |
+ |
|
|
80 |
+ |
@Override |
|
81 |
+ |
public void requestRows( Request request, Callback<List<Object>> callback ) |
|
82 |
+ |
{ |
|
83 |
+ |
final RowIterator it = new RowIterator( request ); |
|
84 |
+ |
Response<List<Object>> response = new Response<List<Object>>() |
|
85 |
+ |
{ |
|
86 |
+ |
@Override |
|
87 |
+ |
public Iterator<List<Object>> getRowValues() |
|
88 |
+ |
{ |
|
89 |
+ |
return it; |
|
90 |
+ |
} |
|
91 |
+ |
}; |
|
92 |
+ |
callback.onRowsReady( request, response ); |
|
93 |
+ |
} |
|
94 |
+ |
|
|
95 |
+ |
@Override |
|
96 |
+ |
protected boolean onRowInserted( int beforeRow ) |
|
97 |
+ |
{ |
|
98 |
+ |
if ( beforeRow < rowValues.size() ) |
|
99 |
+ |
{ |
|
100 |
+ |
rowValues.add( beforeRow, null ); |
|
101 |
+ |
} |
|
102 |
+ |
return true; |
|
103 |
+ |
} |
|
104 |
+ |
|
|
105 |
+ |
@Override |
|
106 |
+ |
protected boolean onRowRemoved( int row ) |
|
107 |
+ |
{ |
|
108 |
+ |
if ( row < rowValues.size() ) |
|
109 |
+ |
{ |
|
110 |
+ |
rowValues.remove( row ); |
|
111 |
+ |
} |
|
112 |
+ |
return true; |
|
113 |
+ |
} |
|
114 |
+ |
|
|
115 |
+ |
@Override |
|
116 |
+ |
protected boolean onSetRowValue( int row, List<Object> rowValue ) |
|
117 |
+ |
{ |
|
118 |
+ |
// Expand to fit row |
|
119 |
+ |
for ( int i = rowValues.size(); i <= row; i++ ) |
|
120 |
+ |
{ |
|
121 |
+ |
rowValues.add( null ); |
|
122 |
+ |
} |
|
123 |
+ |
|
|
124 |
+ |
// Set the new row value |
|
125 |
+ |
rowValues.set( row, rowValue ); |
|
126 |
+ |
return true; |
|
127 |
+ |
} |
|
128 |
+ |
|
|
129 |
+ |
/** |
|
130 |
+ |
* Get the value at a given cell. This is used for testing. |
|
131 |
+ |
* |
|
132 |
+ |
* @param rowIndex the index of the row |
|
133 |
+ |
* @param cellIndex the index of the cell |
|
134 |
+ |
* |
|
135 |
+ |
* @return the cell value, or null if it does not exist |
|
136 |
+ |
*/ |
|
137 |
+ |
Object getCellValue( int rowIndex, int cellIndex ) |
|
138 |
+ |
{ |
|
139 |
+ |
// Row does not exist |
|
140 |
+ |
if ( rowIndex >= rowValues.size() ) |
|
141 |
+ |
{ |
|
142 |
+ |
return null; |
|
143 |
+ |
} |
|
144 |
+ |
|
|
145 |
+ |
// Get the cell value from the row |
|
146 |
+ |
List<Object> rowList = rowValues.get( rowIndex ); |
|
147 |
+ |
if ( rowList != null && rowList.size() > cellIndex ) |
|
148 |
+ |
{ |
|
149 |
+ |
return rowList.get( cellIndex ); |
|
150 |
+ |
} |
|
151 |
+ |
return null; |
132 |
152 |
|
} |
133 |
|
- |
return null; |
134 |
|
- |
} |
135 |
153 |
|
} |