The basics of Eclipse plug-in development (2) First Eclipse plugin

Source: Internet
Author: User

Reprint Source: Http://www.cnblogs.com/liuzhuo.

Use PDE (Plugin development environment) for plug-in development in eclipse. In PDE for everyone to prepare a few templates for your reference. The first step is to use the template to make a simple plugin as the first contact with PDE.

1. Build plug-in engineering

Create a new]->[project by selecting [File]->[...] Start the [New Project] Wizard. In the plug-in development, we can not choose the usual Java project, to choose to create a new plug-in project.

Select [Plug-in engineering] and click [Next] (Figure 2-1). Plug-in Next, enter an appropriate project name (Figure 2-2). It is customary for the project name and plugin ID to be the same. The plug-in ID and Java package are named the same way and must be a world-wide unique name. Here, use Cn.sf.amateras.sample as the project name (= Plugin ID). Click [Next].

Figure 2-1 Building a plug-in project

  

Figure 2-2 Entering plug-in information

  

Enter information such as [plug-in ID], [plug-in version], [Plug-in properties (plug-in author)] (Figure 2-3). [plugin ID] is automatically set to the [project name] you entered earlier. If there are no other special settings, continue clicking [Next].

Figure 2-3 entering the necessary information for generating the project

  

Figure 2-4 Template Selection

  

Figure 2-5 Entering template information

  

The plugin generated from the selected template can modify some information in the picture (Figure 2-5). If there is no special need to set the place click [Finish], generate the project.

  PDE Perspective view

Once the plug-in project is generated, it switches to the PDE (plug-in development) perspective. The project now generates the source code for the plug-in based on the selected [Hello,world] template, and opens the Manifest editor.

Figure 2-6 PDE (plug-in development) perspective

  

2. Plug-in action

Next we look at the plugin action. On the right side of the [overview] page of the manifest editor [launch the Eclipse application (Luanch an Eclipse application)] (Figure 2-7). When clicked, a new Eclipse instance is enabled. This new instance is referred to as the [Runtime Workbench]. The run workbench can be started in the state of the plug-in that is currently being developed so that the developer can confirm the plugin's actions.

Figure 2-7 starting the run-time Workbench

  

When the runtime Workbench is started, a new menu item is added to the menu bar. This menu item is added to the plugin's code. A corresponding button has also been added to the toolbar.

The buttons on the toolbar and the menu items have the same effect, and a [hello,eclipse World] dialog box will appear.

The eclipse version of Hello World is complete. After confirming these actions, you can close the current run-time Workbench, and we will refer to the code of the plug-in project in the next section of the tutorial.

Figure 2-8 the new menu item and toolbar button and the corresponding action in the example

  

MANIFEST. MF and Plugin.xml

A plug-in contains two configuration files for MANIFEST.MF and Plugin.xml. MANIFEST. MF is the handler configuration file for OSGi, which records the plug-in ID, name, author, dependencies of other plugins, and so on. Plugin.xml records information such as the extension of the plug-in and the definition of extension points.

Eclipse3.1 has not used OSGi technology before, all the information of plug-ins is recorded in Plugin.xml. So it is important to adhere to OSGi's profile definition specification when applying to Eclipse3.1 and later versions of plugins that are Eclipse3.1 previous versions.

  

3. Features of PDE

PDE (Plug-in development environment · Plugin Development Enviromment) provides a variety of features for plug-in development. Because the plugin is developed using the Java language, the Java Encoding tool JDT is also used.

 Manifest Editor

One of the most important features that PDE provides is the manifest editor . The editor can provide graphical editing capabilities for the two configuration files of the plug-in. (Fig. 2-9)

There are a lot of configuration items in the editor, and you can toggle the Settings screen from the tab page below the editor.

Figure 2-9 Manifest Editor

  

Table 1 Manifest Editor tab page

Page Description
Overview You can set profile information for a plugin. Build the document for publishing and start the run-time Workbench.
Dependencies Sets the dependencies between plugins.
Runtime You can set export packages and classpath.
Extensions Manages extensions to extension points for the current plug-in.
Extension Points Manages extension points for the current plug-in.
Build Various settings when generating the project.
MANIFEST. Mf Display the source code of MANIFEST.MF
Plugin.xml Display the source code of Plugin.xml
Build.properties Display the source code of Build.properties

  

Some shortcut buttons are displayed at the top of the tab page outside of overview, which can also be used to start the run-time Workbench, export, and so on.

Figure 2-10 Editor Upper

  

  

Here are a few views of the PDE topic. You can find a view that is not displayed in [Window]->[show view].

  [Plugin (plug-ins)] View

A list of all plug-ins that contain the plug-in being developed within the current workspace. Opening the plug-in node can show the resources contained under the plugin, but the plug-in provided by the jar package is not displayed.

Right-click on the plug-in node and select [Open Dependencies] to open the dependency view that is described later to show dependencies between plugins. The selected plug-in can also be introduced into the current workspace as a project in the right-click menu.

  [Dependency (Plug-in Dependencies)] View

Shows the dependencies between plugins. You can represent the plug-ins that the current plug-in relies on and the plug-ins that depend on it.

  [Plug-in registration (plug-in Registry)] View

List of all plugins loaded by the current workbench. Opens the plug-in node, showing the extensions and extension points, dependencies, etc. provided by the plug-in.

  [ERROR log] View

Error log view is not a feature provided by PDE, but is very useful in plug-in development. It can represent errors that occur in the current workbench, and double-clicking on the error can be directed to where the error occurred.

  Plugin-in Selection Spy

Press ALT+SHIFT+F1 at the same time

Plugin-in Selection Spy is a new feature that starts with Eclipse3.4, and is able to display internal information about the editor, View, Wizard, dialog box, and so on, where the current focus is. The existing plug-in as a reference, for plug-in development, the use of this function is more appropriate.

4. Interpreting the code for the sample plug-in

Let's go back to the plug-in project and take a look at the code generated by the plug-in project. First look at the Plugin.xml code.

Code 2-1 Plugin.xml

<?xml version= "1.0" encoding= "UTF-8"? ><?eclipse version= "3.4"?><plugin>            <extension point= "org.eclipse.ui.actionSets" >①<actionset label= "Sample Action Set" Visible= "true" id= "Cn.sf.amateras.sample.actionSet" > <menu label= "Sample &am P Menu "id=" Samplemenu "> <separator name=" Samplegroup "> </s eparator> </menu> <action label= "&sample action" icon= "icons/s                Ample.gif "class=" cn.sf.amateras.sample.actions.SampleAction "②tooltip=" Hello, Eclipse World " Menubarpath= "Samplemenu/samplegroup" toolbarpath= "Samplegroup" id= "Cn.sf.amater As.sample.actions.SampleAction "> </action> </actionSet> </extension></plugin>

In order to add an item to the menu bar, the extension element is used in the file. The ID of the ① specifies the name of the extension point org.eclipse.ui.actionSets. The content in the extension varies according to the extension point. The sample plug-in contains elements such as Actionset, menu, action, and so on.

The class attribute in ② specifies the Cn.sf.amateras.sample.actions.SampleAction class as the action class for a Response menu or toolbar button.

Code 2-2 Sampleaction.java

Package Cn.sf.amateras.sample.actions;import Org.eclipse.jface.action.iaction;import Org.eclipse.jface.viewers.iselection;import Org.eclipse.ui.iworkbenchwindow;import Org.eclipse.ui.iworkbenchwindowactiondelegate;import org.eclipse.jface.dialogs.messagedialog;/** * Our sample Action implements Workbench action delegate. * The action Proxy is created by the Workbench and * shown in the UI. When the user tries to use the action, * This delegate'll be created and execution would be * delegated to it. * @see Iworkbenchwindowactiondelegate */public class Sampleaction implements Iworkbenchwindowactiondelegate {private Iworkbenchwindow window;/** * The constructor. */public sampleaction () {}/** * The action has been activated. The argument of the * method represents the ' real ' action sitting * in the Workbench UI. * @see iworkbenchwindowactiondelegate#run */public void Run (iaction action) {//... ①messagedialog.openinformation (Window.getshell (), "Sample", "Hello, Eclipse World"); /** * Selection in the workbench have been changed.  We * Can change the state of the ' real ' action here * if we want, but this can only happen after * the delegate have been Created. * @see iworkbenchwindowactiondelegate#selectionchanged */public void selectionchanged (iaction action, ISelection Selection) {}/** * We can use the This method to dispose of any system * resources We previously allocated.  * @see iworkbenchwindowactiondelegate#dispose */public void Dispose () {}/** * We'll cache Window object in order to * be Able to provide parent shell for the message dialog. * @see iworkbenchwindowactiondelegate#init */public void init (Iworkbenchwindow window) {this.window = window;}}

The ①run () method records the processing of action execution. A dialog box was opened with the Messagedialog.openinformation method.

Let's take another look at the generated class--activator class. This class manages the life cycle of the plug-in, which is called the plug-in class.

Code 2-3 Activator.java

Package Cn.sf.amateras.sample;import Org.eclipse.jface.resource.imagedescriptor;import  Org.eclipse.ui.plugin.AbstractUIPlugin; //.... ①import org.osgi.framework.bundlecontext;/** * The Activator class controls the plug-in life cycle */public class Activato R extends Abstractuiplugin {//the plug-in idpublic static final String plugin_id = "cn.sf.amateras.sample";//$NON-nls-1$ The shared instanceprivate static Activator plugin;/** * The constructor */public Activator () {}/* * (non-javadoc) * @s EE org.eclipse.ui.plugin.abstractuiplugin#start (org.osgi.framework.BundleContext) */public void Start ( Bundlecontext context) throws Exception {//.... ③super.start (context);p lugin = this;} /* * (non-javadoc) * @see org.eclipse.ui.plugin.abstractuiplugin#stop (org.osgi.framework.BundleContext) */public void Stop (Bundlecontext context) throws Exception {//.... ④plugin = null;super.stop (context);} /** * Returns the shared instance * * @return the shared instance */public static Activator Getdefault () {//.... ②return plugin;} /** * Returns An image descriptor for the image file at the given * plug-in relative path * * @param path The path * @retu RN The image descriptor */public static imagedescriptor Getimagedescriptor (String path) {return Imagedescriptorfromplugin (plugin_id, Path);}}

The plug-in class in the example inherits from Abstractuiplugin (①). If a plugin without UI inherits Abstractuiplugin's parent class, plugin is ready.

The method of the plug-in class is shown in the following table:

Getdefault () Gets the method of the instance of the plug-in class. The plug-in class is Singleton, so this method is provided as a static method.
Start () The processing at the beginning of the plug-in.
Stop () Processing when the plug-in is stopped.
GetLog () Log output to obtain Ilog method.
Getimageregistry () Gets the Imageregistry class that manages the image within the plug-in.
Getperferencestore () Gets the Iperferencestore class that holds the plugin settings.
Getdialogsettings () Gets the Idialogsettings class for the Save dialog settings.
Getworkbench () Get an example of iworkbench.

The above is the code of the plugin generated by the [Hello World] template, in the PDE for us to prepare a lot of templates, you can all try.

The basics of Eclipse plug-in development (2) First Eclipse plugin

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.