1. /*
  2. * @(#)DocumentFilter.java 1.5 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.text;
  8. /**
  9. * <code>DocumentFilter</code>, as the name implies, is a filter for the
  10. * <code>Document</code> mutation methods. When a <code>Document</code>
  11. * containing a <code>DocumentFilter</code> is modified (either through
  12. * <code>insert</code> or <code>remove</code>), it forwards the appropriate
  13. * method invocation to the <code>DocumentFilter</code>. The
  14. * default implementation allows the modification to
  15. * occur. Subclasses can filter the modifications by conditionally invoking
  16. * methods on the superclass, or invoking the necessary methods on
  17. * the passed in <code>FilterBypass</code>. Subclasses should NOT call back
  18. * into the Document for the modification
  19. * instead call into the superclass or the <code>FilterBypass</code>.
  20. * <p>
  21. * When <code>remove</code> or <code>insertString</code> is invoked
  22. * on the <code>DocumentFilter</code>, the <code>DocumentFilter</code>
  23. * may callback into the
  24. * <code>FilterBypass</code> multiple times, or for different regions, but
  25. * it should not callback into the <code>FilterBypass</code> after returning
  26. * from the <code>remove</code> or <code>insertString</code> method.
  27. *
  28. * @see javax.swing.text.Document
  29. *
  30. * @version 1.5 01/23/03
  31. * @since 1.4
  32. */
  33. public class DocumentFilter {
  34. /**
  35. * Invoked prior to removal of the specified region in the
  36. * specified Document. Subclasses that want to conditionally allow
  37. * removal should override this and only call supers implementation as
  38. * necessary, or call directly into the <code>FilterBypass</code> as
  39. * necessary.
  40. *
  41. * @param fb FilterBypass that can be used to mutate Document
  42. * @param offset the offset from the beginning >= 0
  43. * @param length the number of characters to remove >= 0
  44. * @exception BadLocationException some portion of the removal range
  45. * was not a valid part of the document. The location in the exception
  46. * is the first bad position encountered.
  47. */
  48. public void remove(FilterBypass fb, int offset, int length) throws
  49. BadLocationException {
  50. fb.remove(offset, length);
  51. }
  52. /**
  53. * Invoked prior to insertion of text into the
  54. * specified Document. Subclasses that want to conditionally allow
  55. * insertion should override this and only call supers implementation as
  56. * necessary, or call directly into the FilterBypass.
  57. *
  58. * @param fb FilterBypass that can be used to mutate Document
  59. * @param offset the offset into the document to insert the content >= 0.
  60. * All positions that track change at or after the given location
  61. * will move.
  62. * @param string the string to insert
  63. * @param attr the attributes to associate with the inserted
  64. * content. This may be null if there are no attributes.
  65. * @exception BadLocationException the given insert position is not a
  66. * valid position within the document
  67. */
  68. public void insertString(FilterBypass fb, int offset, String string,
  69. AttributeSet attr) throws BadLocationException {
  70. fb.insertString(offset, string, attr);
  71. }
  72. /**
  73. * Invoked prior to replacing a region of text in the
  74. * specified Document. Subclasses that want to conditionally allow
  75. * replace should override this and only call supers implementation as
  76. * necessary, or call directly into the FilterBypass.
  77. *
  78. * @param fb FilterBypass that can be used to mutate Document
  79. * @param offset Location in Document
  80. * @param length Length of text to delete
  81. * @param text Text to insert, null indicates no text to insert
  82. * @param attrs AttributeSet indicating attributes of inserted text,
  83. * null is legal.
  84. * @exception BadLocationException the given insert position is not a
  85. * valid position within the document
  86. */
  87. public void replace(FilterBypass fb, int offset, int length, String text,
  88. AttributeSet attrs) throws BadLocationException {
  89. fb.replace(offset, length, text, attrs);
  90. }
  91. /**
  92. * Used as a way to circumvent calling back into the Document to
  93. * change it. Document implementations that wish to support
  94. * a DocumentFilter must provide an implementation that will
  95. * not callback into the DocumentFilter when the following methods
  96. * are invoked from the DocumentFilter.
  97. */
  98. public static abstract class FilterBypass {
  99. /**
  100. * Returns the Document the mutation is occuring on.
  101. *
  102. * @return Document that remove/insertString will operate on
  103. */
  104. public abstract Document getDocument();
  105. /**
  106. * Removes the specified region of text, bypassing the
  107. * DocumentFilter.
  108. *
  109. * @param offset the offset from the beginning >= 0
  110. * @param length the number of characters to remove >= 0
  111. * @exception BadLocationException some portion of the removal range
  112. * was not a valid part of the document. The location in the
  113. * exception is the first bad position encountered.
  114. */
  115. public abstract void remove(int offset, int length) throws
  116. BadLocationException;
  117. /**
  118. * Inserts the specified text, bypassing the
  119. * DocumentFilter.
  120. * @param offset the offset into the document to insert the
  121. * content >= 0. All positions that track change at or after the
  122. * given location will move.
  123. * @param string the string to insert
  124. * @param attr the attributes to associate with the inserted
  125. * content. This may be null if there are no attributes.
  126. * @exception BadLocationException the given insert position is not a
  127. * valid position within the document
  128. */
  129. public abstract void insertString(int offset, String string,
  130. AttributeSet attr) throws
  131. BadLocationException;
  132. /**
  133. * Deletes the region of text from <code>offset</code> to
  134. * <code>offset + length</code>, and replaces it with
  135. * <code>text</code>.
  136. *
  137. * @param offset Location in Document
  138. * @param length Length of text to delete
  139. * @param string Text to insert, null indicates no text to insert
  140. * @param attrs AttributeSet indicating attributes of inserted text,
  141. * null is legal.
  142. * @exception BadLocationException the given insert is not a
  143. * valid position within the document
  144. */
  145. public abstract void replace(int offset, int length, String string,
  146. AttributeSet attrs) throws
  147. BadLocationException;
  148. }
  149. }