public void RemoveElements(IList <T> elements) { foreach (var element in elements) { if (element == Root) { throw new ArgumentException("It is not allowed to remove the root element"); } } var commonAncestors = TreeElementUtility.FindCommonAncestorsWithinList(elements); foreach (var element in commonAncestors) { element.Parent.Children.Remove(element); element.Parent = null; } TreeElementUtility.TreeToList(Root, _data); Changed(); }
public static void TestListToTreeThrowsExceptionIfRootIsInvalidDepth() { // Arrange var list = new List <TestElement>(); list.Add(new TestElement("root", 0)); list.Add(new TestElement("A", 1)); list.Add(new TestElement("B", 1)); list.Add(new TestElement("Bchild", 2)); // Test var catchedException = false; try { TreeElementUtility.ListToTree(list); } catch (Exception) { catchedException = true; } // Assert Assert.IsTrue(catchedException, "We require the root.Depth to be -1, here it is: " + list[0].Depth); }
public void AddElement(T element, TreeElement parent, int insertPosition) { if (element == null) { throw new ArgumentNullException("element", "element is null"); } if (parent == null) { throw new ArgumentNullException("parent", "parent is null"); } if (parent.Children == null) { parent.Children = new List <TreeElement>(); } parent.Children.Insert(insertPosition, element); element.Parent = parent; TreeElementUtility.UpdateDepthValues(parent); TreeElementUtility.TreeToList(Root, _data); Changed(); }