1. /*
  2. * @(#)AbstractUndoableEdit.java 1.28 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.undo;
  8. import java.io.Serializable;
  9. import javax.swing.UIManager;
  10. /**
  11. * An abstract implementation of <code>UndoableEdit</code>,
  12. * implementing simple responses to all boolean methods in
  13. * that interface.
  14. *
  15. * @version 1.28 01/23/03
  16. * @author Ray Ryan
  17. */
  18. public class AbstractUndoableEdit implements UndoableEdit, Serializable {
  19. /**
  20. * String returned by <code>getUndoPresentationName</code>
  21. * as of Java 2 platform v1.3.1 this field is no longer used. This value
  22. * is now localized and comes from the defaults table with key
  23. * <code>AbstractUndoableEdit.undoText</code>.
  24. *
  25. * @see javax.swing.UIDefaults
  26. */
  27. protected static final String UndoName = "Undo";
  28. /**
  29. * String returned by <code>getRedoPresentationName</code>
  30. * as of Java 2 platform v1.3.1 this field is no longer used. This value
  31. * is now localized and comes from the defaults table with key
  32. * <code>AbstractUndoableEdit.redoText</code>.
  33. *
  34. * @see javax.swing.UIDefaults
  35. */
  36. protected static final String RedoName = "Redo";
  37. /**
  38. * Defaults to true; becomes false if this edit is undone, true
  39. * again if it is redone.
  40. */
  41. boolean hasBeenDone;
  42. /**
  43. * True if this edit has not received <code>die</code> defaults
  44. * to <code>true</code>.
  45. */
  46. boolean alive;
  47. /**
  48. * Creates an <code>AbstractUndoableEdit</code> which defaults
  49. * <code.hasBeenDone</code> and <code>alive</code> to <code>true</code>.
  50. */
  51. public AbstractUndoableEdit() {
  52. super();
  53. hasBeenDone = true;
  54. alive = true;
  55. }
  56. /**
  57. * Sets <code>alive</code> to false. Note that this
  58. * is a one way operation; dead edits cannot be resurrected.
  59. * Sending <code>undo</code> or <code>redo</code> to
  60. * a dead edit results in an exception being thrown.
  61. *
  62. * <p>Typically an edit is killed when it is consolidated by
  63. * another edit's <code>addEdit</code> or <code>replaceEdit</code>
  64. * method, or when it is dequeued from an <code>UndoManager</code>.
  65. */
  66. public void die() {
  67. alive = false;
  68. }
  69. /**
  70. * Throws <code>CannotUndoException</code> if <code>canUndo</code>
  71. * returns <code>false</code>. Sets <code>hasBeenDone</code>
  72. * to <code>false</code>. Subclasses should override to undo the
  73. * operation represented by this edit. Override should begin with
  74. * a call to super.
  75. *
  76. * @exception CannotUndoException if <code>canUndo</code>
  77. * returns <code>false</code>
  78. * @see #canUndo
  79. */
  80. public void undo() throws CannotUndoException {
  81. if (!canUndo()) {
  82. throw new CannotUndoException();
  83. }
  84. hasBeenDone = false;
  85. }
  86. /**
  87. * Returns true if this edit is <code>alive</code>
  88. * and <code>hasBeenDone</code> is <code>true</code>.
  89. *
  90. * @return true if this edit is <code>alive</code>
  91. * and <code>hasBeenDone</code> is <code>true</code>
  92. *
  93. * @see #die
  94. * @see #undo
  95. * @see #redo
  96. */
  97. public boolean canUndo() {
  98. return alive && hasBeenDone;
  99. }
  100. /**
  101. * Throws <code>CannotRedoException</code> if <code>canRedo</code>
  102. * returns false. Sets <code>hasBeenDone</code> to <code>true</code>.
  103. * Subclasses should override to redo the operation represented by
  104. * this edit. Override should begin with a call to super.
  105. *
  106. * @exception CannotRedoException if <code>canRedo</code>
  107. * returns <code>false</code>
  108. * @see #canRedo
  109. */
  110. public void redo() throws CannotRedoException {
  111. if (!canRedo()) {
  112. throw new CannotRedoException();
  113. }
  114. hasBeenDone = true;
  115. }
  116. /**
  117. * Returns <code>true</code> if this edit is <code>alive</code>
  118. * and <code>hasBeenDone</code> is <code>false</code>.
  119. *
  120. * @return <code>true</code> if this edit is <code>alive</code>
  121. * and <code>hasBeenDone</code> is <code>false</code>
  122. * @see #die
  123. * @see #undo
  124. * @see #redo
  125. */
  126. public boolean canRedo() {
  127. return alive && !hasBeenDone;
  128. }
  129. /**
  130. * This default implementation returns false.
  131. *
  132. * @param anEdit the edit to be added
  133. * @return false
  134. *
  135. * @see UndoableEdit#addEdit
  136. */
  137. public boolean addEdit(UndoableEdit anEdit) {
  138. return false;
  139. }
  140. /**
  141. * This default implementation returns false.
  142. *
  143. * @param anEdit the edit to replace
  144. * @return false
  145. *
  146. * @see UndoableEdit#replaceEdit
  147. */
  148. public boolean replaceEdit(UndoableEdit anEdit) {
  149. return false;
  150. }
  151. /**
  152. * This default implementation returns true.
  153. *
  154. * @return true
  155. * @see UndoableEdit#isSignificant
  156. */
  157. public boolean isSignificant() {
  158. return true;
  159. }
  160. /**
  161. * This default implementation returns "". Used by
  162. * <code>getUndoPresentationName</code> and
  163. * <code>getRedoPresentationName</code> to
  164. * construct the strings they return. Subclasses should override to
  165. * return an appropriate description of the operation this edit
  166. * represents.
  167. *
  168. * @return the empty string ""
  169. *
  170. * @see #getUndoPresentationName
  171. * @see #getRedoPresentationName
  172. */
  173. public String getPresentationName() {
  174. return "";
  175. }
  176. /**
  177. * Retreives the value from the defaults table with key
  178. * <code>AbstractUndoableEdit.undoText</code> and returns
  179. * that value followed by a space, followed by
  180. * <code>getPresentationName</code>.
  181. * If <code>getPresentationName</code> returns "",
  182. * then the defaults value is returned alone.
  183. *
  184. * @return the value from the defaults table with key
  185. * <code>AbstractUndoableEdit.undoText</code>, followed
  186. * by a space, followed by <code>getPresentationName</code>
  187. * unless <code>getPresentationName</code> is "" in which
  188. * case, the defaults value is returned alone.
  189. * @see #getPresentationName
  190. */
  191. public String getUndoPresentationName() {
  192. String name = getPresentationName();
  193. if (!"".equals(name)) {
  194. name = UIManager.getString("AbstractUndoableEdit.undoText") +
  195. " " + name;
  196. } else {
  197. name = UIManager.getString("AbstractUndoableEdit.undoText");
  198. }
  199. return name;
  200. }
  201. /**
  202. * Retreives the value from the defaults table with key
  203. * <code>AbstractUndoableEdit.redoText</code> and returns
  204. * that value followed by a space, followed by
  205. * <code>getPresentationName</code>.
  206. * If <code>getPresentationName</code> returns "",
  207. * then the defaults value is returned alone.
  208. *
  209. * @return the value from the defaults table with key
  210. * <code>AbstractUndoableEdit.redoText</code>, followed
  211. * by a space, followed by <code>getPresentationName</code>
  212. * unless <code>getPresentationName</code> is "" in which
  213. * case, the defaults value is returned alone.
  214. * @see #getPresentationName
  215. */
  216. public String getRedoPresentationName() {
  217. String name = getPresentationName();
  218. if (!"".equals(name)) {
  219. name = UIManager.getString("AbstractUndoableEdit.redoText") +
  220. " " + name;
  221. } else {
  222. name = UIManager.getString("AbstractUndoableEdit.redoText");
  223. }
  224. return name;
  225. }
  226. /**
  227. * Returns a string that displays and identifies this
  228. * object's properties.
  229. *
  230. * @return a String representation of this object
  231. */
  232. public String toString()
  233. {
  234. return super.toString()
  235. + " hasBeenDone: " + hasBeenDone
  236. + " alive: " + alive;
  237. }
  238. }