eclipse-rcp - 如何重置 Eclipse e4 RCP 应用程序的透视图?

标签 eclipse-rcp perspective e4

在 application.e4xmi 文件中构建透视图后,我无法通过调用 IWorkbenchPage.resetPerspective() 来重置透视图。

最佳答案

我认为这可能会节省其他人一些时间,并为我自己记录下来。

能够重置 e4 透视图的技巧如下(假设一个基本的 application.e4xmi 带有 PerspectiveStack 元素):

  1. 在您的 application.e4xmi 文件中,在您的 Application/TrimmedWindow 节点下找到您的 PerspectiveStack。记录/设置其 ID。
  2. 在 Eclipse 4 模型编辑器中,将 Perspective(s) 从 PerspectiveStack 下面拖到 Application/Snippets。 (这将使您的透视 ID 注册到 IPerspectiveRegistry,并提供原始状态)。
  3. 创建新的 CopyPerspectiveSnippetProcessor。这将在启动时将您的代码片段中的视角复制到您的 PerspectiveStack 中。这使得您不必在 e4xmi 文件中维护每个透视图元素的两个副本。

    package com.example.application.processors;
    
    import org.eclipse.e4.core.di.annotations.Execute;
    import org.eclipse.e4.ui.model.application.MApplication;
    import org.eclipse.e4.ui.model.application.ui.MUIElement;
    import org.eclipse.e4.ui.model.application.ui.advanced.MPerspective;
    import org.eclipse.e4.ui.model.application.ui.advanced.MPerspectiveStack;
    import org.eclipse.e4.ui.workbench.modeling.EModelService;
    
    /**
     * Copies all snippet perspectives to perspective stack called "MainPerspectiveStack" In order to register/reset perspective and not have to sync two copies in
     * e4xmi.
     * 
     */
    public class CopyPerspectiveSnippetProcessor {
        private static final String MAIN_PERSPECTIVE_STACK_ID = "MainPerspectiveStack";
    
        @Execute
        public void execute(EModelService modelService, MApplication application) {
            MPerspectiveStack perspectiveStack = (MPerspectiveStack) modelService.find(MAIN_PERSPECTIVE_STACK_ID, application);
    
            // Only do this when no other children, or the restored workspace state will be overwritten.
            if (!perspectiveStack.getChildren().isEmpty())
                return;
    
            // clone each snippet that is a perspective and add the cloned perspective into the main PerspectiveStack
            boolean isFirst = true;
            for (MUIElement snippet : application.getSnippets()) {
                if (snippet instanceof MPerspective) {
                    MPerspective perspectiveClone = (MPerspective) modelService.cloneSnippet(application, snippet.getElementId(), null);
                    perspectiveStack.getChildren().add(perspectiveClone);
                    if (isFirst) {
                        perspectiveStack.setSelectedElement(perspectiveClone);
                        isFirst = false;
                    }
                }
            }
        }
    }
    
  4. 将您的 CopyPerspectiveSnippetProcess 注册到您的 plugin.xml 文件中。

    <extension id="MainAppModel" point="org.eclipse.e4.workbench.model">
        <processor beforefragment="false" class="com.example.application.processors.CopyPerspectiveSnippetProcessor"/>
    </extension>
    
  5. 将视角重置为正常。您还需要将透视堆栈和当前透视设置为可见,因为有时可以将它们设置为不可见。示例处理程序可能如下所示:

    import org.eclipse.e4.core.di.annotations.Execute;
    import org.eclipse.e4.ui.model.application.MApplication;
    import org.eclipse.e4.ui.model.application.ui.advanced.MPerspectiveStack;
    import org.eclipse.e4.ui.workbench.modeling.EModelService;
    import org.eclipse.ui.PlatformUI;
    
    public class ResetPerspectiveHandler {
        private static final String MAIN_PERSPECTIVE_STACK_ID = "MainPerspectiveStack";
    
        @Execute
        public void execute(EModelService modelService, MApplication application) {
             MPerspectiveStack perspectiveStack = (MPerspectiveStack) modelService.find(MAIN_PERSPECTIVE_STACK_ID, application);
             PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().resetPerspective();
             perspectiveStack.getSelectedElement().setVisible(true);
             perspectiveStack.setVisible(true);
        }
    }
    

关于eclipse-rcp - 如何重置 Eclipse e4 RCP 应用程序的透视图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19717154/

相关文章:

java - e4 KeyBinding 和处理程序取决于选择

java - Eclipse 3.8(或 Juno)能否在 Java 9 上运行?

css - Visual Studio 中的 webkit 转换

java - 如何从客户支持的角度在后台针对不同的用户组进行票证限制?

java - 如何向 Eclipse e4 RCP 应用程序添加依赖项?

java - 两个处理的项目在e4中使用相同的命令,一个被触发,另一个不被触发

java - 如何在 Eclipse RCP e4 中制作多个窗口

jdbc - Eclipse rcp - 如何加载 jdbc 驱动程序?

java - 如何在 Eclipse RCP 中监听/确定 Activity/最顶层编辑器?

css - 在CSS flip中,TransformZ元素不在最上面