com.vaadin.client.data
Class AbstractRemoteDataSource<T>

java.lang.Object
  extended by com.vaadin.client.data.AbstractRemoteDataSource<T>
Type Parameters:
T - the row type
All Implemented Interfaces:
DataSource<T>
Direct Known Subclasses:
RpcDataSourceConnector.RpcDataSource

public abstract class AbstractRemoteDataSource<T>
extends java.lang.Object
implements DataSource<T>

Base implementation for data sources that fetch data from a remote system. This class takes care of caching data and communicating with the data source user. An implementation of this class should override requestRows(int, int) to trigger asynchronously loading of data. When data is received from the server, new row data should be passed to setRowData(int, List). #setEstimatedSize(int) should be used based on estimations of how many rows are available.

Since:
Author:
Vaadin Ltd

Nested Class Summary
protected  class AbstractRemoteDataSource.RowHandleImpl
           
 
Nested classes/interfaces inherited from interface com.vaadin.client.data.DataSource
DataSource.RowHandle<T>
 
Field Summary
protected  java.util.Collection<T> temporarilyPinnedRows
           
 
Constructor Summary
AbstractRemoteDataSource()
           
 
Method Summary
 void ensureAvailability(int firstRowIndex, int numberOfRows)
          Informs the data source that data for the given range is needed.
 com.vaadin.shared.ui.grid.Range getCachedRange()
          Gets the current range of cached rows
 DataSource.RowHandle<T> getHandle(T row)
          Gets a DataSource.RowHandle of a row object in the cache.
 T getRow(int rowIndex)
          Retrieves the data for the row at the given index.
abstract  java.lang.Object getRowKey(T row)
          Gets a stable key for the row object.
 int indexOf(T row)
          Retrieves the index for given row object.
protected  void insertRowData(int firstRowIndex, int count)
          Informs this data source that new data has been inserted from the server.
protected  void removeRowData(int firstRowIndex, int count)
          Informs this data source that the server has removed data.
protected abstract  void requestRows(int firstRowIndex, int numberOfRows)
          Triggers fetching rows from the remote data source.
protected  void resetDataAndSize(int newSize)
           
 void setCacheStrategy(CacheStrategy cacheStrategy)
          Sets the cache strategy that is used to determine how much data is fetched and cached.
 void setDataChangeHandler(DataChangeHandler dataChangeHandler)
          Sets a data change handler to inform when data is updated, added or removed.
protected  void setRowData(int firstRowIndex, java.util.List<T> rowData)
          Informs this data source that updated data has been sent from the server.
 void transactionPin(java.util.Collection<T> rows)
          Deprecated. You probably don't want to call this method unless you're writing a Renderer for a selection model. Even if you are, be very aware what this method does and how it behaves.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.vaadin.client.data.DataSource
size
 

Field Detail

temporarilyPinnedRows

protected java.util.Collection<T> temporarilyPinnedRows
Constructor Detail

AbstractRemoteDataSource

public AbstractRemoteDataSource()
Method Detail

ensureAvailability

public void ensureAvailability(int firstRowIndex,
                               int numberOfRows)
Description copied from interface: DataSource
Informs the data source that data for the given range is needed. A data source only has one active region at a time, so calling this method discards the previously set range.

This method triggers lazy loading of data if necessary. The change handler registered using DataSource.setDataChangeHandler(DataChangeHandler) is informed when new data has been loaded.

After any possible lazy loading and updates are done, the change handler is informed that new data is available.

Specified by:
ensureAvailability in interface DataSource<T>
Parameters:
firstRowIndex - the index of the first needed row
numberOfRows - the number of needed rows

requestRows

protected abstract void requestRows(int firstRowIndex,
                                    int numberOfRows)
Triggers fetching rows from the remote data source. setRowData(int, List) should be invoked with data for the requested rows when they have been received.

Parameters:
firstRowIndex - the index of the first row to fetch
numberOfRows - the number of rows to fetch

getRow

public T getRow(int rowIndex)
Description copied from interface: DataSource
Retrieves the data for the row at the given index. If the row data is not available, returns null.

This method does not trigger loading of unavailable data. DataSource.ensureAvailability(int, int) should be used to signal what data will be needed.

Specified by:
getRow in interface DataSource<T>
Parameters:
rowIndex - the index of the row to retrieve data for
Returns:
data for the row; or null if no data is available

indexOf

public int indexOf(T row)
Description copied from interface: DataSource
Retrieves the index for given row object.

Note: This method does not verify that the given row object exists at all in this DataSource.

Specified by:
indexOf in interface DataSource<T>
Parameters:
row - the row object
Returns:
index of the row; or -1 if row is not available

setDataChangeHandler

public void setDataChangeHandler(DataChangeHandler dataChangeHandler)
Description copied from interface: DataSource
Sets a data change handler to inform when data is updated, added or removed.

Specified by:
setDataChangeHandler in interface DataSource<T>
Parameters:
dataChangeHandler - the data change handler

setRowData

protected void setRowData(int firstRowIndex,
                          java.util.List<T> rowData)
Informs this data source that updated data has been sent from the server.

Parameters:
firstRowIndex - the index of the first received row
rowData - a list of rows, starting from firstRowIndex

removeRowData

protected void removeRowData(int firstRowIndex,
                             int count)
Informs this data source that the server has removed data.

Parameters:
firstRowIndex - the index of the first removed row
count - the number of removed rows, starting from firstRowIndex

insertRowData

protected void insertRowData(int firstRowIndex,
                             int count)
Informs this data source that new data has been inserted from the server.

Parameters:
firstRowIndex - the destination index of the new row data
count - the number of rows inserted

getCachedRange

public com.vaadin.shared.ui.grid.Range getCachedRange()
Gets the current range of cached rows

Returns:
the range of currently cached rows

setCacheStrategy

public void setCacheStrategy(CacheStrategy cacheStrategy)
Sets the cache strategy that is used to determine how much data is fetched and cached.

The new strategy is immediately used to evaluate whether currently cached rows should be discarded or new rows should be fetched.

Parameters:
cacheStrategy - a cache strategy implementation, not null

getHandle

public DataSource.RowHandle<T> getHandle(T row)
                                  throws java.lang.IllegalStateException
Description copied from interface: DataSource
Gets a DataSource.RowHandle of a row object in the cache.

Specified by:
getHandle in interface DataSource<T>
Parameters:
row - the row object for which to retrieve a row handle
Returns:
a non-null row handle of the given row object
Throws:
java.lang.IllegalStateException

getRowKey

public abstract java.lang.Object getRowKey(T row)
Gets a stable key for the row object.

This method is a workaround for the fact that there is no means to force proper implementations for Object.hashCode() and Object.equals(Object) methods.

Since the same row object will be created several times for the same logical data, the DataSource needs a mechanism to be able to compare two objects, and figure out whether or not they represent the same data. Even if all the fields of an entity would be changed, it still could represent the very same thing (say, a person changes all of her names.)

A very usual and simple example what this could be, is an unique ID for this object that would also be stored in a database.

Parameters:
row - the row object for which to get the key
Returns:
a non-null object that uniquely and consistently represents the row object

transactionPin

@Deprecated
public void transactionPin(java.util.Collection<T> rows)
Deprecated. You probably don't want to call this method unless you're writing a Renderer for a selection model. Even if you are, be very aware what this method does and how it behaves.

Marks rows as pinned when fetching new rows.

This collection of rows are intended to remain pinned if new rows are fetched from the data source, even if some of the pinned rows would fall off the cache and become inactive.

This method does nothing by itself, other than it stores the rows into a field. The implementation needs to make all the adjustments for itself. Check RpcDataSourceConnector.RpcDataSource.requestRows(int, int) for an implementation example.

Parameters:
keys - a collection of rows to keep pinned
See Also:
temporarilyPinnedRows, RpcDataSourceConnector.RpcDataSource.requestRows(int, int)

resetDataAndSize

protected void resetDataAndSize(int newSize)


Copyright © 2000-2014 Vaadin Ltd. All Rights Reserved.