private void rebuildTree() { final TreeStateSnapshot treeStateSnapshot = new TreeStateSnapshot(this); myRootNode.removeAllChildren(); myDescriptorToNodeMap.clear(); myDescriptorToNodeMap.put((TreeDescriptor) myRootNode.getUserObject(), myRootNode); // build tree for (final Breakpoint breakpoint : myBreakpoints) { CheckedTreeNode node = createNode(new BreakpointDescriptor(breakpoint)); node.setChecked(breakpoint.ENABLED); addNode(node); } // remove all package nodes with one child final int count = myRootNode.getChildCount(); final List<CheckedTreeNode> children = new ArrayList<CheckedTreeNode>(); for (int idx = 0; idx < count; idx++) { CheckedTreeNode child = (CheckedTreeNode) myRootNode.getChildAt(idx); if (!(child.getUserObject() instanceof PackageDescriptor)) { children.add(child); continue; } while (child.getUserObject() instanceof PackageDescriptor && child.getChildCount() <= 1) { child = (CheckedTreeNode) child.getChildAt(0); } if (!(child.getUserObject() instanceof PackageDescriptor)) { child = (CheckedTreeNode) child.getParent(); } for (CheckedTreeNode childToRemove = (CheckedTreeNode) child.getParent(); !childToRemove.equals(myRootNode); childToRemove = (CheckedTreeNode) childToRemove.getParent()) { myDescriptorToNodeMap.remove(childToRemove.getUserObject()); } children.add(child); } for (final CheckedTreeNode aChildren : children) { aChildren.removeFromParent(); } myRootNode.removeAllChildren(); for (final CheckedTreeNode child : children) { myRootNode.add(child); } sortChildren(myRootNode); ((DefaultTreeModel) getModel()).nodeStructureChanged(myRootNode); treeStateSnapshot.restore(this); expandPath(new TreePath(myRootNode)); }
private void sortChildren(CheckedTreeNode node) { final int childCount = node.getChildCount(); if (childCount == 0) { return; } final List<CheckedTreeNode> children = new ArrayList<CheckedTreeNode>(childCount); for (int idx = 0; idx < childCount; idx++) { children.add((CheckedTreeNode) node.getChildAt(idx)); } for (CheckedTreeNode child : children) { sortChildren(child); child.removeFromParent(); } Collections.sort(children, myNodeComparator); for (CheckedTreeNode child : children) { node.add(child); } }