GEF中组件删除功能的实现

跟着入门教程一步一步走下来,好歹有个可视化编辑器的样子,看起来还像那么回事。
回过头,发现添加的组件还没有删除功能。现在介绍一下如何实现组件删除的功能。

step 1:要能删除组件必须要有菜单、按钮或者用del键,于是需要在你编辑器的ActionBarContributor中添加相关的代码来获得主工具条上的删除按钮,同时也将激活edit菜单中的delete选项。
public void contributeToToolBar(IToolBarManager toolBarManager) {
// TODO Auto-generated method stub
……
toolBarManager.add(getAction(ActionFactory.DELETE.getId()));
……
}
关于工具条菜单部分功能实现的详细介绍,八进制的blog上有详细介绍。

step 2:动作触发后要有editPart接收相应的request,然后转交给某个editPolicy进行处理。在删除过程中,是被删除组件的editPart接收删除request。在这个editPart中注册一个继承于ComponentEditPolicy的policy,editpart将把request转交给这个policy处理。在policy中需要重写createDeleteCommand(GroupRequest deleteRequest)方法来生成对删除动作进行处理的command对象,并对该对象的属性进行一些设置,然后返回。
代码如下:
NodeEditPart类中
protected void createEditPolicies() {
// TODO Auto-generated method stub
……
installEditPolicy(EditPolicy.COMPONENT_ROLE, new NodeEditPolicy());


……
}

NodeEditPolicy类


public class NodeEditPolicy extends ComponentEditPolicy {

public NodeEditPolicy() {
super();
// TODO Auto-generated constructor stub
}
protected Command createDeleteCommand(GroupRequest deleteRequest) {
// TODO Auto-generated method stub
Object parent = getHost().getParent().getModel();
RemoveNodeCommand command = new RemoveNodeCommand();
command.setParent((BaseModel)parent);
command.setChild((BaseModel)getHost().getModel());
return command;
}

}


step3:实现command类


public class RemoveNodeCommand extends Command {

//private List parent;
private NodeModel parent;
private NodeModel child;

public NodeModel getChild() {
return child;
}

public void setChild(NodeModel child) {
this.child = child;
}

public NodeModel getParent() {
return parent;
}

public void setParent(NodeModel parent) {
this.parent = parent;
}

public RemoveNodeCommand() {
super();
// TODO Auto-generated constructor stub
}

public RemoveNodeCommand(String label) {
super(label);
// TODO Auto-generated constructor stub
}

public void execute() {
// TODO Auto-generated method stub
Assert.isNotNull(parent);
Assert.isNotNull(child);
parent.removeChild(child);
}

}

当然容器类的模型要实现removeChild方法才行
public void removeChild(nodeModelchild){
child.setParent(null);
getChildren().remove(child);
this.fireChildrenChange(child);
}

以上介绍的这些是我进行实现的一个过程,实际实现的时候应该抽象出一个基类模型和对应的editpart,这样就不用重复在每个组件中编写了。实现完成后工具条上的按钮,主菜单中的删除选项,键盘的del键都可以用来进行删除操作。
注意这里没有写command里的redo和undo操作。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值