if (changes != null) {   Element root = getDefaultRootElement(); int start = root.getElementIndex(changes.where); int end = root.getElementIndex(changes.where+changes.length);  if (!(start == 0 && end == 0)) { Element[] removed = new Element[1]; removed[0] = root; Element[] added = new Element[end - start + 1]; for (int i = start; i <= end; i++) added[i - start] = root.getElement(i);  ElementEdit edit = new ElementEdit( root, root.getElementIndex(changes.where), removed, added); event.addEdit(edit); } }
  public void insertString(int offset, String text, AttributeSet attributes)    throws BadLocationException  {    // Just return when no text to insert was given.    if (text == null || text.length() == 0)      return;        DefaultDocumentEvent event =      new DefaultDocumentEvent(offset, text.length(),			       DocumentEvent.EventType.INSERT);        writeLock();    UndoableEdit temp = content.insertString(offset, text);    writeUnlock();        GapContent.UndoInsertString changes = null;    if (content instanceof GapContent)      changes = (GapContent.UndoInsertString) temp;    insertUpdate(event, attributes);    if (changes != null)      {        // We need to add an ElementChange to our DocumentEvent        // so let's set up the parameters        Element root = getDefaultRootElement();        int start = root.getElementIndex(changes.where);        int end = root.getElementIndex(changes.where+changes.length);        if (!(start == 0 && end == 0))          {            Element[] removed = new Element[1];            removed[0] = root;            Element[] added = new Element[end - start + 1];            for (int i = start; i <= end; i++)              added[i - start] = root.getElement(i);            ElementEdit edit = new ElementEdit(                                               root,                                               root.getElementIndex(changes.where),                                               removed, added);            event.addEdit(edit);          }      }    fireInsertUpdate(event);  }