Ejemplo n.º 1
0
  /**
   * Ensures that the specified line and offset is visible by scrolling the text area if necessary.
   *
   * @param line The line to scroll to
   * @param offset The offset in the line to scroll to
   * @return True if scrolling was actually performed, false if the line and offset was already
   *     visible
   */
  public boolean scrollTo(int line, int offset) {
    // visibleLines == 0 before the component is realized
    // we can't do any proper scrolling then, so we have
    // this hack...
    if (visibleLines == 0) {
      setFirstLine(Math.max(0, line - electricScroll));
      return true;
    }

    int newFirstLine = firstLine;
    int newHorizontalOffset = horizontalOffset;

    if (line < firstLine + electricScroll) {
      newFirstLine = Math.max(0, line - electricScroll);
    } else if (line + electricScroll >= firstLine + visibleLines) {
      newFirstLine = (line - visibleLines) + electricScroll + 1;
      if (newFirstLine + visibleLines >= getLineCount())
        newFirstLine = getLineCount() - visibleLines;
      if (newFirstLine < 0) newFirstLine = 0;
    }

    int x = _offsetToX(line, offset);
    int width = painter.getFontMetrics().charWidth('w');

    if (x < 0) {
      newHorizontalOffset = Math.min(0, horizontalOffset - x + width + 5);
    } else if (x + width >= painter.getWidth()) {
      newHorizontalOffset = horizontalOffset + (painter.getWidth() - x) - width - 5;
    }

    return setOrigin(newFirstLine, newHorizontalOffset);
  }
Ejemplo n.º 2
0
  protected void documentChanged(DocumentEvent evt) {
    DocumentEvent.ElementChange ch = evt.getChange(document.getDefaultRootElement());

    int count;
    if (ch == null) count = 0;
    else count = ch.getChildrenAdded().length - ch.getChildrenRemoved().length;

    int line = getLineOfOffset(evt.getOffset());
    if (count == 0) {
      painter.invalidateLine(line);
    }
    // do magic stuff
    else if (line < firstLine) {
      setFirstLine(firstLine + count);
    }
    // end of magic stuff
    else {
      painter.invalidateLineRange(line, firstLine + visibleLines);
      updateScrollBars();
    }
  }