Eclipse Getting Started: Developing Eclipse Plugins

zhaozj2021-02-16  70

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:

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, IResourceChangeEvent.PRE_CLOSE | IResourceChangeEvent.PRE_DELETE | IResourceChangeEvent.PRE_AUTO_BUILD | IResourceChangeEvent.POST_AUTO_BUILD | IResourceChangeEvent.POST_CHANGE);} public static IWorkspace getWorkspace () {// main class ResourcesPlugin plug! 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 () {} // resourceChanged implemented method IResourceChangeListener interface public void resourceChanged (IResourceChangeEvent event) {IResource res = event.getResource (); switch (event.getType ()) {case IResourceChangeEvent.PRE_CLOSE: dispTxt = Res. GetFullPath () "is about to closed!"; break; cassercetheevent.pre_delete: disptxt = res. max, () "is about to be deleted!";

case IResourceChangeEvent.POST_CHANGE: try {event.getDelta () accept (this);.} catch (CoreException e) {e.printStackTrace ();} break; case IResourceChangeEvent.PRE_AUTO_BUILD: try {event.getDelta () accept (this. );} catch (CoreException e) {e.printStackTrace ();} break; case IResourceChangeEvent.POST_AUTO_BUILD:. try {event.getDelta () accept (this);} catch (CoreException e) {e.printStackTrace ();} break;} disp.syncExec (this);} // visit method implemented public boolean visit (IResourceDelta delta) {IResource res = delta.getResource () IResourceDeltaVisitor interface; 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 : DI SPTXT = "resource" res. GetFullPath () "HAS Changed."; Break;} Return True; // Visit the children} // Implement 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, different 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:

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!

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

New Post(0)