gef 的command 是用于执行具体操作的,一般在类的属性中保有要操作的对象, 这些对象是在EditPolicy创建这个类时通过setter注入的,一般是在EditPolicy的相关方法中创建一个command类 ,通过注入相应的要操作的组件信息, 这些信息可能是从request中获得,也可能通过相应的EditPolicy的父类的方法获得,然后注入到command里, 最后通过execute方法执行操作,执行之前还重写canExecute方法判断一下是否可以执行,如果false就不允许execute
command里还有undo ,redo,redo就是默认执行execute,一般不用去管他, undo就是取消操作,一般要重写,而且在command中要保有之前的属性以便undo操作, 这里就涉及一个顺序问题,一般先是执行了某个操作之后再来回滚,所以是先执行execute方法之后才会去执行,undo 。 所以command中那些用来保留的属性应该是在execute方法中设置的,然后在undo方法中再拿来用
public class MoveBendpointCommand extends Command {
//操作的对象
private AbstractConnectionModel connection;
//保留的属性
private Point oldLocation;
//操作的相关属性
private Point newLocation;
//操作的相关属性
private int index;
public AbstractConnectionModel getConnection() {
return connection;
}
public Point getOldlLocation() {
return oldLocation;
}
public Point getNewLocation() {
return newLocation;
}
public int getIndex() {
return index;
}
public void setConnection(Object connection) {
this.connection = (AbstractConnectionModel) connection;
}
public void setOldlLocation(Object oldlLocation) {
this.oldLocation = (Point) oldlLocation;
}
public void setNewLocation(Object newLocation) {
this.newLocation = (Point) newLocation;
}
public void setIndex(int index) {
this.index = index;
}
@Override
public void execute() {
oldLocation=(Point)connection.getBendpoints().get(index);
connection.replaceBendpoint(index, newLocation);
}
@Override
public void undo() {
connection.replaceBendpoint(index, oldLocation);
}
}