/// <summary>
        /// Adds an item in the collection and in the TreeListView
        /// </summary>
        /// <param name="item"></param>
        /// <returns>Index of the item in the collection</returns>
        public virtual int Add(TreeListViewItem item)
        {
            if (TreeListView != null)
            {
                if (TreeListView.InvokeRequired)
                {
                    throw (new Exception("Invoke required"));
                }
            }
            // Do not add the item if the collection owns a TreeListView recursively
            // and the item already owns a TreeListView
            if (TreeListView != null && item.ListView != null)
            {
                throw (new Exception("The Item is already in a TreeListView"));
            }
            int index = GetInsertCollectionIndex(item);

            if (index == -1)
            {
                return(-1);
            }
            if (Parent != null)
            {
                item.SetParent(Parent);
            }
            item.Items.Comparer = this.Comparer;
            int treelistviewindex = GetInsertTreeListViewIndex(item, index);

            // Insert in the ListView
            if (treelistviewindex > -1)
            {
                ListView listview = (ListView)TreeListView;
                listview.Items.Insert(treelistviewindex, (ListViewItem)item);
                if (item.IsExpanded)
                {
                    item.Expand();
                }
                item.SetIndentation();
            }
            // Insert in this collection
            if (index > -1)
            {
                List.Insert(index, item);
            }
            if (index > -1)
            {
                OnItemAdded(new TreeListViewEventArgs(item, TreeListViewAction.Unknown));
            }
            if (Count == 1 && TreeListView != null && Parent != null)
            {
                if (Parent.Visible)
                {
                    Parent.Redraw();
                }
            }
            return(index);
        }
Esempio n. 2
0
 /// <summary>
 /// Check if this node is one of the parents of an item (recursively)
 /// </summary>
 /// <param name="item"></param>
 /// <returns></returns>
 public bool IsAParentOf(TreeListViewItem item)
 {
     TreeListViewItem[] parents = item.ParentsInHierarch;
     foreach (TreeListViewItem parent in parents)
     {
         if (parent == this)
         {
             return(true);
         }
     }
     return(false);
 }
        private int GetInsertTreeListViewIndex(TreeListViewItem item, int collectionindex)
        {
            if (TreeListView == null)
            {
                return(-1);
            }
            if (TreeListView.InvokeRequired)
            {
                throw (new Exception("Invoke required"));
            }
            if (Owner != null)
            {
                int a = 0;
                a++;
            }
            int index = -1;

            // First level item (no parent)
            if (Owner != null && collectionindex != -1)
            {
                if (collectionindex == 0)
                {
                    index = 0;
                }
                else
                {
                    index =
                        this[collectionindex - 1].LastChildIndexInListView + 1;
                }
            }
            else if (Parent != null && collectionindex != -1)
            {
                if (!Parent.Visible || !Parent.IsExpanded)
                {
                    index = -1;
                }
                else
                {
                    if (collectionindex == 0)
                    {
                        index = Parent.Index + 1;
                    }
                    else
                    {
                        index =
                            Parent.Items[collectionindex - 1].LastChildIndexInListView + 1;
                    }
                }
            }
            return(index);
        }
        /// <summary>
        /// Remove an item from the collection and the TreeListView
        /// </summary>
        /// <param name="item"></param>
        public virtual void Remove(TreeListViewItem item)
        {
            TreeListView treelistview = this.TreeListView;

            if (treelistview != null)
            {
                treelistview.BeginUpdate();
            }
            RemoveInternal(item);
            if (treelistview != null)
            {
                treelistview.EndUpdate();
            }
        }
            /// <summary>
            /// Compare two TreeListViewItems
            /// </summary>
            /// <param name="x"></param>
            /// <param name="y"></param>
            /// <returns></returns>
            public int Compare(object x, object y)
            {
                TreeListViewItem a = (TreeListViewItem)x;
                TreeListViewItem b = (TreeListViewItem)y;
                int res            = 0;

                if (Column < a.SubItems.Count && Column < b.SubItems.Count)
                {
                    string   col1    = a.SubItems[Column].Text;
                    string   col2    = b.SubItems[Column].Text;
                    long     number1 = 0;
                    long     number2 = 0;
                    DateTime date1   = DateTime.Now;
                    DateTime date2   = date1;

                    if (long.TryParse(col1, out number1) && long.TryParse(col2, out number2))
                    {
                        res = number2.CompareTo(number1);
                    }
                    else if (DateTime.TryParse(col1, out date1) && DateTime.TryParse(col2, out date2))
                    {
                        res = date1.CompareTo(date2);
                    }
                    else if ((col1.EndsWith(" Byte(s)") || col1.EndsWith(" KB") || col1.EndsWith(" MB") || col1.EndsWith(" GB")) &&
                             ((col2.EndsWith(" Byte(s)") || col2.EndsWith(" KB") || col2.EndsWith(" MB") || col2.EndsWith(" GB"))))
                    {
                        number1 = GetFileSize(col1);
                        number2 = GetFileSize(col2);
                        res     = number2.CompareTo(number1);
                    }
                    else
                    {
                        res = string.CompareOrdinal(a.SubItems[Column].Text.ToUpper(), b.SubItems[Column].Text.ToUpper());
                    }
                }
                switch (SortOrder)
                {
                case SortOrder.Ascending:
                    return(res);

                case SortOrder.Descending:
                    return(-res);

                default:
                    return(0);
                }
            }
        internal void RemoveInternal(TreeListViewItem item)
        {
            if (TreeListView != null)
            {
                if (TreeListView.InvokeRequired)
                {
                    throw (new Exception("Invoke required"));
                }
            }
            int index = GetIndexOf(item);

            if (index == -1)
            {
                return;
            }
            RemoveAtInternal(index);
        }
        /// <summary>
        /// Transforms the collection to an array
        /// </summary>
        public TreeListViewItem[] ToArray()
        {
            if (TreeListView != null)
            {
                if (TreeListView.InvokeRequired)
                {
                    throw (new Exception("Invoke required"));
                }
            }
            int size = this.Count;

            TreeListViewItem[] eltsArray = new TreeListViewItem[size];
            for (int i = 0; i < size; i++)
            {
                eltsArray[i] = this[i];
            }
            return(eltsArray);
        }
        private int GetInsertCollectionIndex(TreeListViewItem item)
        {
            if (TreeListView != null)
            {
                if (TreeListView.InvokeRequired)
                {
                    throw (new Exception("Invoke required"));
                }
            }
            int index = -1;

            if (!_sortable)
            {
                index = Count;
            }
            else if (!Contains(item) && !ListViewContains(item))
            {
                switch (SortOrder)
                {
                // No sortorder -> at the end of the collection
                case System.Windows.Forms.SortOrder.None:
                    index = this.Count;
                    break;

                default:
                    for (int i = 0; i < this.Count; i++)
                    {
                        // Change the index for the compare if the order is descending
                        int indexcompare = i;
                        int comp         = Comparer.Compare(item, this[indexcompare]);
                        if (comp <= 0)
                        {
                            index = indexcompare;
                            break;
                        }
                    }
                    index = index == -1 ? this.Count : index;
                    break;
                }
            }
            return(index);
        }
        /// <summary>
        /// Gets the index of an item in the collection
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int GetIndexOf(TreeListViewItem item)
        {
            if (TreeListView != null)
            {
                if (TreeListView.InvokeRequired)
                {
                    throw (new Exception("Invoke required"));
                }
            }
            int index = -1;

            for (int i = 0; i < this.Count; i++)
            {
                if (this[i] == item)
                {
                    index = i; break;
                }
            }
            return(index);
        }
Esempio n. 10
0
        /// <summary>
        /// Returns true if this collection contains an item
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public virtual bool Contains(TreeListViewItem item)
        {
            if (TreeListView != null)
            {
                if (TreeListView.InvokeRequired)
                {
                    throw (new Exception("Invoke required"));
                }
            }
            bool res = false;

            foreach (TreeListViewItem elt in this)
            {
                if (item == elt)
                {
                    res = true;
                    break;
                }
            }
            return(res);
        }
Esempio n. 11
0
 internal void SetParent(TreeListViewItem parent)
 {
     _parent = parent;
 }
Esempio n. 12
0
 /// <summary>
 /// Index of an item
 /// </summary>
 /// <param name="item"></param>
 /// <returns></returns>
 public int IndexOf(TreeListViewItem item)
 {
     return(base.IndexOf((ListViewItem)item));
 }
Esempio n. 13
0
 public virtual void InsertAt(int index, TreeListViewItem item)
 {
     List.Insert(index, item);
 }
