root.replace(0, removed.length, added); ev.addEdit(new ElementEdit(root, 0, removed, added));
super.insertUpdate(ev, null);
  protected void create(ElementSpec[] data)  {    writeLock();    try      {        // Clear content if there is some.        int len = getLength();        if (len > 0)          remove(0, len);        // Now we insert the content.        StringBuilder b = new StringBuilder();        for (int i = 0; i < data.length; ++i)          {            ElementSpec el = data[i];            if (el.getArray() != null && el.getLength() > 0)              b.append(el.getArray(), el.getOffset(), el.getLength());          }        Content content = getContent();        UndoableEdit cEdit = content.insertString(0, b.toString());        DefaultDocumentEvent ev =          new DefaultDocumentEvent(0, b.length(),                                   DocumentEvent.EventType.INSERT);        ev.addEdit(cEdit);        // We do a little trick here to get the new structure: We instantiate        // a new ElementBuffer with a new root element, insert into that root        // and then reparent the newly created elements to the old root        // element.        BranchElement createRoot =          (BranchElement) createBranchElement(null, null);        Element dummyLeaf = createLeafElement(createRoot, null, 0, 1);        createRoot.replace(0, 0, new Element[]{ dummyLeaf });        ElementBuffer createBuffer = new ElementBuffer(createRoot);        createBuffer.insert(0, b.length(), data, new DefaultDocumentEvent(0, b.length(), DocumentEvent.EventType.INSERT));        // Now the new root is the first child of the createRoot.        Element newRoot = createRoot.getElement(0);        BranchElement root = (BranchElement) getDefaultRootElement();        Element[] added = new Element[newRoot.getElementCount()];        for (int i = 0; i < added.length; ++i)          {            added[i] = newRoot.getElement(i);            ((AbstractElement) added[i]).element_parent = root;          }        Element[] removed = new Element[root.getElementCount()];        for (int i = 0; i < removed.length; ++i)          removed[i] = root.getElement(i);        // Replace the old elements in root with the new and update the event.        root.replace(0, removed.length, added);        ev.addEdit(new ElementEdit(root, 0, removed, added));        fireInsertUpdate(ev);        fireUndoableEditUpdate(new UndoableEditEvent(this, ev));      }    catch (BadLocationException ex)      {        AssertionError err = new AssertionError("Unexpected bad location");        err.initCause(ex);        throw err;      }    finally      {        writeUnlock();      }  }