简体   繁体   English

以编程方式加载 Eclipse RCP 4 默认透视图

[英]Load Eclipse RCP 4 default perspective programmatically

I am creating eclipse RCP 4.x application.我正在创建 eclipse RCP 4.x 应用程序。 Application consist of multiple perspectives.应用程序由多个视角组成。 I want to open default perspective programmatically depending on some condition.我想根据某些条件以编程方式打开默认透视图。 Below code is capable of loading perspective.下面的代码能够加载透视图。

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

But I can not put this code in method which is annotated with @PostContextCreate.但是我不能把这段代码放在用@PostContextCreate 注释的方法中。 Can you suggest any solution for this?你能为此提出任何解决方案吗?

================ As per solution suggested by Greg, I tried following code in Application Lifecycle class. ================ 根据 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);
}

Now I am getting following error at line partService.switchPerspective(element);现在我在 partService.switchPerspective(element); 行出现以下错误

java.lang.IllegalStateException: Application does not have an active window java.lang.IllegalStateException:应用程序没有活动窗口

================Update:================== Added org.eclipse.osgi.services plugin to dependencies. ================更新:================== 向依赖项添加了 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);

    }
}

However now framework not able to inject MApplication,EPartService and EModelService in AppStartupCompleteEventHandler instance.但是现在框架无法在 AppStartupCompleteEventHandler 实例中注入 MApplication、EPartService 和 EModelService。

If you only want to do this in your life cycle class try putting it in a @ProcessAdditions method rather than @PostContextCreate .如果您只想在生命周期类中执行此操作,请尝试将其放入@ProcessAdditions方法而不是@PostContextCreate @ProcessAdditions runs later in the life cycle just before the model is rendered. @ProcessAdditions在模型渲染之前的生命周期中稍后运行。

Update:更新:

Even @PostAdditions is too early to do some UI operations.甚至@PostAdditions进行一些 UI 操作也为时过早。 You need to wait for the application start complete event.您需要等待应用程序启动完成事件。 You can subscribe to this event using the event broker in the @PostContextCreate method:您可以使用@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 is org.osgi.service.event.EventHandler . EventHandlerorg.osgi.service.event.EventHandler

Update: If you want to use injection in the event handler you must create the handler using `ContextInjectionFactory':更新:如果要在事件处理程序中使用注入,则必须使用“ContextInjectionFactory”创建处理程序:

EventHandler handler = ContextInjectionFactory.make(AppStartupCompleteEventHandler.class, context);

where context is the IEclipseContext .其中contextIEclipseContext

Note: You can't use this for a non-static inner class, instead use:注意:您不能将它用于非静态内部类,而是使用:

EventHandler handler = new AppStartupCompleteEventHandler();

ContextInjectionFactory.inject(handler, context);

This method does not support injection on the constructor.此方法不支持对构造函数的注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM