Reputation: 2109
In a normal command handler I can add new tab/part as this code:
@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);
}
Now I want to add new tab/part in one IEventBroker handleEvent.
First, I register the topic in the 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));
}
Then, I add the tab/part in handleEvent:
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);
}
I cannot access or inject those services because of null all. Why? I have injected my object OpenItemEditorHandler2
in the Activator
.
Or can you give some hints about other solutions to add new tab/part?
Thank you very much!
Upvotes: 0
Views: 110
Reputation: 111217
The context returned by EclipseContextFactory.getServiceContext
only has OSGi services, it does not contain most of the normal e4 services so you can't use this to create your class. This means that the activator is not a suitable place to set up your subscribe.
You need to set up the subscribe somewhere where you have access to a proper eclipse context. An AddOn
or the RCP LifeCycle
might be suitable.
In an AddOn
constructor you might have:
@Inject
public MyAddon(IEclipseContext context, IEventBroker eventBroker)
{
eventBroker.subscribe("MY_TOPIC", ContextInjectionFactory.make(OpenItemEditorHandler2.class, context));
}
Upvotes: 1