diff options
author | Aleksi Hietanen <aleksi@vaadin.com> | 2017-05-16 11:57:02 +0300 |
---|---|---|
committer | Pekka Hyvönen <pekka@vaadin.com> | 2017-05-16 11:57:02 +0300 |
commit | efa7f5a4d069556061ba4ceef4fb4d76dae84ef4 (patch) | |
tree | 767b0fdb3146930919cec37e5eaab75422b0867d /documentation | |
parent | eb743d965278d263a4c496bb4e39c067fe2b1a8c (diff) | |
download | vaadin-framework-efa7f5a4d069556061ba4ceef4fb4d76dae84ef4.tar.gz vaadin-framework-efa7f5a4d069556061ba4ceef4fb4d76dae84ef4.zip |
Refactor common methods in in-memory data providers (#9308)
* Refactor common methods of InMemoryHierarchicalDataProvider and ListDataProvider to a single interface
* Rename HierarchyData and InMemoryHierarchicalDataProvider, introduce HasHierarchicalDataProvider
* Additionally adds a helper method for recursive constructing
TreeData with a child item provider.
Diffstat (limited to 'documentation')
-rw-r--r-- | documentation/components/components-tree.asciidoc | 28 | ||||
-rw-r--r-- | documentation/components/components-treegrid.asciidoc | 16 | ||||
-rw-r--r-- | documentation/datamodel/datamodel-hierarchical.asciidoc | 22 |
3 files changed, 33 insertions, 33 deletions
diff --git a/documentation/components/components-tree.asciidoc b/documentation/components/components-tree.asciidoc index dd957a0308..f81342c5ba 100644 --- a/documentation/components/components-tree.asciidoc +++ b/documentation/components/components-tree.asciidoc @@ -31,32 +31,32 @@ image::img/tree-basic.png[width=70%, scaledwidth=100%] [[components.tree.data]] == Binding to Data -[classname]#Tree# is used by binding it to a hierarchical data provider. The data provider can be based on in-memory or back end data. For in-memory data, the [classname]#InMemoryHierarchicalDataProvider# can be used, and for loading data from a back end, you need to implement three methods from the [interfacename]#HierarchicalDataProvider# interface. Usage of both data providers is described in +[classname]#Tree# is used by binding it to a hierarchical data provider. The data provider can be based on in-memory or back end data. For in-memory data, the [classname]#TreeDataProvider# can be used, and for loading data from a back end, you need to implement three methods from the [interfacename]#HierarchicalDataProvider# interface. Usage of both data providers is described in <<dummy/../../../framework/datamodel/datamodel-hierarchical.asciidoc#datamodel.hierarchical,"Hierarchical Data">>. -The [classname]#HierarchyData# class can be used to build the hierarchical data structure, -and it can then be passed on to [classname]#InMemoryHierarchicalDataProvider#. It is simply a hierarchical +The [classname]#TreeData# class can be used to build the hierarchical data structure, +and it can then be passed on to [classname]#TreeDataProvider#. It is simply a hierarchical collection, that the data provider uses to populate the [classname]#Tree#. The [methodname]#setItems# method in [classname]#Tree# can be used to set the root level items. Internally -an [classname]#InMemoryHierarchicalDataProvider# with [classname]#HierarchyData# is used. +an [classname]#TreeDataProvider# with [classname]#TreeData# is used. [source, java] ---- // An initial planet tree Tree<String> tree = new Tree<>(); -HierarchyData<String> hierarchyData = new HierarchyData<>(); +TreeData<String> treeData = new TreeData<>(); // Couple of childless root items -hierarchyData.addItem(null,"Mercury"); -hierarchyData.addItem(null,"Venus"); +treeData.addItem(null,"Mercury"); +treeData.addItem(null,"Venus"); // Items with hierarchy -hierarchyData.addItem(null,"Earth"); -hierarchyData.addItem("Earth","The Moon"); +treeData.addItem(null,"Earth"); +treeData.addItem("Earth","The Moon"); -inMemoryDataProvider = new InMemoryHierarchicalDataProvider<>(hierarchyData); +inMemoryDataProvider = new TreeDataProvider<>(treeData); tree.setDataProvider(inMemoryDataProvider); tree.expand("Earth"); // Expand programmatically ---- @@ -67,9 +67,9 @@ the in-memory data in the tree, you may do it as follows: [source, java] ---- // Add Mars with satellites -hierarchyData.addItem(null, "Mars"); -hierarchyData.addItem("Mars", "Phobos"); -hierarchyData.addItem("Mars", "Deimos"); +treeData.addItem(null, "Mars"); +treeData.addItem("Mars", "Phobos"); +treeData.addItem("Mars", "Deimos"); inMemoryDataProvider.refreshAll(); ---- @@ -208,4 +208,4 @@ You could thereby define the item styling as follows: font-style: italic; } ---- -////
\ No newline at end of file +//// diff --git a/documentation/components/components-treegrid.asciidoc b/documentation/components/components-treegrid.asciidoc index d5659cb556..7f444602d9 100644 --- a/documentation/components/components-treegrid.asciidoc +++ b/documentation/components/components-treegrid.asciidoc @@ -30,7 +30,7 @@ image::img/tree-grid-basic.png[width=70%, scaledwidth=100%] [[components.treegrid.data]] == Binding to Data -[classname]#TreeGrid# is used by binding it to a hierarchical data provider. The data provider can be based on in-memory or back end data. For in-memory data, the [classname]#InMemoryHierarchicalDataProvider# can be used, and for loading data from a back end, you need to implement three methods from the [interfacename]#HierarchicalDataProvider# interface. Usage of both data providers is described in +[classname]#TreeGrid# is used by binding it to a hierarchical data provider. The data provider can be based on in-memory or back end data. For in-memory data, the [classname]#TreeDataProvider# can be used, and for loading data from a back end, you need to implement three methods from the [interfacename]#HierarchicalDataProvider# interface. Usage of both data providers is described in <<dummy/../../../framework/datamodel/datamodel-hierarchical.asciidoc#datamodel.hierarchical,"Hierarchical Data">>. Populating a [classname]#TreeGrid# with in-memory data can be done as follows @@ -39,7 +39,7 @@ Populating a [classname]#TreeGrid# with in-memory data can be done as follows ---- Project rootProject = getRootRroject(); -HierarchyData<Project> data = new HierarchyData<>(); +TreeData<Project> data = new TreeData<>(); // add a root level item with null parent data.addItem(null, rootProject); @@ -48,7 +48,7 @@ rootProject.flattened().forEach( project -> data.addItems(project, project.getSubProjects())); TreeGrid<Project> treeGrid = new TreeGrid<>(); -treeGrid.setDataProvider(new InMemoryHierarchicalDataProvider<>(data)); +treeGrid.setDataProvider(new TreeDataProvider<>(data)); // the first column gets the hierarchy indicator by default treeGrid.addColumn(Project::getName).setCaption("Project Name"); @@ -56,18 +56,18 @@ treeGrid.addColumn(Project::getHoursDone).setCaption("Hours Done"); treeGrid.addColumn(Project::getLastModified).setCaption("Last Modified"); ---- -The [classname]#HierarchyData# class can be used to build the hierarchical data structure, -and it can then be passed on to [classname]#InMemoryHierarchicalDataProvider#. It is simply a hierarchical +The [classname]#TreeData# class can be used to build the hierarchical data structure, +and it can then be passed on to [classname]#TreeDataProvider#. It is simply a hierarchical collection, that the data provider uses to populate the [classname]#TreeGrid#. The [methodname]#setItems# method in [classname]#TreeGrid# can be used to set the root level items. Internally -an [classname]#InMemoryHierarchicalDataProvider# with [classname]#HierarchyData# is used. If at any time you want to modify the in-memory data in the grid, you may do it as follows +an [classname]#TreeDataProvider# with [classname]#TreeData# is used. If at any time you want to modify the in-memory data in the grid, you may do it as follows [source, java] ---- -InMemoryHierarchicalDataProvider<Project> dataProvider = (InMemoryHierarchicalDataProvider<Project>) treeGrid.getDataProvider(); +TreeDataProvider<Project> dataProvider = (TreeDataProvider<Project>) treeGrid.getDataProvider(); -HierarchyData<Project> data = dataProvider.getData(); +TreeData<Project> data = dataProvider.getData(); // add new items data.addItem(null, newProject); data.addItems(newProject, newProject.getChildren()); diff --git a/documentation/datamodel/datamodel-hierarchical.asciidoc b/documentation/datamodel/datamodel-hierarchical.asciidoc index 735317c519..f405bc775f 100644 --- a/documentation/datamodel/datamodel-hierarchical.asciidoc +++ b/documentation/datamodel/datamodel-hierarchical.asciidoc @@ -11,21 +11,21 @@ IMPORTANT: The [interfacename]#HierarchicalDataProvider# is currently being deve The [classname]#Tree# and the [classname]#TreeGrid# components allow you to show data with hierarchical relationships between items. That data can be populated by on-demand from a back end by implementing the [interfacename]#HierarchicalDataProvider# interface. If you have the data available in-memory on the server, -you use the collection style API of [classname]#HierarchyData# and then pass it to a [classname]#InMemoryHierarchicalDataProvider#. This chapter introduces the hierarchical data providers and how they work. +you use the collection style API of [classname]#TreeData# and then pass it to a [classname]#TreeDataProvider#. This chapter introduces the hierarchical data providers and how they work. For using them with the components you should see <<dummy/../../../framework/components/components-tree.asciidoc#components.tree,"Tree">> and <<dummy/../../../framework/components/components-treegrid.asciidoc#components.treegrid,"TreeGrid">> documentation. == In-memory Hierarchical Data -When the hierarchical data is available in the server side memory, you can use it to populate the [classname]#HierarchyData# that is the source of data for an [classname]#InMemoryHierarchicalDataProvider#. It contains collection style API to construct the hierarchical structure of your data, and also verifies that the hierarchy structure is valid. +When the hierarchical data is available in the server side memory, you can use it to populate the [classname]#TreeData# that is the source of data for an [classname]#TreeDataProvider#. It contains collection style API to construct the hierarchical structure of your data, and also verifies that the hierarchy structure is valid. -The following example populates a [classname]#HierarchyData# with two levels of data: +The following example populates a [classname]#TreeData# with two levels of data: [source, java] ---- Collection<Project> projects = service.getProjects(); -HierarchyData<Project> data = new HierarchyData<>(); +TreeData<Project> data = new TreeData<>(); // add root level items data.addItems(null, projects); @@ -33,16 +33,16 @@ data.addItems(null, projects); projects.forEach(project -> data.addItems(project, project.getChildren())); // construct the data provider for the hierarchical data we've built -InMemoryHierarchicalDataProvider<Project> dataProvider = new InMemoryHierarchicalDataProvider<>(data); +TreeDataProvider<Project> dataProvider = new TreeDataProvider<>(data); ---- === Updating data -When adding or removing items from the [classname]#HierarchyData#, you need to always notify the data provider that it should refresh its data. This can be done with the [methodname]#refreshAll# method in the data provider. +When adding or removing items from the [classname]#TreeData#, you need to always notify the data provider that it should refresh its data. This can be done with the [methodname]#refreshAll# method in the data provider. [source, java] ---- -HierarchyData<Project> data = dataProvider.getHierarchyData(); +TreeData<Project> data = dataProvider.getData(); data.addItem(null, newProject); data.addItems(newProject, newProject.getChildren()); @@ -55,14 +55,14 @@ dataProvider.refreshAll(); === Sorting and Filtering -For [classname]#InMemoryHierarchicalDataProvider#, both the sorting and filtering API is the same as in [classname]#ListDataProvider#. Sorting and filtering are applied separately for each hierarchy level, meaning e.g. that for a node that has not passed the filter there are no children shown. +For [classname]#TreeDataProvider#, both the sorting and filtering API is the same as in [classname]#ListDataProvider#. Sorting and filtering are applied separately for each hierarchy level, meaning e.g. that for a node that has not passed the filter there are no children shown. [source, java] ---- // setting sorting or filtering automatically refreshes the data dataProvider.setSortComparator((projectA, projectB) -> projectA.getHours().compareTo(projectB.getHours())); - + dataProvider.setFilter(project -> project.getHours() > 100); ---- @@ -72,7 +72,7 @@ The lazy loading hierarchical data, same concepts apply as with the non-hierarch To load hierarchical data on-demand from your back end, you should extend the [classname]#AbstractHierarchicalDataProvider# class. Then you just have to implement the following three methods: -* `boolean hasChildren(T item)` +* `boolean hasChildren(T item)` ** This tells the data provider whether the given item has children and should be expandable. Note that this method is called frequently and should not do any costly operations. * `int getChildCount(HierarchicalQuery<T, F> query)` @@ -87,7 +87,7 @@ To load hierarchical data on-demand from your back end, you should extend the [c ** The parent node is available in the [classname]#HierarchicalQuery# via the [methodname]#getParent# method, which returns `null` for the root level. ** This method is called whenever the data should be displayed in the UI -Note that the [classname]#HierarchicalQuery# query object contains the relevant information regarding the sorting and filtering. +Note that the [classname]#HierarchicalQuery# query object contains the relevant information regarding the sorting and filtering. The following code snippet shows a simple example on how to building a lazy hierarchical data provider based on file system structure: |