假设以下简化的 EMF 模型结构:
Graph
/ \
Node Edge
在我的 GEF 编辑器中,EditPart
s 的组织方式如下。
GraphEditPart(图 = FreeformLayer)摘录
@Override
protected List<EObject> getModelChildren() {
List<EObject> childrenList = new ArrayList<EObject>();
Graph graph = (Graph) getModel();
childrenList.addAll(graph.getNodes());
return childrenList;
}
NodeEditPart(图=扩展Figure
)摘录(还显示了如何Edges
获取Node
源或目标)
@Override
protected List<Edge> getModelSourceConnections() {
Node node = (Node) getModel();
Graph graph = node.getGraph();
String nodeId = node.getId();
List<Edge> sourceList = new ArrayList<Edge>();
if (graph != null)
sourceList.addAll(graph.getOutEdges(nodeId));
return sourceList;
}
@Override
protected List<Edge> getModelTargetConnections() {
// Same principle as getModelSourceConnections
}
编辑器类摘录(以防万一)
@Override
protected void initializeGraphicalViewer() {
super.initializeGraphicalViewer();
GraphicalViewer viewer = getGraphicalViewer();
viewer.setContents(graph);
ScalableFreeformRootEditPart root = (ScalableFreeformRootEditPart) viewer.getRootEditPart();
ConnectionLayer connLayer = (ConnectionLayer) root.getLayer(LayerConstants.CONNECTION_LAYER);
GraphicalEditPart contentEditPart = (GraphicalEditPart) root.getContents();
ShortestPathConnectionRouter shortestPathConnectionRouter = new ShortestPathConnectionRouter(contentEditPart.getFigure());
connLayer.setConnectionRouter(shortestPathConnectionRouter);
}
所有EditPart
s 都有自己的适配器(扩展org.eclipse.emf.ecore.util.EContentAdapter
或实现org.eclipse.emf.common.notify.Adapter
)。
这导致了一个EditPart
结构,其中NodeEditPart
s 是 的孩子GraphEditPart
,EdgeEditPart
s 是孤儿,即它们没有parent
。Edge
因此,每当添加或删除 s时,我都难以刷新数字。
当我通过在中进行昂贵的迭代添加一个时,我已经设法使更新工作(通知为新创建的必须在()上注册:Edge
GraphAdapter
Edge
Graph
newEdge.setGraph(graph)
if (notification.getOldValue() == null && notification.getNewValue() instanceof Edge) {
for (Object ep : getChildren()) {
if (ep instanceof NodeEditPart) { // There are in fact other EditParts as well
Graph graph = (Graph) getModel();
NodeEditPart nep = (NodeEditPart) ep;
Node n = (Node) nep.getModel();
if (graph.getOutEdges(n.getSId()).contains(notification.getNewValue()) || graph.getInEdges(n.getSId()).contains(notification.getNewValue()))
nep.refresh();
}
[注意:如果 - 顺便说一句 - 你能想到任何更好的方法,请随时用你的解决方案来打我!]
问题
删除模型对象时,我无法可靠地Edge
从编辑器中删除图形!Edge
有时有效,有时无效。
我想不可靠性可能与以下事实有关:(a)我的现实生活模型具有三层抽象,以及(b)不同Adapter
的 EMF 并不总是能识别相同时间顺序的变化(?)。
execute()
fromEdgeDeleteCommand
简单的调用edge.setGraph(null)
,这会触发 EMF 自行清理(即,未连接到图的模型元素从模型中删除)。
当相应的模型对象是孤儿时,如何Edge
在删除相应的模型对象时可靠地删除 s 的数字?EditPart