Exemplo n.º 1
0
        /// <summary>
        /// The workhorse to expand a threaded listview item
        /// </summary>
        /// <param name="lvItem"></param>
        /// <param name="activate">true, if lvItem should be activated</param>
        internal void ExpandListViewItem(ThreadedListViewItem lvItem, bool activate)
        {
            int selIdxsCount = SelectedIndices.Count;
            var selIdxs      = new int[selIdxsCount];

            SelectedIndices.CopyTo(selIdxs, 0);

            ThreadedListViewItem[] newItems;

            if (lvItem != null && lvItem.Collapsed)
            {
                if (RaiseBeforeExpandEventCancel(lvItem))
                {
                    lvItem.StateImageIndex = 0; // switch to non-state image (no +/-)
                    return;
                }

                int paramItemIndex = lvItem.Index;
                int currentIndent  = lvItem.IndentLevel;

//				if (base.SelectedItems.Count > 0 && activate)
//					selectedItemIndex = paramItemIndex;

                newItems = RaiseExpandEvent(lvItem);

                if (newItems == null)
                {
                    ThreadedListViewItem item = _noChildsPlaceHolder;
                    if (item == null)
                    {
                        item = new ThreadedListViewItemPlaceHolder(SR.FeedListNoChildsMessage)
                        {
                            Font = new Font(Font.FontFamily, Font.Size, FontStyle.Regular)
                        };
                    }
                    newItems = new[] { item };
                }

                if (newItems.Length > 1 && ListViewItemSorter != null)
                {
                    // sort new child entries according to listview sortorder
                    Array.Sort(newItems, ListViewItemSorter);
                }

                if (_showInGroups)
                {
                    APIEnableGrouping(false);
                }

                BeginUpdate();
                try
                {
                    lvItem.SetThreadState(true);

                    lock (Items)
                    {
                        foreach (var newListItem in newItems)
                        {
                            // check, if we have  all subitems for correct grouping
                            while (newListItem.SubItems.Count < Columns.Count)
                            {
                                newListItem.SubItems.Add(String.Empty);
                            }

                            newListItem.Parent = lvItem;
                            Items.Insert(paramItemIndex + 1, newListItem);
                            newListItem.IndentLevel = currentIndent + 1;

                            paramItemIndex++;
                        }
                    }
                }
                finally
                {
                    EndUpdate();
                }

                RedrawItems();

                if (_showInGroups)
                {
                    APIEnableGrouping(true);
                }

                // Make the last inserted subfolder visible, then the parent folder visible,
                // per default treeview behavior.
                try
                {
                    EnsureVisible(paramItemIndex - 1);
                    EnsureVisible(lvItem.Index);
                }
                catch (Exception ex)
                {
                    _log.Error("EnsureVisible() failed", ex);
                }

                if (activate)
                {
                    SelectedItems.Clear();
                    lvItem.Selected = true;
                    lvItem.Focused  = true;
                }
                else if (selIdxsCount > 0)
                {
//					foreach (int i in selIdxs) {
//						this.Items[i].Selected = true;
//					}
                }

                RaiseAfterExpandEvent(lvItem, newItems);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Insert items as a replacement for a placeholder item inserted before.
        /// The placeholder item will be identified by the placeHolderTicket to be provided.
        /// </summary>
        /// <param name="placeHolderTicket">The unique placeholder item's ticket. (string)</param>
        /// <param name="newChildItems">ThreadedListViewItem array</param>
        /// <param name="sortOnInsert">True uses the current sort column and order to insert childs.
        /// Set to false, if you want to sort by your own.</param>
        /// <exception cref="ArgumentNullException">If placeHolderTicket is null or empty</exception>
        public void InsertItemsForPlaceHolder(string placeHolderTicket, ThreadedListViewItem[] newChildItems,
                                              bool sortOnInsert)
        {
            if (string.IsNullOrEmpty(placeHolderTicket))
            {
                throw new ArgumentNullException("placeHolderTicket");
            }

            ThreadedListViewItemPlaceHolder placeHolder = null;

            lock (Items)
            {
                for (int i = 0; i < Items.Count; i++)
                {
                    var p = Items[i] as ThreadedListViewItemPlaceHolder;
                    if (p != null)
                    {
                        // found one; check ticket
                        if (p.InsertionPointTicket.Equals(placeHolderTicket))
                        {
                            placeHolder = p;
                            break;
                        }
                    }
                }

                // the ThreadedListViewItemPlaceHolder to work on is not anymore displayed;
                // because of a refresh or user has collapsed the parent meanwhile
                if (placeHolder == null)
                {
                    return;
                }

                int parentItemIndex   = placeHolder.Index;
                int parentIndentLevel = placeHolder.IndentLevel;

                try
                {
                    BeginUpdate();

                    // remove the placeholder
                    Items.RemoveAt(parentItemIndex);

                    if (newChildItems == null || newChildItems.Length == 0)
                    {
                        ThreadedListViewItem item = _noChildsPlaceHolder;
                        if (item == null)
                        {
                            item = new ThreadedListViewItemPlaceHolder(SR.FeedListNoChildsMessage)
                            {
                                Font = new Font(Font.FontFamily, Font.Size, FontStyle.Regular)
                            };
                        }
                        newChildItems = new[] { item };
                    }

                    if (newChildItems.Length > 1 && _sorter.SortOrder != SortOrder.None && sortOnInsert)
                    {
                        // sort new child entries according to listview sortorder
                        Array.Sort(newChildItems, _sorter.GetComparer());
                    }

                    // now insert the new items
                    for (int i = 0; i < newChildItems.Length; i++)
                    {
                        ThreadedListViewItem newListItem = newChildItems[i];
                        newListItem.Parent = placeHolder.Parent;

                        // check, if we have  all subitems for correct grouping
                        while (newListItem.SubItems.Count < Columns.Count)
                        {
                            newListItem.SubItems.Add(String.Empty);
                        }

                        if (parentItemIndex < Items.Count)
                        {
                            //_log.Info("InsertItemsForPlaceHolder() insert at " + String.Format("{0}", parentItemIndex + 1));
                            Items.Insert(parentItemIndex, newListItem);
                        }
                        else
                        {
                            //_log.Info("InsertItemsForPlaceHolder() append.");
                            Items.Add(newListItem);
                        }

                        newListItem.IndentLevel = parentIndentLevel;
                        // only valid after the item is part of the listview items collection!
                        parentItemIndex++;
                    } //iterator.MoveNext
                }
                finally
                {
                    EndUpdate();
                }
            } //lock

            UpdateItems();
        }