Skip to content

Instantly share code, notes, and snippets.

@hakanai
Created March 2, 2010 03:20
Show Gist options
  • Save hakanai/319088 to your computer and use it in GitHub Desktop.
Save hakanai/319088 to your computer and use it in GitHub Desktop.
@Override
public void treeNodesRemoved(TreeModelEvent event)
{
Object parent = event.getTreePath().getLastPathComponent();
List<Object> children = getCachedChildren(parent);
if (children != null)
{
// Optimisation: if the children are null then the caller doesn't need to know about them
// or we would have been asked for them at some point.
Comparator<Object> comparator = getComparator();
Object[] removedChildren = event.getChildren();
removedChildren = Arrays.copyOf(removedChildren, removedChildren.length);
Arrays.sort(removedChildren, comparator);
// The event we fire out has to be in ascending order. We'll do the actual removals in reverse though.
int[] removedChildIndices = new int[removedChildren.length];
for (int i = removedChildren.length - 1; i >= 0; i--)
{
// This does work:
//int removedIndex = children.indexOf(removedChildren[i]);
// This does *NOT* work. Why?
int removedIndex = Collections.binarySearch(children, removedChildren[i], comparator);
assert removedIndex >= 0;
children.remove(removedIndex);
removedChildIndices[i] = removedIndex;
}
fireTreeNodesRemoved(event.getPath(), removedChildIndices, removedChildren);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment