2016-12-15 128 views
0

當E4 RCP應用程序關閉時,更改將記錄/保存在workbench.xmi文件中。Eclipse RCP workbench.xmi控制保存的零件

在零件堆中,應用程序關閉時可能會打開幾個零件。當重新啓動應用程序保存在workbench.xmi這些部件被創建並再次顯示

這些部件被標記爲

<children xsi:type="basic:PartStack" 
       xmi:id="_The_ID" 
       elementId="elemId" 
       contributorURI="URI" 
       containerData="3000"> 
    <tags>NoAutoCollapse</tags> 
</children> 

如何在啓動應用程序時再次控制這些部件以允許/不允許創建零件?

這是爲了防止您的數據模型(與您的零件相連)保存在文件中。

例如,以下序列:

1.- RCP被關閉開有一些部分(即文件到remain.xml文件到待deleted.xml

2.-用戶刪除文件待刪除文件文件刪除數據。

3.- RCP重新啓動,因此待刪除文件文件夾被創建並顯示,沒有數據。

目的是爲了避免創建文件,以待deleted.xml在步驟3 .-

我會讀格雷格-449的答案,也是他在回答問題後延長我的解釋: 「e4 - removing elements from the application model

我有一個workbench.xmi在應用程序關閉時保存了一個PartStack和兩個零件(2個xml文件)(file-to-remaining.xml,待刪除文件)。 xml)

請注意,元素ID是包含文件路徑的字符串。通過part.setElementID(String)方法創建零件。

還要注意的是,部分由一個名爲AutodocuForm.class

<children xsi:type="basic:PartStack" 
      xmi:id="_iDPe2cIDEeaAXZB7N2qOIw" 
      elementId="my-plugin.partstack.0" 
      contributorURI="platform:/plugin/my-plugin" 
      containerData="3066" 
      selectedElement="_6pVbwMNsEeaiI_JEbgNbYQ"> 

      <children xsi:type="basic:Part" 
        xmi:id="_3ZCIocNsEeaiI_JEbgNbYQ" 
        elementId="C:\Users\name\Desktop\file-to-remain.xml" 
        contributorURI="platform:/plugin/my-plugin" 
        contributionURI="bundleclass://my-plugin/my-plugin.autodocu.AutodocuForm" 
        label="file-to-remain.xml" 
        iconURI="platform:/plugin/my-plugin/icons/file_obj.gif" 
        closeable="true"> 
     </children> 

     <children xsi:type="basic:Part" 
        xmi:id="_6pVbwMNsEeaiI_JEbgNbYQ" 
        elementId="C:\Users\name\Desktop\file-to-be-deleted.xml" 
        contributorURI="platform:/plugin/my-plugin" 
        contributionURI="bundleclass://my-plugin/my-plugin.autodocu.AutodocuForm" 
        label="file-to-be-deleted.xml" 
        iconURI="platform:/plugin/my-plugin/icons/file_obj.gif" 
        closeable="true"> 
     </children> 
</children> 

類我創建了一個生命週期類創建:

public class LifeCycleManager { 

    @ProcessRemovals 
    void postContextCreate(IEclipseContext context, MApplication application, EModelService modelService, EPartService partService){ 

     List<MPart> parts = modelService.findElements(application, null, MPart.class, null); 
     for(MPart elParte: parts){ 
      if(elParte.getContributionURI().endsWith("AutodocuForm")){ 
       Path partPath = Paths.get(elParte.getElementId()); 
       if(Files.exists(partPath, LinkOption.NOFOLLOW_LINKS)){ 
        System.out.println("FILE EXISTS INTO THE FILE SYSTEM..."); 
       } 
       else{ 
        System.out.println("FILE DOES NOT EXIST INTO THE FILE SYSTEM..."); 
        MElementContainer<MUIElement> parent = elParte.getParent(); 
        elParte.setToBeRendered(false); 
        parent.getChildren().remove(elParte); 

       } 
      } 
     } 
    } 
} 

如果我刪除「文件將要-deleted.xml「並重新啓動應用程序,partStack內部未顯示該部分,但我獲得以下例外:

