题
全部,
我正在创建一个调色板较少的日食插件,其中正在通过上下文菜单向自定义编辑器添加数字,但没有找到一种方法。任何人都可以指导我如何通过上下文菜单动态地向编辑器添加数字,即添加操作/命令。
由于Eclipse GEF插件的开发发现了较少的示例,因此我添加了我的解决方案,因此其他人则发现它很有用。此代码有助于向编辑器渲染节点。
动作类的源代码将图形渲染给编辑器:
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;
}
}
解决方案
我认为您在这里需要多个不同的东西。请记住,GEF希望您拥有适当的MVC模式,其中您拥有自己的模型,作为视图和编辑派特作为控制器的数字。
从我的头顶上,我会说您至少需要这些东西:
- CreateCommand
- 包含您需要执行的所有模型级别修改,以将新数据添加到数据模型(Unboble and Transactional)
- 创造
- 制作该createCommand实例,用当前选择初始化它,并在EditDomain中执行该命令
- ContectionMenupRovider
- 提供对上下文菜单的创建
如果您碰巧使用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);
}
}
现在发生的事情是,将要求编辑派制作进行创建,因此动作本身不知道命令是如何工作的,是什么使其客观地agaist命令。
因此,为了使事情正常工作,您需要在编辑派台上安装新的EditPolicy。可以在EditParts createfaultEditPolicies()函数上安装EditPolicies。当有createrequest时,此编辑人员必须做出反应和返回命令。这样,任何孩子都可以为自己创造孩子提供自己的命令。
这是其工作原理的好图像(控制器是编辑):
请询问我是否可以为您提供更多帮助。我知道这看起来更复杂,但这使您的生活变得更加轻松,在您做到这一点之后,您实际上很好地理解了命令引用的模式,并且可以在许多不同的地方重复使用。
不隶属于 StackOverflow