【问题标题】:Eclipse IConsole Caret PositionEclipse IConsole 插入符号位置
【发布时间】:2013-09-19 05:52:54
【问题描述】:

我正在实现一个带有 IOConsole 的 Eclipse 插件,它从键盘接收输入并产生输出(IOConsoleInputStream、IOConsoleOutputStream)。 我试图通过按照此处的建议扩展 TextConsoleViewer 将插入符号始终放在最后一个字符

How to set the Caret of the IOConsole

问题在于,当需要在打印输出后更改插入符号的位置时,由另一个线程写入的输出字符(具有对输出流的引用)不计入控制台字符计数。

这是我的代码的链接

https://code.google.com/p/mdpm/source/browse/com.lowcoupling.mdpm.console/src/com/lowcoupling/mdpm/console/MDPMConsole.java

谢谢

【问题讨论】:

    标签: java eclipse


    【解决方案1】:

    setCaretOffset() 的源代码显示,如果您使用大于文本长度的偏移量,则使用文本长度代替,实际上是在文本末尾放置插入符号。因此将Integer.MAX_VALUE 设置为偏移量是一个可行的选择,无需对文本长度进行任何检查。

    如果您无法收到有关刷新何时真正完成的通知,我建议您将插入符号的放置延迟几百毫秒。它不会分散用户的注意力,并为您提供强大的解决方案。

    供参考,这里是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();
    }
    

    【讨论】:

      猜你喜欢
      • 2014-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-06
      • 1970-01-01
      • 2022-08-15
      • 1970-01-01
      相关资源
      最近更新 更多