Eclipse Getting Started: Developing Eclipse Plugins

xiaoxiao2021-03-06  115

Eclipse's most attractive place is its plugin architecture. Important concepts in this system are Extension Points, which is the interface provided for plugins. Each plugin is developed on existing extension points and may have its own extension points to continue development on this plugin.

Due to the plugin, the core part of the Eclipse system is very simple to complete when starting, the work of the platform is started, and the platform of the platform and the plugin for the system. Most of the features implemented in Eclipse are completed by the corresponding plug-in, such as the appearance of the Wrokbench UI plugin completion interface, the Resource Management plugin completes the maintenance or generation of resource management work such as projects or files (in the second example below This plugin will be used), and the Version and Configuration Management (VCM) plugin is responsible for completing version control functions, and so on. Although every feature mentioned above is the function necessary for most IDE environments, Eclipse also made them all the plug-in mode, and even developed the development environment of Java programs (Java Development Tooling, JDT) It is just a normal insert in the Eclipse system. The entire Eclipse architecture is like a large puzzle, which can be plug-in, and the plugin can also be added to the existing plug-in. The following plug-in development example is developed at the observation window extension in the Workbench UI plugin.

The first part of this article introduces one of the development interfaces of Eclipse is the observation window. It usually cooperates with the edit window to display some useful information, here we only generate a watch window that shows welcome information, assumes a new plugin called Welcome.

The first step is to create a Java project with a guide. We can select Fileànew in the menu bar, or use the wizard button in the toolbar, or use the New Wizard dialog box in the resource window with the NEW, open the Wizard dialog, and create a project with the default. And build a Welcome.java file in the project, the code is as follows:

Package com.nidapeng.eclipse.plugin;

Import org.eclipse.swt.widgets.composite;

Import org.eclipse.swt.widgets.label;

Import org.eclipse.swt.swt;

Import org.eclipse.ui.part.viewpart;

