1 /***
2 * Redistribution and use in source and binary forms, with or without
3 * modification, are permitted provided that the following conditions are
4 * met :
5 *
6 * . Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 *
9 * . Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * . The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $Id$
29 */
30
31 package palmed.edit.selection;
32
33 import java.io.InputStream;
34 import java.io.OutputStream;
35 import java.util.Stack;
36 import palmed.io.ISerializable;
37
38 /***
39 * This class implements an undo manager.
40 *
41 * @author Mathieu Champlon
42 * @version $Revision$ $Date$
43 */
44 public final class UndoManager implements ISerializable
45 {
46 /***
47 * The undo actions.
48 */
49 private final Stack undo_;
50 /***
51 * The redo actions.
52 */
53 private final Stack redo_;
54 /***
55 * The size of the undo stack.
56 */
57 private final int size_;
58 /***
59 * Whether the next merge should be skipped or not.
60 */
61 private boolean skipNext_;
62
63 /***
64 * Create an undo/redo manager.
65 *
66 * @param size the size of the undo stack.
67 */
68 public UndoManager( final int size )
69 {
70 undo_ = new Stack();
71 redo_ = new Stack();
72 size_ = size;
73 }
74
75 /***
76 * Undo the last action.
77 */
78 public void undo()
79 {
80 if( !undo_.isEmpty() )
81 {
82 final IAction action = (IAction)undo_.pop();
83 action.undo();
84 redo_.push( action );
85 }
86 }
87
88 /***
89 * Redo the last undone action.
90 */
91 public void redo()
92 {
93 if( !redo_.isEmpty() )
94 {
95 final IAction action = (IAction)redo_.pop();
96 action.redo();
97 undo_.push( action );
98 }
99 }
100
101 /***
102 * Add an action to the undo list.
103 *
104 * @param action the action
105 */
106 public void add( final IAction action )
107 {
108 if( action != null )
109 {
110 if( !merge( action ) )
111 undo_.addElement( action );
112 redo_.removeAllElements();
113 if( undo_.size() > size_ )
114 undo_.removeElementAt( 0 );
115 }
116 }
117
118 private boolean merge( final IAction action )
119 {
120 if( undo_.isEmpty() )
121 return false;
122 if( skipNext_ )
123 {
124 skipNext_ = false;
125 return false;
126 }
127 return ((IAction)undo_.peek()).merge( action );
128 }
129
130 /***
131 * Clear the undo and redo lists.
132 */
133 public void clear()
134 {
135 undo_.removeAllElements();
136 redo_.removeAllElements();
137 }
138
139 /***
140 * {@inheritDoc}
141 */
142 public void marshall( final OutputStream stream )
143 {
144 clear();
145 }
146
147 /***
148 * {@inheritDoc}
149 */
150 public void unmarshall( final InputStream stream )
151 {
152 clear();
153 }
154
155 /***
156 * Skip the next merge.
157 */
158 public void skip()
159 {
160 skipNext_ = true;
161 }
162 }