private void addNodeToTree(
      JTree.DropLocation dropLocation,
      LayerTreeModel model,
      INode node,
      boolean alreadyInTree,
      int offset) {
    TreePath p = dropLocation.getPath();
    boolean noparent = p == null;
    INode parent = null;
    int index = 0;

    if (!noparent) {
      parent = (INode) p.getLastPathComponent();
      index = dropLocation.getChildIndex();

      if (index < 0) index = parent.getChildCount();
      else index += offset;
    }

    if (alreadyInTree) {
      if (!noparent) {
        if (node == parent || nodeAncestorOf(node, parent)) return;
        if (node.getParent() == parent && index > model.getIndexOfChild(parent, node)) index--;
      }

      model.removeNodeFromParent(node, false);
    }

    if (noparent) model.addToRoot(node, true);
    else model.insertNodeInto(node, parent, index, true);
  }