Example #1
0
        /// <summary>
        /// Removes the node from the tree (forrest)
        /// </summary>
        /// <param name="roots">The roots of the tree</param>
        /// <param name="toRemove">The node to remove</param>
        /// <returns>True if the node was found and removed, false otherwise</returns>
        private bool RemoveNode(ObservableCollection <NodeVM> roots, NodeVM toRemove)
        {
            if (roots == null)
            {
                return(false);
            }

            foreach (var root in roots)
            {
                if (root.Path == toRemove.Path)
                {
                    roots.Remove(root);
                    return(true);
                }
                else if (RemoveNode(root.Children, toRemove))
                {
                    if (root.Children.Count == 0)
                    {
                        roots.Remove(root); //propagate up ..
                    }
                    return(true);
                }
            }

            return(false);
        }
Example #2
0
 /// <summary>
 /// Removes the <paramref name="children"/>
 /// </summary>
 /// <param name="children">The children to be removed</param>
 public void RemoveChildren(NodeVM children)
 {
     Children.Remove(children);
 }
Example #3
0
        /// <summary>
        /// Build the root structure (forrest) with file system entries as nodes
        /// </summary>
        /// <param name="rects">Loaded rectangles (images)</param>
        /// <returns>Collection of roots of the trees</returns>
        private ObservableCollection <NodeVM> CreateFSEntries(IEnumerable <PPRect> rects)
        {
            var separator = System.IO.Path.DirectorySeparatorChar;

            NodeVM currNode = null;

            var rootNodes = new ObservableCollection <NodeVM>();

            foreach (var rect in rects)
            {
                var    pathChunks  = rect.Image.ImagePath.Split(separator);
                string currentPath = "";

                if (pathChunks.Length == 0)
                {
                    continue;
                }

                currNode = rootNodes.FirstOrDefault(x => x.Name == pathChunks[0]);
                if (currNode == null)
                {
                    currNode = new NodeVM {
                        Name = pathChunks[0], Children = new ObservableCollection <NodeVM>(), Depth = 0, Path = pathChunks[0], IsDirectory = true, Thumbnail = folderImageVM
                    };
                    rootNodes.Add(currNode);
                }

                currentPath = pathChunks[0];

                for (int i = 1; i < pathChunks.Length; i++)
                {
                    currentPath += System.IO.Path.DirectorySeparatorChar + pathChunks[i];

                    //traverse current structure
                    NodeVM nextNode = null;
                    if ((nextNode = currNode?.Children.FirstOrDefault(x => x.Name == pathChunks[i])) == null)
                    {
                        nextNode = new NodeVM {
                            Name = pathChunks[i], Children = new ObservableCollection <NodeVM>(), Depth = i, Path = currentPath, IsDirectory = i < pathChunks.Length - 1
                        };
                        if (i == pathChunks.Length - 1)
                        {
                            nextNode.Thumbnail = new ImageViewModel(rect.Image);
                        }
                        else
                        {
                            nextNode.Thumbnail = folderImageVM;
                        }
                        currNode.Children.Add(nextNode);
                    }
                    currNode = nextNode;
                }
            }

            var rootsToRemove = new List <NodeVM>();

            //Normalize the paths
            for (int i = 0; i < rootNodes.Count; i++)
            {
                int    j = 0;
                var    normalizedPath = rootNodes[i].Name;
                NodeVM parentNode     = null;
                var    currentNode    = rootNodes[i];

                while (currentNode.Children.Count == 1)
                {
                    normalizedPath = currentNode.Children[0].Path;
                    parentNode     = currentNode;
                    currentNode    = currentNode.Children[0];
                    j++;
                }

                //Path was changed
                if (j > 0)
                {
                    //Delete empty directory
                    if (currentNode.Children.Count == 0 && currentNode.IsDirectory)
                    {
                        rootsToRemove.Add(rootNodes[i]);
                    }
                    else
                    {
                        if (parentNode != null)
                        {
                            parentNode.Children.Clear();
                            parentNode = null;
                        }

                        currentNode.Name = currentNode.Path;
                        rootNodes[i]     = currentNode;
                        //Update the depths for all the children (recursively) of the currentNode

                        int decreaseAmount     = currentNode.Depth;
                        var decreaseDepthStack = new Stack <NodeVM>();
                        decreaseDepthStack.Push(currentNode);

                        while (decreaseDepthStack.Count > 0)
                        {
                            currentNode        = decreaseDepthStack.Pop();
                            currentNode.Depth -= decreaseAmount;
                            foreach (var child in currentNode.Children)
                            {
                                decreaseDepthStack.Push(child);
                            }
                        }
                    }
                }
            }

            foreach (var rootToRemove in rootsToRemove)
            {
                rootNodes.Remove(rootToRemove);
            }

            return(rootNodes);
        }