Another fix to correctly split elements when inserting a new paragraph.

2006-01-31  Lillian Angel  <[EMAIL PROTECTED]>

        * javax/swing/text/DefaultStyledDocument.java
        (changeUpdate): Fixed calls to split to incorporate
        new parameter.
        (insertParagraph): Likewise. Uses 0 as editIndex
        because inserting into a new paragraph.
        (insertContentTag): Fixed check to use
        recreateLeaves. Added a FIXME comment.
        (split): Added a new parameter for edits.

Index: javax/swing/text/DefaultStyledDocument.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/DefaultStyledDocument.java,v
retrieving revision 1.52
diff -u -r1.52 DefaultStyledDocument.java
--- javax/swing/text/DefaultStyledDocument.java	31 Jan 2006 16:43:26 -0000	1.52
+++ javax/swing/text/DefaultStyledDocument.java	31 Jan 2006 21:10:29 -0000
@@ -573,7 +573,7 @@
     {
       // Split up the element at the start offset if necessary.
       Element el = getCharacterElement(offset);
-      Element[] res = split(el, offset, 0);
+      Element[] res = split(el, offset, 0, el.getElementIndex(offset));
       BranchElement par = (BranchElement) el.getParentElement();
       if (res[1] != null)
         {
@@ -599,7 +599,7 @@
 
       int endOffset = offset + length;
       el = getCharacterElement(endOffset);
-      res = split(el, endOffset, 0);
+      res = split(el, endOffset, 0, el.getElementIndex(endOffset));
       par = (BranchElement) el.getParentElement();
       if (res[1] != null)
         {
@@ -789,7 +789,7 @@
     private Element insertParagraph(BranchElement par, int offset)
     {
       Element current = par.getElement(par.getElementIndex(offset));
-      Element[] res = split(current, offset, 0);
+      Element[] res = split(current, offset, 0, 0);
       int index = par.getElementIndex(offset);
       Edit e = getEditForParagraphAndIndex(par, index + 1);
       Element ret;
@@ -971,17 +970,16 @@
         }
       else if (!origParCreated || dir != ElementSpec.OriginateDirection)
         {
+          // FIXME: insert TestTag4
           int end = pos + len;
           Element leaf = createLeafElement(paragraph, tag.getAttributes(), pos, end);
           edit.addAddedElement(leaf);
           
-          // check if there is an overlap with the next element.
+          // recreate all others
           Element next = paragraph.getElement(index);
-          if (pos >= next.getStartOffset() && pos < next.getEndOffset())
+          if (next != null && next.isLeaf())
             {
-              Element nextLeaf = createLeafElement(paragraph, next.getAttributes(),
-                                               end, next.getEndOffset());
-              edit.addAddedElement(nextLeaf);
+              recreateLeaves(end, new ElementSpec[] { tag });
               edit.addRemovedElement(next);              
             }
         }
@@ -1075,6 +1073,8 @@
      *          the offset at which to possibly split
      * @param space
      *          the amount of space to create between the splitted parts
+     * @param editIndex 
+     *          the index of the edit to use
      * @return An array of elements which represent the split result. This array
      *         has two elements, the two parts of the split. The first element
      *         might be null, which means that the element which should be
@@ -1082,7 +1082,7 @@
      *         null, which means that the offset is already at an element
      *         boundary and the element doesn't need to be splitted.
      */
-    private Element[] split(Element el, int offset, int space)
+    private Element[] split(Element el, int offset, int space, int editIndex)
     {
       // If we are at an element boundary, then return an empty array.
       if ((offset == el.getStartOffset() || offset == el.getEndOffset())
@@ -1097,7 +1097,7 @@
         {
           int index = el.getElementIndex(offset);
           Element child = el.getElement(index);
-          Element[] result = split(child, offset, space);
+          Element[] result = split(child, offset, space, editIndex);
           Element[] removed;
           Element[] added;
           Element[] newAdded;
@@ -1130,33 +1130,29 @@
                   if (ind != 0)
                     newAdded[ind] = el2;
                 }
-
-              Edit edit = getEditForParagraphAndIndex((BranchElement) el, index);
+              
+              Edit edit = getEditForParagraphAndIndex((BranchElement) el, editIndex);
               edit.addRemovedElements(removed);
               edit.addAddedElements(added);
-
+              
               BranchElement newPar = (BranchElement) new BranchElement(el.getParentElement(),
                                                                        el.getAttributes());
               newPar.replace(0, 0, newAdded);
               res = new Element[] { null, newPar };
             }
           else
-
             {
               removed = new Element[count - index];
               for (int i = index; i < count; ++i)
                 removed[i - index] = el.getElement(i);
-              added = new Element[0];
-
-              Edit edit = getEditForParagraphAndIndex((BranchElement) el, index);
+              
+              Edit edit = getEditForParagraphAndIndex((BranchElement) el, editIndex);
               edit.addRemovedElements(removed);
-              edit.addAddedElements(added);
-
+              
               BranchElement newPar = (BranchElement) new BranchElement(el.getParentElement(),
                                                                        el.getAttributes());
               newPar.replace(0, 0, removed);
               res = new Element[] { null, newPar };
-
             }
         }
       else if (el instanceof LeafElement)
@@ -1165,8 +1161,8 @@
           Element el1 = createLeafElement(par, el.getAttributes(),
                                           el.getStartOffset(), offset);
 
-          Element el2 = createLeafElement(par, el.getAttributes(), offset
-                                                                   + space,
+          Element el2 = createLeafElement(par, el.getAttributes(), 
+                                          offset + space,
                                           el.getEndOffset());
           res = new Element[] { el1, el2 };
         }

Reply via email to