public void AddElements(IList <T> elements, TreeElement parent, int insertPosition) { if (elements == null) { throw new ArgumentNullException("elements", "elements is null"); } if (elements.Count == 0) { throw new ArgumentNullException("elements", "elements Count is 0: nothing to add"); } if (parent == null) { throw new ArgumentNullException("parent", "parent is null"); } if (parent.children == null) { parent.children = new List <TreeElement>(); } parent.children.InsertRange(insertPosition, elements.Cast <TreeElement> ()); foreach (var element in elements) { element.parent = parent; element.depth = parent.depth + 1; TreeElementUtility.UpdateDepthValues(element); } TreeElementUtility.TreeToList(m_Root, m_Data); Changed(); }
// 移除node public void RemoveElements(IList <T> elements) { foreach (var element in elements) { if (element == m_Root) { throw new ArgumentException("It is not allowed to remove the root element"); } } // 删除elements中,是其他节点的后代节点的节点 // 返回的结果是新的list // (结果只保留顶级的父亲节点) var commonAncestors = TreeElementUtility.FindCommonAncestorsWithinList(elements); // 移除这些剩下的顶级节点 // (这样做的,是为了避免重复的删除节点; // 如果删除了顶级节点,则在顶级节点下面的节点,就不用再删除了,因为已经随顶级节点被删除) foreach (var element in commonAncestors) { element.parent.children.Remove(element); element.parent = null; } // 树结构改变后,重新更新m_data TreeElementUtility.TreeToList(m_Root, m_Data); // 通知数据改变 Changed(); }
public static void TestTreeToListWorks() { // Arrange TestElement root = new TestElement("root", -1); root.children = new List <TreeElement>(); root.children.Add(new TestElement("A", 0)); root.children.Add(new TestElement("B", 0)); root.children.Add(new TestElement("C", 0)); root.children[1].children = new List <TreeElement>(); root.children[1].children.Add(new TestElement("Bchild", 1)); root.children[1].children[0].children = new List <TreeElement>(); root.children[1].children[0].children.Add(new TestElement("Bchildchild", 2)); // Test List <TestElement> result = new List <TestElement>(); TreeElementUtility.TreeToList(root, result); // Assert string[] namesInCorrectOrder = { "root", "A", "B", "Bchild", "Bchildchild", "C" }; Assert.AreEqual(namesInCorrectOrder.Length, result.Count, "Result count is not match"); for (int i = 0; i < namesInCorrectOrder.Length; ++i) { Assert.AreEqual(namesInCorrectOrder[i], result[i].name); } TreeElementUtility.ValidateDepthValues(result); }
// 将elements,移动到parentElement的insertionIndex开始的地方 public void MoveElements(TreeElement parentElement, int insertionIndex, List <TreeElement> elements) { if (insertionIndex < 0) { throw new ArgumentException("Invalid input: insertionIndex is -1, client needs to decide what index elements should be reparented at"); } // Invalid reparenting input if (parentElement == null) { return; } // We are moving items so we adjust the insertion index to accomodate that any items above the insertion index is removed before inserting // 检查有elements中,有多少元素,已经在新的parentElement下面了 // 这样在移动的时候,实际的insertionIndex应该进行更新 if (insertionIndex > 0) { insertionIndex -= parentElement.children.GetRange(0, insertionIndex).Count(elements.Contains); } // Remove draggedItems from their parents foreach (var draggedItem in elements) { draggedItem.parent.children.Remove(draggedItem); // remove from old parent draggedItem.parent = parentElement; // set new parent } if (parentElement.children == null) { parentElement.children = new List <TreeElement>(); } // Insert dragged items under new parent parentElement.children.InsertRange(insertionIndex, elements); // 更新depth TreeElementUtility.UpdateDepthValues(root); // 更新到m_data TreeElementUtility.TreeToList(m_Root, m_Data); // 通知数据更改 Changed(); }
public void RemoveElements(IList <T> elements) { foreach (var element in elements) { if (element == m_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(m_Root, m_Data); Changed(); }
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(m_Root, m_Data); Changed(); }