KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2    SwingWT
3    Copyright(c)2003-2004 Daniel Naab
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: dannaab@users.sourceforge.net
9
10    $Log: CompoundEdit.java,v $
11    Revision 1.2 2004/04/16 22:45:50 dannaab
12    Add copyright msg
13
14 */

15
16 package swingwtx.swing.undo;
17
18 import java.util.*;
19
20 /**
21  * Implementation of UndoableEdit that acts as a container for multiple edits.
22  *
23  * @author Naab
24  * @version %I%, %G%
25  */

26 public class CompoundEdit extends AbstractUndoableEdit
27 {
28     /* List of edits */
29     protected Vector edits = new Vector();
30
31     public CompoundEdit()
32     {
33         super();
34     }
35
36     public boolean canUndo() { return !isInProgress() && super.canUndo(); }
37     public void undo() throws CannotUndoException
38     {
39         super.undo();
40
41         // Undo each edit
42
for (int i=(edits.size()-1); i >= 0; i--)
43         {
44             ((UndoableEdit)edits.elementAt(i)).undo();
45         }
46     }
47
48     public boolean canRedo() { return !isInProgress() && super.canRedo(); }
49     public void redo() throws CannotRedoException
50     {
51         super.redo();
52
53         // Redo each edit
54
Iterator iterator = edits.iterator();
55         while (iterator.hasNext())
56         {
57             UndoableEdit undoableEdit = (UndoableEdit) iterator.next();
58             undoableEdit.redo();
59         }
60     }
61
62     protected UndoableEdit lastEdit()
63     {
64         return edits.size() > 0 ? (UndoableEdit)edits.lastElement() : null;
65     }
66
67     private boolean inProgress = true;
68     public boolean isInProgress() { return inProgress; }
69     public void end() { inProgress = false; }
70
71     public void die()
72     {
73         // Tell each edit to "die"
74
Iterator iterator = edits.iterator();
75         while (iterator.hasNext())
76         {
77             UndoableEdit undoableEdit = (UndoableEdit) iterator.next();
78             undoableEdit.die();
79         }
80
81         super.die();
82     }
83
84     public boolean addEdit(UndoableEdit undoableEdit)
85     {
86         boolean success = false;
87
88         // If this compound edit is complete, don't allow adding new edits.
89
do if (!isInProgress())
90         {
91             UndoableEdit lastEdit = lastEdit();
92
93             success = true;
94             // If this is the first, add it
95
if (edits.size() == 0)
96             {
97                 edits.add(undoableEdit);
98                 break;
99             }
100             // If last is a CompoundEdit, add to it.
101
else if (lastEdit.addEdit(undoableEdit))
102             {
103                 break;
104             }
105             // If adding failed, try popping the last one onto this one.
106
else if (undoableEdit.replaceEdit(lastEdit))
107             {
108                 edits.remove(edits.size()-1);
109                 break;
110             }
111
112             // If we get this far, something failed, so just add to the end of this CompoundEdit
113
edits.add(undoableEdit);
114         } while (false);
115
116         return success;
117     }
118
119     public boolean isSignificant()
120     {
121         boolean isSignificant = false;
122
123         // For a compound edit, we are "significant" if any of our children are "significant"
124
Iterator iterator = edits.iterator();
125         while (iterator.hasNext())
126         {
127             UndoableEdit undoableEdit = (UndoableEdit) iterator.next();
128             if (undoableEdit.isSignificant())
129             {
130                 isSignificant = true;
131                 break;
132             }
133         }
134
135         return isSignificant;
136     }
137
138     public String JavaDoc getPresentationName()
139     {
140         String JavaDoc presentationName;
141
142         UndoableEdit lastEdit = lastEdit();
143         // If there are no edits, let AbstractUndoableEdit return a default.
144
if (lastEdit == null)
145             presentationName = super.getPresentationName();
146         // Presentation name should be the name of the most recent edit
147
else
148             presentationName = lastEdit.getPresentationName();
149
150         return presentationName;
151     }
152
153     public String JavaDoc getUndoPresentationName()
154     {
155         UndoableEdit lastEdit = lastEdit();
156         // If there are no edits, let AbstractUndoableEdit return a default.
157
if (lastEdit == null)
158             return super.getUndoPresentationName();
159         // Presentation name should be the name of the most recent edit
160
else
161             return lastEdit.getUndoPresentationName();
162     }
163
164     public String JavaDoc getRedoPresentationName()
165     {
166         UndoableEdit lastEdit = lastEdit();
167         // If there are no edits, let AbstractUndoableEdit return a default.
168
if (lastEdit == null)
169             return super.getUndoPresentationName();
170         // Presentation name should be the name of the most recent edit
171
else
172             return lastEdit.getUndoPresentationName();
173     }
174 }
175
Popular Tags