Call add feature from within a custom feature [message #1720183] |
Thu, 14 January 2016 18:56  |
Eclipse User |
|
|
|
Hi all!
I'm quite a newbie at graphiti and taking my first steps.
I'm creating a small editor for creating graphs and applying algorithms to them and I've come across an issue.
I've made a custom feature that applies an algorithm that alters the domain objects from my model (creates some new edges in my graph).
After the algortithm is applied (in my custom feature) I'd like to check the model and add the graphics algorithms for my newly created domain objects. I guess the best way would be to use the already-implemented add feature, calling it several times, one for each newly created domaing object that has not a linked GA, right?
Which would be the best way to do this? I've searched across the tutorial but have not seen any example of calling a feature from within another feature. Should I first create and populate a context and then just instantiate and call the feature?
Thanks!
|
|
|
|
Re: Call add feature from within a custom feature [message #1720281 is a reply to message #1720252] |
Fri, 15 January 2016 16:06  |
Eclipse User |
|
|
|
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.04111 seconds