void AddItem(TreeLayerNode node)
    {
        layerTree.BeginUpdate();

        if (layerTree.SelectedNode != null)
        {
            Node n = layerTree.SelectedNode.Tag as Node;

            if (n.IsLeaf)
            {
                node.SetParent(n.Parent, n.Index);
            }
            else
            {
                node.SetParent(n);
            }

            layerTree.SelectedNode.Expand();
        }
        else
        {
            node.SetParent(model.Root);
        }

        layerTree.EndUpdate();
    }
    void MergeSelected()
    {
        if (layerTree.SelectedNode == null)
        {
            return;
        }

        TreeLayerNode selected = (layerTree.SelectedNode.Tag as TreeLayerNode);

        if (selected.Parent.Nodes.Count - 1 == selected.Index || !(selected.LayerNode is Layer))
        {
            return;
        }

        TreeLayerNode target = (selected.Parent.Nodes[layerTree.SelectedNode.Index + 1] as TreeLayerNode);

        if (!(target.LayerNode is Layer))
        {
            return;
        }

        (selected.LayerNode as Layer).MergeAndRemove(target.LayerNode as Layer);

        layerTree.BeginUpdate();

        activeNode = layerTree.SelectedNode.NextNode;
        Editor.SetActiveLayers(new Layer[] { target.LayerNode as Layer });

        selected.Parent = null;

        layerTree.EndUpdate();
    }
    protected void ItemDragOver(object sender, DragEventArgs e)
    {
        if (layerTree.DropPosition.Node == null)
        {
            e.Effect = e.AllowedEffect;
            return;
        }

        if (e.Data.GetDataPresent(typeof(TreeNodeAdv[])))
        {
            TreeNodeAdv[] nodes    = e.Data.GetData(typeof(TreeNodeAdv[])) as TreeNodeAdv[];
            TreeNodeAdv   dropNode = layerTree.DropPosition.Node;

            foreach (TreeNodeAdv treeNode in nodes)
            {
                TreeLayerNode node = treeNode.Tag as TreeLayerNode;
                if (node.IsParentTo(dropNode.Tag as Node) ||
                    (dropNode.IsLeaf && layerTree.DropPosition.Position == NodePosition.Inside))
                {
                    e.Effect = DragDropEffects.None;
                    return;
                }
            }

            e.Effect = e.AllowedEffect;
        }
    }
    void GenerateSample()
    {
        layerTree.BeginUpdate();

        for (int i = 0; i < 2; i++)
        {
            TreeLayerNode node = new TreeLayerNode(this, new LayerFolder("Folder " + i, Editor));
            node.SetParent(model.Root);

            for (int j = 0; j < 3; j++)
            {
                TreeLayerNode layer = new TreeLayerNode(this, new Layer("Layer " + i + "." + j, Editor));
                layer.SetParent(node);
            }
        }

        layerTree.EndUpdate();
    }
    void RemoveSelected()
    {
        layerTree.BeginUpdate();

        TreeNodeAdv[] removedLayers = new TreeNodeAdv[layerTree.SelectedNodes.Count];
        layerTree.SelectedNodes.CopyTo(removedLayers, 0);

        if (removedLayers.Length > 0)
        {
            if (MessageBox.Show("Are you sure you want to delete " + removedLayers.Length + " layers?", "Are you sure?", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                return;
            }
        }

        foreach (TreeNodeAdv layer in removedLayers)
        {
            TreeLayerNode n = layer.Tag as TreeLayerNode;
            n.LayerNode.Remove();
            n.Parent = null;
        }

        layerTree.EndUpdate();
    }