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

时间:2022-05-27 22:46:42

After building a perspective in application.e4xmi file, I am unable to reset perspective by calling IWorkbenchPage.resetPerspective().

在构建应用程序的透视图之后。e4xmi文件,我无法通过调用IWorkbenchPage.resetPerspective()来重置透视图。

1 个解决方案

#1


4  

I thought this may save others some time, as well as document it for myself.

我认为这样可以节省一些时间,同时也可以为自己记录下来。

The trick to being able to reset an e4 perspective is as follows (assumes a basic application.e4xmi with PerspectiveStack element):

能够重置e4透视图的技巧如下(假设一个基本的应用程序)。e4xmi PerspectiveStack元素):

  1. In your application.e4xmi file, locate your PerspectiveStack under your Application/TrimmedWindow node. Record/set its ID.
  2. 在您的应用程序。e4xmi文件,在应用程序/TrimmedWindow节点下定位透视图。记录/设置它的ID。
  3. In Eclipse 4 Model Editor, drag your Perspective(s) from underneath your PerspectiveStack to Application/Snippets. (This will cause your perspective IDs to register with IPerspectiveRegistry, and provide a pristine state).
  4. 在Eclipse 4模型编辑器中,将透视图从透视图的下方拖动到应用程序/代码片段。(这将导致您的透视图id以iviewtiveregistry注册,并提供一个原始状态)。
  5. Create new CopyPerspectiveSnippetProcessor. This will copy the perspectives in your snippets to your PerspectiveStack on startup. This makes it so you don't have to maintain two copies of each perspective element in your e4xmi file.

    创建新的CopyPerspectiveSnippetProcessor。这将把你的代码片段中的透视图复制到你的透视图上。这样您就不必在您的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;
                    }
                }
            }
        }
    }
    
  6. Register your CopyPerspectiveSnippetProcess into your plugin.xml file.

    在你的插件中注册你的copytivesnippetprocess。xml文件。

    <extension id="MainAppModel" point="org.eclipse.e4.workbench.model">
        <processor beforefragment="false" class="com.example.application.processors.CopyPerspectiveSnippetProcessor"/>
    </extension>
    
  7. Reset the perspective as normal. You will also want to set the perspective stack and the current perspective to visible, as these can sometimes be set to invisible. A sample handler might look like:

    将视角重置为正常。您还需要设置透视堆栈和当前的透视图,因为有时可以将它们设置为不可见的。示例处理程序可能如下所示:

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

#1


4  

I thought this may save others some time, as well as document it for myself.

我认为这样可以节省一些时间,同时也可以为自己记录下来。

The trick to being able to reset an e4 perspective is as follows (assumes a basic application.e4xmi with PerspectiveStack element):

能够重置e4透视图的技巧如下(假设一个基本的应用程序)。e4xmi PerspectiveStack元素):

  1. In your application.e4xmi file, locate your PerspectiveStack under your Application/TrimmedWindow node. Record/set its ID.
  2. 在您的应用程序。e4xmi文件,在应用程序/TrimmedWindow节点下定位透视图。记录/设置它的ID。
  3. In Eclipse 4 Model Editor, drag your Perspective(s) from underneath your PerspectiveStack to Application/Snippets. (This will cause your perspective IDs to register with IPerspectiveRegistry, and provide a pristine state).
  4. 在Eclipse 4模型编辑器中,将透视图从透视图的下方拖动到应用程序/代码片段。(这将导致您的透视图id以iviewtiveregistry注册,并提供一个原始状态)。
  5. Create new CopyPerspectiveSnippetProcessor. This will copy the perspectives in your snippets to your PerspectiveStack on startup. This makes it so you don't have to maintain two copies of each perspective element in your e4xmi file.

    创建新的CopyPerspectiveSnippetProcessor。这将把你的代码片段中的透视图复制到你的透视图上。这样您就不必在您的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;
                    }
                }
            }
        }
    }
    
  6. Register your CopyPerspectiveSnippetProcess into your plugin.xml file.

    在你的插件中注册你的copytivesnippetprocess。xml文件。

    <extension id="MainAppModel" point="org.eclipse.e4.workbench.model">
        <processor beforefragment="false" class="com.example.application.processors.CopyPerspectiveSnippetProcessor"/>
    </extension>
    
  7. Reset the perspective as normal. You will also want to set the perspective stack and the current perspective to visible, as these can sometimes be set to invisible. A sample handler might look like:

    将视角重置为正常。您还需要设置透视堆栈和当前的透视图,因为有时可以将它们设置为不可见的。示例处理程序可能如下所示:

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