【问题标题】:How can I switch perspective programmatically after starting an E4 application?如何在启动 E4 应用程序后以编程方式切换透视图?
【发布时间】:2016-07-11 22:28:54
【问题描述】:

场景

我有一个纯 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));
  }
}

问题

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

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

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

【问题讨论】:

  • 你是说pPartService.switchPerspective 在这两种情况下都会被调用吗?
  • 是的,完全正确。我已经在 Application.e4xmi 中使用相应的命令注册了上述处理程序,以便对具有特定透视 id 作为命令参数的某些菜单项执行。对于相同的视角 id,每个菜单项的开关工作正常,但以编程方式使用的开关不能。
  • 我用 Eclipse IDE 的调试器重新检查了这个,在这两种情况下我都得到了完全相同的 PerspectiveImpl 实例。
  • 以防万一:首先执行程序化切换。当应用程序启动时,我单击用于从 UI 元素切换的菜单项。
  • 我不知道为什么你的方法不起作用。我建议要么使用 AddOn 来选择第一个视角,要么在 @ProcessAdditions 上的 LifeCycleHandler 中进行。方法是找到主 MPerspectiveStack,然后使用 MPerspectiveStack.setSelectedItem 设置初始透视图。这样你就不需要切换它,也不需要你的“假”透视图,因为它在渲染应用模型时已经正确设置。

标签: java eclipse-rcp e4


【解决方案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 都会导致方法被跳过,因为这些方法由于尚未初始化而导致 null 值。

【讨论】:

    猜你喜欢
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    • 2013-11-12
    相关资源
    最近更新 更多