介绍一个UndoFramework

简介: 由于其他工作,好多天又没有对MetaModelEngine进行思考了,这两天又腾出时间可以思考一下了,本篇介绍一下在图形编辑器中对操作如何实现Undo操作。   在图形设计器操作中,每个操作按钮都对应到一个命令,很多情况下我们都应该允许用户执行操作后回滚这些操作,或者回滚后又再次执行。

由于其他工作,好多天又没有对MetaModelEngine进行思考了,这两天又腾出时间可以思考一下了,本篇介绍一下在图形编辑器中对操作如何实现Undo操作。

  在图形设计器操作中,每个操作按钮都对应到一个命令,很多情况下我们都应该允许用户执行操作后回滚这些操作,或者回滚后又再次执行。在我做的报表引擎中,我是在每次操作后把设计文件都保留下来,这个在报表设计中是没有问题,但是在毕竟不是很好的设计。接下来要考虑对OpenExpressApp提供建模支持了,所以也需要考虑一下如何让图形设计器更好的支持这种Undo操作。

  在公司的一个项目组中内部是使用命令模式,只是在传统命令模式中增加了一个UnExecute方法,这个方法就是用来做Undo操作的。在codeplex上我找到了一类似的轻量级UndoFramework,后期准备就用它了,在这里我就给大家介绍一下。

UndoFramework项目

Codeplex网站地址:http://undo.codeplex.com/

下载地址:http://undo.codeplex.com/releases/view/29440

项目描述:

  It's a simple framework to add Undo/Redo functionality to your applications, based on the classical Command design pattern. It supports merging actions, nested transactions, delayed execution (execution on top-level transaction commit) and possible non-linear undo history (where you can have a choice of multiple actions to redo).

  The status of the project is Stable (released). I might add more stuff to it later, but right now it fully satisfies my needs. It's implemented in C# 3.0 (Visual Studio 2008) and I can build it for both desktop and Silverlight. The release has both binaries.

现有应用

A good example of where this framework is used is the Live Geometry project (http://livegeometry.codeplex.com). It defines several actions such as AddFigureAction, RemoveFigureAction, MoveAction and SetPropertyAction.

如何使用

  我学习这些东西一般都喜欢先看如何使用,因为从使用方式就能看出封装得是否简单易用。

  以下是一个控制台的演示程序,代码如下:

 

运行后界面如下:

 

下载代码你会看到,它还自带一个Form的例子,感兴趣可以自己去看看


Actions

  所有操作都从 IAction继承下来,必须实现两个操作:一个是执行操作,一个是反执行操作

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/// <summary>
/// Encapsulates a user action (actually two actions: Do and Undo)
/// Can be anything.
/// You can give your implementation any information it needs to be able to
/// execute and rollback what it needs.
/// </summary>
public interface IAction
{
     /// <summary>
     /// Apply changes encapsulated by this object.
     /// </summary>
     void Execute();
 
     /// <summary>
     /// Undo changes made by a previous Execute call.
     /// </summary>
     void UnExecute();
 
     /// <summary>
     /// For most Actions, CanExecute is true when ExecuteCount = 0 (not yet executed)
     /// and false when ExecuteCount = 1 (already executed once)
     /// </summary>
     /// <returns>true if an encapsulated action can be applied</returns>
     bool CanExecute();
 
     /// <returns>true if an action was already executed and can be undone</returns>
     bool CanUnExecute();
 
     /// <summary>
     /// Attempts to take a new incoming action and instead of recording that one
     /// as a new action, just modify the current one so that it's summary effect is
     /// a combination of both.
     /// </summary>
     /// <param name="followingAction"></param>
     /// <returns>true if the action agreed to merge, false if we want the followingAction
     /// to be tracked separately</returns>
     bool TryToMerge(IAction followingAction);
 
     /// <summary>
     /// Defines if the action can be merged with the previous one in the Undo buffer
     /// This is useful for long chains of consecutive operations of the same type,
     /// e.g. dragging something or typing some text
     /// </summary>
     bool AllowToMergeWithPrevious { get ; set ; }
}


 

ActionManager

ActionManager负责跟踪undo/redo记录,提供RecordAction(IAction)来记录操作步骤,提供ActionManager.Undo(), ActionManager.Redo(), CanUndo(), CanRedo()等其他方法。

其完整代码如下:

 

参考

http://blogs.msdn.com/kirillosenkov/archive/2009/06/29/new-codeplex-project-a-simple-undo-redo-framework.aspx 
http://blogs.msdn.com/kirillosenkov/archive/2009/07/02/samples-for-the-undo-framework.aspx

其他Undo框架

 

欢迎转载,转载请注明:转载自周金根 [ http://zhoujg.cnblogs.com/ ]

目录
相关文章
|
运维 负载均衡 网络协议
路由器的简单概述(详细理解+实例精讲)
本文详细的介绍路由器的简单概述,内含详细的实例解析,该文你值得拥有。
|
11月前
|
存储 NoSQL Linux
Linux下安装MongoDB
Linux下安装MongoDB
146 0
|
存储 JavaScript 前端开发
使用CDN方法的方式进行Vue.js的安装
最近公司需要进行一些前端的开发工作用到了Vue前端框架,所以准备自学Vue,顺便几下学习的过程以及一些问题。
917 0
使用CDN方法的方式进行Vue.js的安装
|
前端开发 JavaScript UED
如何更好的去除谷歌浏览器中input自动填充背景?
如何更好的去除谷歌浏览器中input自动填充背景?
如何更好的去除谷歌浏览器中input自动填充背景?
UE-蓝图中的使用正则表达式
实现用于匹配判断的正则表达式
|
弹性计算 关系型数据库 MySQL
基于星轨-数据中台工具的数据盘点
本场景带您体验如何通过星轨-数据中台工具进行业务系统数据库盘点,产出系统清单、表清单和字段清单。
129 0
基于星轨-数据中台工具的数据盘点
|
11月前
|
存储 前端开发 JavaScript
SpringMVC之文件上传下载
SpringMVC之文件上传下载
132 0
|
11月前
|
Java 程序员 Apache
maven是什么?安装+配置
maven是什么?安装+配置
440 0
|
11月前
|
存储 缓存 关系型数据库
小红书万亿级社交网络关系下的图存储系统的架构设计与实践
本文将为你分享小红书面向超大规模社交网络的图存储系统REDtao的架构设计与技术实践过程,希望能带给你启发。
198 0
|
11月前
|
前端开发 网络协议 Java
javaweb的理解
javaweb的理解
66 0