}
}
private void updateSelectionAfterDelete(Runnable runnable) {
Tree tree = (Tree) getControl();
IStructuredSelection selection = (IStructuredSelection) getSelection();
/* Nothing to do, since no selection */
if (selection.isEmpty()) {
runnable.run();
return;
}
/* Look for the minimal Index of all selected Elements */
int minSelectedIndex = Integer.MAX_VALUE;
TreeItemAdapter parentOfMinSelected = new TreeItemAdapter(tree);
/* For each selected Element */
Object[] selectedElements = selection.toArray();
for (Object selectedElement : selectedElements) {
Widget widget = findItem(selectedElement);
if (widget instanceof TreeItem) {
TreeItem item = (TreeItem) widget;
TreeItemAdapter parent = new TreeItemAdapter(item).getParent();
int index = parent.indexOf(item);
minSelectedIndex = Math.min(minSelectedIndex, index);
if (index == minSelectedIndex)
parentOfMinSelected.setItem(parent.getItem());
}
}
/* Perform Deletion */
runnable.run();
Object data = null;
/* Parent itself has been deleted */
if (parentOfMinSelected.getItem().isDisposed())
return;
/* Restore selection to next Element */
if (parentOfMinSelected.getItemCount() > minSelectedIndex)
data = parentOfMinSelected.getItem(minSelectedIndex).getData();
/* Restore selection to last Element */
else if (parentOfMinSelected.getItemCount() > 0)
data = parentOfMinSelected.getItem(parentOfMinSelected.getItemCount() - 1).getData();
/* Restore selection on actual Element */
else
data = parentOfMinSelected.getItem().getData();
/* Apply selection */
if (data != null) {
IStructuredSelection newSelection = new StructuredSelection(data);
setSelection(newSelection);
}
}