Convert Eclipse plug-in to RCP application (on)

Source: Internet
Author: User

Many may ask how to convert an existing Eclipse plug-in into a RCP application.ProgramIn fact, this process is not complex, because the RCP application is also based on the plug-in structure, it can be said that RCP
It is the simplified eclipse platform, but we need to do some customization work on this platform. Converting any traditional Eclipse plug-in project to RCP can be divided into two steps.
Step 1: Create an application.

In the GEF entry-level series (3. Application Instances), I have done a simple GEF application (download ), in this article, I will step by step convert this example to An RCP application (click to download the converted project package ). The application is created by extending the org. Eclipse. Core. runtime. Applications extension point.
It is to let eclipse know what functions your RCP needs, such as the view, menu and toolbar on the interface, and the initial size of the application window. Add the in. xml
The definition of a program is very simple. You can 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 >

Next, our main task is to implement this class, And myapplication must implement
Org. Eclipse. Core. runtime. iplatformrunnable interface, which defines only one run () method. For eclipse
For platform, this method is equivalent to the main () method of the traditional Java program and is the entry method. In all RCP applications, the implementation of this method is almost the same, that is, start
Workbench, and pass a workbenchadvisor instance as a parameter to it, as shown below:

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 ();
}
}
}

Therefore, application customization is actually implemented through this workbenchadvisor instance. Now we want to construct
A subclass of the org. Eclipse. UI. application. workbenchadvisor class, that is, the precedingCodeShown in
Myworkbenchadvisor, And Then override some of its methods. The two methods are more important: createworkbenchwindowadvisor ()
Return to a workbenchwindowadvisor instance. It is not difficult to see from the class name that it serves to customize the application window, including menus and toolbar, which will be detailed later;
Getinitialwindowperspectiveid () returns an ID string of the perspective, which defines the interface layout of the RCP application.
You do not have a perspective defined in the plug-in. Now you have to define a new perspective.

Public   Class Myworkbenchadvisor Extends Workbenchadvisor {

private static final string perspective_id = " COM. example. UI. myperspective " ;

Public workbenchwindowadvisor createworkbenchwindowadvisor (
iworkbenchwindowconfigurer extends ER) {
return New myworkbenchwindowadvisor (worker ER );
}

PublicString getinitialwindowperspectiveid (){
ReturnPerspective_id;
}

Public VoidInitialize (iworkbenchreceiver receiver ){
Super. Initialize (aggreger );

//The workaround call
Workbenchadapterbuilder. registeradapters ();
}
}

Note: The resource view is used in this RCP, and this view depends on org. Eclipse. UI. Ide.
Manually register the adapter in the initialize () method. Otherwise, the existing project cannot be displayed in the resource view. (The resource view is not recommended in RCP.
This call is helpless. Please refer to this bug report)

Now let's take a look at how myworkbenchwindowadvisor is implemented in the previous Code. It inherits from
Org. Eclipse. UI. application. workbenchwindowadvisor class, to define the window size and title to overwrite
Prewindowopen () method, we can see that we also hide the toolbar; to define the menu and toolbar of the window, we should overwrite
The createactionbaradvisor () method. The returned actionbaradvisor instance will be introduced immediately.

Public   Class Myworkbenchwindowadvisor Extends Workbenchwindowadvisor {

PublicMyworkbenchwindowadvisor (iworkbenchwindowconfigurer configurer ){
Super(Aggreger );
}

PublicActionbaradvisor createactionbaradvisor (
Iactionbarconfigurer extends ER ){
Return NewMyactionbaradvisor (aggreger );
}

Public   Void Prewindowopen (){
Iworkbenchwindowreceiver extends er = Getwindowcycler ();
Configurer. setinitialsize ( New Point ( 700 , 500 ));
Configurer. setshowcoolbar ( False );
Configurer. setshowstatusline ( False );
Configurer. settitle ( " My RCP Application " );
}
}

Have you noticed that several new classes (and coming soon) have reference relationships like myapplication-> myworkbenchadvisor-
> Myworkbenchwindowadvisor-> myactionbaradvisor, eclipse before 3.1m5
In the RCP version, there is no actionbaradvisor class. Most application customization work is done in the workbenchwindowadvisor class.
The problem is that the code of this class is very long and the degree of reusability is relatively low. This method is much more convenient now. For example, you can define several actionbaradvisor and then
In workbenchwindowadvisor, you can make a selection based on your needs. The applications you get have different functions.

Now let's take a look at how myactionbaradvisor is implemented, and it inherits
Org. Eclipse. UI. application. actionbaradvisor class. We first construct in makeactions () to appear in the menu
Or the commands on the toolbar. You must call the Register () method to register these commands. The function is to release resources after the application ends, and supports shortcut keys.
Add these commands to the main menu in the fillmenubar () method. Because the toolbar is hidden, the fillcoolbar () method is not overwritten.
Fillstatusline () defines its own status bar. Our implementation of this class is very simple. It is just a menu item for exiting the program. You should add your own commands as needed.

Public   Class Myactionbaradvisor Extends Actionbaradvisor {

PrivateIworkbenchaction exitaction;

PublicMyactionbaradvisor (iactionbarconfigurer extends ER ){
Super(Aggreger );
}

Protected VoidMakeactions (FinalIworkbenchwindow window ){
Exitaction=Actionfactory. Quit. Create (window );
Register (exitaction );
}

Protected   Void Fillmenubar (imenumanager menubar ){
Menumanager filemenu =   New Menumanager ( " & File " ,
Iworkbenchactionconstants. m_file );
Menubar. Add (filemenu );
Filemenu. Add (exitaction );
}
}

Now, all the classes required by the application have been written. Let's check whether the application can be started normally. Choose run> debug... on the eclipse main menu,
Create a running item "gefpractice-RCP" in the "Eclipse application" group on the left of the dialog box.
Run
Application, and then find our Application ID in the drop-down list. Note that in the application extension point, the ID we specified is
"Myapplication", and the ID listed here adds the plug-in ID as the prefix and becomes "GefPractice-RCP.myapplication ",
1.


Figure 1 set to run the application

By default, all Eclipse plug-ins will be started, so redundant menu items and functions will appear in the application. Therefore, you must set this to only start this plug-in by switching
On the plug-ins property page, select "choose plug-ins and fragments to launch from
List, click the "deselect all" button on the right to clear the selection list, check our plug-in project, and then press "add required"
Plug-ins "allows eclipse to automatically add other plug-ins it depends on, as shown in 2.


Figure 2 only start this plug-in

Use this running configuration to start our application and you will get a "clean" interface, as shown in 3. If it is not the title bar of the Editor/view that is unique to eclipse, can you guess it is an eclipse application? For comparison, this is the running of the Eclipse plug-in version.


Figure 3 running applications

After an application is created, the code is completed, but it is not enough to obtain a complete and independently running product. The next post will introduce another step: package applications as products. If you cannot wait, read this branding your application.Article, But this article was written earlier. The next part I want to write is to use. Product to configure the product, which can achieve the same purpose more conveniently. For more information about how to create an application, see rich client tutorial. This tutorial consists of three parts, which I learned at the time, later, according to the development of RCP APIs, it updated the content in a timely manner, which is a rare entry material.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.