无操作次数限制的 Undo/Redo 实现方案

一般的文档软件,图形设计工具,都会有Redo/Undo (即重做/撤消)功能,可是如何也在自己的应用程序当中实现这样的功能,而且是没有操作次数地Redo/Undo?
此时“软件设计模式”就显得很重要。
这里实现的Redo/Undo主要应用的“命令模式”与“备忘录模式”。
《C#设计模式》一书里讲到只用备忘录模式来实现,我看了之后觉得那种方法太烦琐了,我们可以利用

C#2.0的新特性来使之简化。
首先,如果要让我们的每一步操作都是可撤消、重做的,那么我们必须要将这些操作涉及的代码“封装

”到一个方法里去,以供以后调用。而且有撤消、重做,那么必须有两部分代码:
1.正向操作的代码 DoOperation
2.反向(撤消)的代码 UndoOperation.

然后,要实现无次数限制的撤消、重做,我们应该要用两个栈(Stack)来保存操作。

下面的UML 类图:


下面是实现代码:
1.ICommand.cs

None.gif      public   interface  ICommand
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif        ActionHandler DoOperation 
dot.gifget;set;}
ExpandedSubBlockStart.gifContractedSubBlock.gif        ActionHandler UndoOperation 
dot.gifget;set;}
InBlock.gif
InBlock.gif        
void Do();
InBlock.gif        
void Undo();
ExpandedBlockEnd.gif    }

2. 操作委托:

None.gif public   delegate   void  ActionHandler();

3. Command.cs

None.gif      public   class  Command:ICommand 
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private ActionHandler doOperation;
InBlock.gif
InBlock.gif        
private ActionHandler undoOperation;
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
ICommand Members#region ICommand Members
InBlock.gif
InBlock.gif        
public ActionHandler DoOperation
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return doOperation;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                doOperation 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public ActionHandler UndoOperation
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return undoOperation;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                undoOperation 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Do()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            doOperation();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Undo()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            undoOperation();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedBlockEnd.gif    }

4. CommandManager.cs

None.gif      public   static   class  CommandManager
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private static Stack<ICommand> redoMementos;
InBlock.gif
InBlock.gif        
private static Stack<ICommand> undoMementos;
InBlock.gif
InBlock.gif        
static CommandManager()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            redoMementos 
= new Stack<ICommand>();
InBlock.gif            undoMementos 
= new Stack<ICommand>();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 先清空Redo栈,将Command对象压入栈
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="command">Command对象</param>

