Eclipse PlatformUI Examples

In this tutorial, I will use examples to show you how to use the Eclipse PlatformUI class.

PlatformUI is a final class defined in org.eclipse.ui package. It provides access function to the Eclipse Platform User Interface. All methods inside of PlatformUI class are static and the class can not be initialized.

Example 1: Fetch current workbench window’s shell, and add an InputDialog to it

InputDialog inputDialog = new InputDialog(
                    PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
                    "Example",
                    "Enter a value",
                    name,
                    null);
if (inputDialog.open() == InputDialog.OK) {
         return inputDialog.getValue();
}

Example 2: Dynamically invoke a view from your Plug-in program

You know invoke a view by clicking “Window-> Show View” in eclipse. But you can also dynamically/programmatically invoke/display a view in your Plug-in program, such as inside of an action listener. This is essential when you want to display another view based on some user action or result of an action. We can almost always invoke our target by using PlatformUI’s static method.

Here is the code for doing this:

IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
IWorkbenchPage page = workbenchWindow.getActivePage();
page.showView(viewId);

The argument “viewID” of showView() is the ID configured in your plugin.xml file.

In brief, this line of code does the following:

  1. Obtain workbench by using PlatformUI static method(Singleton pattern)
  2. Get the currently active window for the workbench
  3. Acquire the currently active page for the workbench window
  4. Shows the view identified by the given view id

Example 3: Create and run workbench in a standalone application

The following code is normally located in Application.java file.

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

Example 4: Detect if a platform is starting

PlatformUI.isWorkbenchRunning()

The above covers most commonly used methods from PlatformUI. The only one left is the getTestableObject() method which is not commonly used.

1 thought on “Eclipse PlatformUI Examples”

Leave a Comment