content.remove(0, content.length());  buffer = new ElementBuffer(createDefaultRoot());  insert(0, data);
int len = getLength(); if (len > 0) remove(0, len);   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);      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));  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);   root.replace(0, removed.length, added); ev.addEdit(new ElementEdit(root, 0, removed, added));  fireInsertUpdate(ev); fireUndoableEditUpdate(new UndoableEditEvent(this, ev));
  protected void create(ElementSpec[] data)  {    try      {        // Clear content.        content.remove(0, content.length());        // Clear buffer and root element.        buffer = new ElementBuffer(createDefaultRoot());        // Insert the data.        insert(0, data);      }    catch (BadLocationException ex)      {        AssertionError err = new AssertionError("Unexpected bad location");        err.initCause(ex);        throw err;      }  }