使用上下文菜单添加数字 - Eclipse GEF

nam*_*ked 5 java eclipse-gef

所有,

我正在创建一个调色板少eclipse插件,通过上下文菜单向自定义编辑器添加数字,但我找不到方法.任何人都可以指导我如何通过上下文菜单动态添加数字到编辑器,即添加动作/命令.


由于Eclipse GEF插件开发发现了更少的示例,我正在添加我的解决方案,以便其他人发现它很有用.此代码有助于将节点呈现给编辑器.

Action类的源代码,用于向编辑器呈现数字:

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;
 }
}
Run Code Online (Sandbox Code Playgroud)

Tuu*_*oos 7

我认为你需要多种不同的东西.请记住,GEF希望你有适当的MVC模式,你有自己的模型,数字作为View和EditParts作为控制器.

从我的头脑中我会说你至少需要这些东西:

  • CreateCommand
    • 包含将新数据添加到数据模型时需要执行的所有模型级别修改(可撤消和事务性)
  • CreateAction
    • 制作CreateCommand实例,使用当前选择对其进行初始化,并在editdomain中执行该命令
  • ContextMenuProvider
    • 将CreateAction提供给上下文菜单

如果你碰巧使用GMF,当你在命令中进行模型修改时,规范机制会自动为你生成editparts,但如果你没有使用GMF,你必须确保你自己的模型和editparts正在处理和刷新新项目正确添加.

编辑:好的,这里有一些代码建议与播放请求.

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);
  }
}
Run Code Online (Sandbox Code Playgroud)

现在发生的事情是将要求创建editpart,因此动作本身不知道命令如何工作,是什么使它成为客观的命令.

因此,为了使工作正常,您需要在EditPart中安装新的EditPolicy.EditPolicies可以安装在EditParts的createDefaultEditPolicies()函数中.当存在CreateRequest时,此EditPolicy必须响应并返回命令.这样,任何孩子都可以提供自己的命令来为自己创造孩子.

这是一个很好的图像如何工作(控制器是EditPart): 图

请问我是否可以帮助你.我知道这看起来有点复杂,但这会让你自己的生活变得更加容易,而且在你完成之后,你实际上很了解Command-Request Pattern,它可以在许多不同的地方重复使用.