1 /* CompoundEdit.java -- Combines multiple UndoableEdits.
2 Copyright (C) 2002, 2003, 2004 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
.Vector
;
44 * An editing action that consists of multiple
45 * <code>UndoableEdits</code>.
47 * <p>The use of a <code>CompoundEdit</code> is divided in two separate
50 * <ol><li>In the first phase, the <code>CompoundEdit</code> is
51 * initialized. After a new instance of <code>CompoundEdit</code> has
52 * been created, {@link #addEdit(UndoableEdit)} is called for each
53 * element of the compound. To terminate the initialization phase,
54 * call {@link #end()}.</li>
56 * <li>In the second phase, the the <code>CompoundEdit</code> can be
57 * used, typically by invoking {@link #undo()} and {@link
60 * @author Andrew Selkirk (aselkirk@sympatico.ca)
61 * @author Sascha Brawer (brawer@dandelis.ch)
63 public class CompoundEdit
64 extends AbstractUndoableEdit
67 * The identifier of this class in object serialization. Determined
68 * using the serialver tool of Sun J2SE 1.4.1_01.
70 private static final long serialVersionUID
= -6512679249930119683L;
74 * The <code>UndoableEdit</code>s being combined into a compound
77 protected Vector edits
;
81 * Indicates whether the creation of this CompoundEdit is still in
82 * progress. Initially, the value of this flag is
83 * <code>true</code>. The {@link #end()} method changes the flag to
86 private boolean inProgress
;
90 * Constructs a new CompoundEdit.
100 * Undoes all edits that are part of of this
101 * <code>CompoundEdit</code>. The compound elements will receive the
102 * <code>undo</code> message in the reverse order of addition.
104 * @throws CannotUndoException if {@link #canUndo()} returns
105 * <code>false</code>. This can happen if {@link #end()} has not
106 * been called on this <code>CompoundEdit</code>, or if this edit
107 * has already been undone.
113 throws CannotUndoException
115 // AbstractUndoableEdit.undo() will throw a CannotUndoException if
116 // canUndo returns false.
119 for (int i
= edits
.size() - 1; i
>= 0; i
--)
120 ((UndoableEdit
) edits
.elementAt(i
)).undo();
125 * Redoes all edits that are part of of this
126 * <code>CompoundEdit</code>. The compound elements will receive the
127 * <code>undo</code> message in the same order as they were added.
129 * @throws CannotRedoException if {@link #canRedo()} returns
130 * <code>false</code>. This can happen if {@link #end()} has not
131 * been called on this <code>CompoundEdit</code>, or if this edit
132 * has already been redone.
138 throws CannotRedoException
140 // AbstractUndoableEdit.redo() will throw a CannotRedoException if
141 // canRedo returns false.
144 for (int i
= 0; i
< edits
.size(); i
++)
145 ((UndoableEdit
) edits
.elementAt(i
)).redo();
150 * Returns the the <code>UndoableEdit</code> that was last added to
153 protected UndoableEdit
lastEdit()
155 if (edits
.size() == 0)
158 return (UndoableEdit
) edits
.elementAt(edits
.size() - 1);
163 * Informs this edit action, and all compound edits, that they will
164 * no longer be used. Some actions might use this information to
165 * release resources such as open files. Called by {@link
166 * UndoManager} before this action is removed from the edit queue.
168 * <p>The compound elements will receive the
169 * <code>die</code> message in the reverse order of addition.
173 for (int i
= edits
.size() - 1; i
>= 0; i
--)
174 ((UndoableEdit
) edits
.elementAt(i
)).die();
181 * Incorporates another editing action into this one, thus forming a
184 * <p>If this edit’s {@link #end()} method has been called
185 * before, <code>false</code> is returned immediately. Otherwise,
186 * the {@linkplain #lastEdit() last added edit} is given the
187 * opportunity to {@linkplain UndoableEdit#addEdit(UndoableEdit)
188 * incorporate} <code>edit</code>. If this fails, <code>edit</code>
189 * is given the opportunity to {@linkplain
190 * UndoableEdit#replaceEdit(UndoableEdit) replace} the last added
191 * edit. If this fails as well, <code>edit</code> gets added as a
192 * new compound to {@link #edits}.
194 * @param edit the editing action being added.
196 * @return <code>true</code> if <code>edit</code> could somehow be
197 * incorporated; <code>false</code> if <code>edit</code> has not
198 * been incorporated because {@link #end()} was called before.
200 public boolean addEdit(UndoableEdit edit
)
204 // If end has been called before, do nothing.
210 // If edit is the very first edit, just add it to the list.
217 // Try to incorporate edit into last.
218 if (last
.addEdit(edit
))
221 // Try to replace last by edit.
222 if (edit
.replaceEdit(last
))
224 edits
.set(edits
.size() - 1, edit
);
228 // If everything else has failed, add edit to the list of compound
236 * Informs this <code>CompoundEdit</code> that its construction
237 * phase has been completed. After this method has been called,
238 * {@link #undo()} and {@link #redo()} may be called, {@link
239 * #isInProgress()} will return <code>false</code>, and all attempts
240 * to {@linkplain #addEdit(UndoableEdit) add further edits} will
250 * Determines whether it would be possible to undo this editing
251 * action. The result will be <code>true</code> if {@link #end()}
252 * has been called on this <code>CompoundEdit</code>, {@link #die()}
253 * has not yet been called, and the edit has not been undone
256 * @return <code>true</code> to indicate that this action can be
257 * undone; <code>false</code> otherwise.
262 public boolean canUndo()
264 return !inProgress
&& super.canUndo();
269 * Determines whether it would be possible to redo this editing
270 * action. The result will be <code>true</code> if {@link #end()}
271 * has been called on this <code>CompoundEdit</code>, {@link #die()}
272 * has not yet been called, and the edit has not been redone
275 * @return <code>true</code> to indicate that this action can be
276 * redone; <code>false</code> otherwise.
281 public boolean canRedo()
283 return !inProgress
&& super.canRedo();
288 * Determines whether the initial construction phase of this
289 * <code>CompoundEdit</code> is still in progress. During this
290 * phase, edits {@linkplain #addEdit(UndoableEdit) may be
291 * added}. After initialization has been terminated by calling
292 * {@link #end()}, {@link #undo()} and {@link #redo()} can be used.
294 * @return <code>true</code> if the initialization phase is still in
295 * progress; <code>false</code> if {@link #end()} has been called.
299 public boolean isInProgress()
306 * Determines whether this editing action is significant enough for
307 * being seperately undoable by the user. A typical significant
308 * action would be the resizing of an object. However, changing the
309 * selection in a text document would usually not be considered
312 * <p>A <code>CompoundEdit</code> is significant if any of its
313 * elements are significant.
315 public boolean isSignificant()
317 for (int i
= edits
.size() - 1; i
>= 0; i
--)
318 if (((UndoableEdit
) edits
.elementAt(i
)).isSignificant())
326 * Returns a human-readable, localized name that describes this
327 * editing action and can be displayed to the user.
329 * <p>The implementation delegates the call to the {@linkplain
330 * #lastEdit() last added edit action}. If no edit has been added
331 * yet, the inherited implementation will be invoked, which always
332 * returns an empty string.
334 public String
getPresentationName()
340 return super.getPresentationName();
342 return last
.getPresentationName();
347 * Calculates a localized message text for presenting the undo
348 * action to the user.
350 * <p>The implementation delegates the call to the {@linkplain
351 * #lastEdit() last added edit action}. If no edit has been added
352 * yet, the {@linkplain
353 * AbstractUndoableEdit#getUndoPresentationName() inherited
354 * implementation} will be invoked.
356 public String
getUndoPresentationName()
362 return super.getUndoPresentationName();
364 return last
.getUndoPresentationName();
369 * Calculates a localized message text for presenting the redo
370 * action to the user.
372 * <p>The implementation delegates the call to the {@linkplain
373 * #lastEdit() last added edit action}. If no edit has been added
374 * yet, the {@linkplain
375 * AbstractUndoableEdit#getRedoPresentationName() inherited
376 * implementation} will be invoked.
378 public String
getRedoPresentationName()
384 return super.getRedoPresentationName();
386 return last
.getRedoPresentationName();
391 * Calculates a string that may be useful for debugging.
393 public String
toString()
395 return super.toString()
396 + " inProgress: " + inProgress
397 + " edits: " + edits
;