tags:

views:

47

answers:

1

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  A: 

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.

rancidfishbreath
I came up with the same workaround for both CTabFolders and Trees :) I expected some flickering, but didn't see any. And luckily enough CTabFolder actually does fire an event before closing a tab :)
Alexey Romanov