如何在启动E4应用程序后以编程方式切换透视图?

时间:2016-07-06 14:30:00

标签: java eclipse-rcp e4

方案

我有一个纯E4应用程序,我想根据用户的角色为用户选择初始透视图。因此,我有一个开始的视角,其中只包含一个部分。在那部分中,我使用@ PostConstruct-Method检查用户的角色,然后触发切换透视的命令:

初始视图

@Inject
private IEclipseContext eclipseContext;

@PostConstruct
public void initialize() {
  // checking credentials and retrieving roles come here which is pretty long
  // that's why switching perspective is a seperate method 
  // and EclipseContext is injected to instance instead of method
  this.switchPerspective(_usersInitialPerspectiveId)
}

private void switchPerspective(String pTargetPerspectiveId) {
  final ECommandService _commandService = this.eclipseContext.get(ECommandService.class);
  final EHandlerService _handlerService = this.eclipseContext.get(EHandlerService.class);

  final Map<String, Object> _commandParameter = new HashMap<>();
  _commandParameter.put(PluginIdConstants.ID_OF_PARAMETER_FOR_SWITCH_PERSPEKTIVE,
     pZielPerspektiveId);

  final ParameterizedCommand _switchPerspectiveCommand =
     _commandService.createCommand(COMMAND_ID_FOR_SWITCH_PERSPECTIVE,
        _commandParameter);
  _handlerService.executeHandler(_switchPerspectiveCommand);
}

为了从这里切换透视图,我使用与Application.e4xmi中配置的菜单项完全相同的处理程序,如下所示:

透视切换处理程序

@Execute
public void execute(final MWindow pWindow,
                    final EPartService pPartService,
                    final EModelService pModelService,
                    @Named(PluginIdConstants.ID_OF_PARAMETER_FOR_SWITCH_PERSPEKTIVE)
                    final String pPerspectiveId) {

  final List<MPerspective> _perspectives =
       pModelService.findElements(pWindow, pPerspectiveId, MPerspective.class, null);
  if (!(_perspectives.isEmpty())) {
     // Show perspective for looked up id
     pPartService.switchPerspective(_perspectives.get(0));
  }
}

问题

问题非常简单:当使用由菜单项触发的上述处理程序时,它按预期工作并切换透视图。但是我的初始视图使用相同的处理程序(以编程方式触发)不会切换透视图。我调试了代码以检查处理程序是否在两种情况下都获得了相同的信息。

也许我的应用程序还没有完成启动,这就是为什么处理程序没有效果,但如果这是问题,我该如何检查?

欢迎任何关于我可能错过的想法!

1 个答案:

答案 0 :(得分:1)

根据Christoph Keimel的提示,我可以创建一个可行的解决方案(非常感谢!)。这是解决问题的代码:

@ProcessAdditions
private void switchPerspective(final MApplication pApplication,
                               final IApplicationContext pApplicationContext,
                               final EModelService pModelService) {

  final MWindow _window =
     (MWindow) pModelService.find(PluginIdConstants.WINDOW_ID_FOR_MAIN, pApplication);

  final String _appName = pApplicationContext.getBrandingName();
  initializeWindowTitle(_window, _appName);

  final MPerspectiveStack pPerspectiveStack =
     (MPerspectiveStack) pModelService.find(PluginIdConstants.PERSPECTIVE_STACK_ID_FOR_MAIN,
        pAnwendung);

  for (final MPerspective _perspective : pPerspectiveStack.getChildren()) {
     if (_perspektive.getElementId().equalsIgnoreCase(this.startingPerspectiveId)) {
        pPerspectiveStack.setSelectedElement(_perspective);
        break;
     }
  }
}

关于如何注册LifeCycleHandler,您可以查看Lars Vogel's Tutorial

找到此解决方案的主要问题是如何访问透视图堆栈。由于在使用ProcessAdditions注释的方法运行时UI未启动,我必须通过MApplication类型访问应用程序模型 - 这是我的应用程序模型的根元素。结合EModelService,我可以访问我想要的所有UI元素并相应地操作它们。 注入任何UI元素(如MPerspectiveStackMWindow)会导致跳过的方法,因为这些因为尚未初始化而导致空值。

相关问题