Hi,
this patch fixes PR 27166 and provides the DocumentFilter feature. I will
provide a demonstration in the Swing Activity board and a mauve test soon.

The ChangeLog:

2006-04-14  Robert Schuster  <[EMAIL PROTECTED]>

        * javax/swing/text/AbstractDocument.java:
        (getBypass): New method.
        (insertString): Rewritten.
        (remove): Rewritten.
        (replace): Rewritten.
        (insertStringImpl): New method.
        (removeImpl): New method.
        (replaceImpl): New method.
        (AbstractDocument.Bypass): New class.

cya
Robert
Index: javax/swing/text/AbstractDocument.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/AbstractDocument.java,v
retrieving revision 1.54
diff -u -r1.54 AbstractDocument.java
--- javax/swing/text/AbstractDocument.java	25 Mar 2006 18:47:31 -0000	1.54
+++ javax/swing/text/AbstractDocument.java	14 Apr 2006 18:50:02 -0000
@@ -51,6 +51,7 @@
 import javax.swing.event.EventListenerList;
 import javax.swing.event.UndoableEditEvent;
 import javax.swing.event.UndoableEditListener;
+import javax.swing.text.DocumentFilter;
 import javax.swing.tree.TreeNode;
 import javax.swing.undo.AbstractUndoableEdit;
 import javax.swing.undo.CompoundEdit;
@@ -148,6 +149,11 @@
    */
   Object documentCV = new Object();
 
+  /** An instance of a DocumentFilter.FilterBypass which allows calling
+   * the insert, remove and replace method without checking for an installed
+   * document filter.
+   */
+  DocumentFilter.FilterBypass bypass;
   
   /**
    * Creates a new <code>AbstractDocument</code> with the specified
@@ -180,6 +186,19 @@
     content = doc;
     context = ctx;
   }
+  
+  /** Returns the DocumentFilter.FilterBypass instance for this
+   * document and create it if it does not exist yet.
+   *  
+   * @return This document's DocumentFilter.FilterBypass instance.
+   */
+  private DocumentFilter.FilterBypass getBypass()
+  {
+    if (bypass == null)
+      bypass = new Bypass();
+    
+    return bypass;
+  }
 
   /**
    * Returns the paragraph [EMAIL PROTECTED] Element} that holds the specified position.
@@ -521,6 +540,9 @@
   /**
    * Inserts a String into this <code>Document</code> at the specified
    * position and assigning the specified attributes to it.
+   * 
+   * <p>If a [EMAIL PROTECTED] DocumentFilter} is installed in this document, the
+   * corresponding method of the filter object is called.</p>
    *
    * @param offset the location at which the string should be inserted
    * @param text the content to be inserted
@@ -532,6 +554,15 @@
   public void insertString(int offset, String text, AttributeSet attributes)
     throws BadLocationException
   {
+    if (documentFilter != null)
+      documentFilter.insertString(getBypass(), offset, text, attributes);
+    else
+      insertStringImpl(offset, text, attributes);
+  }
+
+  void insertStringImpl(int offset, String text, AttributeSet attributes)
+    throws BadLocationException
+  {
     // Just return when no text to insert was given.
     if (text == null || text.length() == 0)
       return;
@@ -673,6 +704,9 @@
 
   /**
    * Removes a piece of content from this <code>Document</code>.
+   * 
+   * <p>If a [EMAIL PROTECTED] DocumentFilter} is installed in this document, the
+   * corresponding method of the filter object is called.</p>
    *
    * @param offset the start offset of the fragment to be removed
    * @param length the length of the fragment to be removed
@@ -683,6 +717,14 @@
    */
   public void remove(int offset, int length) throws BadLocationException
   {
+    if (documentFilter != null)
+      documentFilter.remove(getBypass(), offset, length);
+    else
+      removeImpl(offset, length);
+  }
+  
+  void removeImpl(int offset, int length) throws BadLocationException
+  {
     DefaultDocumentEvent event =
       new DefaultDocumentEvent(offset, length,
 			       DocumentEvent.EventType.REMOVE);
@@ -707,6 +749,9 @@
   /**
    * Replaces a piece of content in this <code>Document</code> with
    * another piece of content.
+   * 
+   * <p>If a [EMAIL PROTECTED] DocumentFilter} is installed in this document, the
+   * corresponding method of the filter object is called.</p>
    *
    * @param offset the start offset of the fragment to be removed
    * @param length the length of the fragment to be removed
@@ -720,11 +765,21 @@
    * @since 1.4
    */
   public void replace(int offset, int length, String text,
+                      AttributeSet attributes)
+    throws BadLocationException
+  {
+    if (documentFilter != null)
+      documentFilter.replace(getBypass(), offset, length, text, attributes);
+    else
+      replaceImpl(offset, length, text, attributes);
+  }
+  
+  void replaceImpl(int offset, int length, String text,
 		      AttributeSet attributes)
     throws BadLocationException
   {
-    remove(offset, length);
-    insertString(offset, text, attributes);
+    removeImpl(offset, length);
+    insertStringImpl(offset, text, attributes);
   }
 
   /**
@@ -2239,4 +2294,37 @@
 	      + getStartOffset() + "," + getEndOffset() + "\n");
     }
   }
+  
+  /** A class whose methods delegate to the insert, remove and replace methods
+   * of this document which do not check for an installed DocumentFilter.
+   */
+  class Bypass extends DocumentFilter.FilterBypass
+  {
+
+    public Document getDocument()
+    {
+      return AbstractDocument.this;
+    }
+
+    public void insertString(int offset, String string, AttributeSet attr)
+    throws BadLocationException
+    {
+      AbstractDocument.this.insertStringImpl(offset, string, attr);
+    }
+
+    public void remove(int offset, int length)
+    throws BadLocationException
+    {
+      AbstractDocument.this.removeImpl(offset, length);
+    }
+
+    public void replace(int offset, int length, String string,
+                        AttributeSet attrs)
+    throws BadLocationException
+    {
+      AbstractDocument.this.replaceImpl(offset, length, string, attrs);
+    }
+    
+  }
+  
 }

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to