/// <summary> /// Inserts the element passed in as a sibling to the element the navigator is currently on. /// The navigator will move to the inserted element. /// </summary> /// <param name="sibling"></param> /// <returns></returns> public override bool InsertAfter(ElementDefinition sibling) { if (!canInsertSiblingHere()) return false; var insertPosition = positionAfter(); var newSiblingPath = ParentPath + "." + sibling.GetNameFromPath(); if (insertPosition == Count) // At last position Elements.Add(sibling); else Elements.Insert(insertPosition, sibling); // Adjust the sibling's path so it's parent is the same as the current node sibling.Path = newSiblingPath; // Navigate to newly inserted node OrdinalPosition = insertPosition; return true; }
/// <summary> /// Inserts the element passed in as a child of the element the navigator is currently on. /// The navigator will move to the inserted element. /// </summary> /// <param name="child"></param> /// <returns></returns> /// <remarks>You can only insert a child for an element does not have children yet.</remarks> public override bool InsertFirstChild(ElementDefinition child) { if(Count == 0) { // Special case, insert a new root Elements.Add(child); child.Path = child.GetNameFromPath(); OrdinalPosition = 0; return true; } else if(HasChildren) return false; // Cannot insert another child, there's already one. else { var newSiblingPath = Path + "." + child.GetNameFromPath(); if (OrdinalPosition == Count - 1) // At last position Elements.Add(child); else Elements.Insert(OrdinalPosition.Value + 1, child); // Set the name to be a true child -> this actually creates a child child.Path = newSiblingPath; // Navigate to newly inserted child OrdinalPosition += 1; return true; } }
//---------------------------------- // // Methods that alter the list of elements // //---------------------------------- /// <summary> /// Inserts the element passed in as a sibling to the element the navigator is currently on. /// The navigator will move to the inserted element. /// </summary> /// <param name="sibling"></param> /// <returns></returns> public override bool InsertBefore(ElementDefinition sibling) { if (!canInsertSiblingHere()) return false; var newSiblingPath = ParentPath + "." + sibling.GetNameFromPath(); Elements.Insert(OrdinalPosition.Value, sibling); // Adjust the sibling's path so it's parent is the same as the current node sibling.Path = newSiblingPath; // Note: we're now positioned on the newly inserted element return true; }