if (removed == null && newElements == null)
int length = 0; if (linesAdded) length = change.getChildrenAdded().length - 1;   int oldMaxLength = (int) maxLineLength; if (longestLine.getEndOffset() < changes.getOffset() || longestLine.getStartOffset() > changes.getOffset() + changes.getLength())
  protected void updateDamage(DocumentEvent changes, Shape a, ViewFactory f)  {    // Return early and do no updates if the allocation area is null    // (like the RI).    if (a == null)      return;        float oldMaxLineLength = maxLineLength;     Rectangle alloc = a.getBounds();    Element el = getElement();    ElementChange ec = changes.getChange(el);        // If ec is null then no lines were added or removed, just     // repaint the changed line    if (ec == null)      {        int line = el.getElementIndex(changes.getOffset());                // If characters have been removed from the current longest line        // we have to find out which one is the longest now otherwise        // the preferred x-axis span will not shrink.        if (changes.getType() == DocumentEvent.EventType.REMOVE            && el.getElement(line) == longestLine)          {            maxLineLength = -1;            if (determineMaxLineLength() != alloc.width)              preferenceChanged(this, true, false);          }                damageLineRange(line, line, a, getContainer());        return;      }        Element[] removed = ec.getChildrenRemoved();    Element[] newElements = ec.getChildrenAdded();        // If no Elements were added or removed, we just want to repaint    // the area containing the line that was modified    if (removed == null && newElements == null)      {        int line = getElement().getElementIndex(changes.getOffset());                damageLineRange(line, line, a, getContainer());        return;      }    // Check to see if we removed the longest line, if so we have to    // search through all lines and find the longest one again.    if (removed != null)      {        for (int i = 0; i < removed.length; i++)          if (removed[i].equals(longestLine))            {              // reset maxLineLength and search through all lines for longest one              maxLineLength = -1;              if (determineMaxLineLength() != alloc.width)                preferenceChanged(this, true, removed.length != newElements.length);                            ((JTextComponent)getContainer()).repaint();                            return;            }      }        // If we've reached here, that means we haven't removed the longest line    if (newElements == null)      {        // No lines were added, just repaint the container and exit        ((JTextComponent)getContainer()).repaint();                return;      }    //  Make sure we have the metrics    updateMetrics();           // If we've reached here, that means we haven't removed the longest line    // and we have added at least one line, so we have to check if added lines    // are longer than the previous longest line            Segment seg = getLineBuffer();    float longestNewLength = 0;    Element longestNewLine = null;        // Loop through the added lines to check their length    for (int i = 0; i < newElements.length; i++)      {        Element child = newElements[i];        int start = child.getStartOffset();        int end = child.getEndOffset() - 1;        try          {            el.getDocument().getText(start, end - start, seg);          }        catch (BadLocationException ex)          {            AssertionError ae = new AssertionError("Unexpected bad location");	    ae.initCause(ex);	    throw ae;          }                        if (seg == null || seg.array == null || seg.count == 0)          continue;                int width = metrics.charsWidth(seg.array, seg.offset, seg.count);        if (width > longestNewLength)          {            longestNewLine = child;            longestNewLength = width;          }      }        // Check if the longest of the new lines is longer than our previous    // longest line, and if so update our values    if (longestNewLength > maxLineLength)      {        maxLineLength = longestNewLength;        longestLine = longestNewLine;      }        // Report any changes to the preferred sizes of the view    // which may cause the underlying component to be revalidated.    boolean widthChanged = oldMaxLineLength != maxLineLength;    boolean heightChanged = removed.length != newElements.length;     if (widthChanged || heightChanged)      preferenceChanged(this, widthChanged, heightChanged);        // Repaint the container    ((JTextComponent)getContainer()).repaint();  }