2013-10-31 91 views
2

在application.e4xmi文件中构建透视图后,我无法通过调用IWorkbenchPage.resetPerspective()来重置透视图。如何重置Eclipse e4 RCP应用程序的透视图?

+0

你应该写的问题一部分问题,并把答案作为一个答案。然后你可以在24小时后自己接受(这非常好!)。 – oers

+0

感谢您的建议。 – n8n8baby

回答

3

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

诀窍能够重置e4的透视如下(假定与PerspectiveStack元件的基本application.e4xmi):

  1. 在你application.e4xmi文件中找到你的应用在你PerspectiveStack/TrimmedWindow节点。记录/设置其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); 
        } 
    } 
    
+0

我添加了一个测试以防止覆盖持久工作区状态。 – n8n8baby

+0

我发现使用此方法可能会干扰UI元素维度持久性等内容,因此请注意,这可能会产生不可接受的副作用。我们最终能够限制用户界面,从而消除了我们对透视重置的要求。 – n8n8baby

相关问题