用Eclipse插件GEF在编辑器上画好图形,怎么做才能使图形之间相互不重叠?把图形移过去跟另一图形重叠后,放开鼠标
图形会自动移到距离最近的不重叠得区域。请高手指明怎么做?有代码最好。
图形会自动移到距离最近的不重叠得区域。请高手指明怎么做?有代码最好。
解决方案 »
- exe4j 转jar到exe问题
- 如何去掉无用的小数点和零
- 简单GUI界面,提示main函数异常!没能看出来什么原因,帮忙看看,在线等……
- 要做个打字记时的东西希望大家帮忙进来看看
- 分析时间出现问题,求救
- 关于等待 窗口的处理经过的 方法,大家有什么高见没?
- 菜鸟入门百思不得其解???
- JBUILDER7为什么在脚本状态下怎么为光标的位置不对呢?
- 请问那里有《Effective Java Programming Language Guide》的原版书下载或者卖?谢谢(内空)
- 请问这两个程序为什么为这个结果?(请写详细,谢谢)
- 字符串数组元素比较和统计的逻辑
- 我觉得NumberFormatException 根本不能算运行时异常。。。。质疑ing
GEF中都有自带的XYLayoutEditPolicy来进行布局管理的。
getConstraintFor(Rectangle r)这个方法,这个方法就是限制布局的,在里面写控制的算法即可.注意最后返回的
是Rectangle.
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;
}
GEF中都有自带的XYLayoutEditPolicy来进行布局管理的。