TreeGrid and multiple dataproviders Vaadin 8.1

Been fiddling past few days with the TreeGrid and I was wondering if there is a way to represent let’s say 2 different classes in a one Grid in a way that Class A is the root object and Class B is their parent. The amount of Class B may vary. Example:

Lets say that Class A is a workstation and Class B are the employees and I’d like to show data as follows:

  • Workstation Alpha
    [list]
  • Employer 1
  • Employer 2

    [/list]
  • Workstation Bravo
    [list]
  • ​Employer 1
  • Employer 2
  • Employer 3

    [/list]

I’m not even completely sure if TreeGrid is the proper way to show such data, but I as well tried the Tree component and it seems to have same limitations where I can only use one dataprovider and then the other class can’t be used since the dataprovider is for, lets say Class A. Any ideas?

EDIT: Made some progress. Now tackled with “java.lang.IllegalArgumentException: Cannot add the same item multiple times: Document name” Error message. Code below:

Tree<String> overviewTree = new Tree<>("ID-LIST");
        TreeData<String> dataReal = new TreeData<>();
        Attribute attribute = new Attribute();
        for (Metaclass m: metaclassList) {
        m.getId();
        m.getNimike();
        dataReal.addItems(null, m.getNimike());
       
        try {
for (Attribute a:DBcore.getInstance().loadAttributesALL()) {
a.getMcid();
a.getMaid();
if (a.getMcid() == m.getId()) {
dataReal.addItems(m.getNimike(), a.getNimike());
}
}
} catch (SQLException e) {
}
        }
        
        overviewTree.setDataProvider(new TreeDataProvider<>(dataReal));
mainlayout.addComponent(overviewTree);

As Java doesn’t (still :frowning: ) support proper union types, the only suggestions I can think of right now are either using a wrapper class or some typechecking / typecasting.

-Olli

Thank you for your response. I managed to actually have a workaround once again ^^. I fiddled with the Tree component, collected my 2 classes to different lists, List and List where A and B are the classnames. Once they are lists I just go through them, pick the info I want to display and convert it to string and dispaly it in the Tree treename. However, the problem I am now facing is that Tree-component cannot add the same item multiple times. For example I have a field named “Document name” in several different objects in class B. Once I try to list all of the class B objects in the tree, I get the error “Cannot add the same item multiple times: Document name” I wonder if there is a way to allow this?

Edited: To be precise, I get the following error message: java.lang.IllegalArgumentException: Cannot add the same item multiple times: Document name.

And the code I work with is the following :

 Tree<String> overviewTree = new Tree<>("ID-LISTA FOR REAL");
        TreeData<String> dataReal = new TreeData<>();
        Attribute attribute = new Attribute();
        for (Metaclass m: metaclassList) {
        m.getId();
        m.getNimike();
        dataReal.addItems(null, m.getNimike());
       
        try {
for (Attribute a:DBcore.getInstance().loadAttributesALL()) {
a.getMcid();
a.getMaid();
if (a.getMcid() == m.getId()) {
dataReal.addItems(m.getNimike(), a.getNimike());
}
}
} catch (SQLException e) {
}
        }
        
        overviewTree.setDataProvider(new TreeDataProvider<>(dataReal));

mainlayout.addComponent(overviewTree);

Nevermind… Figured it out. Just added an id field to the item → no duplicates.

EDIT: I was in the middle of typing a response, which wasn’t needed anymore, but just for completeness’s sake:

The exception comes from here:
https://github.com/vaadin/framework/blob/master/server/src/main/java/com/vaadin/data/TreeData.java#L179
and the implementation of the contains method is here:
https://github.com/vaadin/framework/blob/master/server/src/main/java/com/vaadin/data/TreeData.java#L480
- I think because two different String objects have the same hashcode, Strings won’t work as the TreeData type very well if you can have multiple entries with the same value.

Thank you Olli. Really appreciate your effort :slight_smile: