Back to top

vogella training Training Books

Eclipse RCP Tutorial based on the 3.x API

Lars Vogel

Version 7.1

04.01.2013

Revision History
Revision 0.1 - 0.7 14.08.2007 - 03.09.2007 Lars
Vogel
Eclipse RCP with Eclipse 3.3
Revision 0.8 - 7.1 03.11.2008 - 04.01.2013 Lars
Vogel
Several bug fixes and enhancements

Eclipse RCP

This tutorial describes how to develop Eclipse RCP applications. It is based on Eclipse 3.7 (Eclipse Indigo).


Table of Contents

1. Eclipse based Applications
1.1. What are Eclipse RCP applications?
1.2. Advantages of Eclipse based applications
2. Eclipse 3.x vs. Eclipse 4
3. What are Extension Points?
4. Main components of an Eclipse RCP application
5. Prerequisite
6. Installation
6.1. Downloading or upgrading
6.2. Download the Eclipse plug-in package
6.3. Update an Eclipse Java IDE
7. Approach
8. Tutorial: Create your first RCP application
8.1. Create an RCP application
8.2. Start an RCP application
9. Tutorial: Create and Launch Product
9.1. Create your product configuration
9.2. Launch your product
10. Application and Advisor Classes
11. WorkbenchParts and Perspectives
11.1. WorkbenchParts - Views and Editors
11.2. Perspective
12. Convention
13. Tutorial: Adding Views to your application
13.1. Create a view
13.2. Add the view to your perspective
13.3. Result
13.4. Add view to perspective via code
14. SWT
15. JFace Viewers
16. Commands
17. Tutorial: Adding an Perspectives
18. Tutorial: System Tray
19. Workbench API
20. Tips and Tricks
20.1. Load an image from your plugin
20.2. Save users layout
20.3. Accessing the status line
20.4. Finding unused dependencies
20.5. Multi-User settings
21. Reading resources from plug-ins
22. Questions and Discussion
23. Links and Literature
23.1. Source Code
23.2. Eclipse Resources
23.3. vogella Resources

1. Eclipse based Applications

1.1. What are Eclipse RCP applications?

Eclipse was originally started as a modular IDE application. In 2004 Eclipse version 3.0 was released. Eclipse 3.0 supported the re-use of the Eclipse platform to build stand-alone applications based on the same technology as the Eclipse IDE.

At this point the term Eclipse RCP was coined. Eclipse RCP is short for Eclipse Rich Client Platform and indicates that the Eclipse platform is used as a basis to create feature-rich stand-alone applications.

The Eclipse 4 platform simplifies and unifies the Eclipse programming model and extends the concept of building Eclipse based applications with new technologies, like dependency injection and declarative styling via CSS files.

1.2. Advantages of Eclipse based applications

The Eclipse platform forms the basis of the most successful Java IDE and therefore is very stable and broadly used. It uses native user interface components which are fast and reliable and with its strong modularity approach it allows to design component based systems.

Companies such as IBM and Google use the Eclipse framework for their products and therefore ensure that Eclipse is flexible, fast and continues to evolve.

The Eclipse platform also fosters a large community of individuals which provide support, information and extensions to the Eclipse framework. Tapping into this Ecosystem allows you to find required resources and information.

2. Eclipse 3.x vs. Eclipse 4

The following describes how to build Eclipse RCP applications based on the Eclipse 3.x programming model. Eclipse 4 makes significant changes to this programming, please see Eclipse 4 Tutorial for an introduction into the Eclipse 4 programming model.

Currently development with Eclipse 3.x programming model is much more stable and has better tool support then developing for Eclipse 4.x programming model. On the other hand the Eclipse 4 programming model is much more flexible and simpler to use.

3. What are Extension Points?

Extension-points define interfaces for other plug-ins to contribute functionality (code and non-code).

They are defined in the plugin.xml file, which must be in the root directory of your plug-in project.

Existing extensions (contributions) are collected during the start of an Eclipse application.

4. 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)

The Eclipse application is the equivalent of the Java main() method. Applications are defined via the extension point org.eclipse.core.runtime.applications and must extend IApplication.

5. 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 do not 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.

6. Installation

6.1. Downloading or upgrading

