Eclipse RCP 4在IEventBroker handleEvent中添加了新的选项卡/部件

时间:2017-10-05 10:09:08

标签: eclipse-plugin eclipse-rcp e4

在普通的命令处理程序中,我可以添加新的选项卡/部分作为此代码:

@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中添加新标签/部件。

首先,我在Activator中注册主题:

@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);
}

由于null all,我无法访问或注入这些服务。为什么?我在OpenItemEditorHandler2中注入了对象Activator

或者您可以提供一些关于添加新标签/部分的其他解决方案的提示吗?

非常感谢!

1 个答案:

答案 0 :(得分:1)

EclipseContextFactory.getServiceContext返回的上下文仅包含OSGi服务,包含大多数正常的e4服务,因此您无法使用它来创建您的类。这意味着激活器不适合设置您的订阅。

您需要在可以访问正确的eclipse上下文的地方设置subscribe。 <{1}}或RCP AddOn可能适用。

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

AddOn