Eclipse RCP 4 在 IEventBroker handleEvent 中添加了新的 tab/part

Eclipse RCP 4 adds new tab/part in an IEventBroker handleEvent

在普通的命令处理程序中,我可以添加新的 tab/part 作为此代码:

@Execute
    public void execute(Shell shell, EPartService partService, MApplication application,EModelService modelService) throws URISyntaxException{
        MPart part = MBasicFactory.INSTANCE.createPart();
        part.setLabel("New file ");
        part.setCloseable(true);
        part.setContributionURI("bundleclass://com.xxx.rcp.app.item_editor/com.xxx.rcp.app.item_editor.parts.ItemEditorPart");
        List<MPartStack> stacks = modelService.findElements(application, "com.xxx.rcp.app.partstack.2", MPartStack.class, null);
        stacks.get(0).getChildren().add(part);
        partService.showPart(part, PartState.ACTIVATE);
    }

现在我想在一个 IEventBroker handleEvent 中添加新的 tab/part。

首先,我在激活器中注册主题:

@Override
    public void start(BundleContext context) throws Exception {
        IEclipseContext serviceContext = EclipseContextFactory.getServiceContext(context);
        IEventBroker eventBroker = serviceContext.get(IEventBroker.class); 
        eventBroker.subscribe("MY_TOPIC", ContextInjectionFactory.make(OpenItemEditorHandler2.class, serviceContext));
}

然后,我在 handleEvent 中添加 tab/part:

public class OpenItemEditorHandler2 implements EventHandler {

//  @Inject 
//  private IEclipseContext serviceContext;

//  @Inject
//  EPartService partService;

//  @Inject
//  MApplication application;

    @Inject
    IEclipseContext serviceContext;

//  @Inject
//  EModelService modelService;

//  @Inject
//  private ECommandService commandService;
//  
//  @Inject
//  private EHandlerService handlerService;

@Override
    public void handleEvent(Event event) {
MPart part = MBasicFactory.INSTANCE.createPart();
        part.setLabel("New file ");
        part.setCloseable(true);
        part.setContributionURI("bundleclass://com.xxx.rcp.app.item_editor/com.xxx.rcp.app.item_editor.parts.ItemEditorPart");
        // get the part stack and show created part 
        EModelService modelService = serviceContext.get(EModelService.class); 
        MApplication application = serviceContext.get(MApplication.class);
        List<MPartStack> stacks = modelService.findElements(application, "com.xxx.rcp.app.partstack.2", MPartStack.class, null);
}

我无法访问或注入这些服务,因为全部为空。为什么?我在 Activator 中注入了我的对象 OpenItemEditorHandler2

或者您能否提供一些关于其他解决方案的提示以添加新的 tab/part?

非常感谢!

EclipseContextFactory.getServiceContext 返回的上下文只有 OSGi 服务,它 包含大部分普通的 e4 服务,所以你不能用它来创建你的 class。这意味着激活器不适合设置您的订阅。

您需要在可以访问适当的 Eclipse 上下文的地方设置订阅。 AddOn 或 RCP LifeCycle 可能是合适的。

AddOn 构造函数中,您可能有:

@Inject
public MyAddon(IEclipseContext context, IEventBroker eventBroker)
{
  eventBroker.subscribe("MY_TOPIC", ContextInjectionFactory.make(OpenItemEditorHandler2.class, context));
}