Search code examples
eclipse-rcpe4

How to register an e4 lifecycle hook when using the compatibility layer?


How do I register a lifecycle hook when migrating a 3.7 based eclipse application to e4?

With pure e4 I would set the lifeCycleURI property of a product using the product-extension point. This is where the application model is defined as well.

My impression is, that the compatibility layer is in charge of all this, because it uses the legacy application model definition. As a consequence, I simply do not know how to get my lifecycle hook into use.


Solution

  • You can still define the life cycle class using the lifeCycleURI in your product definition provided the application that your product specifies calls PlatformUI.createAndRunWorkbench. This runs the code that deals with the life cycle.

    For example in a simple test RCP. The product is:

    <extension
         id="TestRCPView.product"
         point="org.eclipse.core.runtime.products">
      <product
            application="TestRCPView.application"
            name="TestRCPView">
         <property
               name="lifeCycleURI"
               value="bundleclass://TestRCPView/testrcpview.LifeCycle">
         </property>
      </product>
    </extension>
    

    which has the lifeCycleURI.

    The application it refers to is:

    <extension
         id="TestRCPView.application"
         point="org.eclipse.core.runtime.applications">
      <application>
         <run
               class="testrcpview.Application">
         </run>
      </application>
    </extension>
    

    And the testrcpview.Application

    public class Application implements IApplication
    {
      @Override
      public Object start(final IApplicationContext context)
      {
        Display display = PlatformUI.createDisplay();
        try
         {
           int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
           if (returnCode == PlatformUI.RETURN_RESTART)
             return IApplication.EXIT_RESTART;
    
            return IApplication.EXIT_OK;
         }
        finally
         {
           display.dispose();
         }
      }
    
    
      @Override
      public void stop()
      {
        if (!PlatformUI.isWorkbenchRunning())
          return;
    
        IWorkbench workbench = PlatformUI.getWorkbench();
    
        Display display = workbench.getDisplay();
    
        display.syncExec(new Runnable()
          {
            @Override
            public void run()
            {
              if (!display.isDisposed())
                workbench.close();
            }
          });
      }
    }