Public class welcome extends viewpart {

Label label;

Public welcome () {

}

Public void createpartControl (Composite Parent) {

Label = new label (parent, swt.wrap);

Label.setText ("Welcome to Eclipse";

}

Public void setfocus () {

}

}

In order for this program to be compiled normally, configure its compilation environment, that is, specify the desired ClassPath. There are several ways in Eclipse, commonly used: the first is to select the item at the resource window or Java package window, click the right button, select Properties in the open menu, then in the Properties dialog box Choose Java Build PathàLibraries, add three packages with the Add External Jars feature, which are both the eclipse's existing plug-in package, which can be found in the corresponding path below "Your Eclipse installation path / plugins". They are the runtime.jar in the org.eclipse.core.Runtime plugin, ORG.Eclipse.swt in Swt.jar and Org.eclipse.ui in Workbench.Eclipse.ui. The second specified classpath is to first import the three packets mentioned above into a next item in the Eclipse. If imported into the same item as Welcome.java, there is no need to further specify the classpath, otherwise you need to select Java Build PathàProjects in the Properties menu of the project, then select the items where these three packages are. In our project, you have to generate an XML file, and its name must plugin.xml. code show as below:

ID = "com.nidapeng.eclipse.plugin"

Name = "Welcome to Eclipse"

Version = "1.0"

Provider-name = "ni Dapeng">

Point = "org.eclipse.ui.views">

Name = "Welcome"

ID = "com.nidapeng.eclipse.plugin.category1">

Name = "Welcome to Eclipse"

Category = "com.nidapeng.eclipse.plugin.category1"

Class = "com.nidapeng.eclipse.plugin.welcome"

ID = "com.nidapeng.eclipse.plugin.View1">

There are four main tags in Plugin.xml: Plugin, Requires, Runtime, Extension. The properties of the Plugin tag provide basic information for the Welcome plug-in we have to develop. In addition to Name, Version, Provider-name, the most important thing is ID, it requires conflicts with the existing Eclipse plugin ID, so we use The name is the ID of the plugin. The Requires tab is listed in the needed plugin, here we want to use the Eclipse Workbench and SWT API, thus import the org.eclipse.ui plugin. The Runtime label is specified in the file name of the JAR package where we developed the plugin. The Extension tag is the information of the plugin extension point. Org.eclipse.ui.views is the observation window extension provided by the Eclipse system. Our example is a view window (this shows that we are going to further development in org.eclipse.ui.views extension. EXTension also includes two labels, and we will know the meaning of these two labels in subsequent Welcome plug-in steps. It should be noted that the uniqueness of the ID of the Category and View tags, and declare the Welcome plugin class name in the properties of the View. In Eclipse, Plugin.xml provides the default visual editor, which can be done with this editor during writing PLUGIN.XML. If you enter the PLUGIN.XML file source code, you can also check your code with this editor: If the editor does not read it correctly, you have some questions in your Plugin.xml.

After confirming that weclome.java and plugin.xml are correct, you can use the export command in the Eclipse menu bar to export Weclome.java as JAR file, and its name should be consistent with the JAR declared in Plugin.xml. Export Plugin.xml at the same time. The method of installing the Welcome plug-in and the installation Tomcat plug-in method described herein is the same: first establish a com.nidapeng.eclipse.plugin path under the "Eclipse" path, then WECLOME.JAR and PLUGIN. XML is copied to this path. The Eclipse must then be restarted. When the Eclipse is started, it searches for all plugins under the plugin path and registers them (just registration, "only when a plugin is required, Eclipse will start it). Select PerspectiveàShow Viewàothers in a rebooted Eclipse, in the open dialog, we will find the Name property declared in the Category tab in the plugin.xml: Welcome. The View tag Name property is included in the Welcome's support: Welcome to Eclipse. Select it and confirm that the Welcome window will appear in a location on the Eclipse Workbench. If the above operation is performed, no new window is displayed, you can open the show view menu again. At this point, there should be a new one in the menu: Welcome to Eclipse, then select it.

Above we completed a plug-in to observe the window, but this operation process is less convenient for developing a slightly complex plug-in: each test must pack, release, restart the Eclipse system! To this end, Eclipse provides a plug-in (a bit winding): Plug-in Development Environment (PDE). As mentioned earlier this article, the current Eclipse's Release or Stable version provides this plugin, so if the installed Eclipse is one of the two versions, you can directly carry out the following steps. Let's develop a slightly complex plugin with a PDE environment. The first step is still going to create a project, just in the wizard is not using Java projects, but Plug-in project in Plug-in developments. When the application wizard is generated into a new project, pay attention to two points: The first is the PDE project name is the PLUGIN ID, so it is necessary to ensure its uniqueness, here our project name is com.nidapeng.eclipse.plugin.pde. Second, in order to further illustrate the structure of the Eclipse plug-in, in the Plug-In Code Generators, select a default plug-in with a wizard template, as shown in Figure 6:

Figure 6

This type of plug-in class generated by default is not required for us to build, and can also build our project with a generation of air plug-ins, which is just to further illustrate the plugin structure of Eclipse.

After the project is generated, there will be a pdeplugin.java file in our project, which is a plugin class generated by the default. Note that it inherits the Abstractuiplugin class, and the AbstractUiplugin class implements the org.eclipse.ui.plugin interface. In fact, all Eclipse plugins will have a class that implements the PLUGIN interface. This class will be the main class of the new plugin (similar to the Java class with the main () function), which is responsible for managing the survival of the plugin. In our AbstractUiplugin inheritance, the first one of the generated plugins generated in Eclipse is also unique instance in the Singleton mode. In general, a getDefault () method is also implemented in the inheritance subclass. Returns an instance of the current plugin. Moreover, when Eclipse uses the plugin for the first time, this primary class will be the first class that is called, so we can also perform some initialization work in its code. And if the plugin needs to use Preferences, Dialogs, or ImageS resources, you can also get their instances in this class, such as using getDialogSettings (), getImageReGistry () method.

But as mentioned earlier, PDEPLUGIN.JAVA is not required by the following example, we don't have to make any modifications to it. In our first example, the WeClome plugin does not generate Abstractuiplugin at this time, at which point the system automatically generates a default main class for the WeClome plugin (similar to the Java class constructor, if there is no statement, the system will Specifies a default constructor).

The following code is to really realize the function of our new plugin, assuming that this plugin is NOTICEVIEW:

Package com.nidapeng.eclipse.plugin.pde;

Import org.eclipse.core.resources. *;

Import org.eclipse.core.Resources.IResourceChangeEvent; import org.eclipse.core.Runtime.coreException;

Import java.util.resourcebundle;

Import org.eclipse.swt.widgets.label;

Import org.eclipse.swt.widgets.composite;

Import org.eclipse.ui.part.viewpart;

Import org.eclipse.swt.swt;

Import org.eclipse.swt.widgets.display;

Public Class NoticeView Extends ViewPart IMplements

Runnable, IResourceChangelistener, IResourceDeltavisitor {

Private resourcebundle resourcebundle;

PRIVATE LABEL LABEL;

Private Display Disp;

PRIVATE STRING Disptxt;

Public noticeView () {

ResourceSPLUGIN.GETWORKSPACE (). AddResourceChangelistener (this,

IResourceChangeeevent.pre_close

| IResourceChangeeevent.pre_delete

| IResourceChangeeevent.pre_auto_build

| IResourceChangeeevent.post_auto_build

| IResourceChangeeevent.post_change);

}

Public Static iWorkspace getWorkspace () {

// ResourceSplugin plugin's main class!

Return resourcesplugin.getWorkspace ();

}

Public void createpartControl (Composite Parent) {

Label = new label (parent, swt.wrap);

Label.Settext ("Change your Project Status ...");

DISP = Display.getDefault ();

}

Public void setfocus () {

}

// Realize the ResourceChanged method in the IResourceChangeListener interface

Public void resourcechanged (iResourceChangeeevent Event) {

IResource res = event.getResource ();

Switch (Event.gettype ()) {

Case IResourceChangeEvent.pre_close:

Disptxt = Res.GetFullPath () "Is About to Closed!";

Break;

Case IResourceChangeEvent.pre_delete:

Disptxt = Res.GetFullPath () "Is About to be deleted!";

Break;

Case IResourceChangeeevent.post_change:

Try {

Event.getdelta (). accept (this);

} catch (coreexception e) {

E.PrintStackTrace ();

}

Break;

Case IResourceChangeeEvent.pre_auto_build: try {

Event.getdelta (). accept (this);

} catch (coreexception e) {

E.PrintStackTrace ();

}

Break;

Case IResourceChangeeevent.post_auto_build:

Try {

Event.getdelta (). accept (this);

} catch (coreexception e) {

E.PrintStackTrace ();

}

Break;

}

Disp.syncexec (this);

}

/ / Realize the Visit method in the IResourceDeltavisitor interface

Public Boolean Visit (IResourceDelta Delta) {

IResource Res = Delta.getResource ();

Switch (delta.getkind ()) {

Case IResourceDelta.Added:

Disptxt = "resource" res. GetFullPath () "WAS Added."

Break;

Case IResourceDelta.removed:

Disptxt = "resource" res. GetFullPath () "Was removed."

Break;

Case IResourceDelta.changed:

Disptxt = "resource" res. GetFullPath () "HAS Changed."

Break;

}

Return true; // visit the children

}

/ / Realize the Run method in the runnable interface

Public void run () {

Try {

Label.setText (Disptxt);

} catch (exception e) {

E.PrintStackTrace ();

}

}

}

The first Welcome plugin above, this new plugin also inherits ViewPart, and the difference is to implement three interfaces: Runnable, IResourceChangelistener, IResourceDeltaVisitor. Among them, everyone should be familiar with: multithreaded interface. IResourceChangeListener and IResourceDeltaVisitor are resource interfaces in the Eclipse system, where resources are items or files in Eclipse. In the process of running the NoticeView plugin, you can trigger events in both interfaces by adding, opening, deleting items, or files, and displays related information in our viewing window.

In the program, it is in the resourcechanged () function, and does not call the label.setText () method directly like everyone's imagination, but calls Disp.Syncexec (this), where DISP is DISPLAY Type objects. This is because the resourcechanged () method running thread and the Eclipse main thread running in the Lable in the plugin is not the same thread. If the label.setText () method is directly called, an exception will be thrown.

The following is also required to make some changes to the Plugin.xml in the project, mainly, plus the extended point declaration:

ID = "com.nidapeng.eclipse.plugin.pde" name = "PDE plugin"

Version = "1.0.0"

Provider-name = "nidapeng"

Class = "com.nidapeng.eclipse.plugin.pde.pdeplugin">

ID = "noticeView"

Name = "notice view"

Point = "org.eclipse.ui.views">

Name = "notice"

ID = "com.nidapeng.eclipse.plugin.pde.category1">

Name = "NOTICE Resource View"

Category = "com.nidapeng.eclipse.plugin.pde.category1"

Class = "com.nidapeng.eclipse.plugin.pde.noticeView"

ID = "com.nidapeng.eclipse.plugin.pde.view1">

This XML file and the Welcome plugin PLUGIN.XML are very close, and there is not much explanation here.

To run this plugin, you can use the run button in Eclipse because this project is a Plug-in Project, and the item will automatically run in Run-Time Workbench. After running, you will generate a platform with the current Eclipse. You can run the NoticeView plugin directly on this platform. You can use the plugin to do what features can be used, or use the direct Run-Time Workbench mode to debug the plugin. The process of installing plug-in and restarting Eclipse is eliminated. You can see that the process of using PDE development plug-ins is much smarter than the Java development environment!

The development of Eclipse is not limited to the development of plug-ins, which can also replace standard SWing in Java to develop Java-based independent applications GUI development. It has the advantage of being obvious: high speed, resource occupation, cross-platform, code open, and large companies support, etc.

Since Eclipse is still in the development phase, the author found that some performance is not very stable when using it, and some places will encounter a strange problem, requiring users to think about some ways. However, with the current development speed of Eclipse, I believe how long it will, and it will be gradually improved. At present, although eclipse has some shortcomings, it is not very impressed by the general impression of Eclipse, and the speed, the resource occupation is better than Ivj, and most of them are active, and even less unexpected at this stage Exit and other major bugs, I hope that the future Eclipse can truly reach the IVJ function, VisualCafe speed, becoming a major utility to develop software! Reference resources:

Www.eclipse.org is the resource station of Eclipse.

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

New Post(0)