int lineClicked = Math.min(Math.max((int) (y - rec.y) / metrics.getHeight(), 0), root.getElementCount() - 1);  Element line = root.getElement(lineClicked);  Segment s = getLineBuffer(); int start = line.getStartOffset();  int end = line.getEndOffset() - 1;
int line = Math.abs(((int) y - rec.y) / metrics.getHeight()); if (line >= root.getElementCount()) pos = getEndOffset() - 1; else { Element lineEl = root.getElement(line); if (x < rec.x)  pos = lineEl.getStartOffset(); else if (x > rec.x + rec.width)  pos = lineEl.getEndOffset() - 1; else {
  public int viewToModel(float x, float y, Shape a, Position.Bias[] b)  {    Rectangle rec = a.getBounds();    Document doc = getDocument();    Element root = doc.getDefaultRootElement();        // PlainView doesn't support line-wrapping so we can find out which    // Element was clicked on just by the y-position.        // Since the coordinates may be outside of the coordinate space    // of the allocation area (e.g. user dragged mouse outside    // the component) we have to limit the values.    // This has the nice effect that the user can drag the    // mouse above or below the component and it will still    // react to the x values (e.g. when selecting).    int lineClicked      = Math.min(Math.max((int) (y - rec.y) / metrics.getHeight(), 0),                          root.getElementCount() - 1);        Element line = root.getElement(lineClicked);        Segment s = getLineBuffer();    int start = line.getStartOffset();    // We don't want the \n at the end of the line.    int end = line.getEndOffset() - 1;    try    {      doc.getText(start, end - start, s);    }    catch (BadLocationException ble)    {        AssertionError ae = new AssertionError("Unexpected bad location");        ae.initCause(ble);        throw ae;    }        int pos = Utilities.getTabbedTextOffset(s, metrics, rec.x, (int)x, this, start);    return Math.max (0, pos);  }     