Eclipse SWT StyledText 在指定位置强制设置插入符

发布于 2025-01-20 02:36:23 字数 1257 浏览 4 评论 0原文

我有一个编辑器实现,它使用 SWT StyledText 作为编辑器区域。 当使用 CTRL+S(不在工具栏上,焦点仍然存在于 StyledText 上)调用保存操作时,插入符号会丢失。因此,StyledText 不会接收下一个键盘输入。 我们需要手动单击所需的位置来激活插入符号并开始在文本框中键入内容。 我对保存前后的选择和焦点做了一些分析。

   `Control beforeFocusControl = Display.getCurrent().getFocusControl();
    IWorkbenchPart beforeActivePart = getActivePart();
    if (beforeFocusControl instanceof StyledText) {
        StyledText sText = (StyledText) beforeFocusControl;
        Cursor oldCursor = sText.getCursor();
    }

    // save the editor
    EditorWindowManager.getInstance().saveEditors(SnippetEditor);

    Control afterFocusControl = Display.getCurrent().getFocusControl();
    IWorkbenchPart afterActivePart = getActivePart();
    // before and after there is no change in selection
    // beforeFocusControl == afterFocusControl
    // beforeActivePart == afterActivePart
    if (afterFocusControl instanceof StyledText) {
        StyledText sText = (StyledText) afterFocusControl;
        //as old focus control was same i tried force setting the cursor and focus
        sText.setCursor(sText.getCursor());
        sText.forceFocus();
    }`

焦点或工作台选择没有变化。因此,我尝试再次设置插入符号并强制聚焦 StyledText,但这并没有将插入符号恢复到上次编辑的位置。

jface 有没有方便的 api 来做到这一点?我知道它丢失之前最后出现的插入符位置。

I have an editor implementation which uses SWT StyledText as an editor area.
When Save Operation is invoked using CTRL+S (not on the toollbar, still focus is present on StyledText) caret is lost. Due to this next keyboard inputs are not received by StyledText.
We need to manually click desired positoin to activate the caret and start typing in the text box.
I did some anyalysis on selection and focus before and after save.

   `Control beforeFocusControl = Display.getCurrent().getFocusControl();
    IWorkbenchPart beforeActivePart = getActivePart();
    if (beforeFocusControl instanceof StyledText) {
        StyledText sText = (StyledText) beforeFocusControl;
        Cursor oldCursor = sText.getCursor();
    }

    // save the editor
    EditorWindowManager.getInstance().saveEditors(SnippetEditor);

    Control afterFocusControl = Display.getCurrent().getFocusControl();
    IWorkbenchPart afterActivePart = getActivePart();
    // before and after there is no change in selection
    // beforeFocusControl == afterFocusControl
    // beforeActivePart == afterActivePart
    if (afterFocusControl instanceof StyledText) {
        StyledText sText = (StyledText) afterFocusControl;
        //as old focus control was same i tried force setting the cursor and focus
        sText.setCursor(sText.getCursor());
        sText.forceFocus();
    }`

There is no change in focus or workbench selection. So i tried to set the caret again and force focus the StyledText but this did not get back the caret to last edited location.

Is there any convenient api in jface to do this ? I know the caret position where it was last present before it was lost.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文