To get the required Eclipse tooling for plug-in development you have two options. You can download a special version of Eclipse to develop plug-ins or your can upgrade an existing Eclipse Java IDE.

Both approaches require that you have Java already installed.

6.2. Download the Eclipse plug-in package

Browse to the Eclipse download site and download the Eclipse Classic package.

Download Eclipse Plug-in IDE

Extract the downloaded file to your harddisk. Avoid having special characters or spaces in the path to your extract Eclipse.

6.3. 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.

To open the update manager select HelpInstall new Software....

Install General Purpose Tools Eclipse Plug-in Development Environment Eclipse RCP Plug-in Developer Resources from the Eclipse update site for your release. This would be for example http://download.eclipse.org/releases/kepler for the Eclipse 4.3. release. You may have to remove the Group items by category flag to see all available features.

Update dialog showing how the group by flag can be removed

7. Approach

If you teach programming you basically have two extreme choices. The first approach is that you explain everything first and then do it. The second choice is that you first do everything and then explain what you have done.

The following description tends towards the second approach.

We will develop a small RCP application, create a product and launch it, before explaining the project structure, the involved classes and what a product or a launch configuration is.

In my experience its easier to understand an explanation, if you have already created an example.

The risk associated with that approach is that if you run into a problem you do not know how to solve it.

Therefore if you have problem starting your product, I suggest to read the chapter about launch configurations and if that does not help, check the "Products and Branding" chapter.

8. Tutorial: Create your first RCP application

8.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 plug-in 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.

Fourth 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

8.2. Start an RCP application

While it is possible in Eclipse 3.7 to start an application directly, it is consistent to create a product to start the application. Therefore we will not run the application directly but create first an product.

This is also in line with Eclipse 4. In Eclipse 4 you cannot start an Application directly, you always need a product.

9. Tutorial: Create and Launch Product

9.1. Create your product configuration

Right-click on your "de.vogella.rcp.intro.first" project and select NewProduct Configuration .

Creating the product configuration file

Name your product configuration "de.vogella.rcp.intro.first.product". Select "Create a configuration file with basis settings". Press finish.

Open the file de.vogella.rcp.intro.first.product" and select the Overview tab. Enter the name "DeployTest". Leave the ID empty. The name is the default which will be displayed in the title of the window, the ID is not required and entering it leads sometimes to problems. You can change this default name in class ApplicationWorkbenchWindowAdvisor in the method preWindowOpen() via the configurer.setTitle("New title");)

Select the overview tab of the product file

Press New in the "Product Definition" part and select the application of your plug-in "de.vogella.rcp.intro.first.application".

Selecting the application which should be started by the product

Selecting the application which should be started by the product, part II

As a result the "Product Definition" part of the overview tab should now be filled with your selection.

The product identifier is stored as the extension "org.eclipse.core.runtime.product" in the file plugin.xml.

Switch to the Dependencies Tab and click the Add button. Add your application plug-ins. Press the button "Add Required Plug-ins" to add all dependent plug-ins to your product. Save.

Optional create a "splash.bmp" via your favorite graphics tool and save it in the project with contains the product. Also add a message and a progress bar to your splash screen.

Change the launch name to "myapplication".

9.2. Launch your product

On the overview tab press synchronize and then press "Launch an Eclipse application". Make sure your product starts.

The result should look like the following:

Running example application

Congratulations, you have created and started your first RCP application.

10. 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 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.

This class is typically called Application and must implement the interface IApplication.

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 WorkbenchWindow class. 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 preStartUp() and preShutdown() methods. .

11. WorkbenchParts and Perspectives

11.1. WorkbenchParts - Views and Editors

As described earlier the WorkbenchWindow and the WorkbenchPage represent the visual and inner part of the graphical user interface.

The WorkbenchPage contains Parts, which can be Views or Editors.

Views are used to display information in an RCP application; they can also be used to change data. Views extend the abstract class ViewPart. Editors extend the abstract EditorPart. Both extend WorkbenchPart.

An editor typically requires that the user explicitly select "save" to apply the changes to the data while a view typically changes the data immediately. All editors are opened in the same area. Via the perspective you can configure if the editor area is visible or not.

Views and Editors are defined via extension points in the file plugin.xml via the tab "Extensions".

Views are defined via the extension point "org.eclipse.ui.views" and Editors via the extension point "org.eclipse.ui.editors".

