POJOs in a Tree

I want to implement a UI with a splitpane. Left pane contains a tree. When an item in the tree is selected, the right pane contains a form with fields according to the selected item and allows updating it.
(I’m starting off with the similar AddressBook demo).

The items in the tree should be backed by different types of objects (POJOs) and each type has different properties. The tree itself will only show the “name” for each node. But the form should be different for each type of node.

If at all possible, I would also like the tree and the POJO properties to be filterable.

What container and item should I use (or adapt)?

Hi Philip.

I would implement an interface like

interface MyPojo { String getName(); } implementing all my beans used inside the tree. So you can work just with your interface. Your right side, the detail must have a factory creating your detai view. At first I would try to create a form view like

....
createDetailView(MyPojo bean) {
  BeanFieldGroup<MyPojo> fg = new BeanFieldGroup(bean.getClass()); // beware if you have proxies.
    // Maybe you have to extend your interface with a method like Class<? extends MyPojo> getBeanType()

  for (Object propertyId : fg.getUnboundPropertyIds()) {
    myLayout.addComponent(fg.buildAndBind(myI18nService.getText(propertyId), propertyId));
  }

}

About the filterable: I don’t know exactly how you want to filter a bean by a property not existing… but maybe I didn’t get your question right

Hi Wolfgang

Thanks, I will try that.

About filtering. My POJO’s do have properties/values, just that each node type has different ones.
Filtering should be possible on the property values of all node types.
Example tree (Company/Unit/People):

MyCompany
|_ Development
| |_ Mike
| |_ Alex
|_ Accounting
|_ Sarah

Say the node “MyCompany” has a property “address”
The node “development” unit has a property “turnaround”
The people nodes (like “Mike”) have property “birthday”

If I filter by String “1980”, I would like the tree to show all nodes which somehow contain that string, in particular the “people” nodes where the birthday contains “1980”.

I’m still struggling with the basics. For simplicity I adapted my POJOs to be beans so that I could use the provided BeanItem. But how to add those BeanItems to a HierarchicalContainer?

[font=courier new]
HierarchicalContainer ic = new HierarchicalContainer();
BeanItem<Person> elvis = new BeanItem<AddressbookUI.Person>(new Person("Elvis", "Presley", new Date(1935,0,8)));
// how to add elvis to the container here?
// addItem() and addItem(ID) will create new Items in the container
[/font]

Hi,

first the TreeTable uses HierarchicalContainer by default. So you don’t have to instantiate a HierarchicalContainer by your self.

BeanItem<Person> elvis = new BeanItem<AddressbookUI.Person>(new Person("Elvis", "Presley", new Date(1935,0,8)));
TreeTable tTable = new TreeTable();
final Object elvisItemId = tTable.addItem(elvis);

That’s all. With elvisItemId you will find your bean item in your TreeTable. But you also can just use Person instead of BeanIteam

Dear Wolfgang
I’m using Tree not TreeTable (if that’s relevant). Maybe I misunderstand, but the signature of addItem is

public Item addItem(Object itemId); So if I write

Tree tree = new Tree();
Item i = tree.addItem(new MyPojo());

the new MyPojo acts as an ID (or key) and not as a value. I still have to add properties to the returned item so that my Tree shows something. Also, the POJO’s value of equals()/hashcode() is not allowed to change over time, so that it can act as a key. My POJOs typically don’t fulfill this.
I would have expected a signature exactly like you suggest:

Object itemId addItem(Item i); I thought about using a BeanItemContainer but it forces all Beans you want to use to have the same set of properties (specifically, in the call to the construction of the internal BeanItem, it passes the property “model” in the constructor.) I want different POJOs with different properties (some of which overlap, like “name”, which is used for the caption by the tree) in my container.

Long story short: I’m still looking for a Container which

  • implements Hierarchical and Filtering
  • automatically creates the Item and its properties by introspecting Beans on add
  • allows different kind of beans to be added, creating Items which have all the properties of their corresponding Bean (not just a subset).

hi Philipp,

sorry for my wrong example. Here I tested a correct one :slight_smile:

 public final class Person {
        String name;

        public Person(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }



...........

        Tree tree = new Tree();
        Person p1 = new Person("A");
        Person p2 = new Person("A");
        tree.addItem(p1);
        tree.addItem(p2);
        tree.setItemCaption(p1, p1.getName());
        tree.setItemCaption(p2, p2.getName());
        tree.setParent(p1, p2);
        view.addComponent(tree);

You see a tree with two items added (yes the person also is the itemId). The caption is the name. So you have the object/information as the itemId and the visualisation. And on top of this, p1 is the parent of p2.

Hope this helps