1 package com.explosion.utilities;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 /***
24 * @author Stephen Cowx Date created:@06-Feb-2003
25 */
26
27 import java.util.LinkedList;
28
29 import javax.swing.event.UndoableEditEvent;
30 import javax.swing.event.UndoableEditListener;
31 import javax.swing.undo.UndoableEdit;
32
33 public class UndoRedoBuffer implements UndoableEditListener
34 {
35
36 private LinkedList undoBuffer = new LinkedList();
37
38 private LinkedList redoBuffer = new LinkedList();
39
40 private int maxSize = -1;
41
42 public void undoableEditHappened(UndoableEditEvent edit)
43 {
44 undoBuffer.addFirst(edit.getEdit());
45 redoBuffer.clear();
46 constrainToMaximum();
47 }
48
49 public void undo()
50 {
51 if (undoBuffer.size() > 0)
52 {
53 UndoableEdit edit = (UndoableEdit) undoBuffer.getFirst();
54 edit.undo();
55 undoBuffer.removeFirst();
56 redoBuffer.addFirst(edit);
57 }
58 constrainToMaximum();
59 }
60
61 public void redo()
62 {
63 if (redoBuffer.size() > 0)
64 {
65 UndoableEdit edit = (UndoableEdit) redoBuffer.getFirst();
66 edit.redo();
67 undoBuffer.addFirst(edit);
68 redoBuffer.removeFirst();
69 }
70 constrainToMaximum();
71 }
72
73 /***
74 * Returns the redoBuffer.
75 *
76 * @return LinkedList
77 */
78 public LinkedList getRedoBuffer()
79 {
80 return redoBuffer;
81 }
82
83 /***
84 * Returns the undoBuffer.
85 *
86 * @return LinkedList
87 */
88 public LinkedList getUndoBuffer()
89 {
90 return undoBuffer;
91 }
92
93 private void constrainToMaximum()
94 {
95 if (maxSize >= 0 && undoBuffer.size() > maxSize) undoBuffer.removeLast();
96
97 if (maxSize >= 0 && redoBuffer.size() > maxSize) redoBuffer.removeLast();
98 }
99
100 /***
101 * Returns the maxSize.
102 *
103 * @return int
104 */
105 public int getMaxSize()
106 {
107 return maxSize;
108 }
109
110 /***
111 * Sets the maxSize.
112 *
113 * @param maxSize The maxSize to set
114 */
115 public void setMaxSize(int maxSize)
116 {
117 this.maxSize = maxSize;
118 }
119
120 }