/// <summary> /// If the <c>nodes</c> array contain both some nodes and their childs (or relatives up in the hierarchie), those childs are removed and only /// the nodes with the lowest level in the hierarchy are returned. /// </summary> /// <param name="nodes">Collection of nodes</param> /// <returns>Only the nodes who have no parent (or grand parent and so on) in the collection.</returns> public static NGTreeNode[] FilterIndependentNodes(NGTreeNode[] nodes) { System.Collections.Hashtable hash = new System.Collections.Hashtable(); for (int i = 0; i < nodes.Length; i++) { hash.Add(nodes[i], null); } List <NGTreeNode> result = new List <NGTreeNode>(); for (int i = 0; i < nodes.Length; i++) { bool isContained = false; for (NGTreeNode currNode = nodes[i].Parent; currNode != null; currNode = currNode.Parent) { if (hash.ContainsKey(currNode)) { isContained = true; break; } } if (!isContained) { result.Add(nodes[i]); } } return(result.ToArray()); }
static void MoveDown(NGTreeNode[] selNodes, NGTreeNode parent) { if (selNodes[selNodes.Length - 1].Index == parent.Nodes.Count - 1) // if last item is selected, we can't move downwards { return; } for (int i = selNodes.Length - 1; i >= 0; i--) { int idx = selNodes[i].Index; parent._nodes.Swap(idx, idx + 1); } }
static void MoveUp(NGTreeNode[] selNodes, NGTreeNode parent) { if (selNodes[0].Index == 0) // if the first item is selected, we can't move upwards { return; } for (int i = 0; i < selNodes.Length; i++) { int idx = selNodes[i].Index; parent._nodes.Swap(idx, idx - 1); } }
/// <summary> /// Determines if all nodes in the array have the same parent. /// </summary> /// <param name="nodes">Array of nodes.</param> /// <returns>True if all nodes have the same parent. If the array is empty or contains only one element, true is returned. /// If all nodes have no parent (Parent==null), true is returned as well.</returns> public static bool HaveSameParent(NGTreeNode[] nodes) { if (nodes.Length <= 1) { return(true); } NGTreeNode parent = nodes[0].Parent; for (int i = 1; i < nodes.Length; i++) { if (nodes[i].Parent != parent) { return(false); } } return(true); }
/// <summary> /// This procedure will move up or move down some nodes in the tree. /// </summary> /// <param name="iDelta">Number of movement steps. Value less than zero will move up the nodes in the tree, values greater null will move down the nodes in the tree.</param> /// <param name="selNodes">Nodes to move.</param> /// <remarks>The following assumptions must be fullfilled: /// <para>First, the nodes are filtered: If the array contain both a parent node and child nodes of this parent node, /// the child nodes will be not moved.</para> /// <para>The remaining nodes must have the same parent, otherwise an exception is thrown.</para> /// </remarks> static public void MoveUpDown(int iDelta, NGTreeNode[] selNodes) { if (iDelta == 0 || selNodes == null || selNodes.Length == 0) { return; } selNodes = FilterLowestLevelNodes(selNodes); if (!HaveSameParent(selNodes)) { throw new ArgumentException("The nodes in the array have not the same parent, which is neccessary for moving operations"); } System.Diagnostics.Debug.Assert(selNodes.Length > 0); NGTreeNode parent = selNodes[0].Parent; if (parent == null) { throw new ArgumentException("Parent of the nodes is null"); } SortByOrder(selNodes); if (iDelta < 0) { for (int i = 0; i < (-iDelta); i++) { MoveUp(selNodes, parent); } } else { for (int i = 0; i < iDelta; i++) { MoveDown(selNodes, parent); } } }