Adding numbers using the context menu - Eclipse GEF

All,

I am creating a plugin with a smaller eclipse volume, where I add numbers to the user editor through the context menu, but I do not find a way to do this. Can someone advise me how to dynamically add numbers to the editor through the context menu, that is, add actions / commands.


Since the development of the Eclipse GEF plugin finds fewer examples to look at, I am adding my solution so that others find it useful. This code helps display node in the editor.

Source code for the Action class to display shapes in the editor:

public class AddNodeAction extends EditorPartAction
{
 public static final String ADD_NODE = "ADDNODE";

 public AddNodeAction(IEditorPart editor) {
  super(editor);
            setText("Add a Node");
            setId(ADD_NODE);     // Important to set ID
 }

 public void run()
 {
  <ParentModelClass> parent=  (<ParentModelClass>)getEditorPart().getAdapter(<ParentModelClass>.class);

  if (parent== null)
   return;
  CommandStack command = (CommandStack)getEditorPart().getAdapter(CommandStack.class);

  if (command != null)
  {
   CompoundCommand totalCmd = new CompoundCommand();
   <ChildModelToRenderFigureCommand>cmd = new <ChildModelToRenderFigureCommand>(parent);
   cmd.setParent(parent);
   <ChildModelClass> newNode = new <ChildModelClass>();
   cmd.setNode(newNode);
   cmd.setLocation(getLocation()); // Any location you wish to set to
   totalCmd.add(cmd);
   command.execute(totalCmd);
  }
 }

 @Override
 protected boolean calculateEnabled() 
 {
  return true;
 }
}
+5
source share
1 answer

, . , , GEF , MVC, , View EditParts .

, :

  • CreateCommand
    • , , ( )
  • CreateAction
    • CreateCommand, editdomain
  • ContextMenuProvider
    • , CreateAction

GMF, , , GMF, , editparts , .

EDIT: , .

public void run() {
   // Fetch viewer from editor part (might not work, if not, try some other way)
   EditPartViewer viewer = (EditPartViewer) part.getAdapter(EditPartViewer.class);
   // get Target EditPart that is under the mouse
   EditPart targetEditPart = viewer.findObjectAt(getLocation());
   // If nothing under mouse, set root item as target (just playing safe)
   if(targetEditPart == null)
       targetEditPart = viewer.getContents();

   // Make and initialize create request with proper information
   CreateRequest createReq = new CreateRequest();
   createReq.setLocation(getLocation());
   createReq.setFactory(new OwnFactoryImplementation());

   // Ask from target editpart command for this request
   Command command = targetEditPart.getCommand(createReq);

   // If command is ok, and it can be executed, go and execute it on commandstack
  if(command != null && command.canExecute()) {
      viewer.getEditDomain().getCommandStack().execute(command);
  }
}

, editpart , , , agaist .

, , EditPolicy EditPart. EditPolicies EditParts createDefaultEditPolicies(). EditPolicy , CreateRequest. , .

, ( - EditPart): Diagram

, , . , , , , , Command-Request Pattern, .

+7

All Articles