int selectionLength = textInput.getSelectionLength();
TextNode textNode = textInput.getTextNode();
int n = textNode.getCharacterCount();
Bounds leadingSelectionBounds;
if (selectionStart < n) {
leadingSelectionBounds = getCharacterBounds(selectionStart);
} else {
// The insertion point is after the last character
int x;
if (n == 0) {
x = padding.left - scrollLeft + 1;
} else {
Rectangle2D textBounds = glyphVector.getLogicalBounds();
x = (int)Math.ceil(textBounds.getWidth()) + (padding.left - scrollLeft + 1);
}
int y = padding.top + 1;
leadingSelectionBounds = new Bounds(x, y, 0, height - (padding.top + padding.bottom + 2));
}
caret = leadingSelectionBounds.toRectangle();
caret.width = 1;
if (selectionLength > 0) {
Bounds trailingSelectionBounds = getCharacterBounds(selectionStart
+ selectionLength - 1);
selection = new Rectangle(leadingSelectionBounds.x, leadingSelectionBounds.y,
trailingSelectionBounds.x + trailingSelectionBounds.width - leadingSelectionBounds.x,
trailingSelectionBounds.y + trailingSelectionBounds.height - leadingSelectionBounds.y);
} else {