用Eclipse插件GEF在编辑器上画好图形,怎么做才能使图形之间相互不重叠?把图形移过去跟另一图形重叠后,放开鼠标
图形会自动移到距离最近的不重叠得区域。请高手指明怎么做?有代码最好。

解决方案 »

  1.   

    我用GMF做过,其实跟GEF也差不多。有一种思路是在图形的上层模型的EditPart上添加EditPolicy来限制,GMF和
    GEF中都有自带的XYLayoutEditPolicy来进行布局管理的。
      

  2.   

    在放置图形的模型的EditPart中添加一个EditPolicy,这个EditPolicy自己写,继承于XYLayoutEditPolicy,重写
    getConstraintFor(Rectangle r)这个方法,这个方法就是限制布局的,在里面写控制的算法即可.注意最后返回的
    是Rectangle.
      

  3.   


    public class FigureOperateEditPolicy extends XYLayoutEditPolicy { private GraphicalEditPart part = null; protected Object getConstraintFor(ChangeBoundsRequest request, GraphicalEditPart child) {
    part = child;
    return super.getConstraintFor(request, child);
    } public Object getConstraintFor(Rectangle r) { if (r instanceof PrecisionRectangle) {
    PrecisionRectangle pr = (PrecisionRectangle) r;
    if (r.height == -1) {
    r.height = (int) Math.round(pr.preciseHeight);
    }
    if (r.width == -1) {
    r.width = (int) Math.round(pr.preciseWidth);
    }
    }
                    //下面这个EditPart用你自己的,我上面说过
    DataModelDiagramEditPart root = EditPartUtil.getDataModelDiagramEditPart(getHost());
    if (EditPartUtil.hitTest(root, r)) {
    return EditPartUtil.findConstraint(root, part);
    } return super.getConstraintFor(r);
    }}public static boolean hitTest(DataModelDiagramEditPart root, Rectangle r) {
    if (root == null || r == null) {
    return false;
    } List<?> selParts = root.getViewer().getSelectedEditParts();
    for (EditPart child : getAllChildEditPartList(root)) {
                             //下面的EntityEditPart和ViewEditPart换成你的图形的EditPart
    if (child instanceof EntityEditPart || child instanceof ViewEditPart) {
    if (selParts != null && selParts.contains(child)) {
    continue;
    } Rectangle to = ((GraphicalEditPart) child).getFigure().getBounds();
    if (isHitEachOther(r, new PrecisionRectangle(to))) {
    return true;
    }
    }
    } return false;
    }public static boolean isHitEachOther(Rectangle from, Rectangle to) {
    boolean hitH = false; if (from.x > to.x) {
    if (to.x + to.width > from.x) {
    hitH = true;
    }
    } else {
    if (from.x + from.width > to.x) {
    hitH = true;
    }
    } boolean hitV = false;
    if (from.y > to.y) {
    if (to.y + to.height > from.y) {
    hitV = true;
    }
    } else {
    if (from.y + from.height > to.y) {
    hitV = true;
    }
    } return (hitH & hitV);
    }public static List<EditPart> getAllChildEditPartList(EditPart root) {
    List<EditPart> partList = new ArrayList<EditPart>();
    if (root == null) {
    return partList;
    } for (Object obj : root.getChildren()) {
    if (obj instanceof EditPart) {
    EditPart child = (EditPart) obj;
    partList.add(child);
    partList.addAll(getAllChildEditPartList(child));
    }
    } return partList;
    }public static Rectangle findConstraint(DataModelDiagramEditPart root, EditPart part) {
    if (root == null || part == null) {
    return null;
    } List<?> selParts = root.getViewer().getSelectedEditParts();
    if (selParts == null ){
    return null;
    }

    for (EditPart child : getAllChildEditPartList(root)) {
                            //跟上面说的一样,换成你自己的
    if (child instanceof EntityEditPart || child instanceof ViewEditPart) {
    if (!selParts.contains(child)) {
    continue;
    }

    if (part == child) {
    Rectangle to = ((GraphicalEditPart) child).getFigure().getBounds();
    return new PrecisionRectangle(to);
    }
    }
    } return null;
    }
      

  4.   

    其实跟GEF也差不多。有一种思路是在图形的上层模型的EditPart上添加EditPolicy来限制,GMF和 
    GEF中都有自带的XYLayoutEditPolicy来进行布局管理的。