Views must implement the createPartControl() and setFocus() methods.

createPartControl() is used to create the UI components of the View.

createPartControl() will get as parameter a Composite which can be used to construct the user interface. This composite has by default a FillLayout layout manager assigned to it. This layout manager assigns the same space to all components equally.

setFocus() must set the focus to a specific UI component.

11.2. Perspective

A Perspective describes a certain configuration of Views and Editors.

A Perspective is defined via "org.eclipse.ui.perspectives". The WorkbenchParts which are part of a Perspective are either defined via a Java class defined in the extension point "org.eclipse.ui.perspectives" or via the "org.eclipse.ui.perspectiveExtensions".

public class Perspective implements IPerspectiveFactory {
  public void createInitialLayout(IPageLayout layout) {
    layout.addView("de.vogella.rcp.intro.first.MyView", IPageLayout.TOP,
        IPageLayout.RATIO_MAX, IPageLayout.ID_EDITOR_AREA);
  }
} 

layout.addView() adds a view to the perspective. You can also add placeholders for views via the layout.addPlaceholder() method call. This methods accepts wildcards and a View with a matching ID would open in this area. For example if you want to open all views in a specific place you could use the layout.addPlaceholder("*",...) method call, or layout.addPlaceholder("view_id",....) to open a View with the "view_id" ID in this placeholder.

You can also group view via a IFolderLayout which can be created via layout.createFolder() call and by adding Views to this folder via the addView(id) method on IFolderLayout.

12. 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.

13. Tutorial: Adding Views to your application

13.1. Create a view

The following will explain how to add views to your application. We will continue to use the RCP project "de.vogella.rcp.intro.first".

Add the extension "org.eclipse.ui.views" to your plugin. Select "plugin.xml and the tab "Extentions". Right mouse-click on your new view extension and select New -> View. Maintain the id "de.vogella.rcp.intro.view.MyView" and the class "de.vogella.rcp.intro.view.MyView".

Data for the "org.eclipse.ui.views" extension point

Data for the "org.eclipse.ui.views" extension point

Create the class "MyView" by clicking on the "class" hyperlink and maintain the following code. Afterwards your view is ready to be used.

package de.vogella.rcp.intro.first;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

public class MyView extends ViewPart {

  @Override
  public void createPartControl(Composite parent) {
    Text text = new Text(parent, SWT.BORDER);
    text.setText("Imagine a fantastic user interface here");
  }

  @Override
  public void setFocus() {
  }
} 

13.2. Add the view to your perspective

You have to add the view to your perspective. Add the extension "org.eclipse.ui.perspectiveExtensions" to your plugin.

Adding a view to your perspective via the "org.eclipse.ui.perspectiveExtensions" extension

Right-click "*(perspectiveExtension)" -> New -> view". Maintain your view id "de.vogella.rcp.intro.first.MyView". Make the view relative to "org.eclipse.ui.editorss" which is the currently invisible editor area and make the view use all the space by selecting the maximum ratio of "0.95f".

Maintaining the layout data for the view in the "org.eclipse.ui.perspectiveExtensions" extension point

13.3. Result

Run your application to see the result.

Running application with a new view

13.4. Add view to perspective via code

I personally prefer extension points over code. But instead of using the extension point "org.eclipse.ui.perspectiveExtensions" you could also add the view via code to the perspective. For this modify "Perspective.java" to the following.

package de.vogella.rcp.intro.view;

import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;

public class Perspective implements IPerspectiveFactory {

  public void createInitialLayout(IPageLayout layout) {
    layout.addView("de.vogella.rcp.intro.first.MyView", IPageLayout.TOP,
        IPageLayout.RATIO_MAX, IPageLayout.ID_EDITOR_AREA);
  }
} 

14. SWT

SWT is the UI library used by Eclipse. SWT provides the same rich UI components on several platforms using the native widgets of the platform whenever possible. If a widget is not available on a certain platform, SWT emulates the unavailable widget. The native widgets of the OS are accessed by the SWT framework via JNI.

For an introduction into SWT please use the SWT Tutorial.

15. JFace Viewers

JFace provides also viewers which allow to work with Java object directly to create a user interface. Viewers are available for trees, tables, lists and comboboxes. Please see JFaces Tables Tutorial and JFace Tree Viewer Tutorial for a detailed tutorial.

