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