BranchElement root = (BranchElement) getDefaultRootElement(); int start = root.getStartOffset(); int end = root.getEndOffset(); if (position >= end) position = end - 1; else if (position < start) position = start;
Element e = getDefaultRootElement(); while (!e.isLeaf()) e = e.getElement(e.getElementIndex(position));
  public Element getParagraphElement(int position)  {    BranchElement root = (BranchElement) getDefaultRootElement();    int start = root.getStartOffset();    int end = root.getEndOffset();    if (position >= end)      position = end - 1;    else if (position < start)      position = start;    Element par = root.positionToElement(position);    assert par != null : "The paragraph element must not be null";    return par;  }