Beispiel #1
0
        public void Store(TLeaf leadData, HierarchicalTreeSettingsData customSettings = null)
        {
            GetSettings(ref customSettings);

            var parentBranch  = this;
            var folderMatches = customSettings.FolderRegex.Matches(leadData.Path);

            for (var m = 0; m < folderMatches.Count; m++)
            {
                var folderMatch = folderMatches[m];
                var value       = folderMatch.LastGroup().Value;
                if (m == folderMatches.Count - 1)
                {
                    var name      = value;
                    var extension = string.Empty;
                    var nameMatch = customSettings.ExtensionRegex.Match(value);
                    if (nameMatch.Success)
                    {
                        name      = nameMatch.Groups[1].Value;
                        extension = nameMatch.Groups[2].Value;
                    }

                    var leaf = new TreeLeaf <IHierarchicalTreeLeaf>(name, extension, leadData);
                    if (!parentBranch.Leaves.Contains(leaf))
                    {
                        //Remove the old one because the leaf hashCode is Path dependant
                        parentBranch.Leaves.Remove(leaf);
                    }

                    parentBranch.Leaves.Add(leaf);
                    break;
                }

                if (parentBranch.Branches.TryGetValue(value, out var childBranch))
                {
                    parentBranch = childBranch;
                }
                else
                {
                    childBranch = new HierarchicalTree <TLeaf>(customSettings)
                    {
                        parent     = parentBranch,
                        branchName = value
                    };

                    parentBranch.Branches.Add(value, childBranch);
                    parentBranch = childBranch;
                }
            }
        }
Beispiel #2
0
        public void Remove(IHierarchicalTreeLeaf leadData, HierarchicalTreeSettingsData customSettings = null)
        {
            GetSettings(ref customSettings);

            var activeBranch = SearchForBranch(leadData.Path, true, customSettings);

            if (activeBranch == null)
            {
                return;
            }

            var leaf = new TreeLeaf <IHierarchicalTreeLeaf>(string.Empty, string.Empty, leadData);

            activeBranch.Leaves.Remove(leaf);
        }
Beispiel #3
0
        private void GetSettings(ref HierarchicalTreeSettingsData customSettings)
        {
            Assert.IsNotNull(settings, "settings is null, don't forget to call Setup() to properly setup the tree");

            customSettings = customSettings == null ? settings : customSettings;
        }
Beispiel #4
0
 public void Setup(HierarchicalTreeSettingsData settings = null)
 {
     this.settings = settings == null ? HierarchicalTreeSettings.Default.Data : settings;
 }
Beispiel #5
0
 private HierarchicalTree(HierarchicalTreeSettingsData settings)
     : this()
 {
     Setup(settings);
 }
Beispiel #6
0
        private HierarchicalTree <TLeaf> SearchForBranch(string searchPath, bool searchPathIsLeaf, HierarchicalTreeSettingsData customSettings)
        {
            GetSettings(ref customSettings);

            var activeBranch = this;
            var matches      = customSettings.FolderRegex.Matches(searchPath);

            for (var m = 0; m < matches.Count; m++)
            {
                if (searchPathIsLeaf && m == matches.Count - 1)
                {
                    break;
                }

                var match = matches[m];
                if (!match.Success || activeBranch.branches == null)
                {
                    activeBranch = null;
                    break;
                }

                var folder = match.LastGroup().Value;
                if (!activeBranch.branches.TryGetValue(folder, out var nextBranch))
                {
                    activeBranch = null;
                    break;
                }

                activeBranch = nextBranch;
            }

            return(activeBranch);
        }