if (offset > 0) { try { String s = getText(offset - 1, 1); if (s.equals("\n")) { int newEl2EndOffset = end; boolean replaceNext = false; if (rootElement.getElementCount() > elementIndex + 1) { replaceNext = true; newEl2EndOffset = rootElement.getElement(elementIndex + 1).getEndOffset(); } Element newEl1 = createLeafElement(rootElement, firstElement.getAttributes(), firstElement.getStartOffset(), offset); Element newEl2 = createLeafElement (rootElement, firstElement.getAttributes(), offset, newEl2EndOffset); if (replaceNext) rootElement.replace(elementIndex, 2, new Element[] { newEl1, newEl2 }); else rootElement.replace(elementIndex, 1, new Element[] { newEl1, newEl2 }); firstElement = newEl2; elementIndex ++; } } catch (BadLocationException ble) {  AssertionError ae = new AssertionError(); ae.initCause(ble); throw ae; } }
  protected void insertUpdate(DefaultDocumentEvent event,                              AttributeSet attributes)  {    int offset = event.getOffset();    int end = offset + event.getLength();    int elementIndex = rootElement.getElementIndex(offset);    Element firstElement = rootElement.getElement(elementIndex);        // added and removed are Element arrays used to add an ElementEdit    // to the DocumentEvent if there were entire lines added or removed.    Element[] removed = new Element[1];    Element[] added;    try       {        String str = content.getString(0, content.length());        ArrayList elts = new ArrayList();        // Determine how many NEW lines were added by finding the newline        // characters within the newly inserted text        int j = firstElement.getStartOffset();        int i = str.indexOf('\n', offset);        while (i != -1 && i <= end)          {                        // For each new line, create a new element            elts.add(createLeafElement(rootElement, SimpleAttributeSet.EMPTY,                                       j, i + 1));            j = i + 1;            if (j >= str.length())              break;            i = str.indexOf('\n', j);          }        // If there were new lines added we have to add an ElementEdit to         // the DocumentEvent and we have to call rootElement.replace to         // insert the new lines        if (elts.size() != 0)          {            // Set up the ElementEdit by filling the added and removed             // arrays with the proper Elements            added = new Element[elts.size()];            for (int k = 0; k < elts.size(); ++k)              added[k] = (Element) elts.get(k);            removed[0] = firstElement;                        // Now create and add the ElementEdit            ElementEdit e = new ElementEdit(rootElement, elementIndex, removed,                                            added);            event.addEdit(e);                        // And call replace to actually make the changes            ((BranchElement) rootElement).replace(elementIndex, 1, added);          }      }    catch (BadLocationException e)      {        // This shouldn't happen so we throw an AssertionError        AssertionError ae = new AssertionError();        ae.initCause(e);        throw ae;      }    super.insertUpdate(event, attributes);  }