public void Insert_Element_Before(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)
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, NewElement);
elementsDictionary[NewElement.MetadataTerm] = NewElement;
}
}