1. /*
  2. * @(#)DocumentEvent.java 1.18 00/02/02
  3. *
  4. * Copyright 1997-2000 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.event;
  11. import javax.swing.undo.*;
  12. import javax.swing.text.*;
  13. /**
  14. * Interface for document change notifications. This provides
  15. * detailed information to Document observers about how the
  16. * Document changed. It provides high level information such
  17. * as type of change and where it occured, as well as the more
  18. * detailed structural changes (What Elements were inserted and
  19. * removed).
  20. *
  21. * @author Timothy Prinzing
  22. * @version 1.18 02/02/00
  23. * @see javax.swing.text.Document
  24. * @see DocumentListener
  25. */
  26. public interface DocumentEvent {
  27. /**
  28. * Returns the offset within the document of the start
  29. * of the change.
  30. *
  31. * @return the offset >= 0
  32. */
  33. public int getOffset();
  34. /**
  35. * Returns the length of the change.
  36. *
  37. * @return the length >= 0
  38. */
  39. public int getLength();
  40. /**
  41. * Gets the document that sourced the change event.
  42. *
  43. * @returns the document
  44. */
  45. public Document getDocument();
  46. /**
  47. * Gets the type of event.
  48. *
  49. * @return the type
  50. */
  51. public EventType getType();
  52. /**
  53. * Gets the change information for the given element.
  54. * The change information describes what elements were
  55. * added and removed and the location. If there were
  56. * no changes, null is returned.
  57. * <p>
  58. * This method is for observers to discover the structural
  59. * changes that were made. This means that only elements
  60. * that existed prior to the mutation (and still exist after
  61. * the mutatino) need to have ElementChange records.
  62. * The changes made available need not be recursive.
  63. * <p>
  64. * For example, if the an element is removed from it's
  65. * parent, this method should report that the parent
  66. * changed and provide an ElementChange implementation
  67. * that describes the change to the parent. If the
  68. * child element removed had children, these elements
  69. * do not need to be reported as removed.
  70. * <p>
  71. * If an child element is insert into a parent element,
  72. * the parent element should report a change. If the
  73. * child element also had elements inserted into it
  74. * (grandchildren to the parent) these elements need
  75. * not report change.
  76. *
  77. * @param elem the element
  78. * @return the change information, or null if the
  79. * element was not modified
  80. */
  81. public ElementChange getChange(Element elem);
  82. /**
  83. * Enumeration for document event types
  84. */
  85. public static final class EventType {
  86. private EventType(String s) {
  87. typeString = s;
  88. }
  89. /**
  90. * Insert type.
  91. */
  92. public static final EventType INSERT = new EventType("INSERT");
  93. /**
  94. * Remove type.
  95. */
  96. public static final EventType REMOVE = new EventType("REMOVE");
  97. /**
  98. * Change type.
  99. */
  100. public static final EventType CHANGE = new EventType("CHANGE");
  101. /**
  102. * Converts the type to a string.
  103. *
  104. * @return the string
  105. */
  106. public String toString() {
  107. return typeString;
  108. }
  109. private String typeString;
  110. }
  111. /**
  112. * Describes changes made to a specific element.
  113. */
  114. public interface ElementChange {
  115. /**
  116. * Returns the element represented. This is the element
  117. * that was changed.
  118. *
  119. * @return the element
  120. */
  121. public Element getElement();
  122. /**
  123. * Fetches the index within the element represented.
  124. * This is the location that children were added
  125. * and/or removed.
  126. *
  127. * @return the index >= 0
  128. */
  129. public int getIndex();
  130. /**
  131. * Gets the child elements that were removed from the
  132. * given parent element. The element array returned is
  133. * sorted in the order that the elements used to lie in
  134. * the document, and must be contiguous.
  135. *
  136. * @return the child elements
  137. */
  138. public Element[] getChildrenRemoved();
  139. /**
  140. * Gets the child elements that were added to the given
  141. * parent element. The element array returned is in the
  142. * order that the elements lie in the document, and must
  143. * be contiguous.
  144. *
  145. * @return the child elements
  146. */
  147. public Element[] getChildrenAdded();
  148. }
  149. }