Friday, November 25, 2011

Eclipse RCP

1.1. What is Eclipse RCP

Eclipse was originally started as a modular IDE application. In 2004 the Eclipse 3.0 version was released which supported that the Eclipse platform could be re-used to build standalone applications based on the same technology as the Eclipse IDE.
Eclipse RCP allows developers to use the Eclipse platform to create flexible and extensible desktop applications. Eclipse and Eclipse RCP applications are build upon a plug-in architecture. Plug-ins are the smallest deployable and installable software components of Eclipse.
A plug-in is a collection of files and a configuration file (MANIFEST.MF) which describes the plugin and its dependencies.
This plugin architecture allows Eclipse and Eclipse RCP applications to get extended by third parties.
The following picture shows the content of an example RCP application.

Content of an example RCP project

1.2. Advantages of Eclipse RCP

Eclipse RCP is the basis for the most successful Java IDE and therefore very stable and broadly used. It uses native UI components to provide as much as possible a native look and feel and with its strong modularity approach allow to design component based systems.
Companies like IBM and Google use the Eclipse framework heavily for their products and therefore ensure that Eclipse is flexible, fast and continues to evolve.
Eclipse RCP also fosters a large community of Individuals which provide support, information and extensions to the Eclipse framework. Tapping into this Eco-system allow you to easily find required resources and information you need.

2. Eclipse Plug-in Architecture

2.1. Eclipse IDE vrs. Eclipse RCP

An Eclipse application consists out of plug-ins.
From the Eclipse RCP perspective the Eclipse IDE is only one collection of specific plug-ins. The components of the Eclipse IDE are primary the following.

Primary Eclipse IDE components

An Eclipse RCP application use only parts of these components. An Eclipse RCP application typically uses:

Typical components of an Eclipse RCP application

For a headless Eclipse based applications (without UI) only the runtime is necessary.
The OSGi runtime provides the framework to run the modular application. SWT is the standard UI component library used by Eclipse and JFace provides some convenient API on top of SWT. The workbench provides the framework for your application. The workbench is responsible for displaying all other UI components.

2.2.  Plugins, OSGi, Extension Points

As said Eclipse application are build via a collection of plugins. Plugins define their API and their dependencies. The basis for this architecture is the runtime environment Equinox which is the reference implementation of OSGi. Eclipse uses the term "Plugin" and OSGi uses the term "bundle", but both terms mean the same.
OSGi specifies how Eclipse plugins defines:
  • their API - public classes which can be used by other plugins
  • their dependencies - package or plugins which are required for the plugin to run correctly

OSGi defines also services. Services are dynamic components.
In addition to the OSGi functionality you can also use and define extension-points in Eclipse applications. Extension-points define interfaces for other plug-ins to contribute functionality (code and non-code ).
Another or the same plug-in can use extensions, e.g. provide functionality to these extension points.
Extensions and extension-points are described in the file "plugin.xml". This file is a XML file which provides a user interface for editing this file. The Eclipse IDE provides editors for this file (via the so called Plug-in Development Environment).
Existing extensions (contributions) are collected during the start of an Eclipse RCP application. The information in the extension points is converted into so-called descriptors and stored in registries.

2.3.  Main components of an Eclipse RCP application

The minimal required plug-ins to create and run an minimal Eclipse RCP application (with UI) are the two plugins "org.eclipse.core.runtime" and "org.eclipse.ui". Based on these components an Eclipse RCP application must define the following elements:

  • Main program - A RCP main application class implements the interface "IApplication". This class can be viewed as the equivalent to the main method for standard Java application. Eclipse expects that the application class is defined via the extension point "org.eclipse.core.runtime.application".
  • A Perspective - defines the layout of your application. Must be declared via the extension point "org.eclipse.ui.perspective".
  • Workbench Advisor- invisible technical component which controls the appearance of the application (menus, toolbars, perspectives, etc)

2.4. Configuration files

An Eclipse RCP application has two main configuration files.
  • MANIFEST.MF - contains the OSGi configuration information.
  • plugin.xml - Information about the extensions and extension points

3. Prerequisite

The following assumes that you already have some knowledge in using the Eclipse IDE for standard Java development. See Eclipse IDE Tutorial if you don't have this knowledge.
Is also assume that you are familiar with using the Eclipse update manager. See Eclipse Update Manager to learn how to use it.

4. Installation

4.1. Installation

Browse to Eclipse download site and download the package "Eclipse for RCP and RAP Developers". Extract the download to your harddisk. Avoid having special characters or spaces in the path to Eclipse.

4.2. Update an Eclipse Java IDE

In case you have downloaded the Eclipse Java IDE (or any other non RCP flavor) distribution you can use the Eclipse Update Manager to install the plug-ins required for RCP development.
Install "General Purpose Tools" -> "Eclipse Plug-in Development Environment" and "Eclipse RCP Plug-in Developer Resources" from the Indigo update site. You may have to remove the "Group items by category" flag to see these features.

Update dialog showing how the group by flag can be removed


Selecting the Ecipse RCP development package in the update manager.

5. Tutorial: Create your first RCP application

5.1. Create an RCP application

The following explains how to create a simple RCP application. In Eclipse select File-> New Project. From the list select "Plug-In Project".

Selection the Eclipse Plug-in Wizard

Give your plugin the name "de.vogella.rcp.intro.first" .

First page of the Eclipse Plug-in Wizard specifying the project name.

Press "Next" and make the following settings. As we are going to develop an RCP application, select "Yes" at the question "Would you like to create a rich client application".

Second page of the Eclipse Plug-in Wizard specifying the plug-in ID, version, Name, Activator and the RCP type.

Press next and select the template "Hello RCP" .

Third page of the Eclipse Plug-in Wizard selecting the template "Hello RCP".

Press next and select "Add branding" and press Finish.

Forth page of the Eclipse Plug-in Wizard specifying the branding.

As a result a project with the following project structure will be created. Have a look at the different files especially the Java files to get a first feeling about the project structure.

Screenshot of the created project after the wizard finished

5.2. Start your RCP application

Open the file "MANIFEST.MF" by double-clicking on it. You should see an editor and the tab "Overview" should be selected. Click the link "Launch an Eclipse Application".
Overview tag of the Manifest.mf file highlighting the "Launch an Eclipse RCP application" section
The result should look like the following:
Running example application
Congratulations, you have created and started your first RCP application.

5.3. Convention

In this tutorial we will always create RCP applications. Therefore if the instruction says "Create an RCP project" you should create a new plug-in project with the flag "Would you like to create a rich client application" enabled.

6. Application and Advisor Classes

During the startup of an Eclipse RCP application the Eclipse runtime will evaluate which class is defined via the "org.eclipse.core.runtime.application" extension point. This class is typically called Application and must implement the interface IApplication. This is the equivalent of the main class of standard Java programs. This class is responsible for creating the SWT Display and for starting the event loop for the application.

Display display = PlatformUI.createDisplay();
PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
  

PlatformUI.createAndRunWorkbench() creates and runs a Workbench. The Workbench represents the graphical user interface of Eclipse. The visual part of the Workbench is represented via the class WorkbenchWindow. A Workbench can have several WorkbenchWindows opened. The inner part of the WorkbenchWindow is represented via the class WorkbenchPage.
The Workbench is configured via a class of type "WorkbenchAdvisor". This class defines the initial Perspective and defines the WorkbenchWindowAdvisor class.
WorkbenchWindowAdvisor calls the class ActionBarAdvisor which configures Actions for the Workbench and defines initial attribute for the WorkbenchWindow as initial size, title and visibility of the statusline.

public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
 return new ApplicationActionBarAdvisor(configurer);
}

public void preWindowOpen() {
 IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
 configurer.setInitialSize(new Point(400, 300));
 configurer.setShowCoolBar(false);
 configurer.setShowStatusLine(false);
 configurer.setTitle("Todo"); //$NON-NLS-1$
}
  

For example you could set the initial position of the application via postWindowCreate() method.

// For further info see https://bugs.eclipse.org/bugs/show_bug.cgi?id=84938
@Override
public void postWindowCreate() {
 Shell shell = getWindowConfigurer().getWindow().getShell();
 shell.setLocation(100, 400);
 super.postWindowCreate();
}
  

The ActionBarAdvisor class is as of Eclipse 3.7 not important any more as the Action framework is replaced by the superior command framework.
Each adviser allow to configure certain behavior of the application, e.g. the WorkbenchAdvisor allows to perform certain actions at startup or shutdown by overriding the methods preStartUp() and preShutdown().

7. Run configuration

7.1. Overview

A run configuration in Eclipse defines the environment under which a RCP application will be started. For example it defines compiler flags, plugin (classpath) dependencies etc. Sometimes a run configuration is called "launch configuration". If you start your RCP application a run configuration will be automatically created for you.
To see and edit your run configuration select the file "MANIFEST.MF", right-click on it and select -> Run As -> Run Configurations

How to open the launch configuration menu from the MANIFEST.MF file.

In the field "location" you specify their the files will be created which are necessary to run your RCP application.

Shows the launch configuration, highlighting the location setting

7.2. Check the runtime configuration

On the Plug-ins Tab select "Validate plug-ins prior to launching". This will check if you have all required plugins in your launch configuration. If this check reports that some plugins are missing, try clicking the "Add Required-Plug-Ins" button.

Pressing the "Add required Plug-ins" button in the Eclipse launch configuration


Tip

This may solve errors like "One or more bundles are not resolved because the following root constraints are not resolved" or "java.lang.RuntimeException: No application id has been found."

On the tab Arguments you find the parameter -consoleLog. This option allows to see errors in the RCP application in the console view and is very helpful to identify problems.

-consoleLog in the launch configuration

Other useful parameters are -console (which will give you a OSGI console where you can check the status of your application) and -noExit (which will keep the OSGI console open even if the application ends / crashes).

http://www.vogella.de/articles/EclipseRCP/article.html

No comments:

Post a Comment