InBlock.gif        public static void AddNewCommand(ICommand command)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            redoMementos.Clear();
InBlock.gif            undoMementos.Push(command);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从Redo栈中弹出一个Command对象,执行Do操作,并且将该Command对象压入Undo栈
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static void Redo()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (redoMementos.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    ICommand command 
= redoMementos.Pop();
InBlock.gif                    
if (command != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        command.Do();
InBlock.gif                        undoMementos.Push(command);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (InvalidOperationException invalidOperationException)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(invalidOperationException.Message);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从Undo栈中弹出一个Command对象,执行Undo操作,并且将该Command对象压入Redo栈
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static void Undo()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (undoMementos.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    ICommand command 
= undoMementos.Pop();
InBlock.gif                    
if (command != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        command.Undo();
InBlock.gif                        redoMementos.Push(command);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (InvalidOperationException invalidOperationException)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(invalidOperationException.Message);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 清空Redo栈与Undo栈
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static void ClearAll()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ClearRedoStack();
InBlock.gif            ClearUndoStack();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 清空Redo栈
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static void ClearRedoStack()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            redoMementos.Clear();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 清空Undo栈
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static void ClearUndoStack()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            undoMementos.Clear();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 当前在Redo栈中能重做的步骤数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static int RedoStepsCount
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return redoMementos.Count;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 当前在Undo栈中能撤消的步骤数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static int UndoStepsCount
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return undoMementos.Count;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }


5. Redo/Undo测试UI程序(Form), 在Form上添加三个Button控件:btnNew, btnRedo, btnUndo, 逻辑代

码如下:

None.gif      public  partial  class  Form1 : Form
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private int x=10;
InBlock.gif        
private int y=10;
InBlock.gif        
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void btnNew_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            btnRedo.Enabled 
= false;
InBlock.gif            Command command 
= new Command();
InBlock.gif            Label label 
= new Label();
InBlock.gif            Random r 
= new Random();
InBlock.gif            label.Text 
= ((char)r.Next(6599)).ToString();
InBlock.gif            label.Font 
= new Font("Arial"16,FontStyle.Bold);
InBlock.gif            label.AutoSize 
= true;
InBlock.gif            label.Location 
= new Point(x, y);
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//*
InBlock.gif             * 使用匿名委托,更加简单,而且匿名委托方法里还可以使用外部变量。
ExpandedSubBlockEnd.gif             
*/

InBlock.gif            command.DoOperation 
= delegate()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//Do或者Redo操作会执行到的代码
InBlock.gif
                x += label.Width + 10;
InBlock.gif                y 
+= label.Height + 10;
InBlock.gif                
this.Controls.Add(label);
ExpandedSubBlockEnd.gif            }
;
InBlock.gif            command.UndoOperation 
= delegate()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//Undo操作会执行到的代码
InBlock.gif
                x -= label.Width + 10;
InBlock.gif                y 
-= label.Height + 10;
InBlock.gif                
this.Controls.Remove(label);
ExpandedSubBlockEnd.gif            }
;
InBlock.gif            command.Do();
//执行DoOperation相应代码
InBlock.gif
            CommandManager.AddNewCommand(command);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void btnRedo_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            btnUndo.Enabled 
= true;
InBlock.gif            CommandManager.Redo();
InBlock.gif            
if (CommandManager.RedoStepsCount == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                btnRedo.Enabled 
= false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void btnUndo_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            btnRedo.Enabled 
= true;
InBlock.gif            CommandManager.Undo();
InBlock.gif            
if (CommandManager.UndoStepsCount == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                btnUndo.Enabled 
= false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

这是我第一次在Blog上写关于设计模式的心得体会,难免有写得不对之处,希望与大家多交流,多指正不当之处。。。。

转载于:https://www.cnblogs.com/vivid-stanley/archive/2007/02/13/649824.html

在 Eclipse 中,可以使用 `org.eclipse.ui.actions.ActionFactory.UNDO` 和 `org.eclipse.ui.actions.ActionFactory.REDO` 来创建 UndoRedo 动作。然后,将这些动作关联到菜单栏 Edit 中的 UndoRedo 菜单项。 要在 `ViewPart` 中实现 Undo/Redo,您需要遵循以下步骤: 1. 在 `ViewPart` 类中添加以下字段: ```java private IUndoContext undoContext; private IUndoableOperation undoOperation; private IAction undoAction; private IAction redoAction; ``` 2. 在 `createPartControl` 方法中初始化这些字段: ```java public void createPartControl(Composite parent) { // 创建 Undo/Redo 上下文 undoContext = new ObjectUndoContext(new Object()); // 创建 Undo/Redo 动作 undoAction = ActionFactory.UNDO.create(getSite().getWorkbenchWindow()); undoAction.setText("Undo"); undoAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages() .getImageDescriptor(ISharedImages.IMG_TOOL_UNDO)); undoAction.setEnabled(false); redoAction = ActionFactory.REDO.create(getSite().getWorkbenchWindow()); redoAction.setText("Redo"); redoAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages() .getImageDescriptor(ISharedImages.IMG_TOOL_REDO)); redoAction.setEnabled(false); // 将 Undo/Redo 动作添加到菜单栏 Edit 中 IMenuManager menuMgr = getViewSite().getActionBars().getMenuManager(); menuMgr.add(new Separator()); menuMgr.add(undoAction); menuMgr.add(redoAction); } ``` 3. 在 `createPartControl` 方法中为 Undo/Redo 动作添加处理程序: ```java public void createPartControl(Composite parent) { // ... // 为 Undo/Redo 动作添加处理程序 undoAction.setHandler(new UndoActionHandler(getViewSite(), undoContext)); redoAction.setHandler(new RedoActionHandler(getViewSite(), undoContext)); } ``` 4. 在 `createPartControl` 方法中为 `parent` 控件创建上下文菜单,并将 Undo/Redo 动作添加到上下文菜单中: ```java public void createPartControl(Composite parent) { // ... // 为 parent 控件创建上下文菜单,并将 Undo/Redo 动作添加到上下文菜单中 MenuManager menuMgr = new MenuManager(); menuMgr.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); getSite().registerContextMenu(menuMgr, getSite().getSelectionProvider()); parent.setMenu(menuMgr.createContextMenu(parent)); menuMgr.addMenuListener(new IMenuListener() { public void menuAboutToShow(IMenuManager manager) { manager.add(new Separator()); manager.add(undoAction); manager.add(redoAction); } }); } ``` 5. 在 `ViewPart` 类中添加以下方法: ```java public void executeCommand(final IUndoableOperation operation) { // 如果操作不可撤销,则返回 if (!operation.canUndo()) { return; } // 如果当前有未完成的撤消操作,则合并操作 if (undoOperation != null) { undoOperation.addContext(operation.getContext()); undoOperation.add(operation); } else { // 否则,开始新的撤消操作 undoOperation = operation; } // 注册撤消操作 IOperationHistory operationHistory = OperationHistoryFactory.getOperationHistory(); operationHistory.add(undoOperation); // 更新 Undo/Redo 动作的状态 undoAction.setEnabled(undoOperation.canUndo()); redoAction.setEnabled(undoOperation.canRedo()); // 将操作添加到 Undo/Redo 上下文中 undoContext.addMatch(undoOperation); } public void undo() { // 撤消上一个操作 IOperationHistory operationHistory = OperationHistoryFactory.getOperationHistory(); operationHistory.undo(undoContext, null, null); } public void redo() { // 重做上一个操作 IOperationHistory operationHistory = OperationHistoryFactory.getOperationHistory(); operationHistory.redo(undoContext, null, null); } ``` 6. 在 `ViewPart` 类中添加以下代码,以便在视关闭时清除 Undo/Redo 上下文中的操作: ```java public void dispose() { // 清除 Undo/Redo 上下文中的操作 IOperationHistory operationHistory = OperationHistoryFactory.getOperationHistory(); operationHistory.dispose(undoContext, true, true, true); super.dispose(); } ``` 现在,您可以在 `executeCommand` 方法中执行所有需要撤消/恢复支持的操作,并且在菜单栏 Edit 中的 Undo/Redo 菜单项中使用 Undo/Redo 动作来撤消/恢复操作
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值