diff options
author | Aleksi Hietanen <aleksi@vaadin.com> | 2017-05-11 15:48:08 +0300 |
---|---|---|
committer | Henri Sara <henri.sara@gmail.com> | 2017-05-16 15:35:17 +0300 |
commit | ec7d17f90349ba44b63f01721eeaad6cb36c81c1 (patch) | |
tree | bbcfa7329ec697120f0a5282e7d14f3120bfcda2 /server/src/main | |
parent | f5004fe3ce3b979060499390000b47003b4671de (diff) | |
download | vaadin-framework-ec7d17f90349ba44b63f01721eeaad6cb36c81c1.tar.gz vaadin-framework-ec7d17f90349ba44b63f01721eeaad6cb36c81c1.zip |
Add convenience constructors to TreeGrid
Diffstat (limited to 'server/src/main')
-rw-r--r-- | server/src/main/java/com/vaadin/data/provider/TreeDataProvider.java | 4 | ||||
-rw-r--r-- | server/src/main/java/com/vaadin/ui/TreeGrid.java | 118 |
2 files changed, 120 insertions, 2 deletions
diff --git a/server/src/main/java/com/vaadin/data/provider/TreeDataProvider.java b/server/src/main/java/com/vaadin/data/provider/TreeDataProvider.java index b30a286afa..cce1af22d0 100644 --- a/server/src/main/java/com/vaadin/data/provider/TreeDataProvider.java +++ b/server/src/main/java/com/vaadin/data/provider/TreeDataProvider.java @@ -51,9 +51,11 @@ public class TreeDataProvider<T> * visible through this data provider. * * @param treeData - * the backing {@link TreeData} for this provider + * the backing {@link TreeData} for this provider, not + * {@code null} */ public TreeDataProvider(TreeData<T> treeData) { + Objects.requireNonNull(treeData, "treeData cannot be null"); this.treeData = treeData; } diff --git a/server/src/main/java/com/vaadin/ui/TreeGrid.java b/server/src/main/java/com/vaadin/ui/TreeGrid.java index a6b0fecce5..a55cd6aef3 100644 --- a/server/src/main/java/com/vaadin/ui/TreeGrid.java +++ b/server/src/main/java/com/vaadin/ui/TreeGrid.java @@ -21,12 +21,17 @@ import java.util.Collection; import java.util.List; import java.util.Objects; import java.util.Optional; +import java.util.stream.Stream; import org.jsoup.nodes.Attributes; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; +import com.vaadin.data.BeanPropertySet; import com.vaadin.data.HasHierarchicalDataProvider; +import com.vaadin.data.HasValue; +import com.vaadin.data.PropertyDefinition; +import com.vaadin.data.PropertySet; import com.vaadin.data.TreeData; import com.vaadin.data.ValueProvider; import com.vaadin.data.provider.DataProvider; @@ -63,8 +68,71 @@ import com.vaadin.ui.renderers.Renderer; public class TreeGrid<T> extends Grid<T> implements HasHierarchicalDataProvider<T> { + /** + * Creates a new {@code TreeGrid} without support for creating columns based + * on property names. Use an alternative constructor, such as + * {@link TreeGrid#TreeGrid(Class)}, to create a {@code TreeGrid} that + * automatically sets up columns based on the type of presented data. + */ public TreeGrid() { - super(new HierarchicalDataCommunicator<>()); + this(new HierarchicalDataCommunicator<>()); + } + + /** + * Creates a new {@code TreeGrid} that uses reflection based on the provided + * bean type to automatically set up an initial set of columns. All columns + * will be configured using the same {@link Object#toString()} renderer that + * is used by {@link #addColumn(ValueProvider)}. + * + * @param beanType + * the bean type to use, not {@code null} + */ + public TreeGrid(Class<T> beanType) { + this(BeanPropertySet.get(beanType), + new HierarchicalDataCommunicator<>()); + } + + /** + * Creates a new {@code TreeGrid} using the given + * {@code HierarchicalDataProvider}. + * + * @param dataProvider + * the data provider, not {@code null} + */ + public TreeGrid(HierarchicalDataProvider<T, ?> dataProvider) { + this(); + setDataProvider(dataProvider); + } + + /** + * Creates a {@code TreeGrid} using the given in-memory data. + * + * @see TreeData + * + * @param data + * the data to use, not {@code null} + */ + public TreeGrid(TreeData<T> data) { + this(); + setDataProvider(new TreeDataProvider<>(data)); + } + + /** + * Creates a grid using a custom {@link PropertySet} implementation and + * custom data communicator. + * <p> + * Property set is used for configuring the initial columns and resolving + * property names for {@link #addColumn(String)} and + * {@link Column#setEditorComponent(HasValue)}. + * + * @param propertySet + * the property set implementation to use, not {@code null} + * @param dataCommunicator + * the data communicator to use, not {@code null} + */ + protected TreeGrid(PropertySet<T> propertySet, + HierarchicalDataCommunicator<T> dataCommunicator) { + super(propertySet, dataCommunicator); registerRpc(new NodeCollapseRpc() { @Override @@ -99,6 +167,54 @@ public class TreeGrid<T> extends Grid<T> } /** + * Creates a new TreeGrid with the given data communicator and without + * support for creating columns based on property names. + * + * @param dataCommunicator + * the custom data communicator to set + */ + protected TreeGrid(HierarchicalDataCommunicator<T> dataCommunicator) { + this(new PropertySet<T>() { + @Override + public Stream<PropertyDefinition<T, ?>> getProperties() { + // No columns configured by default + return Stream.empty(); + } + + @Override + public Optional<PropertyDefinition<T, ?>> getProperty(String name) { + throw new IllegalStateException( + "A TreeGrid created without a bean type class literal or a custom property set" + + " doesn't support finding properties by name."); + } + }, dataCommunicator); + } + + /** + * Creates a {@code TreeGrid} using a custom {@link PropertySet} + * implementation for creating a default set of columns and for resolving + * property names with {@link #addColumn(String)} and + * {@link Column#setEditorComponent(HasValue)}. + * <p> + * This functionality is provided as static method instead of as a public + * constructor in order to make it possible to use a custom property set + * without creating a subclass while still leaving the public constructors + * focused on the common use cases. + * + * @see TreeGrid#TreeGrid() + * @see TreeGrid#TreeGrid(Class) + * + * @param propertySet + * the property set implementation to use, not {@code null} + * @return a new tree grid using the provided property set, not {@code null} + */ + public static <BEAN> TreeGrid<BEAN> withPropertySet( + PropertySet<BEAN> propertySet) { + return new TreeGrid<BEAN>(propertySet, + new HierarchicalDataCommunicator<>()); + } + + /** * Adds an ExpandListener to this TreeGrid. * * @see ExpandEvent |