1. /*
  2. * @(#)AbstractUndoableEdit.java 1.20 01/11/29
  3. *
  4. * Copyright 2002 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. /**
  10. * An abstract implementation of UndoableEdit, implementing simple
  11. * responses to all boolean methods in that interface.
  12. *
  13. * @version 1.20 11/29/01
  14. * @author Ray Ryan
  15. */
  16. public class AbstractUndoableEdit implements UndoableEdit, Serializable {
  17. /**
  18. * String returned by getUndoPresentationName()
  19. */
  20. protected static final String UndoName = "Undo"; // PENDING(rjrjr): these need
  21. /**
  22. * String returned by getRedoPresentationName()
  23. */
  24. protected static final String RedoName = "Redo"; // to be localizable
  25. /**
  26. * Defaults to true. Becomes false if this edit is undone, true
  27. * again if it is redone.
  28. */
  29. boolean hasBeenDone;
  30. /**
  31. * True if this edit has not received die().
  32. */
  33. boolean alive;
  34. public AbstractUndoableEdit() {
  35. super();
  36. hasBeenDone = true;
  37. alive = true;
  38. }
  39. /**
  40. * Sets alive to false. Note that this is a one way operation:
  41. * dead edits cannot be resurrected. Sending undo() or redo() to
  42. * a dead edit results in an exception being thrown.
  43. *
  44. * Typically an edit is killed when it is consolidated by another
  45. * edit's addEdit() or replaceEdit() method, or when it is
  46. * dequeued from an UndoManager
  47. */
  48. public void die() {
  49. alive = false;
  50. }
  51. /**
  52. * Throws CannotUndoException if canUndo() returns false. Sets
  53. * hasBeenDone to false. Subclasses should override to undo the
  54. * operation represented by this edit. Override should begin with
  55. * a call to super.
  56. *
  57. * @see #canUndo
  58. */
  59. public void undo() throws CannotUndoException {
  60. if (!canUndo()) {
  61. throw new CannotUndoException();
  62. }
  63. hasBeenDone = false;
  64. }
  65. /**
  66. * Returns true if this edit is alive and hasBeenDone is true.
  67. *
  68. * @see #die
  69. * @see #undo
  70. * @see #redo
  71. */
  72. public boolean canUndo() {
  73. return alive && hasBeenDone;
  74. }
  75. /**
  76. * Throws CannotRedoException if canRedo() returns false. Sets
  77. * hasBeenDone to true. Subclasses should override to redo the
  78. * operation represented by this edit. Override should begin with
  79. * a call to super.
  80. *
  81. * @see #canRedo
  82. */
  83. public void redo() throws CannotRedoException {
  84. if (!canRedo()) {
  85. throw new CannotRedoException();
  86. }
  87. hasBeenDone = true;
  88. }
  89. /**
  90. * Returns true if this edit is alive and hasBeenDone is false.
  91. *
  92. * @see #die
  93. * @see #undo
  94. * @see #redo
  95. */
  96. public boolean canRedo() {
  97. return alive && !hasBeenDone;
  98. }
  99. /**
  100. * This default implementation returns false.
  101. *
  102. * @see UndoableEdit#addEdit
  103. */
  104. public boolean addEdit(UndoableEdit anEdit) {
  105. return false;
  106. }
  107. /**
  108. * This default implementation returns false.
  109. *
  110. * @see UndoableEdit#replaceEdit
  111. */
  112. public boolean replaceEdit(UndoableEdit anEdit) {
  113. return false;
  114. }
  115. /**
  116. * This default implementation returns true.
  117. *
  118. * @see UndoableEdit#isSignificant
  119. */
  120. public boolean isSignificant() {
  121. return true;
  122. }
  123. /**
  124. * This default implementation returns "". Used by
  125. * getUndoPresentationName() and getRedoPresentationName() to
  126. * construct the strings they return. Subclasses shoul override to
  127. * return an appropriate description of the operation this edit
  128. * represents.
  129. *
  130. * @see #getUndoPresentationName
  131. * @see #getRedoPresentationName
  132. */
  133. public String getPresentationName() {
  134. return "";
  135. }
  136. /**
  137. * If getPresentationName() returns "", returns
  138. * AbstractUndoableEdit.UndoName. Otherwise returns
  139. * AbstractUndoableEdit.UndoName followed by a space and
  140. * getPresentationName()
  141. *
  142. * @see #getPresentationName
  143. */
  144. public String getUndoPresentationName() {
  145. String name = getPresentationName();
  146. if (name != "") {
  147. name = UndoName + " " + name;
  148. } else {
  149. name = UndoName;
  150. }
  151. return name;
  152. }
  153. /**
  154. * If getPresentationName() returns "", returns
  155. * AbstractUndoableEdit.RedoName. Otherwise returns
  156. * AbstractUndoableEdit.RedoName followed by a space and
  157. * getPresentationName()
  158. *
  159. * @see #getPresentationName
  160. */
  161. public String getRedoPresentationName() {
  162. String name = getPresentationName();
  163. if (name != "") {
  164. name = RedoName + " " + name;
  165. } else {
  166. name = RedoName;
  167. }
  168. return name;
  169. }
  170. /**
  171. * Returns a string that displays and identifies this
  172. * object's properties.
  173. *
  174. * @return a String representation of this object
  175. */
  176. public String toString()
  177. {
  178. return super.toString()
  179. + " hasBeenDone: " + hasBeenDone
  180. + " alive: " + alive;
  181. }
  182. }