An example of a simple Eclipse plugin development - helloworld [reproduced]

xiaoxiao2021-03-06  118

Foreword

Now there is a small entry article developed by the Eclipse plugin on the Internet. Here I write this article is mainly to tell me my own experience and the most beginning learning. At the same time, I hope this article can use the most simple way to let everyone know the basis for developing the Eclipse plugin. It should be noted that you need to learn Eclipse's plugin, you need:

Will use Eclipse to develop Java applications to learn about the concept of plugin This word is aware of the XML knowledge. This article is a introduction article, just explains the development of a plugin, and what will be learned when developing plug-ins? .

Eclipse SDK Overview

The Eclipse we usually use is the Eclipse SDK we mentioned here. This SDK includes a lot of content, as shown below:

Runtime Core (Eclipse Platform) - SDK must have an Eclipse Platform, which does not have any feature that makes sense to end users, it is a base platform that loads all plugins. That is, the minimum collection of Eclipse runtime. Java Development Tool (JDT) - All our development part of Java is completed by this plugin, which forms an environment for Java's most basic editing, compile, running, debugging, publishing. Plug-in Developer Environment (PDE) - Develop plug-ins for plug-ins, which we will find that all work environments are provided by it. It provides tools for automatically creation, processing, debugging, and deploying plugins.

The plugins we have to develop in the future are loaded and running by the platform, while PDE is the development environment for developing plugins. JDT is the development environment of Java code when developing plug-ins.

Create a plugin project

Settings a reference item

A large number of external libraries are required when developing plugins, and these external libraries are primarily provided by the libraries provided in the existing plugins in Eclipse. To develop convenience, we will first unify these external libraries first.

From the resource perspective, use the file> import ...> external plugin and segment. Select the source archive in the next step and create a source folder in the project. To display the selected screen, select ORG.ECLIPSE.UI, then click the Complete button.

Create a project

In Eclipse, you need to create an empty plugin item. In order to make us better understand the source of each file in the plugin, we start from a blank plugin project:

Open New Projects ... Wizard (File> New> Project ...) and select the plugin item from the plug-in development category. Use com.huangdong.examples.Helloworld as the name of the project. By default, the wizard will also set the com.huangdong.examples.helloWorld to the identity. Finally, make sure you have selected a blank plugin item on the plug-in code generator page. When you ask if you want to switch to the Plug-in development perspective, the answer is. Select the com.huangdong.examples.helloworld project and open the Properties dialog. In Java build path properties, select the Item tab, and select the item org.eclipse.ui. These include imported classes required for projects. Rebuild project.

Create a plugin content

Create a new small view

Below we join a simple view for the project:

Create a package com.huangdongld.examples.Hunogdong.examples.HELLOWORLD in the project's SRC directory. Creating a new class called HelloWorldView in this package is ORG.ECLIPSE.UI.PART.VIEWPART.

Add the following code in 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 define a variable lable for this class, which initializes and sets a string of display in the CreatePartControl method.

Exhibition extension

Let Eclipse add this view and need to extend the org.eclipse.ui.views extension point. All of these needs to be described in PLUGIN.XML. The list file describes the plugin, the location of the code where the plugin is located, and the extension being added.

Copy the following to Plugin.xml:

The name, identity, and version of the plugin are defined in the plugin domain. At the same time, in the Runtime domain, the plug-in code will be packaged in the HelloWorld.jar file. Defining the dependent plugin to use in the Requires domain, because we have to use the SWT API and the workbench, org.eclipse.ui is listed. Finally, in Extension, you want to expand org.eclipse.ui.Views extension points. First we define the category of the view in Category. In the Workbench's Display View dialog box, you can use the category to set the relevant views together. Our defined categories are "Hello". At the same time, our view is also named "Hello Greetings", which will appear in the title bar of the "Show View" dialog and view, here we also illustrate the ultimate class that implements this view through the Class ID.

With Plugin.xml definition, Eclipse will really find the behavior of the plugin to do, and the specific Java class that these behaviors finally implemented.

A lot of identities is used in the plug-in list file. Individual extensions typically define configuration parameters that need to be identified (eg, category identifiers for view extension points). We also need to define the plug-in ID. Typically, all the Java package name prefix should be used to ensure that all installed plugins are unique.

The specific names used later in the prefix are completely determined by yourself. However, if the plug-in identification prefix is ​​just the same name as one of the packets, you should avoid using the class name in the package. Otherwise, it will be difficult to distinguish you are viewing the identification name or a class name.

You should also avoid using the same identity for different extension configuration parameters. In the above list, a common identification prefix has been used, but all our identities are unique. This naming method can help us read the file and understand which identifiers are related.

Run and test plugin

Running a plugin is a very simple matter, which gives us good support in PDE. Just want to run> Run> Run> Running Time Play, you can play a repeating plug-in prompt box when running, you can press OK, don't care. This will initiate an Eclipse that has been installed.

After start, select Window> Display View> Others, there will be a classification to Hello in the Display View dialog, you can see Hello Greetings, select the Back Optical button. You can see the following interface in the bottom view:

Here, if you see this picture, which is congratulations, your first Eclipse plug is successful.

转载请注明原文地址:https://www.9cbs.com/read-125832.html

New Post(0)