Getting started with Eclipse plug-in development

Source: Internet
Author: User

Now I have seen many Eclipse plug-in development introductory articles on the Internet. Here I am writing this article mainly to tell you my own experience and initial learning. At the same time, I hope this article will use the simplest method to help you understand the basis for developing Eclipse plug-ins. Note that you need:

Use Eclipse to develop Java applications

Understand the concept of plug-in

Understand some XML knowledge this article is an entry-level article. It only describes the simple steps for developing a plug-in and what technical aspects are involved in developing a plug-in.

 Eclipse SDK Overview

Eclipse is the Eclipse SDK we call here. This SDK contains a lot of content, as shown in:

The runtime core (Eclipse Platform)-SDK must be an Eclipse Platform, which does not have any functions meaningful to end users. It is a basic Platform for loading all plug-ins. That is, the minimum set of Eclipse runtime.

Java Development Tool (JDT)-This plug-in is used to complete all Java-related development, it forms the most basic Java editing, compilation, running, debugging, and publishing environment.

Plug-in developer environment (PDE)-Development plug-in. If we want to develop plug-ins, we will find that all work environments are provided by them. It provides tools for automatic creation, processing, debugging, and deployment of plug-ins.

In the future, all the plug-ins we will develop will be loaded and run by the platform, while the PDDE is the development environment for the Development plug-ins, and the JDT is the development environment for the Java code during the development of the plug-ins.

Create a plug-in Project

Set Reference Project

A large number of external libraries are required to develop plug-ins. These libraries are the libraries provided by various plug-ins in existing Eclipse. For ease of development, we first reference these external libraries from a single project.

From the resource perspective, use File> Import...> external plug-ins and segments.
In the next step, select extract source archive and create the source folder in the project.
To display the screen called selection, select org. eclipse. ui and click Finish.

Create a project

In Eclipse, you need to create an empty plug-in project. To better understand the sources of various files in the plug-in, we start with a blank plug-in project:

1) Open the new project... wizard (File> New> project...) and select a plug-in project from the plug-in development category.

2) use com. huangdong. examples. helloworld as the project name. By default, the wizard also sets com. huangdong. examples. helloworld as the identifier.

3) Finally, make sure that a blank plug-in project is created on the plug-in code generator page.

4) if you want to switch to the plug-in development perspective, the answer is yes.

5) Select the com. huangdong. examples. helloWorld project and open the Properties dialog box.

6) in Java build path properties, select the project tab and select project org. eclipse. ui. These include the import classes required by the project.

7) rebuild the project.
Create a plug-in content

Create a new Small View

Next we will add a simple view for this project:

1) Create the com. huangdong. examples. helloworld package under the src directory of the project.

2) create a new class named HelloWorldView in this package. Its superclass are org. eclipse. ui. part. ViewPart.

Add the following code to HelloWorldView:

Package com. huangdong. examples. helloworld;

Import org. eclipse. swt. SWT;
Import org. eclipse. swt. widgets. Composite;
Import org. eclipse. swt. widgets. Label;
Import org. eclipse. ui. part. ViewPart;

Public class HelloWorldView extends ViewPart {

Label label;

Public void createPartControl (Composite parent ){
Label = new Label (parent, SWT. WRAP );
Label. setText ("Hello World ");
}

Public void setFocus (){}
}

We defined a variable lable for this class, initialized and set a display string in the createPartControl method.

  Protection Exhibition extension points

To add this view to Eclipse, You need to extend the org. eclipse. ui. views extension point. All of these must be described in plugin. xml. This configuration file describes the plug-in, including the location of the plug-in code and the extensions being added.

Copy the following content to plugin. xml:

<? Xml version = "1.0" encoding = "UTF-8"?>
<Plugin id = "com. huangdong. examples. helloworld"
Name = "com. huangdong. examples. helloworld"
Version = "1.0.0"
Provider-name = "HuangDong">

<Runtime>
<Library name = "helloworld. jar"/>
</Runtime>
<Requires>
<Import plugin = "org. eclipse. ui"/>
</Requires>

<Extension point = "org. eclipse. ui. views">
<Category
Name = "Hello"
Id = "com. huangdong. examples. helloworld. hello">
</Category>
<View
Name = "Hello Greetings"
Category = "com. huangdong. examples. helloworld. hello"
Class = "com. huangdong. examples. helloworld. HelloWorldView"
Id = "com. huangdong. examples. helloworld. helloworldview">
</View>
</Extension>

</Plugin>

The plug-in name, identifier, and version are defined in the plugin domain. At the same time, the plug-in code defined in the runtime domain will be packaged in the helloworld. jar file. The dependency plug-in to be used for this plug-in is defined in the requires domain. Since we want to use swt api and workbench, org. eclipse. ui is listed. Finally, the extension explains how to extend the org. eclipse. ui. views extension point. First, we define the view category in category. In the displayed view dialog box of the workbench, you can use the category to bring the relevant views together. The defined category is "Hello ". We also define our view named "Hello Greetings", which will be displayed in the "show View" dialog box and the title bar of the view, here we also use the class Identifier to demonstrate the final class to implement this view.

With the definition of plugin. xml, Eclipse will find the actions that can be performed by the plug-in and the specific Java classes that these actions will ultimately implement.

Many identifiers are used in the plug-in list file. Some extension points usually define the configuration parameters to be identified (for example, the class IDs used for view extension points ). We also need to define the plug-in ID. Generally, the Java package name prefix should be used for all identifiers to ensure that all installed plug-ins are unique.

The specific name used after the prefix is determined by yourself. However, if the plug-in ID prefix is exactly the same as the name of one of the packages, avoid using the class name in the package. Otherwise, it is difficult to tell whether you are checking the identification name or class name.

Avoid using the same identifier for different extension configuration parameters. The public identity prefix (com. huangdong. examples. helloworld) has been used in the above list. However, all our identifiers are unique. This naming method helps us read the file and understand which identifiers are related.

Run and test plug-ins

Running plug-ins is a simple task. These provide us with good support in the PDE. You only need to select "run"> "Run as"> "Run-Time workbench" from the menu. A prompt box for repeated plug-ins will pop up during running. You can skip the page by pressing OK. In this way, an Eclipse with installed plug-ins will be started.

After startup, select Window> display View> others in the menu. In the displayed view dialog box, a category is "Hello". Click "Hello Classification" to view "Hello Greetings". Click "OK. You can see the following interface in the bottom view:

Here, if you see this figure, congratulations, your first Eclipse plug-in has successfully run.

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.