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., 51 Franklin Street, Fifth Floor, 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
51 * <li>In the first phase, the <code>CompoundEdit</code> is
52 * initialized. After a new instance of <code>CompoundEdit</code> has
53 * been created, {@link #addEdit(UndoableEdit)} is called for each
54 * element of the compound. To terminate the initialization phase,
55 * 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
58 * {@link #redo()}.</li>
61 * @author Andrew Selkirk (aselkirk@sympatico.ca)
62 * @author Sascha Brawer (brawer@dandelis.ch)
64 public class CompoundEdit
65 extends AbstractUndoableEdit
68 * The identifier of this class in object serialization. Determined
69 * using the serialver tool of Sun J2SE 1.4.1_01.
71 private static final long serialVersionUID = -6512679249930119683L;
75 * The <code>UndoableEdit</code>s being combined into a compound
78 protected Vector edits;
82 * Indicates whether the creation of this CompoundEdit is still in
83 * progress. Initially, the value of this flag is
84 * <code>true</code>. The {@link #end()} method changes the flag to
87 private boolean inProgress;
91 * Constructs a new CompoundEdit.
101 * Undoes all edits that are part of of this
102 * <code>CompoundEdit</code>. The compound elements will receive the
103 * <code>undo</code> message in the reverse order of addition.
105 * @throws CannotUndoException if {@link #canUndo()} returns
106 * <code>false</code>. This can happen if {@link #end()} has not
107 * been called on this <code>CompoundEdit</code>, or if this edit
108 * has already been undone.
114 throws CannotUndoException
116 // AbstractUndoableEdit.undo() will throw a CannotUndoException if
117 // canUndo returns false.
120 for (int i = edits.size() - 1; i >= 0; i--)
121 ((UndoableEdit) edits.elementAt(i)).undo();
126 * Redoes all edits that are part of of this
127 * <code>CompoundEdit</code>. The compound elements will receive the
128 * <code>undo</code> message in the same order as they were added.
130 * @throws CannotRedoException if {@link #canRedo()} returns
131 * <code>false</code>. This can happen if {@link #end()} has not
132 * been called on this <code>CompoundEdit</code>, or if this edit
133 * has already been redone.
139 throws CannotRedoException
141 // AbstractUndoableEdit.redo() will throw a CannotRedoException if
142 // canRedo returns false.
145 for (int i = 0; i < edits.size(); i++)
146 ((UndoableEdit) edits.elementAt(i)).redo();
151 * Returns the the <code>UndoableEdit</code> that was last added to
154 protected UndoableEdit lastEdit()
156 if (edits.size() == 0)
159 return (UndoableEdit) edits.elementAt(edits.size() - 1);
164 * Informs this edit action, and all compound edits, that they will
165 * no longer be used. Some actions might use this information to
166 * release resources such as open files. Called by {@link
167 * UndoManager} before this action is removed from the edit queue.
169 * <p>The compound elements will receive the
170 * <code>die</code> message in the reverse order of addition.
174 for (int i = edits.size() - 1; i >= 0; i--)
175 ((UndoableEdit) edits.elementAt(i)).die();
182 * Incorporates another editing action into this one, thus forming a
185 * <p>If this edit’s {@link #end()} method has been called
186 * before, <code>false</code> is returned immediately. Otherwise,
187 * the {@linkplain #lastEdit() last added edit} is given the
188 * opportunity to {@linkplain UndoableEdit#addEdit(UndoableEdit)
189 * incorporate} <code>edit</code>. If this fails, <code>edit</code>
190 * is given the opportunity to {@linkplain
191 * UndoableEdit#replaceEdit(UndoableEdit) replace} the last added
192 * edit. If this fails as well, <code>edit</code> gets added as a
193 * new compound to {@link #edits}.
195 * @param edit the editing action being added.
197 * @return <code>true</code> if <code>edit</code> could somehow be
198 * incorporated; <code>false</code> if <code>edit</code> has not
199 * been incorporated because {@link #end()} was called before.
201 public boolean addEdit(UndoableEdit edit)
205 // If end has been called before, do nothing.
211 // If edit is the very first edit, just add it to the list.
218 // Try to incorporate edit into last.
219 if (last.addEdit(edit))
222 // Try to replace last by edit.
223 if (edit.replaceEdit(last))
225 edits.set(edits.size() - 1, edit);
229 // If everything else has failed, add edit to the list of compound
237 * Informs this <code>CompoundEdit</code> that its construction
238 * phase has been completed. After this method has been called,
239 * {@link #undo()} and {@link #redo()} may be called, {@link
240 * #isInProgress()} will return <code>false</code>, and all attempts
241 * to {@linkplain #addEdit(UndoableEdit) add further edits} will
251 * Determines whether it would be possible to undo this editing
252 * action. The result will be <code>true</code> if {@link #end()}
253 * has been called on this <code>CompoundEdit</code>, {@link #die()}
254 * has not yet been called, and the edit has not been undone
257 * @return <code>true</code> to indicate that this action can be
258 * undone; <code>false</code> otherwise.
263 public boolean canUndo()
265 return !inProgress && super.canUndo();
270 * Determines whether it would be possible to redo this editing
271 * action. The result will be <code>true</code> if {@link #end()}
272 * has been called on this <code>CompoundEdit</code>, {@link #die()}
273 * has not yet been called, and the edit has not been redone
276 * @return <code>true</code> to indicate that this action can be
277 * redone; <code>false</code> otherwise.
282 public boolean canRedo()
284 return !inProgress && super.canRedo();
289 * Determines whether the initial construction phase of this
290 * <code>CompoundEdit</code> is still in progress. During this
291 * phase, edits {@linkplain #addEdit(UndoableEdit) may be
292 * added}. After initialization has been terminated by calling
293 * {@link #end()}, {@link #undo()} and {@link #redo()} can be used.
295 * @return <code>true</code> if the initialization phase is still in
296 * progress; <code>false</code> if {@link #end()} has been called.
300 public boolean isInProgress()
307 * Determines whether this editing action is significant enough for
308 * being seperately undoable by the user. A typical significant
309 * action would be the resizing of an object. However, changing the
310 * selection in a text document would usually not be considered
313 * <p>A <code>CompoundEdit</code> is significant if any of its
314 * elements are significant.
316 public boolean isSignificant()
318 for (int i = edits.size() - 1; i >= 0; i--)
319 if (((UndoableEdit) edits.elementAt(i)).isSignificant())
327 * Returns a human-readable, localized name that describes this
328 * editing action and can be displayed to the user.
330 * <p>The implementation delegates the call to the {@linkplain
331 * #lastEdit() last added edit action}. If no edit has been added
332 * yet, the inherited implementation will be invoked, which always
333 * returns an empty string.
335 public String getPresentationName()
341 return super.getPresentationName();
343 return last.getPresentationName();
348 * Calculates a localized message text for presenting the undo
349 * action to the user.
351 * <p>The implementation delegates the call to the {@linkplain
352 * #lastEdit() last added edit action}. If no edit has been added
353 * yet, the {@linkplain
354 * AbstractUndoableEdit#getUndoPresentationName() inherited
355 * implementation} will be invoked.
357 public String getUndoPresentationName()
363 return super.getUndoPresentationName();
365 return last.getUndoPresentationName();
370 * Calculates a localized message text for presenting the redo
371 * action to the user.
373 * <p>The implementation delegates the call to the {@linkplain
374 * #lastEdit() last added edit action}. If no edit has been added
375 * yet, the {@linkplain
376 * AbstractUndoableEdit#getRedoPresentationName() inherited
377 * implementation} will be invoked.
379 public String getRedoPresentationName()
385 return super.getRedoPresentationName();
387 return last.getRedoPresentationName();
392 * Calculates a string that may be useful for debugging.
394 public String toString()
396 return super.toString()
397 + " inProgress: " + inProgress
398 + " edits: " + edits;