所有
我正在创建一个调色板少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;
}
}
答案 0 :(得分:7)
我认为你需要多种不同的东西。请记住,GEF希望您拥有适当的MVC模式,您拥有自己的模型,数字为View和EditParts作为控制器。
从我的头脑中,我会说你至少需要这些东西:
如果您正在使用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);
}
}
现在发生的事情是将要求创建editpart,因此操作本身不知道该命令是如何工作的,是什么使它成为客观的命令。
为了使工作正常,您需要在EditPart中安装新的EditPolicy。 EditPolicies可以安装在EditParts的createDefaultEditPolicies()函数中。当存在CreateRequest时,此EditPolicy必须响应并返回命令。这样,任何孩子都可以为自己创造孩子提供自己的命令。
这是一个很好的图像如何工作(控制器是EditPart):
请问我是否可以帮助你。我知道这看起来有点复杂,但是这会让你自己的生活变得更加轻松,在你完成之后,你实际上非常了解Command-Request Pattern,它可以在很多不同的地方重复使用。