コード例 #1
0
        /// <summary>
        /// Removes the element at the specified index of the <see cref="T:System.Collections.ObjectModel.Collection`1"/>.
        /// </summary>
        /// <param name="index">The zero-based index of the element to remove.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">
        ///     <paramref name="index"/> is less than zero.-or-<paramref name="index"/> is equal to or greater than <see cref="P:System.Collections.ObjectModel.Collection`1.Count"/>.</exception>
        protected override void RemoveItem(int index)
        {
            T item = default(T);

            PerformSyncedAction(() =>
            {
                VerifyIndex(index);
                item = this[index];
            });
            if (!this.SuppressEvents)
            {
                CancelableListModificationEventArgs <T> eventArgs = new CancelableListModificationEventArgs <T>(item);
                this.ElementRemoving.RaiseEvent(this, eventArgs);
                if (eventArgs.Cancel)
                {
                    return;
                }
            }
            // create a command which simply removes the item at the given index and as undo function it re-inserts the item at the index specified.
            // The command created passes the current item at the index specified, but it's not really used, as there's no state to set. The command however has to
            // keep track of the item removed, so the state inside it has to be of type T.
            Command <T> removeCmd = new Command <T>(() => this.PerformRemoveItem(index), () => this[index], i => this.PerformInsertItem(index, i), _cachedCommandDescriptions[ListCommandType.RemoveItem]);

            CommandQueueManagerSingleton.GetInstance().EnqueueAndRunCommand(removeCmd);
        }
コード例 #2
0
        public void CancelableEventsOnCommandifiedListTests()
        {
            CommandifiedList <int> list = new CommandifiedList <int>()
            {
                1, 2, 3
            };
            EventHandler <CancelableListModificationEventArgs <int> > cancelableHandler = delegate(object sender, CancelableListModificationEventArgs <int> e) { e.Cancel = true; };

            list.ElementAdding   += cancelableHandler;
            list.ElementRemoving += cancelableHandler;
            list.ListClearing    += cancelableHandler;
            // reset command queue manager, so it will only undo commands issued after this reset
            CommandQueueManagerSingleton.GetInstance().ResetActiveCommandQueue();

            list.Add(4);
            Assert.AreEqual(3, list.Count);
            Assert.IsFalse(list.Contains(4));
            // undo shouldn't have any effect
            CommandQueueManagerSingleton.GetInstance().UndoLastCommand();
            Assert.AreEqual(3, list.Count);
            Assert.IsFalse(list.Contains(4));

            list.Clear();
            Assert.AreEqual(3, list.Count);
            // undo shouldn't have any effect
            CommandQueueManagerSingleton.GetInstance().UndoLastCommand();
            Assert.AreEqual(3, list.Count);

            list.Remove(1);
            Assert.AreEqual(3, list.Count);
            Assert.IsTrue(list.Contains(1));

            // undo shouldn't have any effect
            CommandQueueManagerSingleton.GetInstance().UndoLastCommand();
            Assert.AreEqual(3, list.Count);
            Assert.IsTrue(list.Contains(1));

            list.ElementAdding   -= cancelableHandler;
            list.ElementRemoving -= cancelableHandler;
            list.ListClearing    -= cancelableHandler;

            list.Add(4);
            Assert.AreEqual(4, list.Count);
            Assert.IsTrue(list.Contains(4));

            list.Remove(1);
            Assert.AreEqual(3, list.Count);
            Assert.IsFalse(list.Contains(1));

            list.Clear();
            Assert.AreEqual(0, list.Count);
        }
コード例 #3
0
        /// <summary>
        /// Removes all elements from the <see cref="T:System.Collections.ObjectModel.Collection`1"/>.
        /// </summary>
        protected override void ClearItems()
        {
            if (!this.SuppressEvents)
            {
                CancelableListModificationEventArgs <T> eventArgs = new CancelableListModificationEventArgs <T>();
                this.ListClearing.RaiseEvent(this, eventArgs);
                if (eventArgs.Cancel)
                {
                    return;
                }
            }
            // create a command which stores the current state into a temp collection and then clears this collection.
            Command <Collection <T> > clearCmd = new Command <Collection <T> >(() => this.PerformClearItems(), () => this.GetCurrentState(), c => this.SetCurrentState(c), "Clear this instance");

            CommandQueueManagerSingleton.GetInstance().EnqueueAndRunCommand(clearCmd);
        }
コード例 #4
0
        /// <summary>
        /// Replaces the element at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index of the element to replace.</param>
        /// <param name="item">The new value for the element at the specified index. The value can be null for reference types.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">
        ///     <paramref name="index"/> is less than zero.-or-<paramref name="index"/> is greater than <see cref="P:System.Collections.ObjectModel.Collection`1.Count"/>.</exception>
        protected override void SetItem(int index, T item)
        {
            VerifyIndex(index);
            if (!this.SuppressEvents)
            {
                CancelableListModificationEventArgs <T> eventArgs = new CancelableListModificationEventArgs <T>(item);
                this.ElementRemoving.RaiseEvent(this, eventArgs);
                if (eventArgs.Cancel)
                {
                    return;
                }
                this.ElementAdding.RaiseEvent(this, eventArgs);
                if (eventArgs.Cancel)
                {
                    return;
                }
            }
            // Create a command which stores the current item at index and sets item as the new item at index.
            Command <T> setCmd = new Command <T>(() => this.PerformSetItem(index, item), () => this[index], i => this.PerformSetItem(index, i), _cachedCommandDescriptions[ListCommandType.SetItem]);

            CommandQueueManagerSingleton.GetInstance().EnqueueAndRunCommand(setCmd);
        }
コード例 #5
0
 /// <summary>
 /// Inserts an element into the <see cref="T:System.Collections.ObjectModel.Collection`1"/> at the specified index.
 /// </summary>
 /// <param name="index">The zero-based index at which <paramref name="item"/> should be inserted.</param>
 /// <param name="item">The object to insert. The value can be null for reference types.</param>
 /// <exception cref="T:System.ArgumentOutOfRangeException">
 ///     <paramref name="index"/> is less than zero.-or-<paramref name="index"/> is greater than <see cref="P:System.Collections.ObjectModel.Collection`1.Count"/>.</exception>
 protected override void InsertItem(int index, T item)
 {
     // index isn't verified as index can be invalid (in the case of an append).
     if (!this.SuppressEvents)
     {
         CancelableListModificationEventArgs <T> eventArgs = new CancelableListModificationEventArgs <T>(item);
         this.ElementAdding.RaiseEvent(this, eventArgs);
         if (eventArgs.Cancel)
         {
             return;
         }
     }
     // we can skip the ceremony of going through the command structure if that's disabled.
     if (CommandQueueManagerSingleton.GetInstance().IsInNonUndoablePeriod)
     {
         this.PerformInsertItem(index, item);
     }
     else
     {
         // create a command which simply inserts the item at the given index and as undo function removes the item at the index specified.
         Command <T> .DoNow(() => this.PerformInsertItem(index, item), () => this.PerformRemoveItem(index), "Insert an item");
     }
 }