Package com.vaadin.data
Class TreeData<T>
- java.lang.Object
-
- com.vaadin.data.TreeData<T>
-
- Type Parameters:
T
- data type
- All Implemented Interfaces:
Serializable
public class TreeData<T> extends Object implements Serializable
Class for representing hierarchical data.Typically used as a backing data source for
TreeDataProvider
.- Since:
- 8.1
- Author:
- Vaadin Ltd
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description TreeData()
Creates an initially empty hierarchical data representation to which items can be added or removed.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description TreeData<T>
addItem(T parent, T item)
Adds a data item as a child ofparent
.TreeData<T>
addItems(Collection<T> rootItems, ValueProvider<T,Collection<T>> childItemProvider)
Adds the given items as root items and uses the given value provider to recursively populate children of the root items.TreeData<T>
addItems(Stream<T> rootItems, ValueProvider<T,Stream<T>> childItemProvider)
Adds the given items as root items and uses the given value provider to recursively populate children of the root items.TreeData<T>
addItems(T parent, Collection<T> items)
Adds a list of data items as children ofparent
.TreeData<T>
addItems(T parent, Stream<T> items)
Adds data items contained in a stream as children ofparent
.TreeData<T>
addItems(T parent, T... items)
Adds a list of data items as children ofparent
.TreeData<T>
addRootItems(Collection<T> items)
Adds the items of the given collection as root items to this structure.TreeData<T>
addRootItems(Stream<T> items)
Adds the items of the given stream as root items to this structure.TreeData<T>
addRootItems(T... items)
Adds the items as root items to this structure.TreeData<T>
clear()
Clear all items from this structure.boolean
contains(T item)
Check whether the given item is in this hierarchy.List<T>
getChildren(T item)
Get the immediate child items for the given item.T
getParent(T item)
Get the parent item for the given item.List<T>
getRootItems()
Gets the root items of this structure.void
moveAfterSibling(T item, T sibling)
Moves an item to the position immediately after a sibling item.TreeData<T>
removeItem(T item)
Remove a given item from this structure.void
setParent(T item, T parent)
Moves an item to become a child of the given parent item.
-
-
-
Method Detail
-
addRootItems
public TreeData<T> addRootItems(T... items)
Adds the items as root items to this structure.- Parameters:
items
- the items to add- Returns:
- this
- Throws:
IllegalArgumentException
- if any of the given items have already been added to this structureNullPointerException
- if any of the items are {code null}
-
addRootItems
public TreeData<T> addRootItems(Collection<T> items)
Adds the items of the given collection as root items to this structure.- Parameters:
items
- the collection of items to add- Returns:
- this
- Throws:
IllegalArgumentException
- if any of the given items have already been added to this structureNullPointerException
- if any of the items are {code null}
-
addRootItems
public TreeData<T> addRootItems(Stream<T> items)
Adds the items of the given stream as root items to this structure.- Parameters:
items
- the stream of root items to add- Returns:
- this
- Throws:
IllegalArgumentException
- if any of the given items have already been added to this structureNullPointerException
- if any of the items are {code null}
-
addItem
public TreeData<T> addItem(T parent, T item)
Adds a data item as a child ofparent
. Call withnull
as parent to add a root level item. The given parent item must already exist in this structure, and an item can only be added to this structure once.- Parameters:
parent
- the parent item for which the items are added as childrenitem
- the item to add- Returns:
- this
- Throws:
IllegalArgumentException
- if parent is not null and not already added to this structureIllegalArgumentException
- if the item has already been added to this structureNullPointerException
- if item is null
-
addItems
public TreeData<T> addItems(T parent, T... items)
Adds a list of data items as children ofparent
. Call withnull
as parent to add root level items. The given parent item must already exist in this structure, and an item can only be added to this structure once.- Parameters:
parent
- the parent item for which the items are added as childrenitems
- the list of items to add- Returns:
- this
- Throws:
IllegalArgumentException
- if parent is not null and not already added to this structureIllegalArgumentException
- if any of the given items have already been added to this structureNullPointerException
- if any of the items are null
-
addItems
public TreeData<T> addItems(T parent, Collection<T> items)
Adds a list of data items as children ofparent
. Call withnull
as parent to add root level items. The given parent item must already exist in this structure, and an item can only be added to this structure once.- Parameters:
parent
- the parent item for which the items are added as childrenitems
- the collection of items to add- Returns:
- this
- Throws:
IllegalArgumentException
- if parent is not null and not already added to this structureIllegalArgumentException
- if any of the given items have already been added to this structureNullPointerException
- if any of the items are null
-
addItems
public TreeData<T> addItems(T parent, Stream<T> items)
Adds data items contained in a stream as children ofparent
. Call withnull
as parent to add root level items. The given parent item must already exist in this structure, and an item can only be added to this structure once.- Parameters:
parent
- the parent item for which the items are added as childrenitems
- stream of items to add- Returns:
- this
- Throws:
IllegalArgumentException
- if parent is not null and not already added to this structureIllegalArgumentException
- if any of the given items have already been added to this structureNullPointerException
- if any of the items are null
-
addItems
public TreeData<T> addItems(Collection<T> rootItems, ValueProvider<T,Collection<T>> childItemProvider)
Adds the given items as root items and uses the given value provider to recursively populate children of the root items.- Parameters:
rootItems
- the root items to addchildItemProvider
- the value provider used to recursively populate this TreeData from the given root items- Returns:
- this
-
addItems
public TreeData<T> addItems(Stream<T> rootItems, ValueProvider<T,Stream<T>> childItemProvider)
Adds the given items as root items and uses the given value provider to recursively populate children of the root items.- Parameters:
rootItems
- the root items to addchildItemProvider
- the value provider used to recursively populate this TreeData from the given root items- Returns:
- this
-
removeItem
public TreeData<T> removeItem(T item)
Remove a given item from this structure. Additionally, this will recursively remove any descendants of the item.- Parameters:
item
- the item to remove, or null to clear all data- Returns:
- this
- Throws:
IllegalArgumentException
- if the item does not exist in this structure
-
clear
public TreeData<T> clear()
Clear all items from this structure. Shorthand for callingremoveItem(Object)
with null.- Returns:
- this
-
getRootItems
public List<T> getRootItems()
Gets the root items of this structure.- Returns:
- an unmodifiable list of root items of this structure
-
getChildren
public List<T> getChildren(T item)
Get the immediate child items for the given item.- Parameters:
item
- the item for which to retrieve child items for, null to retrieve all root items- Returns:
- an unmodifiable list of child items for the given item
- Throws:
IllegalArgumentException
- if the item does not exist in this structure
-
getParent
public T getParent(T item)
Get the parent item for the given item.- Parameters:
item
- the item for which to retrieve the parent item for- Returns:
- parent item for the given item or
null
if the item is a root item. - Throws:
IllegalArgumentException
- if the item does not exist in this structure- Since:
- 8.1.1
-
setParent
public void setParent(T item, T parent)
Moves an item to become a child of the given parent item. The new parent item must exist in the hierarchy. Setting the parent tonull
makes the item a root item. After making changes to the tree data,AbstractDataProvider.refreshAll()
should be called.- Parameters:
item
- the item to be set as the child ofparent
parent
- the item to be set as parent ornull
to set the item as root- Since:
- 8.1
-
moveAfterSibling
public void moveAfterSibling(T item, T sibling)
Moves an item to the position immediately after a sibling item. The two items must have the same parent. After making changes to the tree data,AbstractDataProvider.refreshAll()
should be called.- Parameters:
item
- the item to be movedsibling
- the item after which the moved item will be located, ornull
to move item to first position- Since:
- 8.1
-
contains
public boolean contains(T item)
Check whether the given item is in this hierarchy.- Parameters:
item
- the item to check- Returns:
true
if the item is in this hierarchy,false
if not
-
-