summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/src/main/java/com/vaadin/data/TreeData.java19
-rw-r--r--server/src/test/java/com/vaadin/data/provider/TreeDataProviderTest.java8
2 files changed, 27 insertions, 0 deletions
diff --git a/server/src/main/java/com/vaadin/data/TreeData.java b/server/src/main/java/com/vaadin/data/TreeData.java
index bebf312614..1a5b0e4d3a 100644
--- a/server/src/main/java/com/vaadin/data/TreeData.java
+++ b/server/src/main/java/com/vaadin/data/TreeData.java
@@ -368,6 +368,25 @@ public class TreeData<T> implements Serializable {
}
/**
+ * Get the parent item for the given item.
+ *
+ * @param item
+ * the item for which to retrieve the parent item for
+ * @return parent item for the given item or {@code null} if the item is a
+ * root item.
+ * @throws IllegalArgumentException
+ * if the item does not exist in this structure
+ * @since 8.1.1
+ */
+ public T getParent(T item) {
+ if (!contains(item)) {
+ throw new IllegalArgumentException(
+ "Item '" + item + "' not in hierarchy");
+ }
+ return itemToWrapperMap.get(item).getParent();
+ }
+
+ /**
* Moves an item to become a child of the given parent item. The new parent
* item must exist in the hierarchy. Setting the parent to {@code null}
* makes the item a root item. After making changes to the tree data, {@link
diff --git a/server/src/test/java/com/vaadin/data/provider/TreeDataProviderTest.java b/server/src/test/java/com/vaadin/data/provider/TreeDataProviderTest.java
index 8232d2fc45..3ff94e9988 100644
--- a/server/src/test/java/com/vaadin/data/provider/TreeDataProviderTest.java
+++ b/server/src/test/java/com/vaadin/data/provider/TreeDataProviderTest.java
@@ -81,6 +81,14 @@ public class TreeDataProviderTest
}
@Test
+ public void treeData_get_parent() {
+ StrBean root = rootData.get(0);
+ StrBean firstChild = data.getChildren(root).get(0);
+ Assert.assertNull(data.getParent(root));
+ Assert.assertEquals(root, data.getParent(firstChild));
+ }
+
+ @Test
public void treeData_set_parent() {
StrBean item1 = rootData.get(0);
StrBean item2 = rootData.get(1);