【发布时间】: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