Eclipse RCP Development Basics

Source: Internet
Author: User
Eclipse rcp Development BasicsReprint from click on Open link
RCP Development Environment

An important feature of Eclipse is his plug-in architecture, the eclipse's kernel is small, mostly composed of many functional plug-ins. Rich-Client Platform (RCP) is also based on this plug-in mechanism and, like the architectural pattern of the Eclipse Workbench, programs are composed of several plug-ins that provide the programmatic interface to extend functionality through extension points.

Eclipse RCP programs are typically composed of the main application (org.eclipse.core.runtime.application), Windows (org.eclipse.ui.perspective), and Workbench Advisor. Generally one RCP program requires at least "org.eclipse.core.runtime" and "Org.eclipse.ui" two plug-in support, included in the required plugins option.

two important documents

MANIFEST. Mf:osgi manifest file, used to describe the dependencies of plug-ins and build environment, etc.;

Plugin.xml:eclipse configuration file that describes the plug-in extension point, and so on.

The PDE plug-in provides a graphical editor for these two files.

Configuring the Environment

If the eclipse IDE is not a rcp/plug-in development version, download the Eclipse for rcp/plug-in developers package via Eclipse's online update plug-in feature.


New RCP program, named Rcpproject:


In the RCP application startup process, Eclipse looks for the class for the extension point "org.eclipse.core.runtime.application", and the class is then loaded to run. In this class, create a Display, create and run a Workbench, configure this Workbench through Workbenchadviso R, workbenchadvisor through Workbenchwindowadvisor To implement the display of the interface, and then in this class to implement the menu, toolbars and so on.

Normal Running Program:


Configure Run Configuration

In the Run as-run configuration, check the dependencies of the program in the Plug-in option and automatically add the dependent plug-ins through the Add Required plug-ins. Add Run option –consolelog, which can see the RCP program error message on the command line:

Alternatively, you can set up in Windows-> preference-> Plug-in Development-> Target Platform:


Double-click Plugin.xml or MANIFEST. MF file, into the PDE's graphical editing interface of the Extensiton, increase the extension point org.eclipse.core.runtime.products.

Plugin.xml file:

<?xml version= "1.0" encoding= "UTF-8"?>

<?eclipse version= "3.4"?>

<plugin>

<extension

Id= "Application"

Point= "Org.eclipse.core.runtime.applications" >

<application>

<run

Class= "Rcpproject. Application ">

</run>

</application>

</extension>

<extension

Point= "Org.eclipse.ui.perspectives" >

<perspective

Name= "RCP Perspective"

Class= "Rcpproject. Perspective "

Id= "Rcpproject.perspective" >

</perspective>

</extension>

<extension

point= "Org.eclipse.core.runtime.products" >

<product

application= "Rcpproject.product"

name= "name" >

</product>

</extension>

</plugin>

Open the project run As-run configuration, modify the configuration from run an application to run a product, as shown in the figure:


Run the program (run as a product) if you are prompted "ORG.ECLIPSE.EPP.PACKAGE.RCP is missing":


Then, in run As-run configuration, check the dependencies of the program in the Plug-in option, automatically add the dependent plug-ins through the Add Required plug-ins, and then run the program.

Event Response

The event response mechanism in Eclipse RCP development is implemented by declaring the command component, associating menus, buttons, and so on in the UI with event response functions or classes. The Comand component org.eclipse.ui.commands as an extension point in the Plugin.xml file. Therefore, the basic process of an event response requires three aspects: UI, command component, and event handler function.

Add event to menu

Double-click Plugin.xml or MANIFEST. MF file, into the PDE's graphical editing interface of the Extensiton, increase the extension point org.eclipse.ui.commands.


Add Command's default handler class, double-click DefaultHandler hyperlink, create new class, inherit from Org.eclipse.core.commands.AbstractHandler, code as follows:

package rcpproject.commands;

import Org.eclipse.core.commands.AbstractHandler;

import org.eclipse.core.commands.ExecutionEvent;

import org.eclipse.core.commands.ExecutionException;

import Org.eclipse.core.commands.IHandler;

import Org.eclipse.ui.handlers.HandlerUtil;

Public class Exithandler extends abstracthandler implements Ihandler {

@Override

Public Object Execute (executionevent event) throws executionexception {

Handlerutil.getactiveworkbenchwindow (event). Close ();

return null ;

}

}

This creates an association between the command component and the event handler function, and the following links them to the menu event. Add the "org.eclipse.ui.menus" extension point and create a new menucontribution to set its Localuri to "menu:org.eclipse.ui.main.menu ".

Under Menucontribution, create a new menu, named File:


Create a command for the File menu, set this command to the command component that was created previously, associate it with CommandID, and set the hint content (tooltip).

Creates a new command, binds an event-handling function Handler, and pops up a message box.


Add Command's default handler class, double-click DefaultHandler hyperlink, create new class, inherit from Org.eclipse.core.commands.AbstractHandler, code as follows:

Public class Hellowordhandler extends abstracthandler implements Ihandler {

Public Object Execute (executionevent event) throws executionexception {

messagedialog.openinformation (Handlerutil.getactiveworkbenchwindow (

event). Getshell (), "info", "info for you";

return null ;

}

}

Add event to toolbar

This creates an association between the command component and the event handler function, and the following links them to the toolbar events. Add the "org.eclipse.ui.menus" extension point and create a new menucontribution to set its Localuri to

Toolbar:org.eclipse.ui.main.toolbar


Create the toolbar under Menucontribution and associate it with the command component of the HelloWorld:


You need to modify the code in the Applicationworkbenchwindowadvisor.java file to show the toolbar for the RCP program:

Configurer.setshowcoolbar ( true );

To run the program:


Add event to View toolbar

First, add the extension point org.eclipse.ui.views to the extension and create a new view with the name ID "rcpproject.views.View1" and Rcpproject with the class name . ViewPart1 Association, double-click the class hyperlink to create a new class.

Modify the Perspective.java code, add a Viewpart, the red part is the ID of the Viewpart instance (string type), that is, the Viewpart that was created in Plugin.xml before.

Public class Perspective implements Iperspectivefactory {

Public void createinitiallayout (ipagelayout layout) {

String Editroarea = Layout.geteditorarea ();

Layout.seteditorareavisible ( false );

Layout.setfixed ( true );

Layout.addview ( "Rcpproject.views.View1" , Ipagelayout. Left, 1.0f, Editroarea);

}

}

Under Org.eclipse.ui.menus of the extension point created earlier, create a new menucontribution, set its locationuri to "Toolbar:rcpproject.views.View1" , that is, Toolbar + colon + ID name, then create a command under it and associate it with the event response function HelloWorldHandler.


To run the program:

Add event to drop down menu

At the same extension point in the File menu, add Menucontribution, Locationuri the same as file, create menu, name Fathermenu, and, under Fathermenu, create two command, all linked to Hel Loworld event Handler, at the same time, also establishes two menu, each menu below establishes a command, also links to HelloWorld event Handler.


To run the program:


Add event to toolbar drop-down menu

Follow the steps in "Add Event to toolbar" to establish the Fathercommand button and also link to event HelloWorld.

Related Article

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.