1 /* StateEdit.java -- UndoableEdit for StateEditable implementations.
2 Copyright (C) 2002, 2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax
.swing
.undo
;
41 import java
.util
.Hashtable
;
42 import java
.util
.Iterator
;
45 * A helper class, making it easy to support undo and redo.
47 * <p>The following example shows how to use this class.
49 * <pre> Foo foo; // class Foo implements {@link StateEditable}
52 * edit = new StateEdit(foo, "Name Change");
53 * foo.setName("Jane Doe");
55 * undoManager.addEdit(edit);</pre>
57 * <p>If <code>Foo</code>’s implementation of {@link
58 * StateEditable} considers the name as part of the editable state,
59 * the user can now choose “Undo Name Change” or
60 * “Redo Name Change” from the respective menu. No
61 * further undo support is needed from the application.
63 * <p>The following explains what happens in the example.
65 * <p><ol><li>When a <code>StateEdit</code> is created, the associated
66 * {@link StateEditable} gets asked to store its state into a hash
67 * table, {@link #preState}.</li>
69 * <li>The application will now perform some changes to the edited
70 * object. This typically happens by invoking methods on the edited
73 * <li>The editing phase is terminated by invoking the {@link #end()}
74 * method of the <code>StateEdit</code>. The <code>end()</code> method
77 * <ul><li>The edited object receives a second request for storing
78 * its state. This time, it will use a different hash table, {@link
81 * <li>To increase efficiency, the <code>StateEdit</code> now removes
82 * any entries from {@link #preState} and {@link #postState} that have
83 * the same key, and whose values are equal. Equality is determined
84 * by invoking the <code>equals</code> method inherited from
85 * {@link java.lang.Object}.</li></ul></li>
87 * <li>When the user later chooses to undo the <code>StateEdit</code>,
88 * the edited object is asked to {@linkplain StateEditable#restoreState
89 * restore its state} from the {@link #preState} table. Similarly,
90 * when the user chooses to <i>redo</i> the <code>StateEdit</code>,
91 * the edited object gets asked to restore its state from the {@link
92 * #postState}.</li></ol>
94 * @author Andrew Selkirk (aselkirk@sympatico.ca)
95 * @author Sascha Brawer (brawer@dandelis.ch)
97 public class StateEdit
98 extends AbstractUndoableEdit
101 * The ID of the Java source file in Sun’s Revision Control
102 * System (RCS). This certainly should not be part of the API
103 * specification. But in order to be API-compatible with
104 * Sun’s reference implementation, GNU Classpath also has to
105 * provide this field. However, we do not try to match its value.
107 protected static final String RCSID
= "";
111 * The object which is being edited by this <code>StateEdit</code>.
113 protected StateEditable object
;
117 * The state of <code>object</code> at the time of constructing
118 * this <code>StateEdit</code>.
120 protected Hashtable preState
;
124 * The state of <code>object</code> at the time when {@link #end()}
127 protected Hashtable postState
;
131 * A human-readable name for this edit action.
133 protected String undoRedoName
;
137 * Constructs a <code>StateEdit</code>, specifying the object whose
138 * state is being edited.
140 * @param obj the object whose state is being edited by this
141 * <code>StateEdit</code>.
143 public StateEdit(StateEditable obj
)
150 * Constructs a <code>StateEdit</code>, specifying the object whose
151 * state is being edited.
153 * @param obj the object whose state is being edited by this
154 * <code>StateEdit</code>.
156 * @param name the human-readable name of the editing action.
158 public StateEdit(StateEditable obj
, String name
)
165 * Initializes this <code>StateEdit</code>. The edited object will
166 * be asked to store its current state into {@link #preState}.
168 * @param obj the object being edited.
170 * @param name the human-readable name of the editing action.
172 protected void init(StateEditable obj
, String name
)
176 preState
= new Hashtable();
177 postState
= new Hashtable();
178 obj
.storeState(preState
);
183 * Informs this <code>StateEdit</code> that all edits are finished.
184 * The edited object will be asked to store its state into {@link
185 * #postState}, and any redundant entries will get removed from
186 * {@link #preState} and {@link #postState}.
190 object
.storeState(postState
);
191 removeRedundantState();
196 * Undoes this edit operation. The edited object will be asked to
197 * {@linkplain StateEditable#restoreState restore its state} from
200 * @throws CannotUndoException if {@link #canUndo()} returns
201 * <code>false</code>, for example because this action has already
207 object
.restoreState(preState
);
212 * Redoes this edit operation. The edited object will be asked to
213 * {@linkplain StateEditable#restoreState restore its state} from
214 * {@link #postState}.
216 * @throws CannotRedoException if {@link #canRedo()} returns
217 * <code>false</code>, for example because this action has not yet
223 object
.restoreState(postState
);
228 * Returns a human-readable, localized name that describes this
229 * editing action and can be displayed to the user.
231 * @return the name, or <code>null</code> if no presentation
234 public String
getPresentationName()
241 * Removes all redundant entries from the pre- and post-edit state
242 * hash tables. An entry is considered redundant if it is present
243 * both before and after the edit, and if the two values are equal.
245 protected void removeRedundantState()
247 Iterator i
= preState
.keySet().iterator();
250 Object key
= i
.next();
251 if (postState
.containsKey(key
))
253 if (preState
.get(key
).equals(postState
.get(key
)))
256 postState
.remove(key
);