int cursorAtCharacter(final Location atLocation, final int lineOffset) { final String text = getText(lineOffset); if (text == null) { for (int i = lineOffset; i >= 0; i--) { final String text2 = getText(i); if (text2 != null) { final int at = text2.length(); LOG.debug("character at " + at + " line " + lineOffset); return at; } } } /* * slightly offsetting mouse helps the user position the cursor between * characters near the pointer rather than always after the pointer */ final int x = atLocation.getX() - 3; int at = 0; final int endAt = text.length(); int width = 0; while (at < endAt && x > width) { width += target.getText().charWidth(text.charAt(at)); at++; } LOG.debug("character at " + at + " line " + lineOffset); return at; }
int cursorAtLine(final Location atLocation) { LOG.debug("pointer at " + atLocation); final int y = atLocation.getY(); int lineIndex = displayFromLine + (y / target.getText().getLineHeight()); lineIndex = Math.max(lineIndex, 0); return lineIndex; }