internal void AddAll(List<FavouritesItem> items)
        {
            _tree.BeginUpdate();

            // Add from a list of items, from definition.
            foreach (FavouritesItem fave in items)
            {
                if (fave.Id != null)
                {
                    if (_items.ContainsKey(fave.Id)) continue;

                    BaseItem item = _windowManager.ProjectBrowser.Lookup(fave.Id);

                    FavouritesNode node;
                    if (item == null)
                    {
                        node = new FavouritesNode(fave.Id, fave.Id, false, null);
                    }
                    else
                    {
                        node = new FavouritesNode(item.Name, fave.Id, false, item);
                    }

                    // Update the folder structure.
                    AddNodeAndPath(fave.Path, node);

                    // And the items list.
                    AddInternal(fave.Id, node);
                }
            }

            _tree.EndUpdate();
        }
        private void SelectNode()
        {
            if (_tree.SelectedNode == null)
            {
                return;
            }

            FavouritesNode node = _tree.SelectedNode.Tag as FavouritesNode;

            if (node == null)
            {
                return;
            }
            if (node.IsFolder)
            {
                return;
            }

            // Look it up each time, incase it's changed, file reload etc.
            BaseItem item = _windowManager.ProjectBrowser.Lookup(node.Id);

            // And reset it.
            node.Item = item;

            if (item != null)
            {
                _windowManager.SetActiveItem(item, this);
            }
        }
        private void AddNodeAndPath(string path, FavouritesNode node)
        {
            if (path == string.Empty)
            {
                // No folder.
                _treeModel.Nodes.Add(node);
                return;
            }

            if (_addedPaths.ContainsKey(path))
            {
                // Folder already added.
                _addedPaths[path].Nodes.Add(node);
                return;
            }

            // Folder not added yet, so add it.
            string[]          bits       = path.Split('/');
            string            partial    = string.Empty;
            Collection <Node> nodes      = _treeModel.Nodes;
            FavouritesNode    folderNode = null;

            foreach (string bit in bits)
            {
                if (string.IsNullOrEmpty(bit))
                {
                    continue;
                }

                // Forward slash folder seperater.
                partial += bit + "/";

                if (_addedPaths.ContainsKey(partial))
                {
                    nodes = _addedPaths[partial].Nodes;
                    continue;
                }

                folderNode = new FavouritesNode(bit, partial, true, null);
                nodes.Add(folderNode);
                nodes = folderNode.Nodes;

                // Keep a note of the folders.
                _addedPaths.Add(partial, folderNode);
            }

            if (folderNode == null)
            {
                return;
            }

            folderNode.Nodes.Add(node);
        }
        protected virtual void RemoveInternal(string id)
        {
            FavouritesNode node = _items[id];

            if (node.Parent == null)
            {
                _treeModel.Nodes.Remove(node);
            }
            else
            {
                node.Parent.Nodes.Remove(node);
            }
            _items.Remove(id);
        }
        private void _nodeTextBox_DrawText(object sender, DrawEventArgs e)
        {
            // Grey out ids that are present that no longer have an item.
            FavouritesNode node = (e.Node.Tag as FavouritesNode);

            if (node == null)
            {
                return;
            }

            if (!node.IsFolder && node.Item == null)
            {
                e.TextColor = Color.Gray;
            }
        }
        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            BaseItem item = null;

            if (_tree.SelectedNode != null)
            {
                // If we have a selected node, find the corresponding item.
                FavouritesNode node = _tree.SelectedNode.Tag as FavouritesNode;
                if (node != null)
                {
                    item = node.Item;
                }
            }

            _windowManager.MenuManager.UpdateContextMenu(sender as ContextMenuStrip, item);
        }
        private void removeItemToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_tree.SelectedNode == null)
            {
                return;
            }

            FavouritesNode node = _tree.SelectedNode.Tag as FavouritesNode;

            if (node == null)
            {
                return;
            }

            if (node.IsFolder)
            {
                // Remove path if empty.
                if (_tree.SelectedNode.Children.Count == 0)
                {
                    if (node.Parent == null)
                    {
                        _treeModel.Nodes.Remove(node);
                    }
                    else
                    {
                        node.Parent.Nodes.Remove(node);
                    }

                    string path = node.Id;

                    _addedPaths.Remove(path);
                }
            }
            else
            {
                if (node.Item != null)
                {
                    // Standard remove.
                    Remove(node.Item);
                }
                else
                {
                    // No associated item.
                    RemoveInternal(node.Id);
                }
            }
        }
        internal void GetAll(List <FavouritesItem> items)
        {
            // Create a list of FavouritesItem to persist.
            items.Clear();
            foreach (TreeNodeAdv tnode in _tree.AllNodes)
            {
                FavouritesNode node = tnode.Tag as FavouritesNode;
                if (node == null)
                {
                    continue;
                }
                if (node.IsFolder)
                {
                    continue;
                }

                TreePath treePath = _treeModel.GetPath(node);
                string   path     = string.Empty;

                string id = node.Id;

                int idx = 1;
                foreach (FavouritesNode pathNode in treePath.FullPath)
                {
                    if (idx == treePath.FullPath.Length && !pathNode.IsFolder)
                    {
                        break;
                    }
                    idx++;
                    path = path + pathNode.Text + "/";
                }

                // Don't add the folders themselves.
                if (id != null)
                {
                    items.Add(new FavouritesItem(id, path));
                }
            }
        }
        internal void AddAll(List <FavouritesItem> items)
        {
            _tree.BeginUpdate();

            // Add from a list of items, from definition.
            foreach (FavouritesItem fave in items)
            {
                if (fave.Id != null)
                {
                    if (_items.ContainsKey(fave.Id))
                    {
                        continue;
                    }

                    BaseItem item = _windowManager.ProjectBrowser.Lookup(fave.Id);

                    FavouritesNode node;
                    if (item == null)
                    {
                        node = new FavouritesNode(fave.Id, fave.Id, false, null);
                    }
                    else
                    {
                        node = new FavouritesNode(item.Name, fave.Id, false, item);
                    }

                    // Update the folder structure.
                    AddNodeAndPath(fave.Path, node);

                    // And the items list.
                    AddInternal(fave.Id, node);
                }
            }

            _tree.EndUpdate();
        }
        private void newFolderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string partial = string.Empty;

            FavouritesNode folderNode = new FavouritesNode("New Folder", partial, true, null);

            if (_tree.SelectedNode != null)
            {
                FavouritesNode selectedNode = _tree.SelectedNode.Tag as FavouritesNode;

                if (selectedNode.IsFolder)
                {
                    selectedNode.Nodes.Add(folderNode);
                }
                else
                {
                    int insertIdx;
                    if (_tree.SelectedNode.Parent == null)
                    {
                        // Insert at root level.
                        insertIdx = _treeModel.Nodes.IndexOf(selectedNode);
                        _treeModel.Nodes.Insert(insertIdx, folderNode);
                    }
                    else
                    {
                        // Insert at node level.
                        insertIdx = selectedNode.Parent.Nodes.IndexOf(selectedNode);
                        selectedNode.Parent.Nodes.Insert(insertIdx, folderNode);
                    }
                }
            }
            else
            {
                _treeModel.Nodes.Add(folderNode);
            }
        }
        private void AddWithParent(BaseItem item, Node parent, int idx)
        {
            string id = item.GetID();

            if (_items.ContainsKey(id))
            {
                return;
            }

            Node node = null;

            if (parent != null)
            {
                if (parent is FavouritesNode && (parent as FavouritesNode).IsFolder)
                {
                    // Standard.
                    node = new FavouritesNode(item.Name, id, false, item);
                    if (idx == -1)
                    {
                        parent.Nodes.Add(node);
                    }
                    else
                    {
                        parent.Nodes.Insert(idx, node);
                    }
                }
                else
                {
                    // May be the "root" Node.
                    node = new FavouritesNode(item.Name, id, false, item);

                    if (parent.Parent != null)
                    {
                        if (idx == -1)
                        {
                            parent.Parent.Nodes.Add(node);
                        }
                        else
                        {
                            parent.Parent.Nodes.Insert(idx, node);
                        }
                    }
                    else
                    {
                        if (idx == -1)
                        {
                            _treeModel.Nodes.Add(node);
                        }
                        else
                        {
                            _treeModel.Nodes.Insert(idx, node);
                        }
                    }
                }
            }
            else
            {
                node = new FavouritesNode(item.Name, id, false, item);
                _treeModel.Nodes.Add(node);
            }

            AddInternal(id, (FavouritesNode)node);
        }
 protected virtual void AddInternal(string id, FavouritesNode node)
 {
     _items.Add(id, node);
 }
        private void newFolderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string partial = string.Empty;

            FavouritesNode folderNode = new FavouritesNode("New Folder", partial, true, null);

            if (_tree.SelectedNode != null)
            {
                FavouritesNode selectedNode = _tree.SelectedNode.Tag as FavouritesNode;

                if (selectedNode.IsFolder)
                {
                    selectedNode.Nodes.Add(folderNode);
                }
                else
                {
                    int insertIdx;
                    if (_tree.SelectedNode.Parent == null)
                    {
                        // Insert at root level.
                        insertIdx = _treeModel.Nodes.IndexOf(selectedNode);
                        _treeModel.Nodes.Insert(insertIdx, folderNode);
                    }
                    else
                    {
                        // Insert at node level.
                        insertIdx = selectedNode.Parent.Nodes.IndexOf(selectedNode);
                        selectedNode.Parent.Nodes.Insert(insertIdx, folderNode);
                    }
                }
            }
            else
            {
                _treeModel.Nodes.Add(folderNode);
            }
        }
        private void AddWithParent(BaseItem item, Node parent, int idx)
        {
            string id = item.GetID();

            if (_items.ContainsKey(id)) return;

            Node node = null;
            if (parent != null)
            {
                if (parent is FavouritesNode && (parent as FavouritesNode).IsFolder)
                {
                    // Standard.
                    node = new FavouritesNode(item.Name, id, false, item);
                    if (idx == -1) parent.Nodes.Add(node);
                    else parent.Nodes.Insert(idx, node);
                }
                else
                {
                    // May be the "root" Node.
                    node = new FavouritesNode(item.Name, id, false, item);

                    if (parent.Parent != null)
                    {
                        if (idx == -1) parent.Parent.Nodes.Add(node);
                        else parent.Parent.Nodes.Insert(idx, node);
                    }
                    else
                    {
                        if (idx == -1) _treeModel.Nodes.Add(node);
                        else _treeModel.Nodes.Insert(idx, node);
                    }
                }
            }
            else
            {
                node = new FavouritesNode(item.Name, id, false, item);
                _treeModel.Nodes.Add(node);
            }

            AddInternal(id, (FavouritesNode)node);
        }
        private void AddNodeAndPath(string path, FavouritesNode node)
        {
            if (path == string.Empty)
            {
                // No folder.
                _treeModel.Nodes.Add(node);
                return;
            }

            if (_addedPaths.ContainsKey(path))
            {
                // Folder already added.
                _addedPaths[path].Nodes.Add(node);
                return;
            }

            // Folder not added yet, so add it.
            string[] bits = path.Split('/');
            string partial = string.Empty;
            Collection<Node> nodes = _treeModel.Nodes;
            FavouritesNode folderNode = null;
            foreach (string bit in bits)
            {
                if (string.IsNullOrEmpty(bit)) continue;

                // Forward slash folder seperater.
                partial += bit + "/";

                if (_addedPaths.ContainsKey(partial))
                {
                    nodes = _addedPaths[partial].Nodes;
                    continue;
                }

                folderNode = new FavouritesNode(bit, partial, true, null);
                nodes.Add(folderNode);
                nodes = folderNode.Nodes;

                // Keep a note of the folders.
                _addedPaths.Add(partial, folderNode);
            }

            if (folderNode == null) return;

            folderNode.Nodes.Add(node);
        }
 protected virtual void AddInternal(string id, FavouritesNode node)
 {
     _items.Add(id, node);
 }