16. Commands

A command is a declarative description of a component and is independent from the implementation details. A command can be categorized and a hot key (key binding) can be assigned to it. Commands can be used in menus, toolbars and / or context menus.

See Eclipse Commands Tutorial for an introduction into the commands framework.

17. Tutorial: Adding an Perspectives

Create a new RCP project called "de.vogella.rcp.intro.perspective". Use the "RCP application with a view" as a template. In plugin.xml add a new extension point "org.eclipse.ui.perspectives". Give the perspective with the id "de.vogella.rcp.intro.perspective.perspective" and the name "vogella.com Perspective". Change the class name to "de.vogella.rcp.intro.perspective.Perspective".

Maintaining the "org.eclipse.ui.perspectives" perspective extension point

Click on the "class*" link to create the class. The createInitialLayout() method in your new class is responsible for creating the new perspective. We re-use the existing view in the coding. After this step the perspective is defined but not yet reachable via the application.

package de.vogella.rcp.intro.perspective;

import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;

public class Perspective implements IPerspectiveFactory {

  public void createInitialLayout(IPageLayout layout) {
    String editorArea = layout.getEditorArea();
    layout.setEditorAreaVisible(false);
    layout.setFixed(true);
    layout.addStandaloneView(View.ID,  false, IPageLayout.LEFT, 1.0f, editorArea);
  }

} 

After defining your perspective you also need to enable your application so that the user can select this perspective.

One alternative is to allow the user to select the perspective via the toolbar / coolbar. You can activate the switch between perspectives the ApplicationWorkbenchWindowAdvisor in method preWindowOpen() with configurer.setShowPerspectiveBar(true);

package de.vogella.rcp.intro.perspective;

import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

  public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
    super(configurer);
  }

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

  public void preWindowOpen() {
    IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
    configurer.setInitialSize(new Point(400, 300));
    configurer.setShowStatusLine(false);
    configurer.setTitle("RCP Application");
    configurer.setShowPerspectiveBar(true);
  }
} 

You should now be able to select your perspective interactively.

Selecting the perspective in the running application

In addition you could re-use the Eclipse command "org.eclipse.ui.perspectives.showPerspective" which allows to switch between perspectives. See Eclipse Commands.

18. Tutorial: System Tray

The following add an icon for the RCP application to the system tray and adds a menu to this icon. We add the functionality that if the window is minimized then the program is not visible in the taskpane (only via the tray icon).

Taskpane showing the application icon

Create a new project "de.vogella.rcp.intro.traytest". Use the "Hello RCP" as a template. Open the class "ApplicationWorkbenchWindowAdvisor" and maintain the following code.

package de.vogella.rcp.intro.traytest;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuDetectEvent;
import org.eclipse.swt.events.MenuDetectListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

  private IWorkbenchWindow window;
  private TrayItem trayItem;
  private Image trayImage;

  public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
    super(configurer);
  }

  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("Hello RCP"); //$NON-NLS-1$
  }

  // As of here is the new stuff
  @Override
  public void postWindowOpen() {
    super.postWindowOpen();
    window = getWindowConfigurer().getWindow();
    trayItem = initTaskItem(window);
    // Some OS might not support tray items
    if (trayItem != null) {
      minimizeBehavior();
      // Create exit and about action on the icon
      hookPopupMenu();
    }
  }

  // Add a listener to the shell

  private void minimizeBehavior() {
    window.getShell().addShellListener(new ShellAdapter() {
      // If the window is minimized hide the window
      public void shellIconified(ShellEvent e) {
        window.getShell().setVisible(false);
      }
    });
    // If user double-clicks on the tray icons the application will be
    // visible again
    trayItem.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent e) {
        Shell shell = window.getShell();
        if (!shell.isVisible()) {
          window.getShell().setMinimized(false);
          shell.setVisible(true);
        }
      }
    });
  }

  // We hook up on menu entry which allows to close the application
  private void hookPopupMenu() {
    trayItem.addMenuDetectListener(new MenuDetectListener() {

      @Override
      public void menuDetected(MenuDetectEvent e) {
        Menu menu = new Menu(window.getShell(), SWT.POP_UP);
        // Creates a new menu item that terminates the program
        MenuItem exit = new MenuItem(menu, SWT.NONE);
        exit.setText("Goodbye!");
        exit.addSelectionListener(new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            window.getWorkbench().close();
          }
        });
        // We need to make the menu visible
        menu.setVisible(true);
      }
    });
  }

  // This methods create the tray item and return a reference
  private TrayItem initTaskItem(IWorkbenchWindow window) {
    final Tray tray = window.getShell().getDisplay().getSystemTray();
    TrayItem trayItem = new TrayItem(tray, SWT.NONE);
    trayImage = Activator.getImageDescriptor("/icons/alt_about.gif")
        .createImage();
    trayItem.setImage(trayImage);
    trayItem.setToolTipText("TrayItem");
    return trayItem;

  }

  // We need to clean-up after ourself
  @Override
  public void dispose() {
    if (trayImage != null) {
      trayImage.dispose();
    }
    if (trayItem != null) {
      trayItem.dispose();
    }
  }

} 

Run your application and see that you have a system tray icon. Test the menu and the minimized behavior. If the application is minimized it should not be visible in the taskbar but only in the system tray.

Showing the application icon in the taskpane

19. Workbench API

To interact with the Workbench you have access to the Workbench API. For example you can access the active editor or view and open or close Views and Editors.

Most API calls are done via the WorkbenchPage.

A command handler can get access to the WorkbenchPage via:

IWorkbenchPage page = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage(); 

A view provides access to the Workbench and the WorkbenchWindow via the getViewSite() method. An editor provides the same access via getEditorSite().

IWorkbenchPage page = getViewSite().getPage(); 

IWorkbenchPage page = getEditorSite().getPage(); 

You could also get the Workbench page over the PlatformUI Singleton.

IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage() 

Once you have access to the Page you can call its API. The following demonstrates how to first close all views in the running application and how to open a View with a specific ID.

IViewReference[] views = page.getViewReferences();
  for (IViewReference view : views) {
    page.hideView(view);
  }
  try {
    page.showView(View.ID);
  } catch (PartInitException e) {
    e.printStackTrace();
  }
return null; 

The following shows how to save an editor and how to close it.

IEditorPart editorPart = page.getActiveEditor();
    if (editorPart != null) {
        // Save the editor without confirmation
        page.saveEditor(editorPart, false);
        // Close the editor and save before saving
        page.closeEditor(editorPart, true);
    } 

You can also add a PartListener to the page and listen for change in View and Editors. Here is an example how to add such a listener in ApplicationWorkbenchWindowAdvisor.

@Override
  public void postWindowOpen() {
    super.postWindowOpen();
    IWorkbenchPage page = PlatformUI.getWorkbench()
        .getActiveWorkbenchWindow().getActivePage();

    // Register a part listener for a certain View / Part

    page.addPartListener(new IPartListener2() {
      @Override
      public void partVisible(IWorkbenchPartReference partRef) {
        System.out.println("Part visible: " + partRef.getId());
      }

      @Override
      public void partOpened(IWorkbenchPartReference partRef) {
        System.out.println("Part opened: " + partRef.getId());
      }

      @Override
      public void partInputChanged(IWorkbenchPartReference partRef) {
      }

      @Override
      public void partHidden(IWorkbenchPartReference partRef) {
        System.out.println("Part hidden: " + partRef.getId());
      }

      @Override
      public void partDeactivated(IWorkbenchPartReference partRef) {
        System.out.println("Part deactivated:" + partRef.getId());
      }

      @Override
      public void partClosed(IWorkbenchPartReference partRef) {
        System.out.println("Part closed:" + partRef.getId());
      }

      @Override
      public void partBroughtToTop(IWorkbenchPartReference partRef) {
        System.out.println("Part top:" + partRef.getId());
      }

      @Override
      public void partActivated(IWorkbenchPartReference partRef) {
        System.out.println("Part activated: " + partRef.getId());
      }
    });
  } 

20. Tips and Tricks

20.1. Load an image from your plugin

To load an image from your plug-in your use the AbstractUIPlugin.

AbstractUIPlugin.imageDescriptorFromPlugin(???YourPluginId???,???/icons/your.gif???).createImage(); 

Alternatively you could load the framework directly.

Bundle bundle = FrameworkUtil.getBundle(this.getClass());
URL url = FileLocator.find(bundle, 
  new Path("icons/alt_window_32.gif"), null);