Esempio n. 14
0
 /// <summary>
 /// Create a collection within a TreeListViewItem
 /// </summary>
 /// <param name="parent"></param>
 public TreeListViewItemCollection(TreeListViewItem parent)
 {
     _parent = parent;
 }
Esempio n. 15
0
        internal void ExpandInternal()
        {
            if (IsInATreeListView)
            {
                if (ListView.InvokeRequired)
                {
                    throw (new Exception("Invoke Required"));
                }
            }

            TreeListViewItem selItem = null;

            if (TreeListView != null)
            {
                selItem = TreeListView.FocusedItem;
            }

            // Must set ListView.checkDirection to CheckDirection.None.
            // Forbid recursively checking.
            CheckDirection oldDirection = CheckDirection.All;

            if (ListView != null)
            {
                oldDirection             = ListView._checkDirection;
                ListView._checkDirection = CheckDirection.None;
            }

            // The item wasn't expanded -> raise an event
            if (Visible && !_isexpanded && ListView != null)
            {
                TreeListViewCancelEventArgs e = new TreeListViewCancelEventArgs(
                    this, TreeListViewAction.Expand);
                ListView.RaiseBeforeExpand(e);
                if (e.Cancel)
                {
                    return;
                }
            }

            if (Visible)
            {
                for (int i = Items.Count - 1; i >= 0; i--)
                {
                    TreeListViewItem item = this.Items[i];
                    if (!item.Visible)
                    {
                        ListView LView = this.ListView;
                        LView.Items.Insert(
                            this.Index + 1, item);
                        item.SetIndentation();
                    }
                    if (item.IsExpanded)
                    {
                        item.Expand();
                    }
                }
            }
            // The item wasn't expanded -> raise an event
            if (Visible && !_isexpanded && IsInATreeListView)
            {
                this._isexpanded = true;
                TreeListViewEventArgs e = new TreeListViewEventArgs(
                    this, TreeListViewAction.Expand);
                ListView.RaiseAfterExpand(e);
                if (AfterExpand != null)
                {
                    AfterExpand(this);
                }
            }
            this._isexpanded = true;

            // Reset ListView.checkDirection
            if (IsInATreeListView)
            {
                ListView._checkDirection = oldDirection;
            }
            if (TreeListView != null && selItem != null)
            {
                if (selItem.Visible)
                {
                    selItem.Focused = true;
                }
            }
        }
Esempio n. 16
0
        internal void CollapseInternal()
        {
            if (IsInATreeListView)
            {
                if (ListView.InvokeRequired)
                {
                    throw (new Exception("Invoke Required"));
                }
            }
            TreeListViewItem selItem = null;

            if (TreeListView != null)
            {
                selItem = TreeListView.FocusedItem;
            }
            // The item was expanded -> raise an event
            if (Visible && _isexpanded && ListView != null)
            {
                TreeListViewCancelEventArgs e = new TreeListViewCancelEventArgs(
                    this, TreeListViewAction.Collapse);
                ListView.RaiseBeforeCollapse(e);
                if (e.Cancel)
                {
                    return;
                }
            }

            // Collapse
            if (this.Visible)
            {
                foreach (TreeListViewItem item in Items)
                {
                    item.Hide();
                }
            }

            // The item was expanded -> raise an event
            if (Visible && _isexpanded && IsInATreeListView)
            {
                this._isexpanded = false;
                TreeListViewEventArgs e = new TreeListViewEventArgs(
                    this, TreeListViewAction.Collapse);
                ListView.RaiseAfterCollapse(e);
                if (AfterCollapse != null)
                {
                    AfterCollapse(this);
                }
            }
            this._isexpanded = false;
            if (IsInATreeListView && selItem != null)
            {
                if (selItem.Visible)
                {
                    selItem.Focused = true;
                }
                else
                {
                    ListView listview = (ListView)TreeListView;
                    listview.SelectedItems.Clear();
                    TreeListViewItem[] items = selItem.ParentsInHierarch;
                    for (int i = items.Length - 1; i >= 0; i--)
                    {
                        if (items[i].Visible)
                        {
                            items[i].Focused = true;
                            break;
                        }
                    }
                }
            }
        }
Esempio n. 17
0
 /// <summary>
 /// Returns true if the specified item is in the collection
 /// </summary>
 /// <param name="item"></param>
 /// <returns></returns>
 public bool Contains(TreeListViewItem item)
 {
     return(base.Contains((ListViewItem)item));
 }