Many friends have asked how to convert an existing Eclipse plug-in into an RCP application, which is not complicated, because RCP is also a plug-in architecture, and RCP is a streamlined eclipse platform, but we have to do some customization work on the platform. Converting any traditional Eclipse plug-in project to RCP can be divided into two steps, starting with the first step: building an application.
In the GEF Getting Started series (iii, application examples) I have done a streamlined GEF application (download), and in this one I will take this example to the RCP application (click to download the converted project package) step-by-step. The application (application) is built by extending the org.eclipse.core.runtime.applications extension point to let eclipse know what kind of functionality your RCP needs, such as what views are on the interface, Menus and toolbars, the initial size of the application window, and so on. The definition of adding an application in Plugin.xml is simple enough to specify an ID and a class name as follows.
<extension
id="myapplication"
point="org.eclipse.core.runtime.applications">
<application>
<run class="com.example.application.MyApplication"/>
</application>
</extension>
Our main task next is to implement this class, MyApplication must implement the Org.eclipse.core.runtime.IPlatformRunnable interface, which defines only a run () method, for Eclipse Platform, this method is equivalent to the traditional Java program main () method, is the entry method. The implementation of this method in all RCP applications is almost exactly the same, starting Workbench and passing a workbenchadvisor instance as an argument, as follows:
public class MyApplication implements IPlatformRunnable {
public Object run(Object args) throws Exception {
Display display = PlatformUI.createDisplay();
try {
int returnCode = PlatformUI.createAndRunWorkbench(display, new MyWorkbenchAdvisor());
if (returnCode == PlatformUI.RETURN_RESTART) {
return IPlatformRunnable.EXIT_RESTART;
}
return IPlatformRunnable.EXIT_OK;
} finally {
display.dispose();
}
}
}
So the customization of the application is actually implemented through this workbenchadvisor instance. Now we're going to construct a subclass of the Org.eclipse.ui.application.WorkbenchAdvisor class, which is the myworkbenchadvisor that appears in the code above, and then overwrites some of its methods. It is more important that these two methods: Createworkbenchwindowadvisor () returns a Workbenchwindowadvisor instance, from the class name is not difficult to see its role is to customize the application window, including menus and toolbars, will be described in detail later; Getinitialwindowperspectiveid () returns the ID string of a perspective that defines the interface layout of an RCP application, so if you don't have a perspective defined in the original plugin, you now have to define a new one.
public class MyWorkbenchAdvisor extends WorkbenchAdvisor {
private static final String PERSPECTIVE_ID = "com.example.ui.MyPerspective";
public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(
IWorkbenchWindowConfigurer configurer) {
return new MyWorkbenchWindowAdvisor(configurer);
}
public String getInitialWindowPerspectiveId() {
return PERSPECTIVE_ID;
}
public void initialize(IWorkbenchConfigurer configurer) {
super.initialize(configurer);
//The workaround call
WorkbenchAdapterBuilder.registerAdapters();
}
}