[英]Load Eclipse RCP 4 default perspective programmatically
我正在創建 eclipse RCP 4.x 應用程序。 應用程序由多個視角組成。 我想根據某些條件以編程方式打開默認透視圖。 下面的代碼能夠加載透視圖。
@Execute
public void execute(MApplication app, EPartService partService,
EModelService modelService) {
MPerspective element =
(MPerspective) modelService.find("com.sanyotechnologyindia.desktop.app.perspective.enduser", app);
// now switch perspective
partService.switchPerspective(element);
}
但是我不能把這段代碼放在用@PostContextCreate 注釋的方法中。 你能為此提出任何解決方案嗎?
================ 根據 Greg 建議的解決方案,我在 Application Lifecycle 類中嘗試了以下代碼。
@ProcessAdditions
void processAdditions(MApplication app, EPartService partService,
EModelService modelService){
MPerspective element =
(MPerspective) modelService.find("com.sanyotechnologyindia.desktop.app.perspective.usermanagement", app);
// now switch perspective
partService.switchPerspective(element);
}
現在我在 partService.switchPerspective(element); 行出現以下錯誤
java.lang.IllegalStateException:應用程序沒有活動窗口
================更新:================== 向依賴項添加了 org.eclipse.osgi.services 插件。
@PostContextCreate
public void postContextContext(IEventBroker eventBroker)
{
eventBroker.subscribe(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE,
new AppStartupCompleteEventHandler());
}
private class AppStartupCompleteEventHandler implements EventHandler
{
@Inject private MApplication app;
@Inject private EPartService partService;
@Inject private EModelService modelService;
@Override
public void handleEvent(Event arg0) {
MPerspective element =
(MPerspective) modelService.find("com.sanyotechnologyindia.desktop.app.perspective.usermanagement", app);
partService.switchPerspective(element);
}
}
但是現在框架無法在 AppStartupCompleteEventHandler 實例中注入 MApplication、EPartService 和 EModelService。
如果您只想在生命周期類中執行此操作,請嘗試將其放入@ProcessAdditions
方法而不是@PostContextCreate
。 @ProcessAdditions
在模型渲染之前的生命周期中稍后運行。
更新:
甚至@PostAdditions
進行一些 UI 操作也為時過早。 您需要等待應用程序啟動完成事件。 您可以使用@PostContextCreate
方法中的事件代理訂閱此事件:
@PostContextCreate
public void postContextContext(IEventBroker eventBroker)
{
eventBroker.subscribe(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE,
new AppStartupCompleteEventHandler());
}
private class AppStartupCompleteEventHandler implements EventHandler
{
@Override
public void handleEvent(final Event event)
{
// TODO do UI operations here
}
}
EventHandler
是org.osgi.service.event.EventHandler
。
更新:如果要在事件處理程序中使用注入,則必須使用“ContextInjectionFactory”創建處理程序:
EventHandler handler = ContextInjectionFactory.make(AppStartupCompleteEventHandler.class, context);
其中context
是IEclipseContext
。
注意:您不能將它用於非靜態內部類,而是使用:
EventHandler handler = new AppStartupCompleteEventHandler();
ContextInjectionFactory.inject(handler, context);
此方法不支持對構造函數的注入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.