Reputation: 2169
I am implementing an Eclipse Plugin with an IOConsole receiving input from the keyboard and producing output (IOConsoleInputStream, IOConsoleOutputStream). I am trying to put the caret always at the last character by extending the TextConsoleViewer as what suggested here
How to set the Caret of the IOConsole
The problem is that, when it's time to change the caret position after a printed output, the outputted character, which written by another thread having a reference to the output stream, are not counted in the console characters count.
here is the link to my code
thanks
Upvotes: 4
Views: 545
Reputation: 16780
The source code of setCaretOffset()
shows that if you use an offset greater than the length of the text, then the length of the text is used instead, practically placing the caret at the end of the text. So setting Integer.MAX_VALUE
as offset is a viable option without requiring any checks on text length.
If you can't get a notification about when the flushing has really finished, I suggest you delay the placing of the caret by a few hundred milliseconds. It will not be distractive for the user and provides a robust solution for you.
For reference, here is the source code of setCaretOffset()
:
public void setCaretOffset(int offset) {
checkWidget();
int length = getCharCount();
if (length > 0 && offset != caretOffset) {
if (offset < 0) {
offset = 0;
} else if (offset > length) {
offset = length; // <-- use the length as offset
} else {
if (isLineDelimiter(offset)) {
// offset is inside a multi byte line delimiter. This is an
// illegal operation and an exception is thrown. Fixes 1GDKK3R
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
}
}
setCaretOffset(offset, PREVIOUS_OFFSET_TRAILING);
// clear the selection if the caret is moved.
// don't notify listeners about the selection change.
if (blockSelection) {
clearBlockSelection(true, false);
} else {
clearSelection(false);
}
}
setCaretLocation();
}
Upvotes: 2