Class HierarchicalDataCommunicator<T>
- Type Parameters:
T- the bean type
- All Implemented Interfaces:
Serializable
HierarchicalDataCommunicator is a middleware layer between
HierarchicalDataProvider and the client-side. It handles the loading
and caching of hierarchical data from the data provider based on its
hierarchy format, tracks expanded and collapsed items, and delivers data to
the client based on the requested viewport
range.
The communicator supports data providers that implement in one of the
following formats: HierarchicalDataProvider.HierarchyFormat.NESTED or
HierarchicalDataProvider.HierarchyFormat.FLATTENED.
Nested Hierarchy Format
When using data providers with HierarchicalDataProvider.HierarchyFormat.NESTED, the
communicator stores data in a hierarchical cache structure where each level
is represented by a Cache object, and the root by RootCache.
Before sending data to the client, the visible range is flattened into a
linear list. This allows the client to work with a simplified view, without
handling hierarchical structure directly. The getDepth(Object)
method should be used by the component to get an item's depth and apply
indentation or other visual styling based on hierarchy level.
Flattened Hierarchy Format
When using data providers whose format is HierarchicalDataProvider.HierarchyFormat.FLATTENED,
the communicator maintains all items in a single flat list, managed through
RootCache, which is directly suitable for client-side rendering
without any additional processing. The getDepth(Object) method uses
the data provider's implementation to determine the depth of an item in the
hierarchy.
KeyMapper
For each item in the visible range, the communicator generates a client-side
key using KeyMapper. This key is used to identify the item on the
server when the client sends updates or interaction events for that item such
as selection, expansion, etc.
- Since:
- 1.2
- Author:
- Vaadin Ltd
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class com.vaadin.flow.data.provider.DataCommunicator
DataCommunicator.EmptyDataProvider<T1>, DataCommunicator.Filter<F> -
Field Summary
Fields inherited from class com.vaadin.flow.data.provider.DataCommunicator
DEFAULT_PAGE_INCREASE_COUNT, passivatedByUpdate -
Constructor Summary
ConstructorsConstructorDescriptionHierarchicalDataCommunicator(CompositeDataGenerator<T> dataGenerator, ArrayUpdater arrayUpdater, StateNode stateNode, SerializableSupplier<ValueProvider<T, String>> uniqueKeyProviderSupplier) Construct a new hierarchical data communicator backed by aTreeDataProvider. -
Method Summary
Modifier and TypeMethodDescriptioncollapse(Collection<T> items) Collapses the given items and removes its sub-hierarchy.voidCollapses the given item and removes its sub-hierarchy.voidconfirmUpdate(int updateId) Confirm update with the givenupdateId.expand(Collection<T> items) Expands the given items and schedules a client update to render children (if visible).voidExpands the given item and schedules a client update to render children (if visible).fetchFromProvider(int offset, int limit) Fetches a list of items from the DataProvider.Gets the current data provider from this DataCommunicator.intGetter method for determining the item count of the data.intReturns depth of item in the tree starting from zero representing a root.intEstimates are not supported in HierarchicalDataCommunicatorintEstimates are not supported in HierarchicalDataCommunicatorprotected voidbooleanhasChildren(T item) Returns whether given item has children.booleanReturns true if there is any expanded items.booleanEstimates are not supported in HierarchicalDataCommunicator.booleanisExpanded(T item) Returns whether given item is expanded.preloadFlatRangeBackward(int start, int length) Preloads and returns a range of items from the flattened hierarchy, starting at the specified flat index and spanning the given length.preloadFlatRangeForward(int start, int length) Preloads and returns a range of items from the flattened hierarchy, starting at the specified flat index and spanning the given length.voidReplaces the cached item with a new instance and schedules a client update to re-render this item.voidReplaces the cached item with a new instance and schedules a client update to re-render this item.voidreset()Clears all cached data and recursively re-fetches items from hierarchy levels that are still within the current viewport range, starting from the root level.protected intresolveIndexPath(int... path) Ensures that all items along the specified path are preloaded into the cache, starting from the root level, and returns the flat index of the target item.<F> SerializableConsumer<F>setDataProvider(DataProvider<T, F> dataProvider, F initialFilter) <F> SerializableConsumer<F>setDataProvider(HierarchicalDataProvider<T, F> dataProvider, F initialFilter) Sets the hierarchical data provider for this communicator.voidsetDefinedSize(boolean definedSize) Estimates are not supported in HierarchicalDataCommunicatorvoidsetItemCountEstimate(int itemCountEstimate) Estimates are not supported in HierarchicalDataCommunicatorvoidsetItemCountEstimateIncrease(int itemCountEstimateIncrease) Estimates are not supported in HierarchicalDataCommunicatorvoidsetViewportRange(int start, int length) Sets the range of data to be sent to the client.Methods inherited from class com.vaadin.flow.data.provider.DataCommunicator
buildQuery, computeRequestedRange, computeViewportRange, doUnregister, enablePushUpdates, getBackEndSorting, getFilter, getInMemorySorting, getItem, getItemCount, getKeyMapper, getPageSize, getPassivatedKeys, isFetchEnabled, isItemActive, isPagingEnabled, setBackEndSorting, setCountCallback, setDataProvider, setFetchEnabled, setInMemorySorting, setKeyMapper, setPageSize, setPagingEnabled, setRequestedRange
-
Constructor Details
-
HierarchicalDataCommunicator
public HierarchicalDataCommunicator(CompositeDataGenerator<T> dataGenerator, ArrayUpdater arrayUpdater, StateNode stateNode, SerializableSupplier<ValueProvider<T, String>> uniqueKeyProviderSupplier) Construct a new hierarchical data communicator backed by aTreeDataProvider.- Parameters:
dataGenerator- the data generator functionarrayUpdater- array updater strategystateNode- the state node used to communicate foruniqueKeyProviderSupplier- Unique key provider for a row. If null, then using Grid's default key generator.
-
-
Method Details
-
reset
public void reset()Clears all cached data and recursively re-fetches items from hierarchy levels that are still within the current viewport range, starting from the root level.WARNING: For data providers that use
HierarchicalDataProvider.HierarchyFormat.NESTED, this method will clear all cached hierarchy state, discarding any potential information about the positions of expanded items in the hierarchy. As a result, the viewport's start index may become pointing to a different item if there were cached expanded items before the start index, causing a shift in the currently displayed items.- Overrides:
resetin classDataCommunicator<T>
-
handleDataRefreshEvent
- Overrides:
handleDataRefreshEventin classDataCommunicator<T>
-
refresh
Replaces the cached item with a new instance and schedules a client update to re-render this item.- Overrides:
refreshin classDataCommunicator<T>- Parameters:
item- updated data object; notnull
-
refresh
Replaces the cached item with a new instance and schedules a client update to re-render this item. WhenrefreshChildrenis true, the item's sub-hierarchy is cleared from the cache and scheduled to be re-fetched from the data provider once visible.WARNING: This method is only supported with data providers that use
HierarchicalDataProvider.HierarchyFormat.NESTEDand may cause visible range shift if the refreshed item contains expanded descendants. In such cases, they might not be re-fetched immediately if they are not visible. This can affect the flattened hierarchy size and result in the viewport range pointing to a different set of items than before the refresh.- Parameters:
item- the item to refreshrefreshChildren- whether or not to refresh child items- Throws:
UnsupportedOperationException- ifrefreshChildrenis true and the data provider's hierarchy format is notHierarchicalDataProvider.HierarchyFormat.NESTED- Since:
- 25.0
-
fetchFromProvider
Description copied from class:DataCommunicatorFetches a list of items from the DataProvider.NOTE: the
limitparameter shows how many items the client wants to fetch, but the actual number of results may be greater, and vary from0 to pages * pageSize.- Overrides:
fetchFromProviderin classDataCommunicator<T>- Parameters:
offset- the starting index of the rangelimit- the desired number of results- Returns:
- the list of items in given range
-
getDataProvider
Description copied from class:DataCommunicatorGets the current data provider from this DataCommunicator.- Overrides:
getDataProviderin classDataCommunicator<T>- Returns:
- the data provider
-
setDataProvider
public <F> SerializableConsumer<F> setDataProvider(HierarchicalDataProvider<T, F> dataProvider, F initialFilter) Sets the hierarchical data provider for this communicator.The returned consumer allows updating the filter value used in subsequent queries to the data provider. The consumer is only valid until another data provider is set.
- Type Parameters:
F- the filter type- Parameters:
dataProvider- the hierarchical data provider to use, notnullinitialFilter- the initial filter value, ornullif no filtering is needed- Returns:
- a
SerializableConsumerfor updating the filter value
-
setDataProvider
Alias forsetDataProvider(HierarchicalDataProvider, Object). Only hierarchical data providers are supported.- Overrides:
setDataProviderin classDataCommunicator<T>- Type Parameters:
F- the filter type- Parameters:
dataProvider- the hierarchical data provider to use, notnullinitialFilter- the initial filter value, ornullif no filtering is needed- Returns:
- a
SerializableConsumerfor updating the filter value - Throws:
IllegalArgumentException- if the provided data provider is not a hierarchical data provider
-
confirmUpdate
public void confirmUpdate(int updateId) Description copied from class:DataCommunicatorConfirm update with the givenupdateId.- Overrides:
confirmUpdatein classDataCommunicator<T>- Parameters:
updateId- the update identifier
-
collapse
Collapses the given item and removes its sub-hierarchy. Calling this method will have no effect if the row is already collapsed.- Parameters:
item- the item to collapse
-
collapse
Collapses the given items and removes its sub-hierarchy. Calling this method will have no effect if the row is already collapsed.- Parameters:
items- the items to collapse- Returns:
- the collapsed items
-
expand
Expands the given item and schedules a client update to render children (if visible). Calling this method will have no effect if the item is already expanded or if it has no children.- Parameters:
item- the item to expand
-
expand
Expands the given items and schedules a client update to render children (if visible). Calling this method will have no effect if the item is already expanded or if it has no children.- Parameters:
items- the items to expand- Returns:
- the expanded items
-
hasChildren
Returns whether given item has children.- Parameters:
item- the item to test- Returns:
trueif item has children;falseif not
-
isExpanded
Returns whether given item is expanded.- Parameters:
item- the item to test- Returns:
trueif item is expanded;falseif not
-
getDepth
Returns depth of item in the tree starting from zero representing a root.- Parameters:
item- Target item- Returns:
- depth of item in the tree or -1 if item is null or not found in the cache
-
getDataProviderSize
public int getDataProviderSize()Description copied from class:DataCommunicatorGetter method for determining the item count of the data.This method should be used only with defined size, i.e. when
DataCommunicator.isDefinedSize()returnstrue.Can be overridden by a subclass that uses a specific type of DataProvider and/or query.
- Overrides:
getDataProviderSizein classDataCommunicator<T>- Returns:
- the size of data provider with current filter
-
hasExpandedItems
public boolean hasExpandedItems()Returns true if there is any expanded items.- Returns:
trueif there is any expanded items.
-
resolveIndexPath
protected int resolveIndexPath(int... path) Ensures that all items along the specified path are preloaded into the cache, starting from the root level, and returns the flat index of the target item.- Parameters:
path- the hierarchical path to the item, where each element represents the index within its respective level- Returns:
- the flat index of the target item after resolving all ancestors
- Since:
- 25.0
-
preloadFlatRangeBackward
Preloads and returns a range of items from the flattened hierarchy, starting at the specified flat index and spanning the given length. Items are preloaded in the backward direction, beginning from the givenstartindex and continuing toward lower indexes until the specifiedlengthis reached.NOTE: Backward preloading can affect the position of the start index in the flat list, so it may need to be recalculated if it's used after this method call.
- Parameters:
start- the start index of the range to preloadlength- the length of the range to preload- Returns:
- a list of items preloaded in the specified range
- Since:
- 25.0
-
preloadFlatRangeForward
Preloads and returns a range of items from the flattened hierarchy, starting at the specified flat index and spanning the given length. Items are preloaded in the forward direction, beginning from the givenstartindex and continuing toward higher indexes until the specifiedlengthis reached.- Parameters:
start- the start index of the range to preloadlength- the length of the range to preload- Returns:
- a list of items preloaded in the specified range
- Since:
- 25.0
-
setViewportRange
public void setViewportRange(int start, int length) Description copied from class:DataCommunicatorSets the range of data to be sent to the client.- Overrides:
setViewportRangein classDataCommunicator<T>- Parameters:
start- the start of the viewport rangelength- the end of the viewport range
-
setItemCountEstimate
public void setItemCountEstimate(int itemCountEstimate) Estimates are not supported in HierarchicalDataCommunicator- Overrides:
setItemCountEstimatein classDataCommunicator<T>- Parameters:
itemCountEstimate- the item count estimate to be used
-
getItemCountEstimate
public int getItemCountEstimate()Estimates are not supported in HierarchicalDataCommunicator- Overrides:
getItemCountEstimatein classDataCommunicator<T>- Returns:
- the item count estimate used
-
setItemCountEstimateIncrease
public void setItemCountEstimateIncrease(int itemCountEstimateIncrease) Estimates are not supported in HierarchicalDataCommunicator- Overrides:
setItemCountEstimateIncreasein classDataCommunicator<T>- Parameters:
itemCountEstimateIncrease- the item count estimate step to use
-
getItemCountEstimateIncrease
public int getItemCountEstimateIncrease()Estimates are not supported in HierarchicalDataCommunicator- Overrides:
getItemCountEstimateIncreasein classDataCommunicator<T>- Returns:
- the item count estimate increase
-
setDefinedSize
public void setDefinedSize(boolean definedSize) Estimates are not supported in HierarchicalDataCommunicator- Overrides:
setDefinedSizein classDataCommunicator<T>- Parameters:
definedSize-truefor defined size,falsefor undefined size
-
isDefinedSize
public boolean isDefinedSize()Estimates are not supported in HierarchicalDataCommunicator. Therefore this method will always return true- Overrides:
isDefinedSizein classDataCommunicator<T>- Returns:
truefor defined size,falsefor undefined size
-