Write your Eclipse extension

xiaoxiao2021-03-06  47

Extension is a critical mechanism in Eclipse, and PLUG-I uses the extension to add new features to the Eclipse platform. However, the extension cannot be created freely, and it must be clearly declared in accordance with the specifications defined by the extension point, and Eclipse can recognize these extensions. We can not only use the many ready-made extensions provided by Eclipse, but also define new extensions and extensions on this extension point. Of course, the definition of the extension points is more complicated. However, Eclipse provides the user with a graphical editing interface. We only need to enter some information, Eclipse automatically generates code, making the definition of the extension point very simple. Let's take a look at how to create a new extension point in Eclipse and extends on this extension. We need to do the following work: 1. Designed this extension 2. Define the extension point, that is, write the list file 3 of the extension point. Write code to load the extension of the extension We use the creation of the Worklist extension to take a detailed description. The function of Worklist is: Create a View where the function module available in the tree display system, by double-clicking a module node, executing the extended definition method (Method). In fact, it is equivalent to a console and runs different functions through the console. Since Eclipse is composed of a runtime core and a plurality of plug-ins, we also define the Worklist extension point in a plugin, and the code file for Worklist is also placed in this plugin, so that it is easy to find and modify, nor The code affects the Eclipse itself.

1. Define the extension first we want to create a plugin net.softapp.Worklist that stores new extension information, this plugin extends to org.eclipse.ui.views, the following is the plugin.xml file of the plugin in the Views extension point information : This can be popped up through" Window-> show view-> other " In the "SHOW View" dialog box, select "Worklist View", open the view, and we use this view to display all extended information of the Worklist extension point. The "Show View" dialog displays all views in Eclipse, that is, all org.eclipse.Views extensions extension. Understanding this is important because we can imitate the code of the Views extension point when writing a Worklist extension point code. Below, we have to define the Worklist extension in the net.softapp.worklist plugin. The definition of the extension point is in the case of Eclipse, in general, in the schema directory, we name the file Worklist.exsd.

The content is generated by PDE: [Enter Description of this extension point.]

< / Documentation> < ! - Definition category -> <

! - Define category / ID. When referenced Category, you must point out the ID of the app, and Name gives an intuitive name for display -> < / completion> ! - Defines Worklist / Category to store the Category location.

If category in the nested form is referenced in the form of parent_id / child_id-> < ! - Define Worklist / Class, implementation class name -> - The following is Auto-generated by PDE, unrelated to our programming -> [Enter the first release in which this extension points.] [Enter Extension Point Usage Example Here.]

[Enter API Information> [Enter Information About Supplied Implement) Of this extension point.]

So we define the extended properties. Then add: is defined! 2. After the extension definition is expanded, you will write about the correlation code that parsing this extension. Hiji, Eclipse provides us with a large number of APIs to call and save several code written. In addition, we can also learn from other codes implemented by Eclipse, by imitation to write our own parsing code. This example refers to the resolution section of the View. With View, we define WorkListDescriptor, WorkListRegistry, WorkListRegistryReader. WorkListDescriptor which complete parsing of the definition above, WorkListRegistry stored for other plug-ins to extend workList relevant information, WorkListRegistryReader from WorkListRegistry read the information at our disposal. The code here is whisper, please refer to the ViewDescriptor, ViewRegistry, ViewRegistryReader related code for the View Directive section of the VIEW implementation. 3. Writing the interface part According to the extension of the VIEW, we need to write the interface part. Please refer to the writing of the View plugin here.

Here we add a method to WorkListPlugin extended to read information from the registry: public IWorkListRegistry getWorkListRegistry () {if (workListRegistry == null) {workListRegistry = new WorkListRegistry (); try {WorkListRegistryReader reader = new WorkListRegistryReader () ; reader.readWorkList (Platform.getExtensionRegistry (), workListRegistry);} catch (CoreException e) {// can not safely show a dialog so log it WorkbenchPlugin.log ( "Unable to read workList registry.", e.getStatus ()) ; // $ NON-NLS-1 $}} return workListRegistry;} wherein WorkListRegistryReader.readWorkList defined as follows: / ** * Read the workList extensions within a registry * / public void readWorkList (IExtensionRegistry in, workListRegistry out) throws CoreException {. // this does not seem to really ever be throwing an the exception workListRegistry = out; readRegistry (. in, WorkListPlugin.getDefault () getPluginId (), "workList"); out.mapWorkListToCategories ();} visible, we do not need to write Complex code can read the extension information stored in the registry! We workList extended display is the use of TreeView, the following code (WorkListView): protected TreeViewer createViewer (Composite parent) {TreeViewer viewer = new TreeViewer (parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); viewer.setUseHashlookup ( true); viewer.setContentProvider (new WorkListContentProvider ()); viewer.setLabelProvider (new WorkListLabelProvider ()); workListReg = WorkListPlugin.getDefault () getWorkListRegistry ();. viewer.setInput (workListReg); initListeners (viewer); return viewer; } This will be displayed.

So, how do you implement a certain extension? We add TreeViewer a mouse double-click events to support, key code is as follows: protected void handleDoubleClick (DoubleClickEvent event) {IStructuredSelection selection = (IStructuredSelection) event.getSelection (); Object element = selection.getFirstElement (); TreeViewer viewer = getWorkListViewer (); if (viewer.isExpandable (element)) {viewer.setExpandedState (element, viewer.getExpandedState (element)!);} else {WorkListDescriptor workList = (WorkListDescriptor) element; try {IWorkListPart workListPart = (IWorkListPart) workList.createWorkList (); Worklistpart.run ();} catch (coreexception e) {// Should Add Something to Handle The Exception}}

} Where iWorkListPart is simple, all interfaces that implement Worklist extensions must be implemented: public interface iWorklistPart {public void run ();

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

New Post(0)