Index: Core/Common/src/Core.Common.Controls.TreeView/TreeViewControl.cs =================================================================== diff -u -r59ddec0f297b264a1dec62c22033c5f6070c3ec8 -rb0befca10e0dc13f75621e0e70de003c511114d4 --- Core/Common/src/Core.Common.Controls.TreeView/TreeViewControl.cs (.../TreeViewControl.cs) (revision 59ddec0f297b264a1dec62c22033c5f6070c3ec8) +++ Core/Common/src/Core.Common.Controls.TreeView/TreeViewControl.cs (.../TreeViewControl.cs) (revision b0befca10e0dc13f75621e0e70de003c511114d4) @@ -606,103 +606,96 @@ private void RefreshChildNodes(TreeNode treeNode, TreeNodeInfo treeNodeInfo) { - IList newlyAddedTreeNodes = new List(); - object[] expectedChildNodes = treeNodeInfo.ChildNodeObjects != null - ? treeNodeInfo.ChildNodeObjects(treeNode.Tag) - : new object[0]; + IList newlyAddedChildNodes = new List(); + object[] expectedChildNodeObjects = treeNodeInfo.ChildNodeObjects != null + ? treeNodeInfo.ChildNodeObjects(treeNode.Tag) + : new object[0]; + int expectedChildNodeCount = expectedChildNodeObjects.Length; - for (var nodeIndex = 0; nodeIndex < expectedChildNodes.Length; nodeIndex++) + for (var childNodeIndex = 0; childNodeIndex < expectedChildNodeCount; childNodeIndex++) { - RemovePossiblyOutdatedChildNodeAtIndex(treeNode, nodeIndex, expectedChildNodes); + RemovePossiblyOutdatedChildNodeAtIndex(treeNode, childNodeIndex, expectedChildNodeObjects); - object expectedChildNode = expectedChildNodes[nodeIndex]; + object expectedChildNodeObject = expectedChildNodeObjects[childNodeIndex]; - if (IsExpectedChildNodePresentAtIndex(treeNode, nodeIndex, expectedChildNode)) + if (IsExpectedChildNodePresentAtIndex(treeNode, childNodeIndex, expectedChildNodeObject)) { continue; } - IList currentTreeNodes = treeNode.Nodes.Cast().ToList(); - Dictionary currentTagIndexLookup = currentTreeNodes.Select((node, index) => new - { - node, - index - }) - .ToDictionary(x => x.node.Tag, x => x.index); - - if (currentTagIndexLookup.ContainsKey(expectedChildNode)) + int currentChildNodeIndex = Array.FindIndex(treeNode.Nodes.Cast().ToArray(), c => c.Tag == expectedChildNodeObject); + if (currentChildNodeIndex > -1) { - MoveExistingChildNodeToIndex(treeNode, currentTagIndexLookup, expectedChildNode, currentTreeNodes, nodeIndex); + MoveExistingChildNodeToIndex(treeNode, currentChildNodeIndex, childNodeIndex); } else { - newlyAddedTreeNodes.Add(InsertNewChildNode(treeNode, expectedChildNode, nodeIndex)); + InsertNewChildNodeAtIndex(treeNode, childNodeIndex, expectedChildNodeObject, newlyAddedChildNodes); } } - RemovePossiblyOutdatedChildNodesAtEnd(treeNode, expectedChildNodes); + RemovePossiblyOutdatedChildNodesAtEnd(treeNode, expectedChildNodeCount); - SelectLastNewNode(newlyAddedTreeNodes); + SelectLastNewChildNode(newlyAddedChildNodes); } - private void RemovePossiblyOutdatedChildNodeAtIndex(TreeNode parentNode, int childNodeIndex, IEnumerable expectedChildNodes) + private void RemovePossiblyOutdatedChildNodeAtIndex(TreeNode parentNode, int childNodeIndex, IEnumerable expectedChildNodeObjects) { if (parentNode.Nodes.Count > childNodeIndex) { TreeNode possiblyOutdatedChildNode = parentNode.Nodes[childNodeIndex]; - if (!expectedChildNodes.Contains(possiblyOutdatedChildNode.Tag)) + if (!expectedChildNodeObjects.Contains(possiblyOutdatedChildNode.Tag)) { parentNode.Nodes.Remove(possiblyOutdatedChildNode); RemoveTreeNodeFromLookupRecursively(possiblyOutdatedChildNode); } } } - private static bool IsExpectedChildNodePresentAtIndex(TreeNode parentNode, int childNodeIndex, object expectedChildNode) + private static bool IsExpectedChildNodePresentAtIndex(TreeNode parentNode, int childNodeIndex, object expectedChildNodeData) { - return parentNode.Nodes.Count > childNodeIndex && expectedChildNode.Equals(parentNode.Nodes[childNodeIndex].Tag); + return parentNode.Nodes.Count > childNodeIndex && expectedChildNodeData.Equals(parentNode.Nodes[childNodeIndex].Tag); } - private static void MoveExistingChildNodeToIndex(TreeNode parentNode, Dictionary currentTagIndexLookup, object existingChildNode, IList currentTreeNodes, int indexToMoveChildNodeTo) + private static void MoveExistingChildNodeToIndex(TreeNode parentNode, int currentChildNodeIndex, int newChildNodeIndex) { - int currentTreeNodeIndex = currentTagIndexLookup[existingChildNode]; - TreeNode existingTreeNode = currentTreeNodes[currentTreeNodeIndex]; - parentNode.Nodes.RemoveAt(currentTreeNodeIndex); - parentNode.Nodes.Insert(indexToMoveChildNodeTo, existingTreeNode); + TreeNode existingTreeNode = parentNode.Nodes[currentChildNodeIndex]; + + parentNode.Nodes.RemoveAt(currentChildNodeIndex); + parentNode.Nodes.Insert(newChildNodeIndex, existingTreeNode); } - private TreeNode InsertNewChildNode(TreeNode treeNode, object expectedChildNode, int nodeIndex) + private void InsertNewChildNodeAtIndex(TreeNode parentNode, int childNodeIndex, object expectedChildNodeData, ICollection newlyAddedTreeNodes) { - TreeNode newTreeNode = CreateTreeNode(treeNode, expectedChildNode); - treeNode.Nodes.Insert(nodeIndex, newTreeNode); + TreeNode newTreeNode = CreateTreeNode(parentNode, expectedChildNodeData); - return newTreeNode; + parentNode.Nodes.Insert(childNodeIndex, newTreeNode); + newlyAddedTreeNodes.Add(newTreeNode); } - private static void RemovePossiblyOutdatedChildNodesAtEnd(TreeNode treeNode, object[] expectedChildNodes) + private static void RemovePossiblyOutdatedChildNodesAtEnd(TreeNode parentNode, int expectedChildNodeLength) { - IEnumerable outdatedTreeNodes = treeNode.Nodes.Cast().Skip(expectedChildNodes.Length).ToList(); - foreach (TreeNode outdatedNode in outdatedTreeNodes) + for (int i = parentNode.Nodes.Count - 1; i >= expectedChildNodeLength; i--) { - treeNode.Nodes.Remove(outdatedNode); + parentNode.Nodes.RemoveAt(i); } } - private void SelectLastNewNode(IEnumerable newTreeNodes) + private void SelectLastNewChildNode(IEnumerable newChildNodes) { - TreeNode lastAddedNodeToSetSelectionTo = newTreeNodes.LastOrDefault(node => + TreeNode lastAddedChildNodeToSetSelectionTo = newChildNodes.LastOrDefault(node => { object dataObject = node.Tag; TreeNodeInfo info = TryGetTreeNodeInfoForData(dataObject); return info.EnsureVisibleOnCreate != null && info.EnsureVisibleOnCreate(dataObject, node.Parent.Tag); }); - if (lastAddedNodeToSetSelectionTo != null) + if (lastAddedChildNodeToSetSelectionTo != null) { - lastAddedNodeToSetSelectionTo.EnsureVisible(); - treeView.SelectedNode = lastAddedNodeToSetSelectionTo; + lastAddedChildNodeToSetSelectionTo.EnsureVisible(); + treeView.SelectedNode = lastAddedChildNodeToSetSelectionTo; } }