public static LazyObservableTree <DirectoryInfo> CreateTestTree() { var rootDirectory = new DirectoryInfo(@"C:\"); var directoryComparer = Comparer <DirectoryInfo> .Create((d1, d2) => string.Compare(d1.Name, d2.Name, StringComparison.InvariantCultureIgnoreCase)); var rootTree = new LazyObservableTree <DirectoryInfo>(rootDirectory, directoryComparer, tree => { if (tree.Value == null) { return(new LazyObservableTree <DirectoryInfo> [0]); } try { var dirs = tree.Value.EnumerateDirectories("*", SearchOption.TopDirectoryOnly).ToArray(); var children = dirs.Select(dir => { var ty = new LazyObservableTree <DirectoryInfo>(dir, tree.Comparer, tree.ChildFactory); return(ty); }).ToArray(); return(children); } catch (Exception) { return(new LazyObservableTree <DirectoryInfo> [0]); } }); return(rootTree); }
private static IEnumerable <LazyDirectoryTree> childFactory_Run( LazyObservableTree <DirectoryInfo> parent) { if (parent.Value == null) { throw new NullReferenceException("Error: Directory cannot be null"); } try { var dirs = parent.Value.EnumerateDirectories("*", SearchOption.TopDirectoryOnly).ToArray(); var children = dirs.Select(dir => { var ty = new LazyDirectoryTree(dir); return(ty); }).ToArray(); return(children); } catch (Exception) { var dirTree = parent as LazyDirectoryTree; if (dirTree != null) { dirTree.AccessAuthorised = false; } return(new LazyDirectoryTree[0]); } }
public void TestChildRegenerationOnChildFactoryChange() { var root = CreateTestTree(); var oldChildren = root.Children; var expectedNames = root.Value.EnumerateDirectories("*", SearchOption.TopDirectoryOnly) .Select(dir => dir.Name + "_Test") .Reverse() .OrderBy(dir => dir, DefaultComparers.StringComparer_IgnoreCase) .ToArray(); bool isChildListChanged = false; root.ChildListChanged += (sender, old) => { isChildListChanged = true; Assert.IsTrue(ReferenceEquals(sender, root)); Assert.IsTrue(ReferenceEquals(old, oldChildren)); }; root.ChildFactory = tree => { var dirs = tree.Value.EnumerateDirectories("*", SearchOption.TopDirectoryOnly).ToArray(); var children = dirs.Select(dir => { var newDi = new DirectoryInfo(dir.FullName + "_Test"); var ty = new LazyObservableTree <DirectoryInfo>(newDi, tree.Comparer, tree.ChildFactory); return(ty); }).ToArray(); return(children); }; Assert.IsTrue(isChildListChanged); var newChildNames = root.Children.Select(child => child.Value.Name) .Reverse() .OrderBy(dir => dir, DefaultComparers.StringComparer_IgnoreCase) .ToArray(); Assert.AreEqual(expectedNames.Length, newChildNames.Length); for (int i = 0; i < expectedNames.Length; i++) { string name1 = expectedNames[i]; string name2 = newChildNames[i]; bool namesAreEqual = string.Equals(name1, name2, StringComparison.InvariantCultureIgnoreCase); Assert.IsTrue(namesAreEqual); } int fgs = 0; }
private static IEnumerable <LazyObservableTree <DirectoryInfo> > getChildrenFromSource(LazyObservableTree <DirectoryInfo> root) { Logger.WriteLine($"GenerateCildren: this= {root.Value.FullName}"); var linkTree = root as LinkedLayTree; if (linkTree == null) { return(new LinkedLayTree[0]); } if (!linkTree.Source.AreChildrenGenerated) { //linkTree.RemoveSourceEvents(linkTree.Source); linkTree.Source.GenerateChildren(); //linkTree.AddSourceEvents(linkTree.Source); return(new LinkedLayTree[0]); } var selectedChildren = linkTree.Source.Children.Cast <LazyDirectoryTree>() .Where(child => child.IsSelected != false) .ToArray(); var newChildren = selectedChildren.Select(child => { var newTree = new LinkedLayTree(child); return(newTree); }); return(newChildren); }