我正在创建 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 建议的解决方案,我尝试了应用程序生命周期类中的以下代码。
@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: Application does not have an active window
================更新:================== 将 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 实例中注入(inject) 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
。
更新: 如果您想在事件处理程序中使用注入(inject),则必须使用“ContextInjectionFactory”创建处理程序:
EventHandler handler = ContextInjectionFactory.make(AppStartupCompleteEventHandler.class, context);
其中 context
是 IEclipseContext
。
注意:您不能将其用于非静态内部类,而是使用:
EventHandler handler = new AppStartupCompleteEventHandler();
ContextInjectionFactory.inject(handler, context);
该方法不支持构造函数注入(inject)。
关于java - 以编程方式加载 Eclipse RCP 4 默认透视图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21343933/