Support free tutorials:











vogella training Training Books



Register for the Eclipse 4 application life cycle - Tutorial

Lars Vogel

Version 6.9

09.07.2013

Revision History
Revision 0.1 14.02.2009 Lars
Vogel
created
Revision 0.2 - 6.9 16.02.2009 - 09.07.2013 Lars
Vogel
bug fixes and enhancements

Eclipse e4

This tutorial describes how to use an life cycle handler for Eclipse RCP application.


Table of Contents

1. Eclipse application life cycle
1.1. Connecting to the Eclipse application life cycle
1.2. Accessing application startup parameters
1.3. Close static splash screen
1.4. How to implement a life cycle class
1.5. Example life cycle implementation
2. Life cycle hook and a login screen
2.1. Target
2.2. Create new class
2.3. Register life cycle hook
2.4. Validate
3. Learn more about Eclipse 4 RCP development
4. Support free vogella tutorials
4.1. Thank you
4.2. Questions and Discussion
5. Links and Literature
5.1. Source Code
5.2. Eclipse 4

Get the book Eclipse IDE book

1. Eclipse application life cycle

1.1. Connecting to the Eclipse application life cycle

If an application is started or stopped, it typically requires some central setup or teardown. For example you want to connect to a database or close the database connection.

The Eclipse platform allows you to register a class for predefined events of this life cycle. For example you can use these life cycle hooks to create a login screen or an interactive splash screen before the application is started.

1.2. Accessing application startup parameters

The IApplicationContext object which can get injected into your life cycle class contains information about the startup parameters of your application. You can access these parameters via the getArguments() method.

1.3. Close static splash screen

If you configured in your product configuration file that a static splash screen should be used, you can call the applicationRunning() method on the IApplicationContext object. You typically call this method if you want to replace the static splash screen with a dynamic screen developed with SWT.

1.4. How to implement a life cycle class

The org.eclipse.core.runtime.product extension point allows you to define a class as life cycle callback via a property.

The key for this property is: lifeCycleURI and it points to the class via the bundleclass:// schema.

In the class you can annotate methods with the following annotations. These methods are called by the framework depending on the life cycle of your application.

Table 1. Life cycle annotations

Annotation Description
@PostContextCreate Is called after the Application’s IEclipseContext is created, can be used to add objects, services, etc. to the context. This context is created for the MApplication class.
@ProcessAdditions Is called directly before the model is passed to the renderer, can be used to add additional elements to the model.
@ProcessRemovals Same as @ProcessAdditions but for removals.
@PreSave Is called before the application model is saved. You can modify the model before it is persisted.


1.5. Example life cycle implementation

The following example shows how to register a class in your plugin.xml file as life cycle handler.

<?xml version="1.0" encoding="UTF-8"?>
<plugin>

   <extension
         id="product"
         point="org.eclipse.core.runtime.products">
      <product
        name="testing"
        application="org.eclipse.e4.ui.workbench.swt.E4Application">
         <property
            name="appName"
            value="testing">
         </property>
         <property
           name="applicationXMI"
           value="testing/Application.e4xmi">
         </property>
         <property
           name="applicationCSS"
           value="platform:/plugin/testing/css/default.css">
         </property>
         <property
           name="lifeCycleURI"
           value="bundleclass://testing/testing.LifeCycleManager">
         </property>
      </product>
   </extension>

</plugin> 

The following class displays a Shell until the startup process of your application has finished. You could extend this example to show a progress bar.

package com.example.e4.rcp.todo.lifecycle;

import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.e4.ui.workbench.UIEvents;
import org.eclipse.e4.ui.workbench.lifecycle.PostContextCreate;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;

// for a extended example see
// https://bugs.eclipse.org/382224

public class LifeCycleManager {
  @PostContextCreate
  void postContextCreate(final IEventBroker eventBroker, IApplicationContext context) {
    final Shell shell = new Shell(SWT.TOOL | SWT.NO_TRIM);

    // register for startup completed event and close the shell 
    eventBroker.subscribe(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE,
        new EventHandler() {
          @Override
          public void handleEvent(Event event) {
            shell.close();
            shell.dispose();
            System.out.println("Closed");
            eventBroker.unsubscribe(this);
          }
        });
    // close static splash screen
    context.applicationRunning();
    shell.open();
  }
} 

2. Life cycle hook and a login screen

2.1. Target

In this exercise you create a splash screen which allows the user to enter his or her credentials. This assumes that you have created the PasswordDialog class in ???.

2.2. Create new class

The following development is done again in the com.example.e4.rcp.todo plug-in.

Create the com.example.e4.rcp.todo.lifecycle package and add the following class.

package com.example.e4.rcp.todo.lifecycle;

import org.eclipse.e4.ui.workbench.lifecycle.PostContextCreate;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;

import com.example.e4.rcp.todo.dialogs.PasswordDialog;

// for a extended example see
// https://bugs.eclipse.org/382224
public class Manager {
  @PostContextCreate
  void postContextCreate(IApplicationContext appContext, Display display) {
    final Shell shell = new Shell(SWT.TOOL | SWT.NO_TRIM);
    PasswordDialog dialog = new PasswordDialog(shell);

    // close the static splash screen
    appContext.applicationRunning();

    // position the shell
    setLocation(display, shell);

    if (dialog.open() != Window.OK) {
      // close the application
      System.exit(-1);
    }
  }

  private void setLocation(Display display, Shell shell) {
    Monitor monitor = display.getPrimaryMonitor();
    Rectangle monitorRect = monitor.getBounds();
    Rectangle shellRect = shell.getBounds();
    int x = monitorRect.x + (monitorRect.width - shellRect.width) / 2;
    int y = monitorRect.y + (monitorRect.height - shellRect.height) / 2;
    shell.setLocation(x, y);
  }
} 

Note

The life cycle annotations are is not yet released as official API. See ???.

2.3. Register life cycle hook

Register the Manager class as life cycle handler in your plugin.xml file.

For this, open the plugin.xml in your application plug-in and select the Extensions tab.

Open the org.eclipse.core.runtime.products extension, right click on the existing entry and create a new property.

Lifecycle registration in the product extension part 1

Enter lifeCycleURI as name for the property and use bundleclass://com.example.e4.rcp.todo/com.example.e4.rcp.todo.lifecycle.Manager as the value.

Life cycle registration in the product extension part 1

The following snippet shows the resulting plugin.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<plugin>
   <extension  
         id="product"
         point="org.eclipse.core.runtime.products">
      <product
        name="testing"
        application="org.eclipse.e4.ui.workbench.swt.E4Application">
         <property
               name="appName"
               value="to-do">
         </property>
 <property
    name="lifeCycleURI"
  value=
"bundleclass://com.example.e4.rcp.todo/com.example.e4.rcp.todo.lifecycle.Manager">
  </property>
      </product>
   </extension>

</plugin> 

2.4. Validate

Start your application. The login dialog should get displayed and your application should be started after the user selects the Login button.

3. Learn more about Eclipse 4 RCP development

I hope you enjoyed this tutorial. You find this tutorial and much more information also in the Eclipse 4 RCP book from this author.

4. Support free vogella tutorials

Maintaining high quality free online tutorials is a lot of work. Please support free tutorials by donating or by reporting typos and factual errors.

4.1. Thank you

Please consider a contribution if this article helped you.

Flattr this

4.2. Questions and Discussion

If you find errors in this tutorial, please notify me (see the top of the page). Please note that due to the high volume of feedback I receive, I cannot answer questions to your implementation. Ensure you have read the vogella FAQ as I don't respond to questions already answered there.

5. Links and Literature

5.1. Source Code

Source Code of Examples