This patch makes AbstractDocument return an ElementChange in the
DocumentEvent that it fires after a remove operation.  Adds undoable
edit support to GapContent's remove operation.

2005-10-05  Anthony Balkissoon  <[EMAIL PROTECTED]>

        * javax/swing/text/AbstractDocument.java:
        (remove): If removing content returns an UndoableEdit, then add an 
        ElementEdit to the DocumentEvent before firing.
        * javax/swing/text/GapContent.java:
        (UndoRemove): New class to implement UndoableEdit for remove operation.
        (remove): Return an UndoableEdit instead of null.

--Tony
Index: javax/swing/text/AbstractDocument.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/AbstractDocument.java,v
retrieving revision 1.31
diff -u -r1.31 AbstractDocument.java
--- javax/swing/text/AbstractDocument.java	4 Oct 2005 19:37:19 -0000	1.31
+++ javax/swing/text/AbstractDocument.java	5 Oct 2005 15:12:26 -0000
@@ -616,10 +616,38 @@
     DefaultDocumentEvent event =
       new DefaultDocumentEvent(offset, length,
 			       DocumentEvent.EventType.REMOVE);
+    
+    // Here we set up the parameters for an ElementChange, if one
+    // needs to be added to the DocumentEvent later
+    Element root = getDefaultRootElement();
+    int start = root.getElementIndex(offset);
+    int end = root.getElementIndex(offset + length);
+    
+    Element[] removed = new Element[end - start + 1];
+    for (int i = start; i <= end; i++)
+      removed[i - start] = root.getElement(i);
+    
     removeUpdate(event);
+
+    Element[] added = new Element[1];
+    added[0] = root.getElement(start);
     boolean shouldFire = content.getString(offset, length).length() != 0;
-    content.remove(offset, length);
+    
+    UndoableEdit temp = content.remove(offset, length);
+    
     postRemoveUpdate(event);
+    
+    GapContent.UndoRemove changes = null;
+    if (content instanceof GapContent)
+      changes = (GapContent.UndoRemove) temp;
+
+    if (changes != null)
+      {
+        // We need to add an ElementChange to our DocumentEvent
+        ElementEdit edit = new ElementEdit (root, start, removed, added);
+        event.addEdit(edit);
+      }
+    
     if (shouldFire)
       fireRemoveUpdate(event);
   }
Index: javax/swing/text/GapContent.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/GapContent.java,v
retrieving revision 1.29
diff -u -r1.29 GapContent.java
--- javax/swing/text/GapContent.java	4 Oct 2005 19:37:19 -0000	1.29
+++ javax/swing/text/GapContent.java	5 Oct 2005 15:12:26 -0000
@@ -168,6 +168,44 @@
     
   }
   
+  class UndoRemove extends AbstractUndoableEdit
+  {
+    public int where;
+    String text;
+    public UndoRemove(int start, String removedText)
+    {
+      where = start;
+      text = removedText;
+    }
+
+    public void undo () throws CannotUndoException
+    {
+      super.undo();
+      try
+      {
+        insertString(where, text);
+      }
+      catch (BadLocationException ble)
+      {
+        throw new CannotUndoException();
+      }
+    }
+    
+    public void redo () throws CannotUndoException
+    {
+      super.redo();
+      try
+      {
+        remove(where, text.length());
+      }
+      catch (BadLocationException ble)
+      {
+        throw new CannotRedoException();
+      }
+    }
+    
+  }
+  
   /** The serialization UID (compatible with JDK1.5). */
   private static final long serialVersionUID = -6226052713477823730L;
 
@@ -259,8 +297,7 @@
    * @param where the position where the string is inserted
    * @param str the string that is to be inserted
    * 
-   * @return an UndoableEdit object (currently not supported, so
-   *         <code>null</code> is returned)
+   * @return an UndoableEdit object
    * 
    * @throws BadLocationException if <code>where</code> is not a valid
    *         location in the buffer
@@ -287,8 +324,7 @@
    * @param where the position where the content is to be removed
    * @param nitems number of characters to be removed
    * 
-   * @return an UndoableEdit object (currently not supported, so
-   *         <code>null</code> is returned)
+   * @return an UndoableEdit object
    * 
    * @throws BadLocationException if <code>where</code> is not a valid
    *         location in the buffer
@@ -305,9 +341,10 @@
       throw new BadLocationException("where + nitems cannot be greater"
           + " than the content length", where + nitems);
 
+    String removedText = getString(where, nitems);
     replace(where, nitems, null, 0);
 
-    return null;
+    return new UndoRemove(where, removedText);
   }
 
   /**
_______________________________________________
Classpath-patches mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/classpath-patches

Reply via email to