Tree get item collection as displayed

Hi everyone!

I’m trying to get all items from a tree as they are displayed on the website.
The user can change the order by using drag’n’drop. I’ve noticed that, if the user change order and the programm call tree.rootItemIds(), the order of the items is the same as before.

Does anybody know a way to get the displayed order of tree items?

Thanks

Hi Patrick Pichler.

Some days ago i had the some problem… But i wrote a simple workaround. Use my class like your HierarchicalContainer in tree.
If you need get items by displayed order, iterate over getOrderedIds() method result.
I hope that it helps you. Greetings from Brazil!

Ps. I wrote the class, but the solution is based in a post that i don’t remember the link.

package mypackage.collections;

import java.util.LinkedList;
import com.vaadin.data.util.HierarchicalContainer;

public class OrderedHierarchicalContainer extends HierarchicalContainer {

    private static final long serialVersionUID = 3561891184532542322L;

    public OrderedHierarchicalContainer() {
        super();
    }
    
    public LinkedList<Object> getOrderedIds() {
        return iterateContainer(this);
    }
    
    private LinkedList<Object> iterateContainer(HierarchicalContainer c) {
        
        LinkedList<Object> result = new LinkedList<Object>();
        
        for(Object rootItemId : c.rootItemIds()) {
            result.add((Long)rootItemId);
            iterateChildren(result, c, rootItemId, 1);
        }
        
        return result;
    }

    private void iterateChildren(LinkedList<Object> result, HierarchicalContainer c, Object parentItemId, int indent) {

        try {
            for(Object childItemId : c.getChildren(parentItemId)) {
                result.add((Object)childItemId);
                if(c.hasChildren(childItemId)) {
                    iterateChildren(result, c, childItemId, indent+1);
                }
            }            
        } catch (Exception e) {
            return;
        }
    }

}