KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > undo > CompoundEdit


1 /*
2  * @(#)CompoundEdit.java 1.25 04/05/05
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.undo;
8
9 import java.util.*;
10
11 /**
12  * A concrete subclass of AbstractUndoableEdit, used to assemble little
13  * UndoableEdits into great big ones.
14  *
15  * @version 1.25 05/05/04
16  * @author Ray Ryan
17  */

18 public class CompoundEdit extends AbstractUndoableEdit JavaDoc {
19     /**
20      * True if this edit has never received <code>end</code>.
21      */

22     boolean inProgress;
23
24     /**
25      * The collection of <code>UndoableEdit</code>s
26      * undone/redone en masse by this <code>CompoundEdit</code>.
27      */

28     protected Vector<UndoableEdit JavaDoc> edits;
29
30     public CompoundEdit() {
31     super();
32     inProgress = true;
33     edits = new Vector<UndoableEdit JavaDoc>();
34     }
35
36     /**
37      * Sends <code>undo</code> to all contained
38      * <code>UndoableEdits</code> in the reverse of
39      * the order in which they were added.
40      */

41     public void undo() throws CannotUndoException JavaDoc {
42     super.undo();
43     int i = edits.size();
44     while (i-- > 0) {
45         UndoableEdit JavaDoc e = (UndoableEdit JavaDoc)edits.elementAt(i);
46         e.undo();
47     }
48     }
49
50     /**
51      * Sends <code>redo</code> to all contained
52      * <code>UndoableEdit</code>s in the order in
53      * which they were added.
54      */

55     public void redo() throws CannotRedoException JavaDoc {
56     super.redo();
57     Enumeration cursor = edits.elements();
58     while (cursor.hasMoreElements()) {
59         ((UndoableEdit JavaDoc)cursor.nextElement()).redo();
60     }
61     }
62
63     /**
64      * Returns the last <code>UndoableEdit</code> in
65      * <code>edits</code>, or <code>null</code>
66      * if <code>edits</code> is empty.
67      */

68     protected UndoableEdit JavaDoc lastEdit() {
69     int count = edits.size();
70     if (count > 0)
71         return (UndoableEdit JavaDoc)edits.elementAt(count-1);
72     else
73         return null;
74     }
75
76     /**
77      * Sends <code>die</code> to each subedit,
78      * in the reverse of the order that they were added.
79      */

80     public void die() {
81     int size = edits.size();
82     for (int i = size-1; i >= 0; i--)
83     {
84         UndoableEdit JavaDoc e = (UndoableEdit JavaDoc)edits.elementAt(i);
85 // System.out.println("CompoundEdit(" + i + "): Discarding " +
86
// e.getUndoPresentationName());
87
e.die();
88     }
89     super.die();
90     }
91
92     /**
93      * If this edit is <code>inProgress</code>,
94      * accepts <code>anEdit</code> and returns true.
95      *
96      * <p>The last edit added to this <code>CompoundEdit</code>
97      * is given a chance to <code>addEdit(anEdit)</code>.
98      * If it refuses (returns false), <code>anEdit</code> is
99      * given a chance to <code>replaceEdit</code> the last edit.
100      * If <code>anEdit</code> returns false here,
101      * it is added to <code>edits</code>.
102      *
103      * @param anEdit the edit to be added
104      * @return true if the edit is <code>inProgress</code>;
105      * otherwise returns false
106      */

107     public boolean addEdit(UndoableEdit JavaDoc anEdit) {
108     if (!inProgress) {
109         return false;
110     } else {
111         UndoableEdit JavaDoc last = lastEdit();
112
113         // If this is the first subedit received, just add it.
114
// Otherwise, give the last one a chance to absorb the new
115
// one. If it won't, give the new one a chance to absorb
116
// the last one.
117

118         if (last == null) {
119         edits.addElement(anEdit);
120         }
121         else if (!last.addEdit(anEdit)) {
122         if (anEdit.replaceEdit(last)) {
123             edits.removeElementAt(edits.size()-1);
124         }
125         edits.addElement(anEdit);
126         }
127
128         return true;
129     }
130     }
131
132     /**
133      * Sets <code>inProgress</code> to false.
134      *
135      * @see #canUndo
136      * @see #canRedo
137      */

138     public void end() {
139     inProgress = false;
140     }
141
142     /**
143      * Returns false if <code>isInProgress</code> or if super
144      * returns false.
145      *
146      * @see #isInProgress
147      */

148     public boolean canUndo() {
149     return !isInProgress() && super.canUndo();
150     }
151
152     /**
153      * Returns false if <code>isInProgress</code> or if super
154      * returns false.
155      *
156      * @see #isInProgress
157      */

158     public boolean canRedo() {
159     return !isInProgress() && super.canRedo();
160     }
161
162     /**
163      * Returns true if this edit is in progress--that is, it has not
164      * received end. This generally means that edits are still being
165      * added to it.
166      *
167      * @see #end
168      */

169     public boolean isInProgress() {
170     return inProgress;
171     }
172
173     /**
174      * Returns true if any of the <code>UndoableEdit</code>s
175      * in <code>edits</code> do.
176      * Returns false if they all return false.
177      */

178     public boolean isSignificant() {
179     Enumeration cursor = edits.elements();
180     while (cursor.hasMoreElements()) {
181         if (((UndoableEdit JavaDoc)cursor.nextElement()).isSignificant()) {
182         return true;
183         }
184     }
185     return false;
186     }
187
188     /**
189      * Returns <code>getPresentationName</code> from the
190      * last <code>UndoableEdit</code> added to
191      * <code>edits</code>. If <code>edits</code> is empty,
192      * calls super.
193      */

194     public String JavaDoc getPresentationName() {
195     UndoableEdit JavaDoc last = lastEdit();
196     if (last != null) {
197         return last.getPresentationName();
198     } else {
199         return super.getPresentationName();
200     }
201     }
202         
203     /**
204      * Returns <code>getUndoPresentationName</code>
205      * from the last <code>UndoableEdit</code>
206      * added to <code>edits</code>.
207      * If <code>edits</code> is empty, calls super.
208      */

209     public String JavaDoc getUndoPresentationName() {
210     UndoableEdit JavaDoc last = lastEdit();
211     if (last != null) {
212         return last.getUndoPresentationName();
213     } else {
214         return super.getUndoPresentationName();
215     }
216     }
217         
218     /**
219      * Returns <code>getRedoPresentationName</code>
220      * from the last <code>UndoableEdit</code>
221      * added to <code>edits</code>.
222      * If <code>edits</code> is empty, calls super.
223      */

224     public String JavaDoc getRedoPresentationName() {
225     UndoableEdit JavaDoc last = lastEdit();
226     if (last != null) {
227         return last.getRedoPresentationName();
228     } else {
229         return super.getRedoPresentationName();
230     }
231     }
232         
233     /**
234      * Returns a string that displays and identifies this
235      * object's properties.
236      *
237      * @return a String representation of this object
238      */

239     public String JavaDoc toString()
240     {
241     return super.toString()
242         + " inProgress: " + inProgress
243         + " edits: " + edits;
244     }
245 }
246
Popular Tags