Eclipse RCP 4 добавляет новую вкладку / часть в IEventBroker handleEvent

В обычном обработчике команд я могу добавить новую вкладку / часть как этот код:

@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.

Сначала я регистрирую тему в Активаторе:

@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:

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

Я не могу получить доступ к этим службам или внедрить их из-за отсутствия всех Зачем? Я ввел свой объект OpenItemEditorHandler2 в Activator,

Или вы можете дать некоторые советы о других решениях, чтобы добавить новую вкладку / часть?

Большое спасибо!

1 ответ

Решение

Контекст, возвращаемый EclipseContextFactory.getServiceContext имеет только сервисы OSGi, он не содержит большинство обычных сервисов e4, поэтому вы не можете использовать его для создания своего класса. Это означает, что активатор не подходит для настройки вашей подписки.

Вам нужно настроить подписку где-нибудь, где у вас есть доступ к правильному контексту затмения. AddOn или РКП LifeCycle может подойти.

В AddOn Конструктор у вас может быть:

@Inject
public MyAddon(IEclipseContext context, IEventBroker eventBroker)
{
  eventBroker.subscribe("MY_TOPIC", ContextInjectionFactory.make(OpenItemEditorHandler2.class, context));
}
Другие вопросы по тегам