Vaadin 8 Tree with multiple dataProviders for hierarchical structure

I am using a tree to display different objects in hierarchical structure. Each element in the tree are of different object type and based on selection of the tree node i display different view in UI. Here is an example of Tree:-

* Connection1
 |_User1
    |_Environment1
	|_Environment2
* Connection2
  |_User2
    |_Environment3
	|_Environment4

Current Tree code allows only one dataProvider of specific object type.
How do I handle the about usecase, I dont want to convert these objects to string and display tree of string. What is the alternate approach? This is a basic tree usecase.

You will need a wrapper object in that case - something that contains a reference to the actual object, but allows you to specify just a single data type for the Tree. Either that or your data objects could implement a common interface.

-Olli

Olli Tietäväinen:
You will need a wrapper object in that case - something that contains a reference to the actual object, but allows you to specify just a single data type for the Tree. Either that or your data objects could implement a common interface.

-Olli

Thanks Olli for quick reply. Cant we not implement HierarchicalDataProvider?? and do //connectionTree.setDataProvider(new ConnectionTreeDataProvider()); and connectionTree.setData(connectionList);
Unfortunately, I am finding it hard to find good example for HierarchicalDataProvider. Appreciate if you could give me good example for this problem.

It’s not just about the DataProvider, I think you would need to reimplement the whole DataCommunicator mechanism too. I’m not saying it’s not possible, but you will have an easier time by using a wrapper.

Olli Tietäväinen:
You will need a wrapper object in that case - something that contains a reference to the actual object, but allows you to specify just a single data type for the Tree. Either that or your data objects could implement a common interface.

-Olli

Thank you for the solution, implemented a common interface for all objects in the tree and using single data provider I was able to get the tree loaded.

SDS SDS:

Olli Tietäväinen:
You will need a wrapper object in that case - something that contains a reference to the actual object, but allows you to specify just a single data type for the Tree. Either that or your data objects could implement a common interface.

-Olli

Thank you for the solution, implemented a common interface for all objects in the tree and using single data provider I was able to get the tree loaded.

I am trying to add multiple Connection on add new Connection Action. Should I reconstruct the tree data provider from Root node?? I am doing the below code

Collection<ABSWrapper> aliasList = AbsUI.get().getAliasManager().getABSWrapper();

		TreeData<ABSWrapper> data = new TreeData<>();
		// add root level items
		data.addItems(null, aliasList);

		for (ABSWrapper alias : aliasList) {

			User user = alias.getUser();
			data.addItem(alias, user);

			List<ABSWrapperEnvironment> userEnvList = user.getUserWrapperEnvList();

			for (ABSWrapperEnvironment absWrapperEnvironment : userEnvList) {
				data.addItem(user, absWrapperEnvironment);
			}

		}
		// construct the data provider for the hierarchical data we've built
		TreeDataProvider<ABSWrapper> dataProvider = new TreeDataProvider<>(data);

		connectionTree.setDataProvider(dataProvider);

However it is throwing java.lang.IllegalArgumentException: Cannot add the same item multiple times: user1
at com.vaadin.data.TreeData.addItem(TreeData.java:166)

How do i solve this problem?? Appreciate if you could provide links to examples or document which will help me solve these problems.

You will need to provide working equals and hashCode methods for your wrapper class; you can’t have identical objects in the hierarchy.