KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)AbstractUndoableEdit.java 1.29 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.undo;
9
10 import java.io.Serializable JavaDoc;
11 import javax.swing.UIManager JavaDoc;
12
13 /**
14  * An abstract implementation of <code>UndoableEdit</code>,
15  * implementing simple responses to all boolean methods in
16  * that interface.
17  *
18  * @version 1.29 12/19/03
19  * @author Ray Ryan
20  */

21 public class AbstractUndoableEdit implements UndoableEdit JavaDoc, Serializable JavaDoc {
22
23     /**
24      * String returned by <code>getUndoPresentationName</code>;
25      * as of Java 2 platform v1.3.1 this field is no longer used. This value
26      * is now localized and comes from the defaults table with key
27      * <code>AbstractUndoableEdit.undoText</code>.
28      *
29      * @see javax.swing.UIDefaults
30      */

31     protected static final String JavaDoc UndoName = "Undo";
32
33     /**
34      * String returned by <code>getRedoPresentationName</code>;
35      * as of Java 2 platform v1.3.1 this field is no longer used. This value
36      * is now localized and comes from the defaults table with key
37      * <code>AbstractUndoableEdit.redoText</code>.
38      *
39      * @see javax.swing.UIDefaults
40      */

41     protected static final String JavaDoc RedoName = "Redo";
42
43     /**
44      * Defaults to true; becomes false if this edit is undone, true
45      * again if it is redone.
46      */

47     boolean hasBeenDone;
48
49     /**
50      * True if this edit has not received <code>die</code>; defaults
51      * to <code>true</code>.
52      */

53     boolean alive;
54
55     /**
56      * Creates an <code>AbstractUndoableEdit</code> which defaults
57      * <code.hasBeenDone</code> and <code>alive</code> to <code>true</code>.
58      */

59     public AbstractUndoableEdit() {
60     super();
61
62     hasBeenDone = true;
63     alive = true;
64     }
65
66     /**
67      * Sets <code>alive</code> to false. Note that this
68      * is a one way operation; dead edits cannot be resurrected.
69      * Sending <code>undo</code> or <code>redo</code> to
70      * a dead edit results in an exception being thrown.
71      *
72      * <p>Typically an edit is killed when it is consolidated by
73      * another edit's <code>addEdit</code> or <code>replaceEdit</code>
74      * method, or when it is dequeued from an <code>UndoManager</code>.
75      */

76     public void die() {
77     alive = false;
78     }
79
80     /**
81      * Throws <code>CannotUndoException</code> if <code>canUndo</code>
82      * returns <code>false</code>. Sets <code>hasBeenDone</code>
83      * to <code>false</code>. Subclasses should override to undo the
84      * operation represented by this edit. Override should begin with
85      * a call to super.
86      *
87      * @exception CannotUndoException if <code>canUndo</code>
88      * returns <code>false</code>
89      * @see #canUndo
90      */

91     public void undo() throws CannotUndoException JavaDoc {
92     if (!canUndo()) {
93         throw new CannotUndoException JavaDoc();
94     }
95     hasBeenDone = false;
96     }
97
98     /**
99      * Returns true if this edit is <code>alive</code>
100      * and <code>hasBeenDone</code> is <code>true</code>.
101      *
102      * @return true if this edit is <code>alive</code>
103      * and <code>hasBeenDone</code> is <code>true</code>
104      *
105      * @see #die
106      * @see #undo
107      * @see #redo
108      */

109     public boolean canUndo() {
110     return alive && hasBeenDone;
111     }
112
113     /**
114      * Throws <code>CannotRedoException</code> if <code>canRedo</code>
115      * returns false. Sets <code>hasBeenDone</code> to <code>true</code>.
116      * Subclasses should override to redo the operation represented by
117      * this edit. Override should begin with a call to super.
118      *
119      * @exception CannotRedoException if <code>canRedo</code>
120      * returns <code>false</code>
121      * @see #canRedo
122      */

123     public void redo() throws CannotRedoException JavaDoc {
124     if (!canRedo()) {
125         throw new CannotRedoException JavaDoc();
126     }
127     hasBeenDone = true;
128     }
129
130     /**
131      * Returns <code>true</code> if this edit is <code>alive</code>
132      * and <code>hasBeenDone</code> is <code>false</code>.
133      *
134      * @return <code>true</code> if this edit is <code>alive</code>
135      * and <code>hasBeenDone</code> is <code>false</code>
136      * @see #die
137      * @see #undo
138      * @see #redo
139      */

140     public boolean canRedo() {
141     return alive && !hasBeenDone;
142     }
143     
144     /**
145      * This default implementation returns false.
146      *
147      * @param anEdit the edit to be added
148      * @return false
149      *
150      * @see UndoableEdit#addEdit
151      */

152     public boolean addEdit(UndoableEdit JavaDoc anEdit) {
153     return false;
154     }
155
156     /**
157      * This default implementation returns false.
158      *
159      * @param anEdit the edit to replace
160      * @return false
161      *
162      * @see UndoableEdit#replaceEdit
163      */

164     public boolean replaceEdit(UndoableEdit JavaDoc anEdit) {
165     return false;
166     }
167
168     /**
169      * This default implementation returns true.
170      *
171      * @return true
172      * @see UndoableEdit#isSignificant
173      */

174     public boolean isSignificant() {
175     return true;
176     }
177
178     /**
179      * This default implementation returns "". Used by
180      * <code>getUndoPresentationName</code> and
181      * <code>getRedoPresentationName</code> to
182      * construct the strings they return. Subclasses should override to
183      * return an appropriate description of the operation this edit
184      * represents.
185      *
186      * @return the empty string ""
187      *
188      * @see #getUndoPresentationName
189      * @see #getRedoPresentationName
190      */

191     public String JavaDoc getPresentationName() {
192     return "";
193     }
194
195     /**
196      * Retreives the value from the defaults table with key
197      * <code>AbstractUndoableEdit.undoText</code> and returns
198      * that value followed by a space, followed by
199      * <code>getPresentationName</code>.
200      * If <code>getPresentationName</code> returns "",
201      * then the defaults value is returned alone.
202      *
203      * @return the value from the defaults table with key
204      * <code>AbstractUndoableEdit.undoText</code>, followed
205      * by a space, followed by <code>getPresentationName</code>
206      * unless <code>getPresentationName</code> is "" in which
207      * case, the defaults value is returned alone.
208      * @see #getPresentationName
209      */

210     public String JavaDoc getUndoPresentationName() {
211     String JavaDoc name = getPresentationName();
212     if (!"".equals(name)) {
213         name = UIManager.getString("AbstractUndoableEdit.undoText") +
214                 " " + name;
215     } else {
216         name = UIManager.getString("AbstractUndoableEdit.undoText");
217     }
218
219     return name;
220     }
221
222     /**
223      * Retreives the value from the defaults table with key
224      * <code>AbstractUndoableEdit.redoText</code> and returns
225      * that value followed by a space, followed by
226      * <code>getPresentationName</code>.
227      * If <code>getPresentationName</code> returns "",
228      * then the defaults value is returned alone.
229      *
230      * @return the value from the defaults table with key
231      * <code>AbstractUndoableEdit.redoText</code>, followed
232      * by a space, followed by <code>getPresentationName</code>
233      * unless <code>getPresentationName</code> is "" in which
234      * case, the defaults value is returned alone.
235      * @see #getPresentationName
236      */

237     public String JavaDoc getRedoPresentationName() {
238     String JavaDoc name = getPresentationName();
239     if (!"".equals(name)) {
240         name = UIManager.getString("AbstractUndoableEdit.redoText") +
241                 " " + name;
242     } else {
243         name = UIManager.getString("AbstractUndoableEdit.redoText");
244     }
245
246     return name;
247     }
248
249     /**
250      * Returns a string that displays and identifies this
251      * object's properties.
252      *
253      * @return a String representation of this object
254      */

255     public String JavaDoc toString()
256     {
257     return super.toString()
258         + " hasBeenDone: " + hasBeenDone
259         + " alive: " + alive;
260     }
261 }
262
263
Popular Tags