Functions of the contributionitemprovider extension point in GMF and org. eclipse. UI. editexceptions is similar to adding actions to the specified editor. However, you can use contributionitemprovider to add actions to the view and add popupmenu and other functions, it is equivalent to a set of multiple action-related extension points provided by eclipse. The following example shows how to use contributionitemprovider to add action to editor.
First, describe the contributionitemprovider Implementation Method in plugin. xml. In the following XMLCodeFirst, use the contributionitemprovider element to specify the implementation Class As COM. my. mycontributionitemprovider. This implementation class can be divided into two situations: Generally, it inherits from the abstractcontributionitemprovider provided by GMF; you can also directly implement icontributionitemprovider, in the latter case, no other elements under the contributionitemprovider element need to be defined. All actions can be constructed using Java code in the contributetoactionbars () and contributetopopupmenu () methods.
< Extension Point = "Org. Eclipse. GMF. runtime. Common. UI. Services. Action. contributionitemproviders" > < Contributionitemprovider Checkpluginloaded = "False" Class = "Com. My. mycontributionitemprovider" > < Priority Name = "Low" /> < Partcontribution ID = "Com. My. rulediagrameditorid" > < Partaction ID = "Showconsole" Menubarpath = "/Window/views" > </ Partaction > </ Partcontribution > </ Contributionitemprovider > </ Extension >
Now we will discuss the inheritance of abstractcontributionitemprovider. We need to implement the createaction () method, which accepts actionid as a parameter. The actionid parameter is in plugin. in XML, the partcontribution element is used to specify the editor to which the action will be added, then, use the partaction element to specify other parameters such as the ID of the action to be added and the location of menubarpath.
In fact, the main function of abstractcontributionitemprovider is to parse the XML elements under contributionitemprovider and call the createaction () method based on the content of these elements () you can get the actionid and create the actual action class based on it. The following is the mycontributionitemprovider code corresponding to the preceding XML snippet:
Public Class Mycontributionitemprovider Extends Abstractcontributionitemprovider {@ override Protected Iaction createaction (string actionid, iworkbenchpartdescriptor partdescriptor ){ If (Actionid. Equals ("showconsole" ) {Iaction action = New Showviewaction ("& console", "console. View. ID" ); Action. setimagedescriptor (); Return Action ;} Return Super . Createaction (actionid, partdescriptor );} Class Showviewaction Extends Action {}}
Note that if editor and contributionitemprovider are not in the same plugin, you must specify the checkpluginloaded attribute of the contributionitemprovider element in plugin. XML to false. Otherwise, the contributionitemprovider will not be loaded. (Supplement 08/01/02: If the menupath settings are incorrect, contributionitemprovider may not be loaded. The correct menupath is "/file/print ")
Several reference links:
Http://dev.eclipse.org/newslists/news.eclipse.modeling.gmf/msg06035.html
Http://dev.eclipse.org/newslists/news.eclipse.technology.gmf/msg04270.html
Http://dev.eclipse.org/newslists/news.eclipse.modeling.gmf/msg00757.html
Http://dev.eclipse.org/newslists/news.eclipse.modeling.gmf/msg08196.html