1. /*
  2. * @(#)UndoableEditSupport.java 1.18 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 javax.swing.event.*;
  9. import java.util.*;
  10. /**
  11. * A support class used for managing <code>UndoableEdit</code> listeners.
  12. *
  13. * @author Ray Ryan
  14. * @version 1.18 01/23/03
  15. */
  16. public class UndoableEditSupport {
  17. protected int updateLevel;
  18. protected CompoundEdit compoundEdit;
  19. protected Vector listeners;
  20. protected Object realSource;
  21. /**
  22. * Constructs an <code>UndoableEditSupport</code> object.
  23. */
  24. public UndoableEditSupport() {
  25. this(null);
  26. }
  27. /**
  28. * Constructs an <code>UndoableEditSupport</code> object.
  29. *
  30. * @param r an <code>Object</code>
  31. */
  32. public UndoableEditSupport(Object r) {
  33. realSource = r == null ? this : r;
  34. updateLevel = 0;
  35. compoundEdit = null;
  36. listeners = new Vector();
  37. }
  38. /**
  39. * Registers an <code>UndoableEditListener</code>.
  40. * The listener is notified whenever an edit occurs which can be undone.
  41. *
  42. * @param l an <code>UndoableEditListener</code> object
  43. * @see #removeUndoableEditListener
  44. */
  45. public synchronized void addUndoableEditListener(UndoableEditListener l) {
  46. listeners.addElement(l);
  47. }
  48. /**
  49. * Removes an <code>UndoableEditListener</code>.
  50. *
  51. * @param l the <code>UndoableEditListener</code> object to be removed
  52. * @see #addUndoableEditListener
  53. */
  54. public synchronized void removeUndoableEditListener(UndoableEditListener l)
  55. {
  56. listeners.removeElement(l);
  57. }
  58. /**
  59. * Returns an array of all the <code>UndoableEditListener</code>s added
  60. * to this UndoableEditSupport with addUndoableEditListener().
  61. *
  62. * @return all of the <code>UndoableEditListener</code>s added or an empty
  63. * array if no listeners have been added
  64. * @since 1.4
  65. */
  66. public synchronized UndoableEditListener[] getUndoableEditListeners() {
  67. return (UndoableEditListener[])(listeners.toArray(
  68. new UndoableEditListener[0]));
  69. }
  70. /**
  71. * Called only from <code>postEdit</code> and <code>endUpdate</code>. Calls
  72. * <code>undoableEditHappened</code> in all listeners. No synchronization
  73. * is performed here, since the two calling methods are synchronized.
  74. */
  75. protected void _postEdit(UndoableEdit e) {
  76. UndoableEditEvent ev = new UndoableEditEvent(realSource, e);
  77. Enumeration cursor = ((Vector)listeners.clone()).elements();
  78. while (cursor.hasMoreElements()) {
  79. ((UndoableEditListener)cursor.nextElement()).
  80. undoableEditHappened(ev);
  81. }
  82. }
  83. /**
  84. * DEADLOCK WARNING: Calling this method may call
  85. * <code>undoableEditHappened</code> in all listeners.
  86. * It is unwise to call this method from one of its listeners.
  87. */
  88. public synchronized void postEdit(UndoableEdit e) {
  89. if (updateLevel == 0) {
  90. _postEdit(e);
  91. } else {
  92. // PENDING(rjrjr) Throw an exception if this fails?
  93. compoundEdit.addEdit(e);
  94. }
  95. }
  96. /**
  97. * Returns the update level value.
  98. *
  99. * @return an integer representing the update level
  100. */
  101. public int getUpdateLevel() {
  102. return updateLevel;
  103. }
  104. /**
  105. *
  106. */
  107. public synchronized void beginUpdate() {
  108. if (updateLevel == 0) {
  109. compoundEdit = createCompoundEdit();
  110. }
  111. updateLevel++;
  112. }
  113. /**
  114. * Called only from <code>beginUpdate</code>.
  115. * Exposed here for subclasses' use.
  116. */
  117. protected CompoundEdit createCompoundEdit() {
  118. return new CompoundEdit();
  119. }
  120. /**
  121. * DEADLOCK WARNING: Calling this method may call
  122. * <code>undoableEditHappened</code> in all listeners.
  123. * It is unwise to call this method from one of its listeners.
  124. */
  125. public synchronized void endUpdate() {
  126. updateLevel--;
  127. if (updateLevel == 0) {
  128. compoundEdit.end();
  129. _postEdit(compoundEdit);
  130. compoundEdit = null;
  131. }
  132. }
  133. /**
  134. * Returns a string that displays and identifies this
  135. * object's properties.
  136. *
  137. * @return a <code>String</code> representation of this object
  138. */
  139. public String toString() {
  140. return super.toString() +
  141. " updateLevel: " + updateLevel +
  142. " listeners: " + listeners +
  143. " compoundEdit: " + compoundEdit;
  144. }
  145. }