SobekCM.Core.UI_Configuration.Citation.CitationFieldSet.Insert_Element_After C# (CSharp) Method

Insert_Element_After() public method

Adds a new citation element after an existing elements in this field set
If an element exists with the same MetadataTerm, it is removed first. If the ID provided to add this element after does not exist, this is just appended to the very end.
public Insert_Element_After ( CitationElement NewElement, string RelativeElementID ) : void
NewElement CitationElement New citation element to add
RelativeElementID string MetadataTerm for the element after which the new citation element should be inserted
return void
        public void Insert_Element_After(CitationElement NewElement, string RelativeElementID )
        {
            // Ensure the dictionary is built (i.e., not null)
            if (elementsDictionary == null) elementsDictionary = new Dictionary<string, CitationElement>(StringComparer.OrdinalIgnoreCase);

            // Check that the count in the dictionary seems right
            if (elementsDictionary.Count != Elements.Count)
            {
                foreach (CitationElement thisElement in Elements)
                    elementsDictionary[thisElement.MetadataTerm] = thisElement;
            }

            // Does the relative element id exist?
            if (!elementsDictionary.ContainsKey(RelativeElementID))
            {
                // Relative doesn't exist.. just append
                Append_Element(NewElement);
            }

            // Find the index of the relative element
            int relativeIndex = Elements.IndexOf(elementsDictionary[RelativeElementID]);
            if ((relativeIndex < 0) || (relativeIndex + 1 == Elements.Count))
                Append_Element(NewElement);
            else
            {
                // If this element already exists, remove it
                if (elementsDictionary.ContainsKey(NewElement.MetadataTerm))
                {
                    CitationElement existing = elementsDictionary[NewElement.MetadataTerm];
                    Elements.Remove(existing);
                }

                // Insert at the right spot
                Elements.Insert(relativeIndex + 1, NewElement);
                elementsDictionary[NewElement.MetadataTerm] = NewElement;
            }
        }