在SWT中禁用“Detached” - 动作 - “Wrong parent” - 异常

时间:2015-02-02 15:11:44

标签: java swt eclipse-rcp

我正在使用Eclipse SWT-Application在项目中工作。在所有视图的选项卡上都有默认操作,例如“Detached”-Action,它接受选项卡并为其创建一个分离窗口。 在我们的例子中,这导致以下例外:

    Widget has the wrong parent
java.lang.IllegalArgumentException: Widget has the wrong parent
    at org.eclipse.swt.SWT.error(SWT.java:4064)
    at org.eclipse.swt.SWT.error(SWT.java:3998)
    at org.eclipse.swt.SWT.error(SWT.java:3969)
    at org.eclipse.swt.widgets.Widget.error(Widget.java:468)
    at org.eclipse.swt.widgets.Control.setMenu(Control.java:3159)
    at OUR-APPLICATION - Table
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1077)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1062)
    at org.eclipse.swt.widgets.Widget.showMenu(Widget.java:1463)
    at org.eclipse.swt.widgets.Widget.wmContextMenu(Widget.java:1559)
    at org.eclipse.swt.widgets.Table.windowProc(Table.java:5617)
    at org.eclipse.swt.widgets.Display.windowProc(Display.java:4886)
    at org.eclipse.swt.internal.win32.OS.CallWindowProcW(Native Method)
    at org.eclipse.swt.internal.win32.OS.CallWindowProc(OS.java:2362)
    at org.eclipse.swt.widgets.Table.callWindowProc(Table.java:436)
    at org.eclipse.swt.widgets.Table.callWindowProc(Table.java:430)
    at org.eclipse.swt.widgets.Table.windowProc(Table.java:5691)
    at org.eclipse.swt.widgets.Display.windowProc(Display.java:4873)
    at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
    at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:2459)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3655)
    at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640)
    at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)
    at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2438)
    at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:671)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:664)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
    at OUR-APPLICATION.run(Application.java:154)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at OUR-APPLICATION.Application.start(Application.java:107)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:619)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:574)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1407)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1383)

我知道ENABLE_DETACHED_VIEWS中有一个名为IWorkbenchPreferenceConstants的常量,其默认值为true。

我现在尝试一般禁用“Detached”-Action(也提示如何在一个视图中禁用它)。有没有办法可以覆盖所述常量的默认值,或者从我的选项卡上的右键菜单中删除“Detached”-action的任何其他方式?

感谢您的帮助

编辑: 我们正在使用SWT 3.6

1 个答案:

答案 0 :(得分:0)

好的,我自己发现了。据我所知,你必须延长WorkbenchAdvisor。 在此子类中,您必须覆盖createWorkbenchWindowAdvisor,您可以在其中覆盖IWorkbenchPreferenceConstants,如下所示:

@Override
    public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
        PlatformUI.getPreferenceStore().setValue(IWorkbenchPreferenceConstants.ENABLE_DETACHED_VIEWS, false);
        return new ApplicationWorkbenchWindowAdvisor(configurer);
    }

这将阻止应用程序分离任何视图,并删除右键单击操作“已分离”。

请注意(根据我的发现)此解决方案仅适用于版本3.X