[英]Insert vertex inside a Graph
I want to add a new vertex inside an existing graph. 我想在现有图形内添加一个新顶点。 So I created a new cell and i'm attempted to reconnected my edge but my graph doesn't update (for the edges)
因此,我创建了一个新单元格,并尝试重新连接边缘,但是我的图形没有更新(对于边缘)
This is my code : 这是我的代码:
mxGraph graph = editor.getGraph();
mxCell selectedElt = (mxCell) graph.getSelectionCell();
Object cells[] = { selectedElt };
if (selectedElt.isEdge()) {
// cell is an edge, so we have source and target
System.out.println("Source : " + selectedElt.getSource().getId());
System.out.println("Target : " + selectedElt.getTarget().getId());
} else {
// edge before
mxCell beforeEdge = (mxCell) selectedElt.getEdgeAt(0);
// edge after
mxCell afterEdge = (mxCell) selectedElt.getEdgeAt(1);
// moving down the selected cell
graph.moveCells(cells, 0, 50);
// create a new vertex
GraphStyle graphStyle = new GraphStyle(graph);
mxCell cell = new mxCell("AAM",
new mxGeometry(selectedElt.getGeometry().getX(), selectedElt.getGeometry().getY(), 80, 50),
graphStyle.getCalculatorStyleName());
cell.setVertex(true);
beforeEdge.setTarget(cell);
graph.insertEdge(graph.getDefaultParent(), "e33", "", cell, selectedElt);
graph.addCell(cell);
graph.repaint();
}
Instead of calling beforeEdge.setTarget(cell)
try cell.insertEdge(beforeEdge, false)
. 而不是调用
beforeEdge.setTarget(cell)
尝试cell.insertEdge(beforeEdge, false)
。 This will remove the Edge from the previous vertex and add it to the new vertex. 这将从先前的顶点中删除边缘,并将其添加到新的顶点中。
Btw. 顺便说一句。 I suggest to wrapp your code into a try-finally block, like this:
我建议将代码包装到try-finally块中,如下所示:
graph.getModel().beginUpdate();
try {
// do all the graph related stuff
}
finally {
graph.getModel().endUpdate();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.