Monday, April 12, 2010

How to find the topmost element of a treepath in a TreeViewer ?

IStructuredSelection sel = (IStructuredSelection) tree
.getSelection();
List topMostElementList= new ArrayList();
TreePath[] path = ((TreeSelection) sel).getPaths();
if (path != null && path.length > 0) {
for (int i = path[0].getSegmentCount(); i > 0; i--) {
// traverses and makes sure it's a Binding, then remove
if (path[0].getSegment(i - 1) instanceof RootElement) {
topMostElementList.add((EObject) path[0].getSegment(i - 1));
}
}
return topMostElementList;

No comments: