2013-10-31 88 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

相關問題