-
Notifications
You must be signed in to change notification settings - Fork 729
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename HierarchyData and InMemoryHierarchicalDataProvider, introduce …
…HasHierarchicalDataProvider Additionally adds a helper method for recursive constructing TreeData with a child item provider.
- Loading branch information
Showing
23 changed files
with
338 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
server/src/main/java/com/vaadin/data/HasHierarchicalDataProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package com.vaadin.data; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import com.vaadin.data.provider.DataProvider; | ||
import com.vaadin.data.provider.HierarchicalDataProvider; | ||
import com.vaadin.data.provider.TreeDataProvider; | ||
|
||
public interface HasHierarchicalDataProvider<T> extends HasDataProvider<T> { | ||
|
||
/** | ||
* TODO | ||
* | ||
* @return TODO | ||
* @throws IllegalStateException | ||
* if TODO | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public default TreeData<T> getTreeData() { | ||
if (getDataProvider() instanceof TreeDataProvider) { | ||
return ((TreeDataProvider<T>) getDataProvider()).getTreeData(); | ||
} else { | ||
throw new IllegalStateException(""); | ||
} | ||
} | ||
|
||
/** | ||
* TODO | ||
* | ||
* @param rootItems | ||
* TODO | ||
* @param childItemProvider | ||
* TODO | ||
*/ | ||
public default void setItems(Collection<T> rootItems, | ||
ValueProvider<T, Collection<T>> childItemProvider) { | ||
Objects.requireNonNull(rootItems, "Given root items may not be null"); | ||
Objects.requireNonNull(childItemProvider, | ||
"Given child item provider may not be null"); | ||
setDataProvider(new TreeDataProvider<>( | ||
new TreeData<T>().addItems(rootItems, childItemProvider))); | ||
} | ||
|
||
/** | ||
* Sets the data items of this component provided as a collection. | ||
* <p> | ||
* The provided items are wrapped into a {@link TreeDataProvider} backed by | ||
* a flat {@link TreeData} structure. The data provider instance is used as | ||
* a parameter for the {@link #setDataProvider(DataProvider)} method. It | ||
* means that the items collection can be accessed later on via | ||
* {@link #getTreeData()}: | ||
* | ||
* <pre> | ||
* <code> | ||
* HasHierarchicalDataProvider<String> treeGrid = new TreeGrid<>(); | ||
* treeGrid.setItems(Arrays.asList("a","b")); | ||
* ... | ||
* | ||
* TreeData<String> data = treeGrid.getTreeData(); | ||
* </code> | ||
* </pre> | ||
* <p> | ||
* The returned HierarchyData instance may be used as-is to add, remove or | ||
* modify items in the hierarchy. These modifications to the object are not | ||
* automatically reflected back to the TreeGrid. Items modified should be | ||
* refreshed with {@link HierarchicalDataProvider#refreshItem(Object)} and | ||
* when adding or removing items | ||
* {@link HierarchicalDataProvider#refreshAll()} should be called. | ||
* | ||
* @param items | ||
* the data items to display, not {@code null} | ||
*/ | ||
@Override | ||
public default void setItems(Collection<T> items) { | ||
Objects.requireNonNull(items, "Given collection may not be null"); | ||
setDataProvider(new TreeDataProvider<>( | ||
new TreeData<T>().addItems(null, items))); | ||
} | ||
|
||
/** | ||
* Sets the data items of this component provided as a stream. | ||
* <p> | ||
* The provided items are wrapped into a {@link TreeDataProvider} backed by | ||
* a flat {@link TreeData} structure. The data provider instance is used as | ||
* a parameter for the {@link #setDataProvider(DataProvider)} method. It | ||
* means that the items collection can be accessed later on via | ||
* {@link #getTreeData()}: | ||
* | ||
* <pre> | ||
* <code> | ||
* HasHierarchicalDataProvider<String> treeGrid = new TreeGrid<>(); | ||
* treeGrid.setItems(Stream.of("a","b")); | ||
* ... | ||
* | ||
* TreeData<String> data = treeGrid.getTreeData(); | ||
* </code> | ||
* </pre> | ||
* <p> | ||
* The returned HierarchyData instance may be used as-is to add, remove or | ||
* modify items in the hierarchy. These modifications to the object are not | ||
* automatically reflected back to the TreeGrid. Items modified should be | ||
* refreshed with {@link HierarchicalDataProvider#refreshItem(Object)} and | ||
* when adding or removing items | ||
* {@link HierarchicalDataProvider#refreshAll()} should be called. | ||
* | ||
* @param items | ||
* the data items to display, not {@code null} | ||
*/ | ||
@Override | ||
public default void setItems(Stream<T> items) { | ||
Objects.requireNonNull(items, "Given stream may not be null"); | ||
setItems(items.collect(Collectors.toList())); | ||
} | ||
|
||
/** | ||
* Sets the data items of this listing. | ||
* <p> | ||
* The provided items are wrapped into a {@link TreeDataProvider} backed by | ||
* a flat {@link TreeData} structure. The data provider instance is used as | ||
* a parameter for the {@link #setDataProvider(DataProvider)} method. It | ||
* means that the items collection can be accessed later on via | ||
* {@link #getTreeData()}: | ||
* | ||
* <pre> | ||
* <code> | ||
* TreeGrid<String> treeGrid = new TreeGrid<>(); | ||
* treeGrid.setItems("a","b"); | ||
* ... | ||
* | ||
* TreeData<String> data = treeGrid.getTreeData(); | ||
* </code> | ||
* </pre> | ||
* <p> | ||
* The returned HierarchyData instance may be used as-is to add, remove or | ||
* modify items in the hierarchy. These modifications to the object are not | ||
* automatically reflected back to the TreeGrid. Items modified should be | ||
* refreshed with {@link HierarchicalDataProvider#refreshItem(Object)} and | ||
* when adding or removing items | ||
* {@link HierarchicalDataProvider#refreshAll()} should be called. | ||
* | ||
* @param items | ||
* the data items to display, not {@code null} | ||
*/ | ||
@Override | ||
public default void setItems(@SuppressWarnings("unchecked") T... items) { | ||
Objects.requireNonNull(items, "Given items may not be null"); | ||
setItems(Arrays.asList(items)); | ||
} | ||
} |
Oops, something went wrong.