双桅渔船  
漂泊,没有终点
日历
<2006年1月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234
统计
  • 随笔 - 5
  • 文章 - 0
  • 评论 - 3
  • 引用 - 0

导航

常用链接

留言簿(1)

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 

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

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操作。

posted on 2006-01-08 12:33 USTCEric 阅读(475) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 
 
Copyright © USTCEric Powered by: 博客园 模板提供:沪江博客