summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorTeemu Suo-Anttila <tsuoanttila@users.noreply.github.com>2017-05-16 09:36:22 +0300
committerPekka Hyvönen <pekka@vaadin.com>2017-05-16 09:36:22 +0300
commit04e7259fb497e47bcd6d664e87c243db5badd934 (patch)
tree92a82bdc081308162205d734fe0c3e4eefe5725b /server
parente788fa906b968a298414e60cbb15cc7d6389b8a1 (diff)
downloadvaadin-framework-04e7259fb497e47bcd6d664e87c243db5badd934.tar.gz
vaadin-framework-04e7259fb497e47bcd6d664e87c243db5badd934.zip
Add events and functionality to Tree (#9318)
* Add ItemClick event to Tree * Add collapse provider and style generator to Tree
Diffstat (limited to 'server')
-rw-r--r--server/src/main/java/com/vaadin/ui/Tree.java116
1 files changed, 116 insertions, 0 deletions
diff --git a/server/src/main/java/com/vaadin/ui/Tree.java b/server/src/main/java/com/vaadin/ui/Tree.java
index 8f02c8c88f..97e1b94cec 100644
--- a/server/src/main/java/com/vaadin/ui/Tree.java
+++ b/server/src/main/java/com/vaadin/ui/Tree.java
@@ -15,6 +15,7 @@
*/
package com.vaadin.ui;
+import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
@@ -30,15 +31,19 @@ import com.vaadin.data.provider.DataProvider;
import com.vaadin.data.provider.HierarchicalDataProvider;
import com.vaadin.event.CollapseEvent;
import com.vaadin.event.CollapseEvent.CollapseListener;
+import com.vaadin.event.ConnectorEvent;
import com.vaadin.event.ExpandEvent;
import com.vaadin.event.ExpandEvent.ExpandListener;
+import com.vaadin.event.SerializableEventListener;
import com.vaadin.event.selection.SelectionListener;
import com.vaadin.server.Resource;
+import com.vaadin.server.SerializablePredicate;
import com.vaadin.shared.Registration;
import com.vaadin.shared.ui.grid.HeightMode;
import com.vaadin.shared.ui.tree.TreeRendererState;
import com.vaadin.ui.Grid.SelectionMode;
import com.vaadin.ui.renderers.AbstractRenderer;
+import com.vaadin.util.ReflectTools;
import elemental.json.JsonObject;
@@ -54,6 +59,72 @@ import elemental.json.JsonObject;
*/
public class Tree<T> extends Composite implements HasDataProvider<T> {
+ @Deprecated
+ private static final Method ITEM_CLICK_METHOD = ReflectTools
+ .findMethod(ItemClickListener.class, "itemClick", ItemClick.class);
+
+ /**
+ * A listener for item click events.
+ *
+ * @param <T>
+ * the tree item type
+ *
+ * @see ItemClick
+ * @see Registration
+ * @since 8.1
+ */
+ @FunctionalInterface
+ public interface ItemClickListener<T> extends SerializableEventListener {
+ /**
+ * Invoked when this listener receives a item click event from a Tree to
+ * which it has been added.
+ *
+ * @param event
+ * the received event, not {@code null}
+ */
+ public void itemClick(Tree.ItemClick<T> event);
+ }
+
+ /**
+ * Tree item click event.
+ *
+ * @param <T>
+ * the data type of tree
+ * @since 8.1
+ */
+ public static class ItemClick<T> extends ConnectorEvent {
+
+ private final T item;
+
+ /**
+ * Constructs a new item click.
+ *
+ * @param source
+ * the tree component
+ * @param item
+ * the clicked item
+ */
+ protected ItemClick(Tree<T> source, T item) {
+ super(source);
+ this.item = item;
+ }
+
+ /**
+ * Returns the clicked item.
+ *
+ * @return the clicked item
+ */
+ public T getItem() {
+ return item;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public Tree<T> getSource() {
+ return (Tree<T>) super.getSource();
+ }
+ }
+
/**
* String renderer that handles icon resources and stores their identifiers
* into data objects.
@@ -142,6 +213,8 @@ public class Tree<T> extends Composite implements HasDataProvider<T> {
e.isUserOriginated()));
treeGrid.addCollapseListener(e -> fireCollapseEvent(
e.getCollapsedItem(), e.isUserOriginated()));
+ treeGrid.addItemClickListener(
+ e -> fireEvent(new ItemClick<>(this, e.getItem())));
}
/**
@@ -389,6 +462,49 @@ public class Tree<T> extends Composite implements HasDataProvider<T> {
}
/**
+ * Sets the item collapse allowed provider for this Tree. The provider
+ * should return {@code true} for any item that the user can collapse.
+ * <p>
+ * <strong>Note:</strong> This callback will be accessed often when sending
+ * data to the client. The callback should not do any costly operations.
+ *
+ * @param provider
+ * the item collapse allowed provider, not {@code null}
+ */
+ public void setItemCollapseAllowedProvider(
+ SerializablePredicate<T> provider) {
+ treeGrid.setItemCollapseAllowedProvider(provider);
+ }
+
+ /**
+ * Sets the style generator that is used for generating class names for
+ * items in this tree. Returning null from the generator results in no
+ * custom style name being set.
+ *
+ * @see StyleGenerator
+ *
+ * @param styleGenerator
+ * the item style generator to set, not {@code null}
+ * @throws NullPointerException
+ * if {@code styleGenerator} is {@code null}
+ */
+ public void setStyleGenerator(StyleGenerator<T> styleGenerator) {
+ treeGrid.setStyleGenerator(styleGenerator);
+ }
+
+ /**
+ * Adds an item click listener. The listener is called when an item of this
+ * {@code Tree} is clicked.
+ *
+ * @param listener
+ * the item click listener, not null
+ * @return a registration for the listener
+ */
+ public Registration addItemClickListener(ItemClickListener<T> listener) {
+ return addListener(ItemClick.class, listener, ITEM_CLICK_METHOD);
+ }
+
+ /**
* @deprecated This component's height is always set to be undefined.
* Calling this method will have no effect.
*/