tree related question

Hi there,
I have a question about the tree ui widget.
When I invoke tree.addItem() I pass in objects of a type that I created, so I have something like this:
tree.addItem(new MyOwnType(…withFieldsSetAsFit));
Now, I would also like to be able to retreive that and, at most cast them, into my MyOwnType. What do you think is the best way to do this?
Thanks in advance.

The
addItem()
method comes from the underlying IndexedContainer. The MyOwnType which you pass as a parameter is actually an item ID. An item ID can be any object that uniquelly identifies an item in the container - in the database world, you could consider the item ID as the primary key for a row.

Whenever you interact with the Tree and get some events, such as a ValueChangeEvent, the event contains the item ID of the select item. The item ID is of type Object, but if you have only used MyOwnTypes as item IDs, then you can simply cast the object.

public void valueChange(ValueChangeEvent event) {
   MyOwnType my = (MyOwnType) event.getProperty().getValue();
   ...
}

Hi Kim, thanks for your response.
Seems like I missed to provide more detail to my question.
on an event handler, all easy. what you provided works fine. Also, (MyOwnType)tree.getValue();, where suitable, works cool.
But, I want to have something like this:
MyOwnType treeNode = (MyOwnType)tree.getItem(someObjectOfMyOwnType);

In my particular case, I want to maintain node uniqueness in the tree, and everytime I add a child to a node, I want a field of it to increase by one. To facilitate the reasoning, let me emphasize that I my problem is at the time of building the tree, not when handling an event.

Maybe this functionality is inexistent and someone needs to implement it, which I’d be glad to do. what do you think?

getItem() takes as its parameter an item ID, which in your case would be the MyOwnType. So someObjectOfMyOwnType is your MyOwnType. I don’t quite understand what you are trying to achieve.

If you just need to update a property of the
parent
whenever you add a child, then you should have own application logic for that. For example, something in the these lines

public void addChild(MyOwnType object, MyOwnType parent) {
    Item item = container.addItem(object);
    // populate item properties

    Item parentItem = container.getItem(parent);
    if(parentItem != null) {
       parentItem.getItemProperty("counter").setValue(object.getChildren().size());
    }

    for(MyOwnType child : object.getChildren()) {
       addChild(child, object);
    }
}

well, I could do that, but then the counter is a field in the Item, and not in the itemId (which is of my own type) and I want my own type to know of the things I need it to know instead of putting information elsewhere, in this case in Item. I do think that I ought to be able to get itemIds by Item or something along these lines… a way to get back the info I gave to the tree, other than through events.

Nope, you can’t get the item id based on the item. It’s a key-value pair. As mentioned, you’ll need to tackle this problem purely in your application logic. You can always apply the same code from my previous example, but replace lines 5-8 with something like parent.increaseCounter();

… or then you make a Map yourself where the Item is the key and the item ID is the value.

yeah, seems like I’ll have to find my way around this. thanks for your time man. take care.