【发布时间】: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