|
|
Re: Call add feature from within a custom feature [message #1720281 is a reply to message #1720252] |
Fri, 15 January 2016 21:06 |
David G. Messages: 10 Registered: April 2015 Location: Madrid, Spain |
Junior Member |
|
|
Thanks for the tip!
On with this, I'm adding 'Edge's (my domain object) wich are represented as connections, so I need to provide an ICreateConnectionConext.
For this, I need to provide the anchors, and it has been tricky, but finally got it working.
I'll post the code here, so maybe it helps someone else. Any improvements or corrections are more than welcome!
public void execute(ICustomContext context) {
// Access the graph
Graph theGraph = ExampleUtil.getRootGraph(getDiagram());
// Instance the related algorithm
ConvexHullAlgorithm gda = new ConvexHullAlgorithm();
// Initialize the algorithm with the graph
gda.setInitialGraph(theGraph);
// Run the algorithm to the end
gda.runToEnd();
// Model is modified now, with new edges
// Get linkservice to ask for relations between DOs and PEs
ILinkService linkserv = Graphiti.getLinkService();
// Check the edges in the domain model
for (Edge edge : theGraph.getEdges()) {
// If an edge without graphical representation is found...
if (linkserv.getPictogramElements(getDiagram(), edge).isEmpty()) {
// Call add feature to add the edge
// We need the source anchor (from the first connected node)
Anchor sourceAnchor = null;
for (PictogramElement aPe : linkserv.getPictogramElements(getDiagram(),
edge.getConnectedNodes().get(0))) {
if (aPe instanceof ContainerShape) {
sourceAnchor = ((ContainerShape) aPe).getAnchors().get(0);
}
}
// And the target anchor (from the second connected node)
Anchor targetAnchor = null;
for (PictogramElement aPe : linkserv.getPictogramElements(getDiagram(),
edge.getConnectedNodes().get(1))) {
if (aPe instanceof ContainerShape) {
targetAnchor = ((ContainerShape) aPe).getAnchors().get(0);
}
}
// Now we can create the add connection context with both anchors
AddConnectionContext acc = new AddConnectionContext(sourceAnchor, targetAnchor);
// ... and the new edge
acc.setNewObject(edge);
// ... to be created into the diagram
acc.setTargetContainer(getDiagram());
// Instantiate the add feature
AddEdgeConnectionFeature addCF = new AddEdgeConnectionFeature(getFeatureProvider());
// And, if it can be added
if (addCF.canAdd(acc)) {
// Add and update
PictogramElement pe = addCF.add(acc);
updatePictogramElement(pe);
}
}
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.03417 seconds