title: Eclipse RCP workbench.xmi control of saved parts
body: <p>When an E4 RCP application is closed, changes are recorded/saved in workbench.xmi file.</p>
  
  <p>Within a partstack, several parts may be open at the time when the application is closed.</p>
  
  <p>These parts are tagged as</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;children xsi:type="basic:PartStack"
                  xmi:id="_The_ID"
                  elementId="elemId"
                  contributorURI="URI"
                  containerData="3000"&gt;
    &lt;tags&gt;NoAutoCollapse&lt;/tags&gt;
  &lt;/children&gt;
  </code></pre>
  
  <p>When the application is restarted these parts saved at workbench.xmi are created and displayed again.</p>
  
  <p>How can control these parts during startup the application again in order to allow/not-allow a part to be created?</p>
  
  <p>This is to be used in case that your data model (connected with your part) is saved in a file.</p>
  
  <p>For example, the following sequence:</p>
  
  <p>1.- RCP is closed with some parts open (i.e <em>file-to-remain.xml</em> and <em>file-to-be-deleted.xml</em>)</p>
  
  <p>2.- The user delete the file <em>file-to-be-deleted.xml</em> deleting the data.</p>
  
  <p>3.- RCP is restarted, hence <em>file-to-be-deleted.xml</em> is created and displayed with no data.</p>
  
  <p>The objective is to avoid the creation of <em>file-to-be-deleted.xml</em> in step 3.-</p>
  
  <p>I will extend my explanations after reading greg-449 answer and also his answer in question: "<a href="https://stackoverflow.com/questions/28040312/e4-removing-elements-from-the-application-model">e4 - removing elements from the application model</a>"</p>
  
  <p>I have a workbench.xmi saved containing one PartStack and two Parts (2 xml files) at the time when the applications was closed (file-to-remain.xml, file-to-be-deleted.xml)</p>
  
  <p>Note that the element ID is a string containing the file path. Done at part creation by part.setElementID(String) method.</p>
  
  <p>Also note that the parts are created by a class named AutodocuForm.class</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;children xsi:type="basic:PartStack" 
            xmi:id="_iDPe2cIDEeaAXZB7N2qOIw" 
            elementId="my-plugin.partstack.0" 
            contributorURI="platform:/plugin/my-plugin" 
            containerData="3066" 
            selectedElement="_6pVbwMNsEeaiI_JEbgNbYQ"&gt;
  
            &lt;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"&gt;
           &lt;/children&gt;
  
           &lt;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"&gt;
           &lt;/children&gt;
  &lt;/children&gt;
  </code></pre>
  
  <p>I have created a LifeCycle class:</p>
  
  
  
  <pre class="lang-java prettyprint-override"><code>public class LifeCycleManager {
  
      @ProcessRemovals
      void postContextCreate(IEclipseContext context, MApplication application, EModelService modelService, EPartService partService){
  
          List&lt;MPart&gt; 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&lt;MUIElement&gt; parent = elParte.getParent();
                      elParte.setToBeRendered(false);
                      parent.getChildren().remove(elParte);
  
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>If I delete "file-to-be-deleted.xml" and restart the application, the part is not shown inside the partStack, but I obtain the following exception:</p>
  
  <pre class="lang-java prettyprint-override"><code>!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: org.eclipse.e4.ui.model.application.ui.basic.impl.PartStackImpl@39478c45 
              (elementId: my-plugin.partstack.0, 
                tags: [NoAutoCollapse], 
                contributorURI: platform:/plugin/my-plugin) 
              (widget: CTabFolder {}, 
                renderer: org.eclipse.e4.ui.workbench.renderers.swt.StackRenderer@1b62d923, 
                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)
  ...
  ...
  </code></pre>
  
  <p>It seems to me that the part is deleted from the model, but the Part Stack is not updated.</p>
  
  <p>Thanks in advance</p>