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 :
The content is generated by PDE: XML Version = '1.0' encoding = 'UTF-8'?>
! - Define category / ID. When referenced Category, you must point out the ID of the app, and Name gives an intuitive name for display ->
If category in the nested form is referenced in the form of parent_id / child_id->
schema> So we define the extended properties. Then add:
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 ();