Example #1
0
        /// <summary>
        /// Registers an <see cref="IInvokable"/> implementation to the <see cref="ITransaction"/>.
        /// </summary>
        /// <param name="invokation">The invokation to register.</param>
        /// <exception cref="ArgumentNullException"><paramref name="invokation"/> is a <see langword="null"/> reference.</exception>
        public void RegisterInvokation(IInvokable invokation)
        {
            if (invokation == null)
            {
                throw new ArgumentNullException("invokation");
            }

            if (this.state != UndoRedoState.RollingBack)
            {
                IInvokableTransaction recordingTransaction = this.FetchRecordingTransaction();
                if (recordingTransaction != null)
                {
                    recordingTransaction.RegisterInvokation(invokation);
                }
                else
                {
                    using (IInvokableTransaction transaction = this.InnerCreateTransaction(string.Empty))
                    {
                        transaction.RegisterInvokation(invokation);
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Commits the provided transaction.
        /// </summary>
        /// <param name="transaction">The transaction to commit.</param>
        /// <exception cref="ArgumentNullException"><paramref name="transaction"/> is a <see langword="null"/> reference.</exception>
        /// <exception cref="ArgumentException">The <see cref="UndoManager"/> does not contain <paramref name="transaction"/>.</exception>
        void ITransactionManager.CommitTransaction(IInvokableTransaction transaction)
        {
            if (transaction == null)
            {
                throw new ArgumentNullException("transaction");
            }

            if (!this.openTransactions.Contains(transaction))
            {
                throw new ArgumentException("Cannot find the transaction to commit", "transaction");
            }

            // only switch the state to committing if the undo manager is not doing another task (e.g. undoing).
            UndoRedoState currentState = this.state == UndoRedoState.Idle ? UndoRedoState.Committing : this.state;

            using (new StateSwitcher(this, currentState))
            {
                while (this.openTransactions.Contains(transaction))
                {
                    IInvokableTransaction toCommit = this.openTransactions.Pop();

                    if (toCommit.Any())
                    {
                        if (toCommit.Equals(transaction))
                        {
                            Stack <IInvokableTransaction> history = this.IsUndoing ? this.redoHistory : this.undoHistory;
                            history.Push(transaction);
                        }
                        else
                        {
                            IInvokableTransaction topMost = this.openTransactions.Peek();
                            topMost.RegisterInvokation(toCommit);
                        }
                    }
                }
            }
        }