コード例 #1
0
        internal void InsertItems(object parent, List <object> items, int itemsLevel, ViewItem target)
        {
            if (items == null || items.Count == 0 || (parent == this && target != null && target.Selected))
            {
                return;
            }
            if (ItemsCollection.Sort.SortType != SortProperties.SortTypes.None)
            {
                MessageBox.Show("Корректная вставка объектов возможна только после отмены сортировки");
                return;
            }
            if (ItemsCollection.Filter.Any)
            {
                MessageBox.Show("Корректная вставка объектов возможна только после отмены фильтрации");
                return;
            }
            List <UndoRedoManager.RemoveCommand> removeCommands = null;

            UndoRedoManager.PasteCommand pasteCommand = null;
            IsInserting = true;
            bool select = false;

            if (parent == this)
            {
                removeCommands = Remove(false, true);
            }
            if (ItemsCollection.Count == 0)
            {
                if (PasteOptions.PasteType == PasteOptions.PasteTypes.InChildren || (PasteOptions.PasteType == PasteOptions.PasteTypes.InLevel && (itemsLevel == 1 || itemsLevel == int.MaxValue)))
                {
                    ItemsCollection.Insert(0, items);
                    if (IsUndoRedo)
                    {
                        pasteCommand = new UndoRedoManager.PasteCommand(items, null, 0);
                    }
                    select = true;
                }
            }
            else if ((target == null ? ItemsCollection.Count - 1 : target.Index) is int index && index >= 0)
            {
                if (PasteOptions.PasteType == PasteOptions.PasteTypes.InLevel)
                {
                    object           selectItem = ItemsCollection.GetItem(index);
                    IChildCollection selectColl = selectItem as IChildCollection;
                    if (selectColl == null || itemsLevel <= selectColl.DropLevel)
                    {
                        while (selectColl != null && itemsLevel != selectColl.DropLevel)
                        {
                            selectColl = selectColl.Parent as IChildCollection;
                        }
                        if (selectColl == null || (itemsLevel == 1 && selectColl.DropLevel == 1))
                        {
                            if (ItemsCollection.SourceContainer.IndexOf(selectColl == null ? selectItem : selectColl) is int iIndex && iIndex != -1)
                            {
                                ItemsCollection.Insert(iIndex + 1, items);
                                if (IsUndoRedo)
                                {
                                    pasteCommand = new UndoRedoManager.PasteCommand(items, null, iIndex + 1);
                                }
                                select = true;
                            }
                        }
                        else if (selectColl.DropLevel == itemsLevel && selectColl.Parent is IChildCollection pcoll)
                        {
                            for (int j = 0; j < pcoll.Count; j++)
                            {
                                if (pcoll[j] == selectColl)
                                {
                                    pcoll.InsertRange(j + 1, items);
                                    if (IsUndoRedo)
                                    {
                                        pasteCommand = new UndoRedoManager.PasteCommand(items, pcoll, j + 1);
                                    }
                                    select = true;
                                    break;
                                }
                            }
                        }
                    }
                    else if (selectColl != null && itemsLevel == selectColl.DropLevel + 1)
                    {
                        int iIndex = selectColl.Count;
                        selectColl.InsertRange(iIndex, items);
                        selectColl.Dropped = true;
                        if (IsUndoRedo)
                        {
                            pasteCommand = new UndoRedoManager.PasteCommand(items, selectColl, iIndex);
                        }
                        select = true;
                    }
                }
                else if (PasteOptions.PasteType == PasteOptions.PasteTypes.InChildren && ItemsCollection.GetItem(index) is IChildCollection collection)
                {
                    int iIndex = collection.Count - 1;
                    collection.InsertRange(iIndex, items);
                    collection.Dropped = true;
                    if (IsUndoRedo)
                    {
                        pasteCommand = new UndoRedoManager.PasteCommand(items, collection, iIndex);
                    }
                    select = true;
                }
            }
            if (select)
            {
                Select(items);
            }
            if (IsUndoRedo && (removeCommands != null || pasteCommand != null))
            {
                UndoRedoManager.RegistredNewCommand(new UndoRedoManager.MoveCommand(removeCommands, pasteCommand));
            }
            IsInserting = false;
        }