BranchElement previous = (BranchElement) parent.getElement(parentIndex);   BranchElement newBranch = (BranchElement) createBranchElement(parent, previous.getAttributes());              int previousIndex = previous.getElementIndex(offset); int numReplaced = previous.getElementCount() - previousIndex; Element previousLeaf = previous.getElement(previousIndex); AttributeSet prevLeafAtts = previous.getAttributes();    Element newPreviousLeaf = createLeafElement(previous, prevLeafAtts, previousLeaf.getStartOffset(), offset);    Element firstLeafInNewBranch = createLeafElement(newBranch, prevLeafAtts, offset, previousLeaf.getEndOffset());    Element[] newLeaves = new Element[numReplaced]; newLeaves[0] = firstLeafInNewBranch; for (int i = 1; i < numReplaced; i++) newLeaves[i] = previous.getElement(previousIndex + i); newBranch.replace(0, 0, newLeaves);    int removeSize = previous.getElementCount() - previousIndex; Element[] remove = new Element[removeSize]; for (int j = 0; j < removeSize; j++) remove[j] = previous.getElement(previousIndex + j); Edit edit = getEditForParagraphAndIndex(previous, previousIndex); edit.addRemovedElements(remove);   int index = parentIndex + 1; Edit edit2 = getEditForParagraphAndIndex(parent, index); edit2.addAddedElement(newBranch); elementStack.push(newBranch);
AttributeSet parentAtts = parent.getAttributes(); Element toFracture = parent.getElement(parentIndex); int parSize = parent.getElementCount(); Edit edit = getEditForParagraphAndIndex(parent, parentIndex);  Element frac = toFracture; int leftIns = 0; int indexOfFrac = toFracture.getElementIndex(offset); int size = toFracture.getElementCount();   frac = toFracture.getElement(indexOfFrac); while (!frac.isLeaf()) frac = frac.getElement(frac.getElementIndex(offset));  AttributeSet atts = frac.getAttributes(); int fracStart = frac.getStartOffset(); int fracEnd = frac.getEndOffset(); if (offset > fracStart && offset < fracEnd) {   BranchElement rightBranch = new BranchElement(parent, parentAtts);    BranchElement leftBranch = null; Element[] added = null; if (edit.added.size() > 0 || edit.removed.size() > 0) { added = new Element[] { rightBranch };   parentIndex++; } else { leftBranch = new BranchElement(parent, parentAtts); added = new Element[] { leftBranch, rightBranch };   Element leftFracturedLeaf = createLeafElement(leftBranch, atts, fracStart, offset); leftBranch.replace(leftIns, 0, new Element[] { leftFracturedLeaf }); }  if (!toFracture.isLeaf()) {  if (indexOfFrac > 0 && leftBranch != null) { Element[] add = new Element[indexOfFrac]; for (int i = 0; i < indexOfFrac; i++) add[i] = toFracture.getElement(i); leftIns = add.length; leftBranch.replace(0, 0, add); }  int count = size - indexOfFrac - 1; if (count > 0) { Element[] add = new Element[count]; int j = 0; int i = indexOfFrac + 1; while (j < count) add[j++] = toFracture.getElement(i++); rightBranch.replace(0, 0, add); } }    int rm = 0; int end = fracEnd; Element next = rightBranch.getElement(0); if (next != null && next.isLeaf() && next.getAttributes().isEqual(atts)) { end = next.getEndOffset(); rm = 1; }  Element rightFracturedLeaf = createLeafElement(rightBranch, atts, offset, end); rightBranch.replace(0, rm, new Element[] { rightFracturedLeaf });    int remove = parSize - parentIndex; Element[] removed = new Element[0]; Element[] added2 = new Element[0]; if (remove > 0) { removed = new Element[remove]; int s = 0; for (int j = parentIndex; j < parSize; j++) removed[s++] = parent.getElement(j); edit.addRemovedElements(removed); added2 = recreateAfterFracture(removed, parent, 1, rightBranch.getEndOffset()); }  edit.addAddedElements(added); edit.addAddedElements(added2); elementStack.push(rightBranch); lastFractured = rightFracturedLeaf; } else fracNotCreated = true;
    private void insertFracture(ElementSpec tag)    {      // FIXME: This method may be incomplete.  We must make sure the       // appropriate edits were added and the correct paragraph element      // is pushed onto the top of the elementStack so future inserts go       // to the right paragraph.            // This is the parent of the paragraph about to be fractured.  We will      // create a new child of this parent.      BranchElement parent = (BranchElement) elementStack.peek();      int parentIndex = parent.getElementIndex(offset);            // This is the old paragraph.  We must remove all its children that       // occur after offset and move them to a new paragraph.  We must      // also recreate its child that occurs at offset to have the proper      // end offset.  The remainder of this child will also go in the new      // paragraph.      BranchElement previous = (BranchElement) parent.getElement(parentIndex);            // This is the new paragraph.      BranchElement newBranch =         (BranchElement) createBranchElement(parent, previous.getAttributes());                  // The steps we must take to properly fracture are:      // 1. Recreate the LeafElement at offset to have the correct end offset.      // 2. Create a new LeafElement with the remainder of the LeafElement in       //    #1 ==> this is whatever was in that LeafElement to the right of the      //    inserted newline.      // 3. Find the paragraph at offset and remove all its children that       //    occur _after_ offset.  These will be moved to the newly created      //    paragraph.      // 4. Move the LeafElement created in #2 and all the LeafElements removed      //    in #3 to the newly created paragraph.      // 5. Add the new paragraph to the parent.      int previousIndex = previous.getElementIndex(offset);      int numReplaced = previous.getElementCount() - previousIndex;      Element previousLeaf = previous.getElement(previousIndex);      AttributeSet prevLeafAtts = previous.getAttributes();            // This recreates the child at offset to have the proper end offset.        // (Step 1).      Element newPreviousLeaf =         createLeafElement(previous,                           prevLeafAtts, previousLeaf.getStartOffset(),                           offset);      // This creates the new child, which is the remainder of the old child.        // (Step 2).            Element firstLeafInNewBranch =         createLeafElement(newBranch, prevLeafAtts,                           offset, previousLeaf.getEndOffset());            // Now we move the new LeafElement and all the old children that occurred      // after the offset to the new paragraph.  (Step 4).      Element[] newLeaves = new Element[numReplaced];      newLeaves[0] = firstLeafInNewBranch;      for (int i = 1; i < numReplaced; i++)        newLeaves[i] = previous.getElement(previousIndex + i);      newBranch.replace(0, 0, newLeaves);                  // Now we remove the children after the offset from the previous       // paragraph. (Step 3).      int removeSize = previous.getElementCount() - previousIndex;      Element[] remove = new Element[removeSize];      for (int j = 0; j < removeSize; j++)        remove[j] = previous.getElement(previousIndex + j);      Edit edit = getEditForParagraphAndIndex(previous, previousIndex);      edit.addRemovedElements(remove);            // Finally we add the new paragraph to the parent. (Step 5).      int index = parentIndex + 1;      Edit edit2 = getEditForParagraphAndIndex(parent, index);      edit2.addAddedElement(newBranch);      elementStack.push(newBranch);    }