com.vaadin.flow.data.provider.
Class AbstractDataView<T>
Type Parameters:
T
- data type
All Implemented Interfaces:
DataView<T>
, Serializable
Direct Known Subclasses:
AbstractLazyDataView
, AbstractListDataView
, CheckboxGroupDataView
, ComboBoxDataView
, GridDataView
, ListBoxDataView
, RadioButtonGroupDataView
, SelectDataView
Abstract data view implementation which handles parts that apply for any type of data.
See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Component
protected SerializableSupplier<? extends DataProvider<T,
?>> protected static final String
protected static final String
-
Constructor Summary
ConstructorsConstructorDescriptionAbstractDataView
(SerializableSupplier<? extends DataProvider<T, ?>> dataProviderSupplier, Component component) Creates a new instance of
AbstractDataView
subclass and verifies the passed data provider is compatible with this data view implementation. -
Method Summary
Modifier and TypeMethodDescriptionaddIdentifierProviderChangeListener
(ComponentEventListener<IdentifierProviderChangeEvent<T, ?>> listener) Add an identifier provider change listener that is fired when a custom identifier provider is set with
setIdentifierProvider(IdentifierProvider)
.Add an item count change listener that is fired when the item count changes.
protected boolean
protected IdentifierProvider<T>
getItemIndex
(T item) Gets the index of the given item from the data available to the component.
protected int
getItemIndex
(T item, Stream<T> stream) Gets item index from the given stream.
getItems()
Get the full data available to the component.
protected abstract Class<?>
Returns supported
DataProvider
type for thisDataView
.void
Notifies the component that all the items should be refreshed.
void
refreshItem
(T item) Notifies the component that the item has been updated and thus should be refreshed.
void
setIdentifierProvider
(IdentifierProvider<T> identifierProvider) Sets an identifier provider, which returns an identifier for the given item.
protected final void
verifyDataProviderType
(DataProvider<T, ?> dataProvider) Verifies an obtained
DataProvider
type is appropriate for current Data View type.protected final void
verifyDataProviderType
(Class<?> dataProviderType) Verifies an obtained
DataProvider
type is appropriate for current Data View type.
-
Field Details
-
NULL_ITEM_ERROR_MESSAGE
See Also:
-
NULL_IDENTIFIER_ERROR_MESSAGE
See Also:
-
dataProviderSupplier
-
component
-
-
Constructor Details
-
AbstractDataView
public AbstractDataView(SerializableSupplier<? extends DataProvider<T, ?>> dataProviderSupplier, Component component) Creates a new instance of
AbstractDataView
subclass and verifies the passed data provider is compatible with this data view implementation.Parameters:
dataProviderSupplier
- supplier from which the DataProvider can be gottencomponent
- the component that the dataView is bound to
-
-
Method Details
-
addItemCountChangeListener
public Registration addItemCountChangeListener(ComponentEventListener<ItemCountChangeEvent<?>> listener) Description copied from interface:
DataView
Add an item count change listener that is fired when the item count changes. This can happen for instance when filtering the items.
Item count change listener is bound to the component and will be retained even if the data changes by setting of a new items or
DataProvider
to component.NOTE: when the component supports lazy loading (implements
HasLazyDataView
) and a count callback has not been provided, an estimate of the item count is used and increased until the actual count has been reached. When the estimate is used, the event is fired with theItemCountChangeEvent.isItemCountEstimated()
returningtrue
.Specified by:
addItemCountChangeListener
in interfaceDataView<T>
Parameters:
listener
- item count change listener to registerReturns:
registration for removing the listener
-
getSupportedDataProviderType
Returns supported
DataProvider
type for thisDataView
.Returns:
supported data provider type
-
verifyDataProviderType
Verifies an obtained
DataProvider
type is appropriate for current Data View type.Parameters:
dataProviderType
- data provider type to be verifiedThrows:
IllegalStateException
- if data provider type is incompatible with data view type -
verifyDataProviderType
Verifies an obtained
DataProvider
type is appropriate for current Data View type. If the data provider is a wrapper, then the wrapped data provider is verified too.Parameters:
dataProvider
- data provider to be verifiedThrows:
IllegalStateException
- if data provider type is incompatible with data view type -
getItemIndex
Description copied from interface:
DataView
Gets the index of the given item from the data available to the component. Data is filtered and sorted the same way as in the component.
Specified by:
getItemIndex
in interfaceDataView<T>
Parameters:
item
- item to get index forReturns:
index of the item or empty optional if the item is not found
-
getItems
Description copied from interface:
DataView
Get the full data available to the component. Data is filtered and sorted the same way as in the component.
Consumers of the returned stream are responsible for closing it when all the stream operations are done to ensure that any resources feeding the stream are properly released. Failure to close the stream might lead to resource leaks.
It is strongly recommended to use a try-with-resources block to automatically close the stream after its terminal operation has been executed. Below is an example of how to properly use and close the stream:
try (Stream<T> stream = dataView.getItems()) { stream.forEach(System.out::println); // Example terminal operation }
-
refreshItem
Description copied from interface:
DataView
Notifies the component that the item has been updated and thus should be refreshed.
For this to work properly, the item must either implement
Object.equals(Object)
andObject.hashCode()
to consider both the old and the new item instances to be equal, or alternatively use theDataView.setIdentifierProvider(IdentifierProvider)
to set an appropriate item's identifier.This method delegates the update to
DataProvider.refreshItem(Object)
.Specified by:
refreshItem
in interfaceDataView<T>
Parameters:
item
- item containing updated stateSee Also:
-
refreshAll
public void refreshAll()Description copied from interface:
DataView
Notifies the component that all the items should be refreshed.
Specified by:
refreshAll
in interfaceDataView<T>
-
setIdentifierProvider
Description copied from interface:
DataView
Sets an identifier provider, which returns an identifier for the given item. The identifier is used for comparing the equality of items. Usage example:
dataView.setIdentifiedProvider(Item::getId);
.Specified by:
setIdentifierProvider
in interfaceDataView<T>
Parameters:
identifierProvider
- function that returns the non-null identifier for a given item -
getIdentifierProvider
-
addIdentifierProviderChangeListener
public Registration addIdentifierProviderChangeListener(ComponentEventListener<IdentifierProviderChangeEvent<T, ?>> listener) Add an identifier provider change listener that is fired when a custom identifier provider is set with
setIdentifierProvider(IdentifierProvider)
.Can be used by components to get notified that a new identifier provider has been set through the data view.
Parameters:
listener
- identifier provider change listener to registerReturns:
registration for removing the listener
-
equals
-
getItemIndex
Gets item index from the given stream.
Parameters:
item
- the item to get index forstream
- the stream to get index fromReturns:
the index of the item in the stream, or -1 if not found
-