Here is a somewhat simplified example of such an implementation.
Chapter 5
[ 135 ]
Using GridDataSource
First of all, let's modify the IDataSource interface, adding to it a method for
returning a selected range of celebrities:
public interface IDataSource
{
List
getAllCelebrities();
Celebrity getCelebrityById(long id);
void addCelebrity(Celebrity c);
List getRange(int indexFrom, int indexTo);
}
Next, we need to implement this method in the available implementation of this
interface. Add the following method to the MockDataSource class:
public List getRange(int indexFrom, int indexTo)
{
List result = new ArrayList();
for (int i = indexFrom; i <= indexTo; i++)
{
result.add(celebrities.get(i));
}
return result;
}
The code is quite simple, we are returning a subset of the existing collection starting
from one index value and ending with the other. In a real-life implementation, we
would probably check whether indexTo is bigger than indexFrom, but here, let's
keep things simple.
Here is one possible implementation of GridDataSource. There are plenty of output
statements in it that do not do anything very useful, but they will allow us to witness
the inner life of Grid and GridDataSet in tandem.
Pages:
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183