对于 SWT/JFace 中缺乏 PreSelection 事件有什么解决方法吗?

发布于 2024-09-04 02:40:09 字数 543 浏览 2 评论 0原文

在我的应用程序中,我希望用户在离开选项卡之前保存所有更改(实现为 CTabFolder)。

我尝试处理 SelectionEvent,但它在选项卡更改后触发(那么为什么它甚至有一个 doit 字段?它会触发吗?在更改其他一些控件之前?)

查看 Bugzilla,我发现 https: //bugs.eclipse.org/bugs/show_bug.cgi?id=193453https://bugs.eclipse.org/bugs/show_bug.cgi?id=193064,这两个问题都没有修复。

由于这个要求可能很常见,有人有解决方法吗?

In my application I want the user to save any changes before he leaves a tab (implemented as CTabFolder).

I tried to handle SelectionEvent, but it fires after the tab has been changed (so why does it even have a doit field? Does it fire before change for some other controls?)

Looking on Bugzilla, I've found https://bugs.eclipse.org/bugs/show_bug.cgi?id=193453 and https://bugs.eclipse.org/bugs/show_bug.cgi?id=193064, neither of which is fixed.

Since this requirement is probably common, does anybody have a workaround?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘亓 2024-09-11 02:40:09

我有一个与 org.eclipse.ui.part.MultiPageEditorPart 一起使用的解决方法,它由 CTabFolder 支持。我将对其进行调整以实现直接的 CTabFolder 实现。

首先使用选择侦听器:

tabFolder.addSelectionListener(new SelectionAdapter() {
    public void widgetSelected(SelectionEvent e) {
        pageChange(tabFolder.indexOf((CTabItem) e.item));
    }
});

然后我像这样实现 pageChange():

protected void pageChange(int newPageIndex) {
    boolean changingPages = this.changingPages;
    this.changingPages = true;
    int oldPageIndex = tabFolder.getSelectionIndex();

    if (isDirty() && !changingPages) {
        tabFolder.setSelection(oldPageIndex);

        if (canChangePages()) {
            tabFolder.setSelection(newPageIndex);
        }
    }

    this.changingPages = false;
}

在 canChangePages() 中,我弹出一个“是否要保存”对话框,并让用户有机会选择“是”、“否”或“取消”。 Yes 保存信息并返回 true。 No 将信息恢复到上次保存的状态并返回 true。取消只是返回 false。您可能只想尝试保存并仅在保存失败时返回 false。

在调用 canChangePages() 之前切换回旧页面可能看起来很奇怪。此调用执行速度很快,因此给人一种标签从未切换过的错觉。无论 canChangePages() 花费多长时间,用户都不会看到选项卡更改,除非该方法批准了。

I have a workaround that works with org.eclipse.ui.part.MultiPageEditorPart which is backed by a CTabFolder. I'll adapt it for a straight CTabFolder implementation.

First off use the selection listener:

tabFolder.addSelectionListener(new SelectionAdapter() {
    public void widgetSelected(SelectionEvent e) {
        pageChange(tabFolder.indexOf((CTabItem) e.item));
    }
});

Then I implement pageChange() like this:

protected void pageChange(int newPageIndex) {
    boolean changingPages = this.changingPages;
    this.changingPages = true;
    int oldPageIndex = tabFolder.getSelectionIndex();

    if (isDirty() && !changingPages) {
        tabFolder.setSelection(oldPageIndex);

        if (canChangePages()) {
            tabFolder.setSelection(newPageIndex);
        }
    }

    this.changingPages = false;
}

In canChangePages() I pop up a do you want to save dialog and give the user an opportunity to select yes, no, or cancel. Yes saves the info and returns true. No reverts the info to the last saved state and returns true. Cancel simply returns false. You may simply want to try saving and return false only if the save fails.

It may look weird that I switch back to the old page before calling canChangePages(). This call executes quickly so it gives the illusion the tab never switched. No matter how long canChangePages() takes the user will not see a tab change unless it is approved by that method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文