private void Add()
 {
     if (!AllItems.Any(x => x.IsEmpty))
     {
         AllItems.Insert(0, new NamespaceItem(""));
     }
 }
Exemple #2
0
        /// <summary>
        /// Adds (inserts before) a child KmlItem to this nodes lists of children.
        /// If item to insert before is null or not contained, it will be added at the end.
        /// This is the basic add method, derived classes can override but should
        /// always call base.Add(beforeItem, newItem) within.
        /// Public Add, AddRange, InsertBefore and InsertAfter all use this protected
        /// method to access the lists.
        /// <see cref="KML.KmlNode.Add(KML.KmlItem)"/>
        /// </summary>
        /// <param name="beforeItem">The KmlItem where the new item should be inserted before</param>
        /// <param name="newItem">The KmlItem to add</param>
        protected virtual void Add(KmlItem beforeItem, KmlItem newItem)
        {
            // ensure that item.Parent is this node
            if (newItem.Parent != this)
            {
                RemapParent(newItem, this);
            }

            // Not add always to end of AllItems, add well ordered: attribs first, then nodes.
            // Like Add(attrib), Add(Node), Add(attrib) should result in attrib, attrib, node
            if (newItem is KmlAttrib && !(beforeItem is KmlAttrib) && Children.Count > 0)
            {
                Syntax.Warning(newItem, "KML attribute should not come after nodes, will be fixed when saved");
                beforeItem = Children[0];
            }

            if (beforeItem != null && AllItems.Contains(beforeItem))
            {
                AllItems.Insert(AllItems.IndexOf(beforeItem), newItem);
            }
            else
            {
                AllItems.Add(newItem);
            }

            if (newItem is KmlNode)
            {
                if (beforeItem is KmlNode && Children.Contains((KmlNode)beforeItem))
                {
                    Children.Insert(Children.IndexOf((KmlNode)beforeItem), (KmlNode)newItem);
                }
                else
                {
                    Children.Add((KmlNode)newItem);
                }
                InvokeChildrenChanged();
            }
            else if (newItem is KmlAttrib)
            {
                KmlAttrib attrib = (KmlAttrib)newItem;
                if (attrib.Name.ToLower() == "name")
                {
                    if (Name.Length == 0)
                    {
                        Name = attrib.Value;

                        // Get notified when Name changes
                        attrib.AttribValueChanged += Name_Changed;
                        attrib.CanBeDeleted        = false;

                        // And notify that the name changed
                        InvokeToStringChanged();
                    }
                }

                if (beforeItem is KmlAttrib && Attribs.Contains((KmlAttrib)beforeItem))
                {
                    Attribs.Insert(Attribs.IndexOf((KmlAttrib)beforeItem), attrib);
                }
                else
                {
                    Attribs.Add(attrib);
                }
                InvokeAttribChanged();
            }
            else
            {
                if (beforeItem != null && Unknown.Contains(newItem))
                {
                    Unknown.Insert(Unknown.IndexOf(beforeItem), newItem);
                }
                else
                {
                    Unknown.Add(newItem);
                }
                Syntax.Warning(this, "Unknown line in persistence file: " + newItem.ToString());
            }
        }