!ENTRY org.eclipse.e4.ui.workbench.swt 4 2 2016-12-20 11:09:38.601 
!MESSAGE Problems occurred when invoking code from plug-in: "org.eclipse.e4.ui.workbench.swt". 
!STACK 0 
java.lang.NullPointerException 
at org.eclipse.e4.ui.workbench.renderers.swt.LazyStackRenderer.showTab(LazyStackRenderer.java:160) 
... 
... 

!ENTRY org.eclipse.e4.ui.workbench 4 0 2016-12-20 11:09:38.601 
!MESSAGE Exception occurred while rendering: [email protected]39478c45 
      (elementId: my-plugin.partstack.0, 
       tags: [NoAutoCollapse], 
       contributorURI: platform:/plugin/my-plugin) 
      (widget: CTabFolder {}, 
       renderer: [email protected], 
       toBeRendered: true, 
       onTop: false, 
       visible: true, 
       containerData: 3066, 
       accessibilityPhrase: null) 
!STACK 0 
java.lang.NullPointerException 
at org.eclipse.e4.ui.workbench.renderers.swt.LazyStackRenderer.showTab(LazyStackRenderer.java:160) 
... 
... 

在我看來,零件已從模型中刪除,但零件堆棧未更新。

在此先感謝

+0

僅允許步驟2 RCP應用程序內部。 –

+1

如果將setToBeRendered設置爲false,則可能無需從父項中刪除。 –

+0

它工作正常刪除'parent.getChildren()。remove(elParte);'並添加一些代碼在下面的答案中定義。 –

回答

0

您可以指定-clearPersistedState選項使RCP忽略workbench.xmi,準確地打開如Application.e4xmi描述。

您也可以指定-persistState false停止workbench.xmi首先保存。

在.product文件編輯器的「啓動」選項卡的「程序參數」部分中指定這些參數。

沒有任何支持在啓動過程中恢復模型的一部分。 workbench.xmi只是應用程序模型的副本,與您退出RCP時的副本相同。

如果您有要打開的部件列表,您可以在RCP啓動期間執行此操作,可能是在'應用程序啓動完成'事件中。這很可能是在你的生命週期類中的方法(如果有的話):

@Optional 
@Inject 
public void appStartupComplete(@UIEventTopic(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE) Event event, 
           EPartService partService) 
{ 
    // TODO call partService.showPart for parts to be opened 
} 
+0

如果指定-clearPersistedState,則會刪除文件workbench.xmi。目的是將保存在workbench.xmi中的文件列表保存爲在重新啓動時打開,並使用MPart.getpersistedState()恢復Map

+0

在啓動過程中添加關於啓動部分的註釋回答。 –

+0

在greg-449答案後添加額外的細節 –

0

通過去除parent.getChildren().remove(elParte);異常被解決。 此外,有必要去除部從所述模型(從部分堆棧列表)作爲下一代碼段被示出:

public class LifeCycleManager { 

@ProcessRemovals 
void postContextCreate(IEclipseContext context, MApplication application, EModelService modelService, EPartService partService){ 

    List<MPart> parts = modelService.findElements(application, null, MPart.class, null); 
    for(MPart elParte: parts){ 
     if(elParte.getContributionURI().endsWith("AutodocuForm")){ 
      Path partPath = Paths.get(elParte.getElementId()); 
      if(Files.exists(partPath, LinkOption.NOFOLLOW_LINKS)){ 
       System.out.println("FILE EXISTS INTO THE FILE SYSTEM..."); 
      } 
      else{ 
       System.out.println("FILE DOES NOT EXIST INTO THE FILE SYSTEM..."); 
       MElementContainer<MUIElement> parent = elParte.getParent(); 
       elParte.setToBeRendered(false); 
       //parent.getChildren().remove(elParte); 
       Iterator it = parent.getChildren().iterator(); 
       elParte.setToBeRendered(false); 
       while(it.hasNext()){ 
        MUIElement el = (MUIElement) it.next(); 
        if(el.getElementId().equals(elParte.getElementId())){ 
         //Remove the part from the PartStack list 
         parent.getChildren().remove(el); 
         //Remove the selected element to avoid that the 
         //deleted file was the selected element prior to 
         //deletion, which is stored into the 
         //workbench.xmi file 
         parent.setSelectedElement(null); 
         break; 
        } 
       } 
      } 
     } 
    } 
} 
}