Beispiel #1
0
        /// <summary>
        /// Do specified action.
        /// </summary>
        /// <param name="action">Action to be performed</param>
        /// <param name="perform">True to perform immediately, false to add into stack only</param>
        /// <param name="isCanUndo">Specifies that whether the action can be undone,
        /// sometimes an action might not necessary to be undone even it implements the
        /// IUndoable interface.</param>
        private void Do(IAction action, bool perform, bool isCanUndo)
        {
            Logger.Log(LOGKEY, string.Format("{0} action: {1}[{2}]", perform ? "do" : "add", action.GetType().Name, action.GetName()));

            if (BeforePerformAction != null)
            {
                var arg = new ActionEventArgs(action, ActionBehavior.Do);
                BeforePerformAction(this, arg);
                if (arg.Cancel)
                {
                    return;
                }
            }

            if (perform)
            {
                action.Do();
            }

            if (action is IUndoableAction && isCanUndo)
            {
                redoStack.Clear();
                undoStack.Add(action as IUndoableAction);

                if (undoStack.Count() > capacity)
                {
                    int removedActions = undoStack.Count() - capacity;
                    undoStack.RemoveRange(0, removedActions);
                    Logger.Log(LOGKEY, "action stack full. remove " + (removedActions) + " action(s).");
                }
            }

            AfterPerformAction?.Invoke(this, new ActionEventArgs(action, ActionBehavior.Do));
        }
Beispiel #2
0
        public IAction Redo()
        {
            if (redoStack.Count > 0)
            {
                IUndoableAction action = null;

                while (redoStack.Count > 0)
                {
                    action = redoStack.Pop();
                    Logger.Log(LOGKEY, "redo action: " + action.ToString());

                    if (BeforePerformAction != null)
                    {
                        var arg = new ActionEventArgs(action, ActionBehavior.Redo);
                        BeforePerformAction(this, arg);
                        if (arg.Cancel)
                        {
                            break;
                        }
                    }

                    action.Redo();
                    undoStack.Add(action);

                    AfterPerformAction?.Invoke(this, new ActionEventArgs(action, ActionBehavior.Redo));

                    if (!(action is ISerialUndoAction))
                    {
                        break;
                    }
                }

                return(action);
            }
            else
            {
                return(null);
            }
        }
Beispiel #3
0
        public IAction Undo()
        {
            if (undoStack.Count() > 0)
            {
                IUndoableAction action = null;

                while (undoStack.Count > 0)
                {
                    action = undoStack.Last();
                    Logger.Log(LOGKEY, "undo action: " + action.ToString());

                    // before event
                    if (BeforePerformAction != null)
                    {
                        var arg = new ActionEventArgs(action, ActionBehavior.Undo);
                        BeforePerformAction(this, new ActionEventArgs(action, ActionBehavior.Undo));
                        if (arg.Cancel)
                        {
                            break;
                        }
                    }

                    undoStack.Remove(action);
                    action.Undo();
                    redoStack.Push(action);

                    // after event
                    AfterPerformAction?.Invoke(this, new ActionEventArgs(action, ActionBehavior.Undo));

                    if (!(action is ISerialUndoAction))
                    {
                        break;
                    }
                }

                return(action);
            }
            return(null);
        }