An example of a simple Eclipse plug-in development--helloworld "reprint"

Source: Internet
Author: User
Preface

Now on the Internet you can see a lot of the introduction of Eclipse plug-in development, here I write the purpose of this article is to tell you my own experience and the first learning. It is also hoped that this article will use the easiest way to get a sense of the basics of developing eclipse plug-ins. Note that to learn about Eclipse's plug-in development, you need to: use Eclipse to develop a Java application to understand the concept of a plug-in this article is an introductory article, just to show you the simple steps to develop a plug-in, Also understand what the technical side is involved in developing plug-ins.

Eclipse SDK Overview

The eclipse we typically use is what we call the Eclipse SDK, which includes a lot of content, as shown in the following figure: the runtime core (Eclipse Platform) -The SDK must be an eclipse Platform , it does not have any meaningful functionality for end users, it is a basic platform for loading all plug-ins. That is, the runtime minimum collection of eclipse. Java Development Tools (JDT) -All of our Java-related development is done by this plugin, which forms the most basic editing, compiling, running, debugging, and publishing environment for Java. Plug-in Developer Environment (PDE) -Plug-in plugin development, if we want to develop plug-ins, we will find that all the work environment is provided by it. It provides tools for automating the creation, processing, debugging, and deployment of Plug-ins.

the plug-ins we will develop in the future are loaded and run by the platform, while PDE is the development environment for developing plug-ins, JDT is the development environment of Java code when developing plug-ins. Create a plug-in project Settings Reference Project

The development of Plug-ins requires a large number of external libraries, primarily those provided by the various plug-ins in existing eclipse. For development convenience, we first use these external libraries as a unified reference for a project. From the resource perspective, use file > Import ...> external plug-ins and segments . In the next step, select the extract source archive and create the source folder in your project. to the screen where the display is called Select , select Org.eclipse.ui, and then click the Finish button. Create Project

To create an empty plug-in project in Eclipse, in order for us to better understand the source of each file in the plug-in, we start with a blank plugin project: Open the new project ... Wizard ( file > New > Project ... ) and select the plug-in Project from the plug-in development category. Use Com.huangdong.examples.helloworld as the name of the project. By default, the wizard also sets the Com.huangdong.examples.helloworld to identity. Finally, make sure that you select the Create a blank plug-in project on the Plug-in Code Builder page . When asked if you want to switch to the plugin development perspective, Answer Yes . Select the com.huangdong.examples.helloWorld project and open the Properties dialog box. In the Java build path properties, select the Project tab and select Project org.eclipse.ui. These contain the import classes that the project needs. reconstruction projects. Create a plugin content Create a new thumbnail

Let's add a very simple view to the project: Create the package Com.huangdong.examples.helloworldin the src directory of the project. In this package, you create a new class called Helloworldview whose superclass is Org.eclipse.ui.part.ViewPart.

Add the following code to the 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 the class, initialized and set a display string in the Createpartcontrol method. Exhibition extension point

Let eclipse add this view and you need to extend the org.eclipse.ui.views extension point. All of these need to be described in plugin.xml . The manifest file describes the plug-in, including the location of the plug-in's code and the extension that is being added.

Copy the following content to Plugin.xml:

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

The name, identity, and version of the plug-in are defined in the plugin domain. At the same time, the plug-in code defined in the runtime domain is packaged in the Helloworld.jar file. The plug-in is defined in the requires domain, and the org.eclipse.ui is listed because we want to use the SWT API and Workbench. Finally, it is explained in the extension that you want to extend the org.eclipse.ui.views extension point. First we defined the categories of views in category, and in the Workbench Display View dialog box, you can use categories to bring together related views. The category we define is named "Hello". Also defines our view, called "Hello greetings", which will be displayed in the Display View dialog box and the title bar of the view, where we also use the class ID to illustrate the final class that implements this view.

By Plugin.xml's definition, eclipse will really find the behavior that plug-ins can do, and the concrete Java classes that these behaviors ultimately implement.

A number of identities are used in the plug-in manifest file. Individual extension points typically define the configuration parameters that need to be identified (for example, the category identifier for the view extension point above). We also want to define the plug-in identity. Typically, you should use the Java package name prefix for all identities to ensure that all installed plug-ins are unique.

The specific name used after the prefix is entirely up to you. However, if the plug-in identity prefix is exactly the same as the name of one of the packages, you should avoid using the class name in the package. Otherwise, it is difficult to tell whether you are viewing the identity name or the class name.

you should also avoid using the same identity for different extended configuration parameters. In the above list, the public identification prefix (COM.HUANGDONG.EXAMPLES.HELLOWORLD) has been used, but all of our identities are unique. This naming method helps us to read the files and understand which identities are relevant. running and testing plug-ins

Running Plug-ins is a very simple thing, which gives us good support in PDE. Just select run > Run as > Run-time console in the menu, and pop up a repeating plugin prompt box at run time, you can click OK to skip, don't care. This will start an eclipse with a plugin already installed.

Select window > Display View > Others in the menu after startup, in the Display View dialog box There will be a category for Hello, click on the hello category will see hello Greetings, select the back point OK button. The following interfaces are visible in the bottom view:

Here, if you see this picture, congratulations, your first Eclipse plug-in ran successfully.

Attached: The Eclipse Project package file used in this document, please go back to your workspace directory after downloading.

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.