Tuesday, January 13, 2009

undo history management in Flex

So I have a major flu which puts me in bed bored out of my skull...
I have this ongoing project, a Flex application, which is basically a sort of mind mapping system crossed with a drawing application. Now one of the next feature requests that the client made is an undo history.

Yikez.

So, I've been toying around with how to do it, and came up with, what I think, is a workable solution. I don't follow the usual command pattern which you find in most google searches on flex + undo history. What I basically needed was a recording of all the property changes on a bunch of objects.
I.e. property color was set to #00000, property width was set to 150, etc, etc.
These changes have to be recorded chronologically and undoing would basically be a matter of popping entries off a stack.

So, my solution makes use of some metadata and requires the objects that will be monitored to implement their properties with getters and setters.
An undoable property would look something like this:


[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"));
}
}

The object that implements this property needs to be registered once with a history manager class which analyzes the metadata and sets up eventlisteners,etc. In my implementation I use Christophe Herremans as3reflect library for retrieving the metadata.

My undo history manager also supports simple transactions, in the case several property changes represent only one user gesture. This is simply a matter of calling startTransaction and endTransaction on the history manager.

Anyways, I'm still busy cleaning up the code, finishing the documentation and making a little example application. But when I'm finished I'll release this on google code so maybe somebody else can benefit from this as well.

Or maybe someone on the interwebs thinks my solution is absolutely ridiculous, then I'd like to hear about it too :)

For now, I'm going back to being sick with the flu.

Cheers.

No comments:

Post a Comment

About Me

My photo

Co-Owner at Stack & Heap (http://www.stackandheap.com)
Lead developer for the Spring Actionscript (http://www.springactionscript.org) and AS3Commons (http://www.as3commons.org) projects.
I like noise.