01: package bluej.editor.moe;
02:
03: import java.util.LinkedList;
04:
05: import javax.swing.event.UndoableEditEvent;
06: import javax.swing.event.UndoableEditListener;
07: import javax.swing.undo.CompoundEdit;
08: import javax.swing.undo.UndoManager;
09: import javax.swing.undo.UndoableEdit;
10:
11: /**
12: * An undo/redo manager for the editor. A stack of compound edits is maintained;
13: * the "beginCompoundEdit()" and "endCompoundEdit()" methods can be used to
14: * create a compound edit (which is treated as a single edit for undo/redo purposes).
15: *
16: * @author Davin McCall
17: */
18: public class MoeUndoManager implements UndoableEditListener {
19: LinkedList editStack;
20: UndoManager undoManager;
21: CompoundEdit currentEdit;
22: MoeEditor editor;
23:
24: public MoeUndoManager(MoeEditor editor) {
25: this .editor = editor;
26: undoManager = new UndoManager();
27: currentEdit = undoManager;
28: editStack = new LinkedList();
29: }
30:
31: public void undoableEditHappened(UndoableEditEvent e) {
32: addEdit(e.getEdit());
33: }
34:
35: public void addEdit(UndoableEdit edit) {
36: currentEdit.addEdit(edit);
37: if (currentEdit == undoManager) {
38: editor.updateUndoControls();
39: editor.updateRedoControls();
40: }
41: }
42:
43: public void beginCompoundEdit() {
44: editStack.add(currentEdit);
45: currentEdit = new CompoundEdit();
46: }
47:
48: public void endCompoundEdit() {
49: currentEdit.end();
50: CompoundEdit lastEdit = (CompoundEdit) editStack.removeLast();
51: lastEdit.addEdit(currentEdit);
52: currentEdit = lastEdit;
53:
54: if (currentEdit == undoManager) {
55: editor.updateUndoControls();
56: editor.updateRedoControls();
57: }
58: }
59:
60: public boolean canUndo() {
61: return undoManager.canUndo();
62: }
63:
64: public boolean canRedo() {
65: return undoManager.canRedo();
66: }
67:
68: public void undo() {
69: undoManager.undo();
70: }
71:
72: public void redo() {
73: undoManager.redo();
74: }
75: }
|