internal ProjectItem MoveToInsertionPoint(ProjectItem itemToMove, TreeItemInsertionRule insertionRule, ProjectItem insertionPoint = null, int relativePosition = -1) { switch (insertionRule) { case TreeItemInsertionRule.AsSiblingBeforeInsertionPoint: this.ChangeParentAndPosition(itemToMove, newParent: insertionPoint.Parent, newPosition: insertionPoint.Position); return(itemToMove); case TreeItemInsertionRule.AsSiblingAfterInsertionPoint: int branchLastItemPosition = GetBranchLastItem(insertionPoint).Position; this.ChangeParentAndPosition(itemToMove, newParent: insertionPoint.Parent, newPosition: branchLastItemPosition + 1); return(itemToMove); case TreeItemInsertionRule.AsChildAsFirstNode: this.ChangeParentAndPosition(itemToMove, newParent: insertionPoint, newPosition: insertionPoint.Position + 1); return(itemToMove); case TreeItemInsertionRule.AsChildAsLastNode: branchLastItemPosition = GetBranchLastItem(insertionPoint).Position; this.ChangeParentAndPosition(itemToMove, newParent: insertionPoint, newPosition: branchLastItemPosition + 1); return(itemToMove); case TreeItemInsertionRule.AsChildAtPosition: Assertion.Require(relativePosition > 0, "Relative position must be greater than zero."); this.ChangeParentAndPosition(itemToMove, newParent: insertionPoint, newPosition: insertionPoint.Position + 1 + relativePosition); return(itemToMove); case TreeItemInsertionRule.AsTreeRootAtStart: this.ChangePosition(itemToMove, 1); return(itemToMove); case TreeItemInsertionRule.AsTreeRootAtEnd: return(itemToMove); case TreeItemInsertionRule.AtRelativePosition: Assertion.Require(relativePosition > 0, "Relative position must be greater than zero."); int indexOfInsertionPoint = this.itemsList.IndexOf(insertionPoint); this.itemsList.Remove(itemToMove); itemToMove.SetPosition(indexOfInsertionPoint + relativePosition + 1); this.itemsList.Insert(indexOfInsertionPoint + relativePosition, itemToMove); EmpiriaLog.Trace($"Item {itemToMove.Name} inserted at position {indexOfInsertionPoint}/{relativePosition}: [{itemToMove.Position}] // {insertionPoint.Name}."); this.RefreshPositions(); return(itemToMove); default: throw Assertion.EnsureNoReachThisCode($"Unrecognized insertion rule '{insertionRule.ToString()}'."); } }
static private int SetNewActivityInsertionRule(ProjectItemStateChange toInsertActivity, WhatIfResult current, WhatIfResult newModelResult) { int position = -1; var parent = current.StateChanges.Find(x => !x.ProjectItem.IsEmptyInstance && x.Template.UID == toInsertActivity.Template.Parent.UID); if (parent != null) { var newSiblings = current.StateChanges.FindAll(x => x.ProjectItem.IsEmptyInstance && x.ParentStateChange.UID == parent.UID); var parentIndex = current.StateChanges.IndexOf(parent); var currentSiblings = current.StateChanges.FindAll(x => !x.ProjectItem.IsEmptyInstance && x.ProjectItem.Parent.UID == parent.ProjectItem.UID); if (currentSiblings.Count != 0) { toInsertActivity.InsertionPoint = parent.ProjectItem; toInsertActivity.InsertionRule = TreeItemInsertionRule.AsChildAsLastNode; position = parentIndex + currentSiblings.Count + newSiblings.Count + 1; EmpiriaLog.Trace($"Rule 0: {toInsertActivity.Template.Name} at position {position}."); } else { toInsertActivity.ParentStateChange = parent; toInsertActivity.InsertionPoint = parent.ProjectItem; toInsertActivity.InsertionRule = TreeItemInsertionRule.AsChildAsLastNode; position = parentIndex + newSiblings.Count + 1; EmpiriaLog.Trace($"Rule 1: {toInsertActivity.Template.Name} at position {position}."); } } else { Assertion.Require(toInsertActivity.ParentStateChange, nameof(toInsertActivity.ParentStateChange)); var newParent = current.StateChanges.Find(x => x.UID == toInsertActivity.ParentStateChange.UID); var newParentIdx = current.StateChanges.IndexOf(newParent); var currentSiblings = current.StateChanges.FindAll(x => x.ParentStateChange != null && x.ParentStateChange.UID == toInsertActivity.ParentStateChange.UID); toInsertActivity.ParentStateChange = newParent; toInsertActivity.InsertionPoint = ProjectItem.Empty; toInsertActivity.InsertionRule = TreeItemInsertionRule.AsChildAsLastNode; position = newParentIdx + currentSiblings.Count + 1; EmpiriaLog.Trace($"Rule 2: {toInsertActivity.Template.Name} at position {position}."); } return(position); }