A small Flex undo/edit history library that can record and undo property changes on arbritrary objects
This is a small library that enables a programmer to register an object with an 'Undohistory' manager object which will record property changes on the specified object.
To indicate which properties need to be monitored the developer can add metadata tags to the property getter/setter methods. For example:
[Undoable]
public function get testStringProperty():String
{
return _testStringProperty;
}
[Bindable(event="testStringPropertyChanged")]
public function set testStringProperty(value:String):void
{
if (value != _testStringProperty)
{
_testStringProperty = value;
dispatchEvent(new Event("testStringPropertyChanged"));
}
}
All changes are monitored and recorded by a manager object, which can be used like this in an application:
``` //Create registration object and hostory manager: var objReg:IObjectRegistration = new ObjectRegistration(); var hm:IUndoHistoryManager = new UndoHistoryManager(objReg);
//Create and initialize an object whose class contains [Undoable] metadata var t:UndoAbleTestClass = new UndoAbleTestClass(); t.testStringProperty = "test1";
//Add the object to the history manager so it will register its class and monitor its changes: hm.addObject(t);
//As a simple test, change a property
t.testStringProperty = "test3";
//let the history manager revert to the previous property value: hm.undo();
if (t.testStringProperty == "test1") { Alert.show("property change was undone!"); } ```
The library also supports simple transactions, in the case several property changes represent only one user gesture.
I've included a few unit tests and an example application that shows how to set up the code and use the library.
Source can be retrieved directly from the SVN repository. Please let me know of any bugs you may find.
In my implementation I have used Christophe Herreman's excellent as3reflect library for the metadata extraction bits.
N.B. When you use the custom metadata don't forget to add this compiler switch to your application:
-keep-as3-metadata+=Undoable
If you don't then the compiler will omit your metadata in the resulting swf and the library will not be able to do its job...