0 0

GEF中节点移动时,坐标错误?5

大家好:
       我根据教程写了一个简单的GEF程序,在移动节点的时候,节点总是不能正确的移动到新的位置,请大家帮忙看一下:
这个就是节点的EditPart。
public class EllipsePart extends AbstractGraphicalEditPart implements
        PropertyChangeListener {

    @Override
    protected IFigure createFigure() {
        EllipseFigure figure = new EllipseFigure();
        figure.setLayoutManager(new XYLayout());
        return figure;
    }

    protected void refreshVisuals() {
        Ellipse node = (Ellipse) this.getModel();
        Point location = node.getLocation();
        System.out.println(node+"->location:" + location);
        Rectangle rectangle = new Rectangle(location, node.getSize());
        ((GraphicalEditPart) getParent()).setLayoutConstraint(this,
                getFigure(), rectangle);

    }

    protected void createEditPolicies() {
      
    }

    @Override
    public void activate() {
        if (isActive()) {
            return;
        }
        super.activate();
        ((Ellipse) getModel()).addPropertyChangeListener(this);
    }

    @Override
    public void deactivate() {
        if (!isActive()) {
            return;
        }
        super.deactivate();
        ((Ellipse) getModel()).removePropertyChangeListener(this);
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals("location"))
            refreshVisuals();
    }

}
这个是代表整个图的EditPart:
public class DiagramPart extends AbstractGraphicalEditPart implements
        PropertyChangeListener {

    @Override
    protected IFigure createFigure() {
        return new DiagramFigure();
    }

    protected List getModelChildren() {

        List children = ((Diagram) getModel()).getChildren();

        return children;

    }

    @Override
    protected void createEditPolicies() {
        installEditPolicy(EditPolicy.COMPONENT_ROLE, new DiagramLayoutEditPolicy());
    }

    @Override
    public void activate() {
        if (isActive()) {
            return;
        }
        super.activate();
        ((Diagram) getModel()).addPropertyChangeListener(this);
    }

    @Override
    public void deactivate() {
        if (!isActive()) {
            return;
        }
        super.deactivate();
        ((Diagram) getModel()).removePropertyChangeListener(this);
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        String propertyName = evt.getPropertyName();
        if ("children".equals(propertyName)) {
            refreshChildren();
        }
    }

}

DiagramLayoutEditPolicy.java

public class DiagramLayoutEditPolicy extends XYLayoutEditPolicy {
    // Abstract methods from ConstrainedLayoutEditPolicy
    protected Command createAddCommand(EditPart child, Object constraint) {
        return null; // no support for adding
    }

    protected Command createChangeConstraintCommand(EditPart child,
            Object constraint) {
        // return a command to move the part to the location given by the
        // constraint
        System.out.println("old xy:" + ((Ellipse) child.getModel()).getLocation());
        System.out.println("new xy:" + ((Rectangle) constraint).getLocation());
        MoveCommand locationCommand = new MoveCommand();
        locationCommand.setNode((Ellipse) child.getModel());
        locationCommand.setLocation(((Rectangle) constraint).getLocation());
        return locationCommand;
    }

    protected Command getCreateCommand(CreateRequest request) {
        return null; // no support for creating
    }

    protected Command getDeleteDependantCommand(Request request) {
        return null; // no support for deleting a dependant
    }
}

MoveCommand.java

public class MoveCommand extends Command {
    private Ellipse node;
    private Point newPos;
    private Point oldPos;

    public void setLocation(Point p) {
        this.newPos = p;
    }

    public void setNode(Ellipse node) {
        this.node = node;
    }

    public String getLabel() {
        return "Move Node";
    }

    public void execute() {
        oldPos = this.node.getLocation();
        this.node.setLocation(newPos);
        System.out.println(node+"->after execute:" + this.node.getLocation());
    }

    public void undo() {
        this.node.setLocation(oldPos);
    }

    public void redo() {
        this.node.setLocation(newPos);
    }
}

根据上面的程序,在控制台system out出来的结果是这样的:
old xy:Point(192, 253)
new xy:Point(252, 231)
com.ziscloud.zcdiagram.diagram.Ellipse@9c6201->location:Point(192, 253)
com.ziscloud.zcdiagram.diagram.Ellipse@9c6201->after execute:Point(252, 231)

好像节点EllipsePart中的refreshVisuals方法在MoveCommand中的execute方法先执行,使得在refreshVisuals时,节点的坐标不是最新的,请问这是什么回事啊?
2009年6月05日 12:58
目前还没有答案

相关推荐

Global site tag (gtag.js) - Google Analytics