【问题标题】:Adding figures using contextual menu - Eclipse GEF使用上下文菜单添加图形 - Eclipse GEF
【发布时间】:2010-12-24 17:46:26
【问题描述】:

全部,

我正在创建一个无调色板的 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;
 }
}

【问题讨论】:

    标签: java eclipse-gef


    【解决方案1】:

    我认为您在这里需要多种不同的东西。请记住,GEF 希望您拥有适当的 MVC 模式,您可以在其中拥有自己的模型,将图形作为视图,将 EditParts 作为控制器。

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

    • 创建命令
      • 包含您需要的所有模型级别修改 执行以将新数据添加到您的 数据模型(可撤销和事务性)
    • 创建操作
      • 创建 CreateCommand 实例,使用当前选择对其进行初始化并在编辑域中执行该命令
    • 上下文菜单提供者
      • 为上下文菜单提供 CreateAction

    如果您碰巧使用 GMF,当您在命令中进行模型修改时,规范机制会自动为您生成编辑部分,但如果您不使用 GMF,则必须确保您自己的模型和编辑部分正在处理并刷新正确添加的新项目。

    编辑: 好的,这里有一些关于请求的代码建议。

    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,因此操作本身不知道命令是如何工作的,是什么使它客观地反对命令。

    因此,为了使事情顺利进行,您需要将新的 EditPolicy 安装到您的 EditPart。 EditPolicies 可以安装在 EditParts createDefaultEditPolicies() 函数上。此 EditPolicy 必须在有 CreateRequest 时做出反应并返回命令。这样,任何孩子都可以提供自己的命令来为自己创建孩子。

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

    请问我是否可以为您提供更多帮助。我知道这看起来有点复杂,但这会让你自己的生活更轻松,并且在你这样做之后,你实际上已经很好地理解了命令请求模式,并且它可以在许多不同的地方重复使用。

    【讨论】:

    • 谢谢!!在发帖之前我已经找到了解决方案:) .. 用代码更新了我的帖子。如果您对此有任何建议,请告诉我。
    • 看起来不错,做法也很简单。当然,使用来自该位置鼠标下的editpart的请求会更合适。这使得鼠标下的元素可以决定要添加什么样的子元素。
    • Hotzilla:同意..但我正在向编辑器添加数字,即父母本身,所以我认为不需要任何孩子的编辑部分。如果我需要关注孩子的编辑部分,您能否建议我更改代码?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 2023-03-09
    • 2012-06-12
    相关资源
    最近更新 更多