Monday, August 16, 2010

Extracting selected pivot tree items

The pivot TreeView only returns selected paths. A path is a list of list indexes which refer to the underlying data model of a tree. Unfortunately, the treeview doesn't have a convenience method for extracting the items referenced by those paths. Here's that missing method:
public static List extractSelectedObjects(TreeView tv)
    {
        List retList = new ArrayList();
        
        for (Path p : tv.getSelectedPaths())
        {
            Object currBranch = tv.getTreeData();

            for (int i : p)
            {
                currBranch = ((List) currBranch).get(i);
            }
            retList.add(currBranch);
        }

        return retList;
    }