2017-10-05 68 views
0

在一個正常的命令處理程序,我可以添加新的選項卡/一部分,此代碼:的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); 
} 

我無法訪問或全部注入,因爲空的那些服務。爲什麼?我在Activator中注入了我的對象OpenItemEditorHandler2

或者您可以提供一些關於添加新標籤頁/部件的其他解決方案的提示嗎?

非常感謝!

回答

1

EclipseContextFactory.getServiceContext返回的上下文只有OSGi服務,它不包含而不包含包含大部分正常的e4服務,因此您不能使用它來創建您的類。這意味着激活者不適合設置您的訂閱。

你需要設置訂閱的地方,你可以訪問正確的eclipse上下文。 AddOn或RCP LifeCycle可能是合適的。

AddOn構造函數,你可能有:

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

我添加了一個新插件的應用模型。在bundleclass中,我添加了「Activator」中的代碼。但'handleEvent'沒有被調用。關於'LifeCycle',這些代碼位於另一個RCP應用程序的插件中。如何添加LifeCycle?謝謝! – aviit

+0

您只能在主RCP插件中使用LifeCycle。 AddOn中的代碼不應**使用'EclipseContextFactory.getServiceContext',直接注入'IEclipseContext'。查看更新回答 –

+0

謝謝,greg!代碼工作。但是有一個問題:在MyAddon中,我只能將這些服務注入到方法/構造函數中,而將其注入到字段中。 – aviit

相關問題