在我的应用程序中,我希望用户在离开选项卡(实现为CTabFolder)之前保存所有更改.
In my application I want the user to save any changes before he leaves a tab (implemented as CTabFolder).
我尝试处理SelectionEvent,但是在更改选项卡后会触发 (因此,为什么它甚至有一个doit字段?在更改其他控件之前会触发吗? )
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?)
在Bugzilla上,我发现 bugs.eclipse /bugs/show_bug.cgi?id = 193453 和 https ://bugs.eclipse/bugs/show_bug.cgi?id = 193064 ,但两者均未修复.
Looking on Bugzilla, I've found bugs.eclipse/bugs/show_bug.cgi?id=193453 and bugs.eclipse/bugs/show_bug.cgi?id=193064, neither of which is fixed.
由于此要求可能很常见,因此有人有解决方法吗?
Since this requirement is probably common, does anybody have a workaround?
推荐答案我有一个变通办法,可与由CTabFolder支持的org.eclipse.ui.part.MultiPageEditorPart一起使用.我将其修改为直接的CTabFolder实现.
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.
首先使用选择侦听器:
tabFolder.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { pageChange(tabFolder.indexOf((CTabItem) e.item)); } });然后我像这样实现pageChange():
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; }在canChangePages()中,我弹出一个要保存的对话框,并为用户提供选择是,否或取消的机会.是,保存信息并返回true.否将信息恢复到上一次保存的状态并返回true.取消仅返回false.您可能只想尝试保存并仅在保存失败时才返回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.
在调用canChangePages()之前切换回旧页面可能看起来很奇怪.该调用快速执行,因此给选项卡从未切换的错觉.除非canChangePages()花费了多长时间,否则用户不会看到制表符更改,除非该选项卡被该方法批准.
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.
更多推荐
在SWT/JFace中缺少预选事件的任何解决方法?
发布评论