From 04e7259fb497e47bcd6d664e87c243db5badd934 Mon Sep 17 00:00:00 2001 From: Teemu Suo-Anttila Date: Tue, 16 May 2017 09:36:22 +0300 Subject: Add events and functionality to Tree (#9318) * Add ItemClick event to Tree * Add collapse provider and style generator to Tree --- server/src/main/java/com/vaadin/ui/Tree.java | 116 +++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) (limited to 'server') 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 extends Composite implements HasDataProvider { + @Deprecated + private static final Method ITEM_CLICK_METHOD = ReflectTools + .findMethod(ItemClickListener.class, "itemClick", ItemClick.class); + + /** + * A listener for item click events. + * + * @param + * the tree item type + * + * @see ItemClick + * @see Registration + * @since 8.1 + */ + @FunctionalInterface + public interface ItemClickListener 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 event); + } + + /** + * Tree item click event. + * + * @param + * the data type of tree + * @since 8.1 + */ + public static class ItemClick extends ConnectorEvent { + + private final T item; + + /** + * Constructs a new item click. + * + * @param source + * the tree component + * @param item + * the clicked item + */ + protected ItemClick(Tree 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 getSource() { + return (Tree) super.getSource(); + } + } + /** * String renderer that handles icon resources and stores their identifiers * into data objects. @@ -142,6 +213,8 @@ public class Tree extends Composite implements HasDataProvider { e.isUserOriginated())); treeGrid.addCollapseListener(e -> fireCollapseEvent( e.getCollapsedItem(), e.isUserOriginated())); + treeGrid.addItemClickListener( + e -> fireEvent(new ItemClick<>(this, e.getItem()))); } /** @@ -388,6 +461,49 @@ public class Tree extends Composite implements HasDataProvider { treeGrid.getDataCommunicator().reset(); } + /** + * Sets the item collapse allowed provider for this Tree. The provider + * should return {@code true} for any item that the user can collapse. + *

+ * Note: 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 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 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 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. -- cgit v1.2.3