Esempio n. 1
0
        /// <summary>
        /// Adds the given elements to the tree model
        /// </summary>
        /// <param name="elements"></param>
        /// <param name="parent"></param>
        /// <param name="insertPosition"></param>
        public void AddElements(IList <T> elements, TreeElement parent, int insertPosition)
        {
            if (elements == null)
            {
                throw new ArgumentNullException("elements", "elements is null");
            }
            if (elements.Count == 0)
            {
                throw new ArgumentNullException("elements", "elements Count is 0: nothing to add");
            }
            if (parent == null)
            {
                throw new ArgumentNullException("parent", "parent is null");
            }

            if (parent.children == null)
            {
                parent.children = new List <TreeElement>();
            }

            parent.children.InsertRange(insertPosition, elements.Cast <TreeElement>());
            foreach (var element in elements)
            {
                element.parent = parent;
                element.depth  = parent.depth + 1;
                TreeElement.UpdateDepthValues(element);
            }

            TreeElement.TreeToList(this.root, this.data);

            OnChanged();
        }
Esempio n. 2
0
 public void Repair()
 {
     //this.Clear();
     if (!hasRoot)
     {
         this.AddRoot();
     }
     TreeElement.UpdateDepthValues(this.root);
 }
Esempio n. 3
0
        /// <summary>
        /// Reparents the given elements
        /// </summary>
        /// <param name="parentElement"></param>
        /// <param name="insertionIndex"></param>
        /// <param name="elements"></param>
        public void MoveElements(TreeElement parentElement, int insertionIndex, params TreeElement[] elements)
        {
            if (insertionIndex < 0)
            {
                throw new ArgumentException("Invalid input: insertionIndex is -1, client needs to decide what index elements should be reparented at");
            }

            // Invalid reparenting input
            if (parentElement == null)
            {
                return;
            }

            // We are moving items so we adjust the insertion index to accomodate that any items above the insertion index is removed before inserting
            if (insertionIndex > 0)
            {
                insertionIndex -= parentElement.children.GetRange(0, insertionIndex).Count(elements.Contains);
            }

            // Remove draggedItems from their parents
            foreach (var draggedItem in elements)
            {
                draggedItem.parent.children.Remove(draggedItem); // remove from old parent
                draggedItem.parent = parentElement;              // set new parent
            }

            if (parentElement.children == null)
            {
                parentElement.children = new List <TreeElement>();
            }

            // Insert dragged items under new parent
            parentElement.children.InsertRange(insertionIndex, elements);

            TreeElement.UpdateDepthValues(root);
            TreeElement.TreeToList(this.root, this.data);

            OnChanged();
        }
Esempio n. 4
0
        /// <summary>
        /// Adds an element onto the tree
        /// </summary>
        /// <param name="element"></param>
        /// <param name="parent"></param>
        /// <param name="insertPosition"></param>
        public void AddElement(T element, TreeElement parent, int insertPosition)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element", "element is null");
            }
            if (parent == null)
            {
                throw new ArgumentNullException("parent", "parent is null");
            }

            if (parent.children == null)
            {
                parent.children = new List <TreeElement>();
            }

            parent.children.Insert(insertPosition, element);
            element.parent = parent;

            TreeElement.UpdateDepthValues(parent);
            TreeElement.TreeToList(this.root, this.data);

            OnChanged();
        }