T
- the data typeF
- the filter typepublic class HierarchyMapper<T,F> extends Object implements DataGenerator<T>
Keeps track of the expanded nodes, and size of of the subtrees for each expanded node.
This class is framework internal implementation details, and can be changed / moved at any point. This means that you should not directly use this for anything.
Constructor and Description |
---|
HierarchyMapper(HierarchicalDataProvider<T,F> provider)
Constructs a new HierarchyMapper.
|
Modifier and Type | Method and Description |
---|---|
Range |
collapse(T item,
Integer position)
Collapses the given item.
|
void |
destroyAllData()
Informs the
DataGenerator that all data has been dropped. |
void |
destroyData(T item)
Informs the
DataGenerator that the given data item has been
dropped and is no longer needed. |
Range |
doCollapse(T item,
Optional<Integer> position)
Deprecated.
Use
collapse(Object, Integer) instead. |
Range |
doExpand(T item,
Optional<Integer> position)
Deprecated.
Use
expand(Object, Integer) instead. |
Range |
expand(T item,
Integer position)
Expands the given item.
|
Stream<T> |
fetchItems(Range range)
Gets a stream of items in the form of a flattened hierarchy from the
back-end and filters the wanted results from the list.
|
Stream<T> |
fetchItems(T parent,
Range range)
Gets a stream of children for the given item in the form of a flattened
hierarchy from the back-end and filter the wanted results from the list.
|
void |
generateData(T item,
elemental.json.JsonObject jsonObject)
Adds custom data for the given item to its serialized
JsonObject
representation. |
List<QuerySortOrder> |
getBackEndSorting()
Gets the current back-end sorting.
|
HierarchicalDataProvider<T,F> |
getDataProvider()
Gets the
HierarchicalDataProvider for this
HierarchyMapper . |
F |
getFilter()
Gets the current filter.
|
Optional<Integer> |
getIndexOf(T target)
Finds the current index of given object.
|
Comparator<T> |
getInMemorySorting()
Gets the current in-memory sorting.
|
ItemCollapseAllowedProvider<T> |
getItemCollapseAllowedProvider()
Gets the current item collapse allowed provider.
|
Integer |
getParentIndex(T item)
Finds the index of the parent of the item in given target index.
|
protected T |
getParentOfItem(T item)
Find parent for the given item among open folders.
|
int |
getTreeSize()
Returns the size of the currently expanded hierarchy.
|
boolean |
hasChildren(T item)
Returns whether given item has children.
|
boolean |
isExpanded(T item)
Returns whether the given item is expanded.
|
protected void |
registerChildren(T parent,
List<T> childList)
Register parent and child items into inner structures.
|
protected void |
removeChildren(Object id)
Removes from internal parent-children bookkeeping all children of an item
identified by a given id.
|
protected List<Object> |
removeChildrenRecursively(Object id)
Removes recursively from internal parent-children bookkeeping all
children of an item identified by a given id.
|
void |
setBackEndSorting(List<QuerySortOrder> backEndSorting)
Sets the current back-end sorting.
|
void |
setFilter(Object filter)
Sets the current filter.
|
void |
setInitialized(boolean initialize)
Set the initialization state of this class.
|
void |
setInMemorySorting(Comparator<T> inMemorySorting)
Sets the current in-memory sorting.
|
void |
setItemCollapseAllowedProvider(ItemCollapseAllowedProvider<T> itemCollapseAllowedProvider)
Sets the current item collapse allowed provider.
|
void |
useActiveDataOptimization(boolean use)
Set whether you want to optimize the internal data handling to only
consider root nodes that have descendants within the client-side cache.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
refreshData
public HierarchyMapper(HierarchicalDataProvider<T,F> provider)
provider
- the hierarchical data provider for this mapperpublic int getTreeSize()
public Integer getParentIndex(T item)
item
- the item to get the parent ofpublic boolean isExpanded(T item)
item
- the item to testtrue
if item is expanded; false
if notpublic Range expand(T item, Integer position)
item
- the item to expandposition
- the index of the item@Deprecated public Range doExpand(T item, Optional<Integer> position)
expand(Object, Integer)
instead.item
- the item to expandposition
- the index of itempublic Range collapse(T item, Integer position)
If this HierarchyMapper uses active data optimization, this method always returns an empty range if the collapsed item is sufficiently far beyond the active data range (not sharing a root node with any active items). The optimization doesn't affect the data before the active data range, since those changes affect the indexes of the active range.
item
- the item to collapse (cannot be null)position
- the index of the itemuseActiveDataOptimization(boolean)
@Deprecated public Range doCollapse(T item, Optional<Integer> position)
collapse(Object, Integer)
instead.item
- the item to collapseposition
- the index of itempublic void generateData(T item, elemental.json.JsonObject jsonObject)
DataGenerator
JsonObject
representation. This JSON object will be sent to client-side
DataProvider.generateData
in interface DataGenerator<T>
item
- the data item being serializedjsonObject
- the JSON object being sent to the clientpublic ItemCollapseAllowedProvider<T> getItemCollapseAllowedProvider()
public void setItemCollapseAllowedProvider(ItemCollapseAllowedProvider<T> itemCollapseAllowedProvider)
itemCollapseAllowedProvider
- the item collapse allowed providerpublic Comparator<T> getInMemorySorting()
public void setInMemorySorting(Comparator<T> inMemorySorting)
inMemorySorting
- the in-memory sortingpublic List<QuerySortOrder> getBackEndSorting()
public void setBackEndSorting(List<QuerySortOrder> backEndSorting)
backEndSorting
- the back-end sortingpublic F getFilter()
public void setFilter(Object filter)
filter
- the filterpublic HierarchicalDataProvider<T,F> getDataProvider()
HierarchicalDataProvider
for this
HierarchyMapper
.public boolean hasChildren(T item)
item
- the node to testtrue
if node has children; false
if notpublic Stream<T> fetchItems(Range range)
NOTE: For performance and data consistency reasons this method should
only be called for the data range that is currently getting sent to the
client. If you wish to fetch the items for some other purpose, use
#fetchItems(T, Range)
for a null
parent instead. By
default this method is called for the first time when the related
component is attached.
range
- the requested item rangesetInitialized(boolean)
,
useActiveDataOptimization(boolean)
public Stream<T> fetchItems(T parent, Range range)
parent
- the parent item for the fetchrange
- the requested item rangeprotected T getParentOfItem(T item)
item
- the itemnull
for root items or if the parent is
closedprotected List<Object> removeChildrenRecursively(Object id)
The internal bookkeeping should be used over data provider when handling a collapse, because it matches the latest data that has been sent to the client. If the data provider gets updated within the same server round-trip as the collapse, the client-side update will still be pending, and using fresh data can lead to incorrect row removals from the client.
id
- id of the item whose children should be removedprotected void removeChildren(Object id)
NOTE: Older implementation of this method also updated the expansion bookkeeping and iterated over the entire hierarchy, as well as suggested overriding this method in subclasses for removing obsolete data to avoid memory leaks. That should no longer be necessary with the new logic.
id
- the item idremoveChildrenRecursively(Object)
public Optional<Integer> getIndexOf(T target)
NOTE: If this HierarchyMapper hasn't been initialized and the items requested for the first time, root node list is still empty and no search is performed.
target
- the target object to findprotected void registerChildren(T parent, List<T> childList)
NOTE: Older implementation of this class suggested overriding this and other methods, mainly for the purpose of avoiding memory leaks. That should no longer be necessary with the new logic.
This method assumes that any added, moved, or removed data is handled separately (only updating collapsed contents, or refreshing the whole data provider after the update), because making such changes here couldn't update the client side correctly.
parent
- the parent itemchildList
- list of children to be registered for the given parent, can be
emptypublic void destroyAllData()
DataGenerator
DataGenerator
that all data has been dropped. This
method should clean up any unneeded information stored for items.destroyAllData
in interface DataGenerator<T>
public void destroyData(T item)
DataGenerator
DataGenerator
that the given data item has been
dropped and is no longer needed. This method should clean up any unneeded
information stored for this item.destroyData
in interface DataGenerator<T>
item
- the dropped data itempublic void useActiveDataOptimization(boolean use)
true
.use
- true
if you want to optimize, false
otherwisepublic void setInitialized(boolean initialize)
true
when the related component is attached and data communicator
makes the first data request for updating the client-side cache, and
false
when all the data is reset. Before initialization no expand
data is sent to client, as the assumption is that the client hasn't been
initialized yet either. Collapse doesn't have a corresponding check,
because the assumption is that the internal caches are empty and the
attempt to collapse would result in an empty row range anyway.
NOTE: Do not set this class uninitialized for optimization purposes
without calling destroyAllData()
afterwards, otherwise the
internal caches are likely to get out of sync with both data communicator
and the client side and be unable to recover gracefully.
initialize
- true
to mark initialized, false
to mark
requiring initializationCopyright © 2022 Vaadin Ltd. All rights reserved.