public void RemoveFromParent() { if (Parent == null) return; Parent.Children.Remove(this); this.Parent = null; }
static void Main(string[] args) { #region Start building tree out of TestTreeNodes objects TestTreeNode topNode = new TestTreeNode { NodeInfo = "TopNode" }; TestTreeNode level2Node1 = topNode.AddChild("Level2Node_1"); TestTreeNode level2Node2 = topNode.AddChild("Level2Node_2"); TestTreeNode level3Node1 = level2Node1.AddChild("Level3Node_1"); TestTreeNode level3Node2 = level2Node1.AddChild("Level3Node_2"); TestTreeNode level3Node3 = level2Node2.AddChild("Level3Node_3"); TestTreeNode level3Node4 = level2Node2.AddChild("Level3Node_4"); #endregion End tree building // to parent function Func<TestTreeNode, TestTreeNode> toParentFn = (treeNode) => treeNode.Parent; // to children function Func<TestTreeNode, IEnumerable<TestTreeNode>> toChildrenFn = (treeNode) => treeNode.Children; Console.WriteLine("Print ancestors of node level3Node3"); foreach (var treeNode in level3Node3.Ancestors(toParentFn)) Console.WriteLine("\t" + treeNode.NodeInfo); Console.WriteLine("\n"); Console.WriteLine("Print self and ancestors of node level3Node3"); foreach (var treeNode in level3Node3.SelfAndAncestors(toParentFn)) Console.WriteLine("\t" + treeNode.NodeInfo); Console.WriteLine("\nPrint whole tree"); foreach (var treeNodeInfo in topNode.SelfAndDescendantsWithLevelInfo(toChildrenFn)) { // shift the string by the level number plus 1 tabs string tabs = new string('\t', treeNodeInfo.Level + 1); Console.WriteLine(tabs + treeNodeInfo.TheNode.NodeInfo); } }
static void Main(string[] args) { #region Start building tree out of TestTreeNodes objects TestTreeNode topNode = new TestTreeNode { NodeInfo = "TopNode" }; TestTreeNode level2Node1 = topNode.AddChild("Level2Node_1"); TestTreeNode level2Node2 = topNode.AddChild("Level2Node_2"); TestTreeNode level3Node1 = level2Node1.AddChild("Level3Node_1"); TestTreeNode level3Node2 = level2Node1.AddChild("Level3Node_2"); TestTreeNode level3Node3 = level2Node2.AddChild("Level3Node_3"); TestTreeNode level3Node4 = level2Node2.AddChild("Level3Node_4"); #endregion End tree building Func<TestTreeNode, TestTreeNode> toParentTreeFunction = (node) => node.Parent; Func<TestTreeNode, IEnumerable<TestTreeNode>> toChildrenTreeFunction = (node) => node.Children; IEnumerable<TreeNodeInfo<TestTreeNode>> allTreeNodes = topNode.SelfAndDescendantsWithLevelInfo(toChildrenTreeFunction); // print all the tree nodes Console.WriteLine("\nPrint all nodes"); foreach (TreeNodeInfo<TestTreeNode> treeNodeWithLevelInfo in allTreeNodes) { string shiftToRight = new string('\t', treeNodeWithLevelInfo.Level + 1); Console.WriteLine(shiftToRight + treeNodeWithLevelInfo.TheNode.NodeInfo); } // create REvent REvent<TestTreeNode, string> aTestEvent = new REvent<TestTreeNode, string>(); // assign handlers for each of the foreach (TreeNodeInfo<TestTreeNode> treeNodeWithLevelInfo in allTreeNodes) { TestTreeNode currentNode = treeNodeWithLevelInfo.TheNode; aTestEvent.AddHander ( currentNode, (str) => { Console.WriteLine("Target Node: " + currentNode.NodeInfo + "\t\t\tSource Node: " + str); } ); } Console.WriteLine("\nTesting event bubbling:"); aTestEvent.RaiseBubbleEvent(level3Node3, toParentTreeFunction, level3Node3.NodeInfo); Console.WriteLine("\nTesting event tunneling:"); aTestEvent.RaiseTunnelEvent(level3Node3, toParentTreeFunction, level3Node3.NodeInfo); Console.WriteLine("\nTesting event Direct Event (without bubbling and tunneling):"); aTestEvent.RaiseEvent(level3Node3, level3Node3.NodeInfo); Console.WriteLine("\nTesting event propagation to descendents:"); aTestEvent.RaiseEventPropagateToDescendents(level2Node1, toChildrenTreeFunction, level2Node1.NodeInfo); // stopping propagation by returning false from a handler aTestEvent.RemoveAllHandlers(level2Node2); aTestEvent.AddHander ( level2Node2, (eventInfo) => { Console.WriteLine("Terminating event propagation at node " + level2Node2.NodeInfo); eventInfo.IsCanceled = true; }); // terminate event propagation at this node Console.WriteLine("\nTesting event bubbling with event propagation termination:"); aTestEvent.RaiseBubbleEvent(level3Node3, toParentTreeFunction, level3Node3.NodeInfo); Console.WriteLine("\nTesting event tunneling with event propagation termination:"); aTestEvent.RaiseTunnelEvent(level3Node3, toParentTreeFunction, level3Node3.NodeInfo); Console.WriteLine("\nTesting event propagation from the TopNode to its descendents with event propagation termination:"); aTestEvent.RaiseEventPropagateToDescendents(topNode, toChildrenTreeFunction, topNode.NodeInfo); }