Created
March 7, 2010 07:23
-
-
Save bluepapa32/324227 to your computer and use it in GitHub Desktop.
Undo Manager in JavaFX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Undoable { | |
public var undo: function(): Void; | |
public var redo: function(): Void; | |
} | |
public class UndoManager { | |
public-init var limit = 10; | |
public-read var redoable: Boolean; | |
public-read var undoable: Boolean; | |
var undoables: Undoable[]; | |
var pos = -1 on replace { | |
redoable = (pos < sizeof undoables - 1); | |
undoable = (-1 < pos); | |
} | |
public function redo(): Void { | |
if (redoable) { | |
undoables[++pos].redo(); | |
} | |
} | |
public function undo(): Void { | |
if (undoable) { | |
undoables[pos].undo(); | |
pos--; | |
} | |
} | |
public function add(undoable: Undoable) { | |
delete undoables[pos + 1..]; | |
insert undoable into undoables; | |
pos++; | |
if (sizeof undoables > limit) { | |
delete undoables[0]; | |
pos--; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment