在 E4 中使用 StatusHandler

Using StatusHandler in E4

在E3中,每个class可以处理这样的状态:

IStatus status = new Status(IStatus.WARNING, TestPlugin.PLUGIN_ID, "Hello World");
StatusManager.getManager().handle(status, StatusManager.SHOW);

现在看来有点棘手了。 This documentation 猜测这应该适用于支持注入的 classes:

@Inject Provider<StatusHandler> statusHandler;
@Inject Provider<StatusReportingService> statusReportingService;

public void foo() {
  try {
    // some operation
  } catch (SomeException ex) {
     statusHandler.get().handle(ex, IStatus.ERROR, "Error Message", statusReportingService.get());
  }
}

然而,事实并非如此。由于示例中没有导入(为什么会有?在 java 中只有 4 个 Provider classes),我猜他们的意思是 javax.inject.Provider 并试图注入 org.eclipse.jface.util.StatusHandler:

org.eclipse.e4.core.di.InjectionException: Unable to instantiate public org.eclipse.jface.util.StatusHandler()
    at org.eclipse.e4.core.internal.di.ConstructorRequestor.execute(ConstructorRequestor.java:44)
    at org.eclipse.e4.core.internal.di.InjectorImpl.internalMake(InjectorImpl.java:373)
    at org.eclipse.e4.core.internal.di.InjectorImpl.makeFromProvider(InjectorImpl.java:325)
    at org.eclipse.e4.core.internal.di.ProviderImpl.get(ProviderImpl.java:34)
    at org.acme.project.Main.createStatusHandler(StatusUtil.java:26)

所以也许这不对StatusHandler?如何在 E4 中使用 StatusHandler

看起来这从未实现过。相反,可以注入 org.eclipse.e4.core.services.statusreporter.StatusReporter class 并用于记录或显示错误。

@Inject
StatusReporter statusReporter;

statusReporter.report(status, StatusReporter.SHOW | StatusReporter.LOG);