private CheckedTreeNode insertNewTool(final CheckedTreeNode groupNode, final Tool toolCopy) {
   CheckedTreeNode toolNode = new CheckedTreeNode(toolCopy);
   toolNode.setChecked(toolCopy.isEnabled());
   ((ToolsGroup) groupNode.getUserObject()).addElement(toolCopy);
   groupNode.add(toolNode);
   nodeWasInserted(toolNode);
   return toolNode;
 }
 /**
  * @param parentDescriptor a descriptor of the childNode (possibly not existing) to attach to
  * @param childNode the childNode to be attached
  * @return either parent node if it has just been created or null, if the node has been attached
  *     to already existing childNode
  */
 private CheckedTreeNode attachNodeToParent(
     final TreeDescriptor parentDescriptor, final CheckedTreeNode childNode) {
   CheckedTreeNode parentNode = myDescriptorToNodeMap.get(parentDescriptor);
   try {
     if (parentNode != null) {
       parentNode.add(childNode);
       return null; // added to already existing, so stop iteration over appenders
     }
     parentNode = createNode(parentDescriptor);
     parentNode.add(childNode);
     return parentNode;
   } finally {
     if (parentNode != null && parentNode.getChildCount() == 1) {
       expandPath(new TreePath(parentNode.getPath()));
     }
   }
 }
  private CheckedTreeNode insertNewGroup(final ToolsGroup groupCopy) {
    CheckedTreeNode root = getTreeRoot();
    CheckedTreeNode groupNode = new CheckedTreeNode(groupCopy);
    root.add(groupNode);
    for (Tool tool : groupCopy.getElements()) {
      insertNewTool(groupNode, tool);
    }

    return groupNode;
  }
 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);
   }
 }