您可以从 ITreeSelection 中获取父级。例如
public class DirectoryView extends ViewPart
{
public void createPartControl(Composite parent)
{
TreeViewer treeViewer = new TreeViewer(parent);
treeViewer.setContentProvider(new DirectoryProvider());
treeViewer.setInput(new File(System.getProperty("user.home")));
treeViewer.addDoubleClickListener(new IDoubleClickListener()
{
public void doubleClick(DoubleClickEvent event)
{
ITreeSelection selection = ((ITreeSelection)event.getSelection());
System.out.println(selection.getPaths()[0].getParentPath().getLastSegment());
}
});
}
class DirectoryProvider implements ITreeContentProvider
{
public Object[] getChildren(Object parentElement)
{
File[] folders = ((File) parentElement).listFiles();
return folders == null ? new File[0] : folders;
}
public Object getParent(Object element)
{
return ((File) element).getParent();
}
public boolean hasChildren(Object element)
{
return getChildren(element).length >0;
}
public Object[] getElements(Object inputElement)
{
return getChildren(inputElement);
}
public void dispose()
{
// ignore
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput)
{
// ignore
}
}
public void setFocus()
{
// ignore
}
}