}
// If the user wants to overwrite text...
if (textMode==OVERWRITE_MODE && !"\n".equals(text)) {
Caret caret = getCaret();
int caretPos = caret.getDot();
Document doc = getDocument();
Element map = doc.getDefaultRootElement();
int curLine = map.getElementIndex(caretPos);
int lastLine = map.getElementCount() - 1;
try {
// If we're not at the end of a line, select the characters
// that will be overwritten (otherwise JTextArea will simply
// insert in front of them).
int curLineEnd = getLineEndOffset(curLine);
if (caretPos==caret.getMark() && caretPos!=curLineEnd) {
if (curLine==lastLine)
caretPos = Math.min(caretPos+text.length(), curLineEnd);
else
caretPos = Math.min(caretPos+text.length(), curLineEnd-1);
caret.moveDot(caretPos);//moveCaretPosition(caretPos);
}
} catch (BadLocationException ble) { // Never happens
UIManager.getLookAndFeel().provideErrorFeedback(this);
ble.printStackTrace();