Add a custom action to the GMF Application

Source: Internet
Author: User

Assume that the name of the project that GMF generates for you is com. example. digoal: Add a custom command in the right-click menu and associate it with the model element named activity. That is, only right-click an element of activity type, this custom command is available in the pop-up menu. The function of this command is to simply change the name attribute of the activity to "modified activity ". The implementation steps are as follows:

1. If you have not created one before, create one named COM. example. digoal. plugm plugin Project (hereinafter referred to as "custom project"). The purpose of creating this project is to customize the project and generate a GMF project.CodeSeparated;

2. Implement the org. Eclipse. UI. popupmenus extension point in the custom project. A "change" menu item will be added in the right-click menu, and the "name" command will be provided below;

 <  Extension  Point  = "Org. Eclipse. UI. popupmenus"  >    <  Objectcontribution  Adaptable = "False"  ID  = "Com. example. Custom. objectcontribution. activityeditpart"  Objectclass  = "Com. example. digoal. Edit. parts. activityeditpart"  >       <  Menu  ID  = "Bmachange"  Label  = "& Amp; change"  Path  = "Additions" >          <  Separator  Name  = "Group1"  />       </  Menu  >       <  Action  Class  = "Com. example. digoal. Popup. changeactivitynameaction"  Enablesfor  = "1"  ID = "Com. example. digoal. Popup. changeactivitynameaction"  Label  = "& Amp; name"  Menubarpath  = "Bmachange/group1"  />    </  Objectcontribution  >  </  Extension  > 

3. Implement the changeactivitynameaction class defined in the previous step. This class must not only implement iobjectactiondelegate (Requirements for popupmenus extension points), but also inherit from the abstractactiondelegate class (GMF requirements ). What we need to do is implement the dorun () method. first obtain the currently selected editpart, and then create a setrequest instance, which contains all the information for modifying the attribute operation, including the target object, attribute name, and new attribute value. Because the GetModel () method of editpart in GMF is not an element in the business model, but a view object. You need to call view # getelement () to obtain the element in the business model, in the code, we use viewutil # resolvesemanticelement () to directly obtain the activity object. In addition, GMF uses the transaction project of emft to operate the model. Therefore, the editpart. geteditingdomain () method produces a transactionaleditingdomain type.

With the request, we use it as the construction parameter to create a setvaluecommand (), which is a GMF command (implement Org. eclipse. GMF. runtime. common. core. command. icommand), used to change the attribute value. Finally, we need to execute this command. We know that the command is executed using commandstack, so that undo/Redo can be executed, but editpart. getdiagrameditdomain (). the commandstack obtained by getdiagramcommandstack () can only execute GEF commands (Org. eclipse. GEF. commands. command), so we need to wrap our command with icommandproxy (), so there is no problem.

 Public   Class ChangeactivitynameactionExtends  Abstractactiondelegate  Implements  Iobjectactiondelegate {  Protected   Void  Dorun (iprogressmonitor progressmonitor ){  //  Get the selected edit part Istructuredselection structuredselection = Getstructuredselection (); object selection = Structuredselection. getfirstelement (); igraphicaleditpart editpart =(Igraphicaleditpart) selection;  //  Create a command to modify its property Setrequest request = New  Setrequest (editpart. geteditingdomain (), viewutil. resolvesemanticelement (View) editpart. GetModel ()),  //  The semantic model Bmapackage. literals. activitycategory name, //  Name feature of activity "Modified activity "); //  New name value Setvaluecommand command = New  Setvaluecommand (request );  //  Do the work Editpart.getdiagrameditdomain().getdiagramcommandstack(cmd.exe cute ( New  Icommandproxy (command ));}} 

Update: You can use igraphicaleditpart # resolvesemanticelement () to directly obtain the eobject corresponding to the editpart. igraphicaleditpart # getnotationview () is the view object, which works the same as GetModel.

Run the following command:

Refer:

    • In the logic example provided by GMF, The createlogicelementactiondelegate. Java File
    • GMF tips, change names of newly created elements
    • GMF tutorial Part 3

Update (2007/07/17): It seems that GMF is more recommended to use ioperationhistory to modify the model. For example, in the action's dorun () method, write as follows:

Abstracttransactionalcommand command = New  Abstracttransactionalcommand (editingdomain, "Modifying the model" , Collections. empty_list ){  Protected  Commandresult doexecutewithresult (iprogressmonitor monitor, iadaptable info)  Throws  Executionexception { //  Modify the model here          Return  Commandresult. newokcommandresult ();}};  Try  {Operationhistoryfactory.getoperationhistory(cmd.exe cute (command,  New Subprogressmonitor (progressmonitor, 1 ), Null  );}  Catch  (Executionexception e) {mindmapdiagrameditorplugin. getinstance (). logerror ( "Unable to create model and digoal", e ); // $ NON-NLS-1 $ }

As mentioned in the GMF newsgroup: "In a GMF application, you shoshould probably never execute commands in a commandstack, because it will not be worth the effort of coordinating the iundocontexts applied to these commands and your GMF implements acttransactionalcommands to ensure that the Undo/Redo menus make sense. ", original article link

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.