Eclipse e4 - 在对话框打开时隐藏部件

时间:2018-06-14 15:13:02

标签: java eclipse-rcp rcp e4

我们目前正在开发纯e4中的应用程序。

我们有一个Use Case,应该打开ProgressMonitorDialog,同时隐藏所有打开的部分。

代码:

IRunnableWithProgress runnable = monitor -> {
    Collection<MPart> allParts = partService.getParts();
    monitor.beginTask("Do Stuff", allParts.size());
    for (MPart part : allParts) {
      partService.hidePart(part);
      monitor.worked(1);
    }
    monitor.done();
  };

ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell);
dialog.setCancelable(false);
dialog.setOpenOnRun(true);
dialog.run(true, false, runnable);

问题是,在执行期间(第一次尝试隐藏部件时)会抛出异常:

java.lang.IllegalStateException: Application does not have an active window
at org.eclipse.e4.ui.internal.workbench.ApplicationPartServiceImpl.getActiveWindowService(ApplicationPartServiceImpl.java:46)
at org.eclipse.e4.ui.internal.workbench.ApplicationPartServiceImpl.hidePart(ApplicationPartServiceImpl.java:145)
at org.eclipse.e4.ui.internal.workbench.ApplicationPartServiceImpl.hidePart(ApplicationPartServiceImpl.java:145)
at mine.lambda$3(Handler.java:100)
at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:37)
at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:182)
at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4213)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3820)
at org.eclipse.jface.operation.ModalContext$ModalContextThread.block(ModalContext.java:165)
at org.eclipse.jface.operation.ModalContext.run(ModalContext.java:369)
at org.eclipse.jface.dialogs.ProgressMonitorDialog.run(ProgressMonitorDialog.java:483)
at mine.showProgressDialog(Helper.java:160)
at mine.execute(Handler.java:82)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:55)
at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:305)
at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:239)
at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:132)
at org.eclipse.e4.core.commands.internal.HandlerServiceHandler.execute(HandlerServiceHandler.java:152)
at org.eclipse.core.commands.Command.executeWithChecks(Command.java:494)
at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:487)
at org.eclipse.e4.core.commands.internal.HandlerServiceImpl.executeHandler(HandlerServiceImpl.java:210)
at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.executeItem(HandledContributionItem.java:431)
at org.eclipse.e4.ui.workbench.renderers.swt.AbstractContributionItem.handleWidgetSelection(AbstractContributionItem.java:446)
at org.eclipse.e4.ui.workbench.renderers.swt.AbstractContributionItem.lambda$2(AbstractContributionItem.java:472)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:86)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4428)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1079)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4238)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3817)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1150)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1039)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:153)
at org.eclipse.e4.ui.internal.workbench.swt.E4Application.start(E4Application.java:162)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:388)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:243)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:653)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:590)
at org.eclipse.equinox.launcher.Main.run(Main.java:1499)
at org.eclipse.equinox.launcher.Main.main(Main.java:1472)

我也尝试使用UiSynchronize运行执行代码,但仍然得到相同的异常。

在对话框打开时,我可以做什么来使用PartService和部件吗?

提前致谢!

1 个答案:

答案 0 :(得分:0)

如果查看堆栈跟踪,您使用的EPartService是应用程序部件服务ApplicationPartServiceImpl。这会尝试将所有内容委托给当前部件中的零件服务 - 但是当对话框打开时,没有当前零件,因此您会收到此错误。

相反,您需要明确获取主窗口的零件服务,例如:

EModelService modelService = ... model service

MApplication application = .... the application

MWindow window = (MWindow)modelService.find("main window id", application);

EPartService partService = window.getContext().get(EPartService.class);