Fixed caret positioning and remove only trailing whitespace on setText