1 |
/* AbstractTableModel.java -- |
/* AbstractUndoableEdit.java |
2 |
Copyright (C) 2002, 2003 Free Software Foundation, Inc. |
Copyright (C) 2002, 2003 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
39 |
package javax.swing.undo; |
package javax.swing.undo; |
40 |
|
|
41 |
import java.io.Serializable; |
import java.io.Serializable; |
42 |
|
import javax.swing.UIManager; |
43 |
|
|
44 |
|
|
45 |
/** |
/** |
46 |
* AbstractUndoableEdit |
* A default implementation of <code>UndoableEdit</code> that can be |
47 |
* @author Andrew Selkirk |
* used as a base for implementing editing operations. |
48 |
|
* |
49 |
|
* @author Andrew Selkirk (aselkirk@sympatico.ca) |
50 |
|
* @author Sascha Brawer (brawer@dandelis.ch) |
51 |
*/ |
*/ |
52 |
public class AbstractUndoableEdit implements UndoableEdit, Serializable |
public class AbstractUndoableEdit |
53 |
|
implements UndoableEdit, Serializable |
54 |
{ |
{ |
55 |
|
/** |
56 |
|
* The serialization ID. Verified using the <code>serialver</code> |
57 |
|
* tool of Apple/Sun JDK 1.3.1 on MacOS X 10.1.5, and Sun JDK |
58 |
|
* 1.4.1_01 on GNU/Linux. |
59 |
|
*/ |
60 |
static final long serialVersionUID = 580150227676302096L; |
static final long serialVersionUID = 580150227676302096L; |
61 |
|
|
|
//------------------------------------------------------------- |
|
|
// Constants -------------------------------------------------- |
|
|
//------------------------------------------------------------- |
|
62 |
|
|
63 |
/** |
/** |
64 |
* String returned by getRedoPresentationName() |
* The constant string “Undo”, which was returned by |
65 |
|
* {@link #getUndoPresentationName()} on early versions of the |
66 |
|
* platform. However, this field has become obsolete with version |
67 |
|
* 1.3.1. That method now retrieves a localized string from the |
68 |
|
* {@link javax.swing.UIManager}, using the key |
69 |
|
* <code>“AbstractUndoableEdit.undoText”</code>. |
70 |
*/ |
*/ |
71 |
protected static final String RedoName = "Redo"; |
protected static final String UndoName = "Undo"; |
72 |
|
|
73 |
|
|
74 |
/** |
/** |
75 |
* String returned by getUndoPresentationName() |
* The constant string “Redo”, which was returned by |
76 |
|
* {@link #getRedoPresentationName()} on early versions of the |
77 |
|
* platform. However, this field has become obsolete with version |
78 |
|
* 1.3.1. That method now retrieves a localized string from the |
79 |
|
* {@link javax.swing.UIManager}, using the key |
80 |
|
* <code>“AbstractUndoableEdit.redoText”</code>. |
81 |
*/ |
*/ |
82 |
protected static final String UndoName = "Undo"; |
protected static final String RedoName = "Redo"; |
|
|
|
83 |
|
|
|
//------------------------------------------------------------- |
|
|
// Variables -------------------------------------------------- |
|
|
//------------------------------------------------------------- |
|
84 |
|
|
85 |
/** |
/** |
86 |
* TODO |
* Indicates whether this editing action has been executed. A value |
87 |
|
* of <code>true</code> means that the action was performed, or that |
88 |
|
* a redo operation was successful. A value of <code>false</code> |
89 |
|
* means that the action has not yet performed, or that an undo |
90 |
|
* operation was successful. |
91 |
*/ |
*/ |
92 |
private boolean hasBeenDone = false; |
private boolean hasBeenDone; |
93 |
|
|
94 |
|
|
95 |
/** |
/** |
96 |
* The edit is alive |
* Indicates whether this editing action is still alive. The value |
97 |
|
* is set to <code>true</code> by the constructor, and to |
98 |
|
* <code>false</code> by the {@link #die()} method. |
99 |
*/ |
*/ |
100 |
private boolean alive = true; |
private boolean alive; |
|
|
|
101 |
|
|
|
//------------------------------------------------------------- |
|
|
// Initialization --------------------------------------------- |
|
|
//------------------------------------------------------------- |
|
102 |
|
|
103 |
/** |
/** |
104 |
* Create new AbstractUndoableEdit |
* Constructs a new <code>AbstractUndoableEdit</code>. The initial |
105 |
|
* state is that the editing action is alive, and |
106 |
|
* <code>hasBeenDone</code> is <code>true</code>. |
107 |
*/ |
*/ |
108 |
public AbstractUndoableEdit() |
public AbstractUndoableEdit() |
109 |
{ |
{ |
110 |
} // AbstractUndoableEdit() |
// The API specification is not clear, but Mauve test code has |
111 |
|
// determined that hasBeenDone is initially set to true. |
112 |
|
alive = hasBeenDone = true; |
113 |
|
} |
114 |
|
|
|
//------------------------------------------------------------- |
|
|
// Interface: UndoableEdit ------------------------------------ |
|
|
//------------------------------------------------------------- |
|
115 |
|
|
116 |
/** |
/** |
117 |
* addEdit |
* Undoes this editing action. |
118 |
* @param anEdit TODO |
* |
119 |
* @returns TODO |
* @throws CannotUndoException if {@link #canUndo()} returns |
120 |
|
* <code>false</code>, for example because this action has already |
121 |
|
* been undone. |
122 |
|
* |
123 |
|
* @see #canUndo() |
124 |
|
* @see #redo() |
125 |
*/ |
*/ |
126 |
public boolean addEdit(UndoableEdit anEdit) |
public void undo() |
127 |
|
throws CannotUndoException |
128 |
{ |
{ |
129 |
return false; |
if (!canUndo()) |
130 |
} // addEdit() |
throw new CannotUndoException(); |
131 |
|
hasBeenDone = false; |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
/** |
/** |
136 |
* canRedo() |
* Determines whether it would be possible to undo this editing |
137 |
* @returns true if redoable, false otherwise |
* action. |
138 |
|
* |
139 |
|
* @returns <code>true</code> to indicate that this action can be |
140 |
|
* undone, <code>false</code> otherwise. |
141 |
|
* |
142 |
|
* @see #undo() |
143 |
|
* @see #canRedo() |
144 |
*/ |
*/ |
145 |
public boolean canRedo() |
public boolean canUndo() |
146 |
{ |
{ |
147 |
if (alive == true && hasBeenDone == false) |
return alive && hasBeenDone; |
148 |
return true; |
} |
149 |
return false; |
|
150 |
} // canRedo() |
|
|
|
|
151 |
/** |
/** |
152 |
* canUndo() |
* Redoes this editing action. |
153 |
* @returns true if undoable, false otherwise |
* |
154 |
|
* @throws CannotRedoException if {@link #canRedo()} returns |
155 |
|
* <code>false</code>, for example because this action has not |
156 |
|
* yet been undone. |
157 |
|
* |
158 |
|
* @see #canRedo() |
159 |
|
* @see #undo() |
160 |
*/ |
*/ |
161 |
public boolean canUndo() |
public void redo() |
162 |
|
throws CannotRedoException |
163 |
{ |
{ |
164 |
if (alive == true && hasBeenDone == true) |
if (!canRedo()) |
165 |
return true; |
throw new CannotRedoException(); |
166 |
return false; |
hasBeenDone = true; |
167 |
} // canUndo() |
} |
168 |
|
|
169 |
|
|
170 |
/** |
/** |
171 |
* die |
* Determines whether it would be possible to redo this editing |
172 |
|
* action. |
173 |
|
* |
174 |
|
* @returns <code>true</code> to indicate that this action can be |
175 |
|
* redone, <code>false</code> otherwise. |
176 |
|
* |
177 |
|
* @see #redo() |
178 |
|
* @see #canUndo() |
179 |
*/ |
*/ |
180 |
public void die() |
public boolean canRedo() |
181 |
{ |
{ |
182 |
alive = false; |
return alive && !hasBeenDone; |
183 |
} // die() |
} |
184 |
|
|
185 |
|
|
186 |
/** |
/** |
187 |
* getPresentation |
* Informs this edit action that it will no longer be used. Some |
188 |
* @returns TODO |
* actions might use this information to release resources, for |
189 |
|
* example open files. Called by {@link UndoManager} before this |
190 |
|
* action is removed from the edit queue. |
191 |
*/ |
*/ |
192 |
public String getPresentationName() |
public void die() |
193 |
{ |
{ |
194 |
return ""; |
alive = false; |
195 |
} // getPresentationName() |
} |
196 |
|
|
197 |
|
|
198 |
/** |
/** |
199 |
* getRedoPresentationName |
* Incorporates another editing action into this one, thus forming a |
200 |
* @returns TODO |
* combined action. |
201 |
|
* |
202 |
|
* <p>The default implementation always returns <code>false</code>, |
203 |
|
* indicating that the editing action could not be incorporated. |
204 |
|
* |
205 |
|
* @param edit the editing action to be incorporated. |
206 |
*/ |
*/ |
207 |
public String getRedoPresentationName() |
public boolean addEdit(UndoableEdit edit) |
208 |
{ |
{ |
209 |
if (getPresentationName().equals("")) |
return false; |
210 |
return RedoName; |
} |
211 |
return RedoName + " " + getPresentationName(); |
|
212 |
} // getRedoPresentationName() |
|
|
|
|
213 |
/** |
/** |
214 |
* getUndoPresentationName |
* Incorporates another editing action into this one, thus forming a |
215 |
* @returns TODO |
* combined action that replaces the argument action. |
216 |
|
* |
217 |
|
* <p>The default implementation always returns <code>false</code>, |
218 |
|
* indicating that the argument action should not be replaced. |
219 |
|
* |
220 |
|
* @param edit the editing action to be replaced. |
221 |
*/ |
*/ |
222 |
public String getUndoPresentationName() |
public boolean replaceEdit(UndoableEdit edit) |
223 |
{ |
{ |
224 |
if (getPresentationName().equals("")) |
return false; |
225 |
return UndoName; |
} |
226 |
return UndoName + " " + getPresentationName(); |
|
227 |
} // getUndoPresentationName() |
|
|
|
|
228 |
/** |
/** |
229 |
* isSignificant |
* Determines whether this editing action is significant enough for |
230 |
* @returns true |
* being seperately undoable by the user. A typical significant |
231 |
|
* action would be the resizing of an object. However, changing the |
232 |
|
* selection in a text document would usually not be considered |
233 |
|
* significant. |
234 |
|
* |
235 |
|
* <p>The default implementation returns <code>true</code>. |
236 |
|
* |
237 |
|
* @returns <code>true</code> to indicate that the action is |
238 |
|
* significant enough for being separately undoable, or |
239 |
|
* <code>false</code> otherwise. |
240 |
*/ |
*/ |
241 |
public boolean isSignificant() |
public boolean isSignificant() |
242 |
{ |
{ |
243 |
return true; |
return true; |
244 |
} // isSignificant() |
} |
245 |
|
|
246 |
|
|
247 |
/** |
/** |
248 |
* redo |
* Returns a human-readable, localized name that describes this |
249 |
* @throws CannotRedoException TODO |
* editing action and can be displayed to the user. |
250 |
|
* |
251 |
|
* <p>The default implementation returns an empty string. |
252 |
*/ |
*/ |
253 |
public void redo() throws CannotRedoException |
public String getPresentationName() |
254 |
{ |
{ |
255 |
if (! canRedo()) |
return ""; |
256 |
throw new CannotRedoException(); |
} |
257 |
hasBeenDone = true; |
|
|
} // redo() |
|
258 |
|
|
259 |
/** |
/** |
260 |
* replaceEdit |
* Calculates a localized name for presenting the undo action to the |
261 |
* @param anEdit TODO |
* user. |
262 |
* @returns TODO |
* |
263 |
|
* <p>The default implementation returns the concatenation of the |
264 |
|
* string “Undo” and the action name, which is |
265 |
|
* determined by calling {@link #getPresentationName()}. |
266 |
|
* |
267 |
|
* <p>The string “Undo” is retrieved from the {@link |
268 |
|
* javax.swing.UIManager}, using the key |
269 |
|
* <code>“AbstractUndoableEdit.undoText”</code>. This |
270 |
|
* allows the text to be localized. |
271 |
*/ |
*/ |
272 |
public boolean replaceEdit(UndoableEdit anEdit) |
public String getUndoPresentationName() |
273 |
{ |
{ |
274 |
return false; |
String msg, pres; |
275 |
} // replaceEdit() |
|
276 |
|
msg = UIManager.getString("AbstractUndoableEdit.undoText"); |
277 |
|
if (msg == null) |
278 |
|
msg = UndoName; |
279 |
|
|
280 |
|
pres = getPresentationName(); |
281 |
|
if ((pres == null) || (pres.length() == 0)) |
282 |
|
return msg; |
283 |
|
else |
284 |
|
return msg + ' ' + pres; |
285 |
|
} |
286 |
|
|
287 |
|
|
288 |
/** |
/** |
289 |
* String representation |
* Calculates a localized name for presenting the redo action to the |
290 |
* @returns String representation |
* user. |
291 |
|
* |
292 |
|
* <p>The default implementation returns the concatenation of the |
293 |
|
* string “Redo” and the action name, which is |
294 |
|
* determined by calling {@link #getPresentationName()}. |
295 |
|
* |
296 |
|
* <p>The string “Redo” is retrieved from the {@link |
297 |
|
* javax.swing.UIManager}, using the key |
298 |
|
* <code>“AbstractUndoableEdit.redoText”</code>. This |
299 |
|
* allows the text to be localized. |
300 |
*/ |
*/ |
301 |
public String toString() |
public String getRedoPresentationName() |
302 |
{ |
{ |
303 |
return (super.toString() + " hasBeenDone: " + hasBeenDone |
String msg, pres; |
304 |
+ " alive: " + alive); |
|
305 |
|
msg = UIManager.getString("AbstractUndoableEdit.redoText"); |
306 |
|
if (msg == null) |
307 |
|
msg = RedoName; |
308 |
|
|
309 |
|
pres = getPresentationName(); |
310 |
|
if ((pres == null) || (pres.length() == 0)) |
311 |
|
return msg; |
312 |
|
else |
313 |
|
return msg + ' ' + pres; |
314 |
} |
} |
315 |
|
|
316 |
/** |
|
317 |
* undo |
public String toString() |
|
* @throws CannotUndoException TODO |
|
|
*/ |
|
|
public void undo() throws CannotUndoException |
|
318 |
{ |
{ |
319 |
if (! canUndo()) |
return super.toString() |
320 |
throw new CannotUndoException(); |
+ " hasBeenDone: " + hasBeenDone |
321 |
hasBeenDone = false; |
+ " alive: " + alive; |
322 |
} // undo() |
} |
323 |
} // AbstractUndoableEdit |
} |