Any workarounds to lack of PreSelection events in SWT/JFace?

為{幸葍}努か 提交于 2019-12-24 12:18:38

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/2958279/any-workarounds-to-lack-of-preselection-events-in-swt-jface

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!