Image image = ImageDescriptor.createFromURL(url).createImage(); 

20.2. Save users layout

To remember the user's layout and window size for the next time you start your application, add configurer.setSaveAndRestore(true); to the initialize method of ApplicationWorkbenchAdvisor.

package addactiontoview;

import org.eclipse.ui.application.IWorkbenchConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchAdvisor;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {

  private static final String PERSPECTIVE_ID = "AddActiontoView.perspective";

  public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
    return new ApplicationWorkbenchWindowAdvisor(configurer);
  }

  public String getInitialWindowPerspectiveId() {
    return PERSPECTIVE_ID;
  }

  @Override
  public void initialize(IWorkbenchConfigurer configurer) {
    super.initialize(configurer);
    configurer.setSaveAndRestore(true);
  }

} 

Eclipse has a pre-defined command to reset the perspective. See Eclipse Commands .

20.3. Accessing the status line

The status line in an RCP application can be used to give the user some information about the running application. The shared message area can be used by all parts of the application to write messages to this area. The whole RCP application has access to the information in the shared status line therefore the information in the shared status line might be overwritten.

To show the status line in your RCP application use the "ApplicationWorkbenchWindowAdvisor" and set the status line visible in the method preWindowOpen().

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
    //...
    @Override
    public void preWindowOpen() {
      // ....
        configurer.setShowStatusLine(false);
    }
} 

The following writes a text to the status line from a view.

IStatusLineManager manager = getViewSite().getActionBars().getStatusLineManager();
manager.setMessage("Information for the status line"); 

From an editor you can access the status line via the following:

IEditorPart.getEditorSite().getActionBars(); 

20.4. Finding unused dependencies

In the file plugin.xml (tab dependencies) you define on which plugins your plug-in depends. Of course you should only define the required plugins here. You can check if you have any dependency maintained which is actually not used, by selecting Dependency Analysis -> Find unused dependencies.

How to remove unused dependencies

20.5. Multi-User settings

Eclipse RCP applications save configuration files in the folder ".metadata". In the standard settings the Eclipse RCP installation directory will be used for this folder. If several users are using the same installation folder, then you should supply the parameter "-data" to specify an alternative location. If you specify the value "@user.home/applicationname" the configuration will be saved in a user specific folder.

21. Reading resources from plug-ins

You can access files in your plug-in via two different approaches: via the FileLocator class or via an Eclipse specific URL. Both approaches require that your plug-in defines a dependency to the org.eclipse.core.runtime plug-in.

Both approaches return a URL which can be used to get an InputStream.

The following code shows an example with FileLocator.

Bundle bundle = FrameworkUtil.getBundle(this.getClass());
URL url = FileLocator.find(bundle, 
  new Path("path_to_file"), null); 

The following code shows an example for the usage of the Eclipse specific URL.

URL url = null;

try {

  url = new URL("platform:/plugin/" 
    + "your_bundle-symbolicname"
  + "path_to_file");
  
  } catch (MalformedURLException e1) {
    e1.printStackTrace();
} 

Using the URL to create an InputStream and to read the file is demonstrated in the next code example.

public String readTextFile(URL url) {
  InputStream inputStream = null;
  String output = "";
   try {
    inputStream = url.openConnection().getInputStream();
    BufferedReader in = 
        new BufferedReader(new InputStreamReader(inputStream));
    String inputLine;

    while ((inputLine = in.readLine()) != null) {
      output += inputLine;
    }
    in.close();

   } catch (IOException e) {
    e.printStackTrace();
  } finally {
    if (inputStream != null) {
      try {
        inputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
  return output;
} 

22. Questions and Discussion

Before posting questions, please see the vogella FAQ. If you have questions or find an error in this article please use the www.vogella.com Google Group. I have created a short list how to create good questions which might also help you.

23. Links and Literature

23.1. Source Code

Source Code of Examples

23.3. vogella Resources

vogella Training Android and Eclipse Training from the vogella team

Android Tutorial Introduction to Android Programming

GWT Tutorial Program in Java and compile to JavaScript and HTML

Eclipse RCP Tutorial Create native applications in Java

JUnit Tutorial Test your application

Git Tutorial Put everything you have under distributed version control system