private void compactNodes(T node) {
    for (int i = 0; i < node.getChildCount(); i++) {
      MPSTreeNode child = (MPSTreeNode) node.getChildAt(i);
      if (myBuilder.isNamespaceNode(child)) {
        compactNodes((T) child);
      }
    }

    if (node.getParent() != null
        && // skip root
        node.getChildCount() == 1
        && myBuilder.isNamespaceNode((MPSTreeNode) node.getChildAt(0))) {
      T child = (T) node.getChildAt(0);
      myBuilder.setName(node, myBuilder.getName(node) + "." + myBuilder.getName(child));

      Enumeration children = child.children();
      List<MPSTreeNode> oldChildren = new ArrayList<MPSTreeNode>();
      while (children.hasMoreElements()) {
        oldChildren.add((MPSTreeNode) children.nextElement());
      }
      for (MPSTreeNode c : oldChildren) {
        child.remove(c);
        node.add(c);
      }

      node.remove(child);
    }
  }
  private void sortTree(T node) {
    List<MPSTreeNode> nodes = new ArrayList<MPSTreeNode>();
    List<T> namespaces = new ArrayList<T>();
    for (int i = 0; i < node.getChildCount(); i++) {
      MPSTreeNode child = (MPSTreeNode) node.getChildAt(i);
      if (myBuilder.isNamespaceNode(child)) {
        sortTree((T) child);
        namespaces.add((T) child);
      } else {
        nodes.add(child);
      }
    }

    Collections.sort(namespaces, new ToStringComparator());
    Collections.sort(
        nodes, new jetbrains.mps.ide.projectPane.logicalview.nodes.ModuleTreeNodeComparator());

    node.removeAllChildren();

    for (T ns : namespaces) {
      node.add(ns);
    }

    for (MPSTreeNode n : nodes) {
      node.add(n);
    }
  }
  private T getSubnamespace(T sourceNode, List<String> pathElements, IOperationContext context) {
    if (pathElements.size() == 0) return sourceNode;

    String first = pathElements.get(0);
    List<String> otherElements = pathElements.subList(1, pathElements.size());

    for (int i = 0; i < sourceNode.getChildCount(); i++) {
      if (myBuilder.isNamespaceNode((MPSTreeNode) sourceNode.getChildAt(i))) {
        T child = (T) sourceNode.getChildAt(i);
        if (first.equals(myBuilder.getName(child))) {
          return getSubnamespace(child, otherElements, context);
        }
      }
    }

    IModule module = (context != null) ? context.getModule() : null;
    T newChild =
        myBuilder.createNamespaceNode(first, new ModuleChangingOperationContext(module, context));

    sourceNode.add(newChild);

    return getSubnamespace(newChild, otherElements, context);
  }