Skip to content

Instantly share code, notes, and snippets.

@bluepapa32
Created March 7, 2010 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bluepapa32/324227 to your computer and use it in GitHub Desktop.
Save bluepapa32/324227 to your computer and use it in GitHub Desktop.
Undo Manager in JavaFX
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