com.vaadin.client.widget.grid.datasources
Class ListDataSource<T>

java.lang.Object
  extended by com.vaadin.client.widget.grid.datasources.ListDataSource<T>
All Implemented Interfaces:
DataSource<T>

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

A simple list based on an in-memory data source for simply adding a list of row pojos to the grid. Based on a wrapped list instance which supports adding and removing of items.

Usage:

 ListDataSource<Integer> ds = new ListDataSource<Integer>(1, 2, 3, 4);
 
 // Add item to the data source
 ds.asList().add(5);
 
 // Remove item from the data source
 ds.asList().remove(3);
 
 // Add multiple items
 ds.asList().addAll(Arrays.asList(5, 6, 7));
 

Since:
7.4
Author:
Vaadin Ltd

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.vaadin.client.data.DataSource
DataSource.RowHandle<T>
 
Constructor Summary
ListDataSource(java.util.List<T> datasource)
          Constructs a new list data source.
ListDataSource(T... rows)
          Constructs a data source with a set of rows.
 
Method Summary
 java.util.List<T> asList()
          Gets the list that backs this datasource.
 void ensureAvailability(int firstRowIndex, int numberOfRows)
          Informs the data source that data for the given range is needed.
 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.
 SelectAllHandler<T> getSelectAllHandler()
          Returns a SelectAllHandler for this ListDataSource.
 int indexOf(T row)
          Retrieves the index for given row object.
 void setDataChangeHandler(DataChangeHandler dataChangeHandler)
          Sets a data change handler to inform when data is updated, added or removed.
 int size()
          Returns the number of rows in the data source.
 void sort(java.util.Comparator<T> comparator)
          Sort entire container according to a Comparator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ListDataSource

public ListDataSource(java.util.List<T> datasource)
Constructs a new list data source.

Note: Modifications to the original list will not be reflected in the data source after the data source has been constructed. To add or remove items to the data source after it has been constructed use asList().

Parameters:
datasource - The list to use for providing the data to the grid

ListDataSource

public ListDataSource(T... rows)
Constructs a data source with a set of rows. You can dynamically add and remove rows from the data source via the list you get from asList()

Parameters:
rows - The rows to initially add to the data source
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

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

size

public int size()
Description copied from interface: DataSource
Returns the number of rows in the data source.

Specified by:
size in interface DataSource<T>
Returns:
the current size of the data source

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

asList

public java.util.List<T> asList()
Gets the list that backs this datasource. Any changes made to this list will be reflected in the datasource.

Note: The list is not the same list as passed into the data source via the constructor.

Returns:
Returns a list implementation that wraps the real list that backs the data source and provides events for the data source listeners.

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

sort

public void sort(java.util.Comparator<T> comparator)
Sort entire container according to a Comparator.

Parameters:
comparator - a comparator object, which compares two data source entries (beans/pojos)

indexOf

public int indexOf(T row)
Retrieves the index for given row object.

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

Parameters:
row - the row object
Returns:
index of the row; or -1 if row is not available

getSelectAllHandler

public SelectAllHandler<T> getSelectAllHandler()
Returns a SelectAllHandler for this ListDataSource.

Returns:
select all handler


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