if (longestLine == null) findLongestLine(0, element.getElementCount() - 1);
  protected void updateDamage(DocumentEvent changes, Shape a, ViewFactory f)  {    // This happens during initialization.    if (metrics == null)      {        updateMetrics();        preferenceChanged(null, true, true);        return;      }        Element element = getElement();    ElementChange change = changes.getChange(element);    if (changes.getType() == DocumentEvent.EventType.INSERT)      {        // Handles character/line insertion.        // Determine if lines have been added. In this case we repaint        // differently.        boolean linesAdded = true;        if (change == null)          linesAdded = false;        // Determine the start line.        int start;        if (linesAdded)          start = change.getIndex();        else          start = element.getElementIndex(changes.getOffset());            // Determine the length of the updated region.        int length = 0;        if (linesAdded)          length = change.getChildrenAdded().length - 1;        // Update the longest line and length.        int oldMaxLength = (int) maxLineLength;        if (longestLine.getEndOffset() < changes.getOffset()            || longestLine.getStartOffset() > changes.getOffset()                                             + changes.getLength())      {            findLongestLine(start, start + length);      }        else      {            findLongestLine(0, element.getElementCount() - 1);      }        // Trigger a preference change so that the layout gets updated        // correctly.        preferenceChanged(null, maxLineLength != oldMaxLength, linesAdded);        // Damage the updated line range.        int endLine = start;        if (linesAdded)          endLine = element.getElementCount() - 1;        damageLineRange(start, endLine, a, getContainer());                 }    else          {        // Handles character/lines removals.                        // Update the longest line and length and trigger preference changed.        int oldMaxLength = (int) maxLineLength;        if (change != null)          {            // Line(s) have been removed.            findLongestLine(0, element.getElementCount() - 1);            preferenceChanged(null, maxLineLength != oldMaxLength, true);          }        else          {            // No line has been removed.            int lineNo = getElement().getElementIndex(changes.getOffset());            Element line = getElement().getElement(lineNo);            if (longestLine == line)          {                findLongestLine(0, element.getElementCount() - 1);                preferenceChanged(null, maxLineLength != oldMaxLength, false);          }            damageLineRange(lineNo, lineNo, a, getContainer());      }      }  }