Home » Modeling » GMF (Graphical Modeling Framework) » Issue with ViewService.createEdge()
Issue with ViewService.createEdge() [message #791378] |
Sun, 05 February 2012 18:29 |
Marcello Vitaletti Messages: 7 Registered: July 2009 |
Junior Member |
|
|
I am having problems trying to programmatically generate the notation model from a semantic model which is already populated.
In debugging mode I see the issue is because the ViewService instance for some reason does not have providers for the create-edge operation.
An inspection of plugin-xml in the generated diagram project seems to indicate that all providers are configured
<extension point="org.eclipse.gmf.runtime.diagram.core.viewProviders" id="view-provider">
<?gmfgen generated="true"?>
<viewProvider class="mymodel.diagram.providers.MymodelViewProvider">
<Priority name="Lowest"/>
<context viewClass="org.eclipse.gmf.runtime.notation.Diagram" semanticHints="Mymodel"/>
<context viewClass="org.eclipse.gmf.runtime.notation.Node" semanticHints="2001,2002,2003"/>
<context viewClass="org.eclipse.gmf.runtime.notation.Edge" semanticHints="4001,4002,4003,4004"/>
</viewProvider>
</extension>
As a further check, I wrote the following code querying the ViewService service whether a provider is available for the node and edge create operations.
private boolean testViewServiceProviders(Mymodel model, Diagram diagram) {
Link testLink = model.getLinks().get(0); // Model class
Node testNode = testLink.getFromNode(); // Model class
View containerView = diagram; // Empty diagram, just created
IAdaptable semanticAdapter1 = new EObjectAdapter(testNode);
String semanticHint1 = MymodelVisualIDRegistry.getType(visualID(testNode));
boolean hasNodeViewProvider = ViewService.getInstance().provides(
org.eclipse.gmf.runtime.notation.Node.class,
semanticAdapter1, containerView, semanticHint1,
ViewUtil.APPEND, true, PreferencesHint.USE_DEFAULTS);
System.out.println("provides Node View Provider ? "+hasNodeViewProvider);
IAdaptable semanticAdapter2 = new EObjectAdapter(testLink);
String semanticHint2 = MymodelVisualIDRegistry.getType(visualID(testLink));
boolean hasEdgeViewProvider = ViewService.getInstance().provides(
org.eclipse.gmf.runtime.notation.Edge.class,
semanticAdapter2, containerView, semanticHint2,
ViewUtil.APPEND, true, PreferencesHint.USE_DEFAULTS);
System.out.println("provides Edge View Provider ? "+hasEdgeViewProvider);
return hasNodeViewProvider && hasEdgeViewProvider;
}
The code answers positively to the 1st check (createNode) but negatively to the 2nd check (createEdge).
Can you spot anything wrong with the above code? Or, should I report a bug? If so, any hint at a possible work-around?
Thanks a lot,
Marcello
|
|
|
Re: Issue with ViewService.createEdge() [message #791998 is a reply to message #791378] |
Mon, 06 February 2012 13:52 |
Michael Golubev Messages: 383 Registered: July 2009 |
Senior Member |
|
|
Hello,
If you look at the generated MymodelViewProvider class, you will see the difference wih respect to the usage of
IElementType elementType = getSemanticElementType(op.getSemanticAdapter()); in the methods of protected boolean provides(CreateNodeViewOperation op) { and protected boolean provides(CreateEdgeViewOperation op) {
That is, while the node-creation logic may work without an adapter to IElementType.class, the edge creation ultimately requires such adapter.
Changing the adapter from EObjectAdapter to something like EObjectAndElementTypeAdapter here should solve the problem.
Regards,
Michael "Borlander" Golubev
at Montages Think Tank, Prague, Czech Republic
Montages AG, Zürich, Switzerland
|
|
|
Re: Issue with ViewService.createEdge() [message #793818 is a reply to message #791998] |
Wed, 08 February 2012 16:03 |
Marcello Vitaletti Messages: 7 Registered: July 2009 |
Junior Member |
|
|
Hi Michael, thanks a lot. Following your explanation I am now able to create both nodes and edges.
Perhaps I want to contribute a few more details in case someone else needs to solve the same problem.
Here I am applying the same concept to a gmf-xtext sample code where the semantic model defines Entity and Reference, which I want to display as Node and Edge view elements, respectively.
The important thing is that I cannot use one of the ViewService static methods to create an Edge because in that case a semantic adapter would be created by the ViewService using EObjectAdapter, which as you explained cannot do for Edges...
I don't know whether this should be considered a bug, however I avoided this problem by calling one of the non static methods on the singleton instance:
IElementTypeAwareAdapter semanticAdapter = new EObjectAndElementTypeAdapter(reference, refVisualID);
Edge viewEdge = ViewService.getInstance().createEdge(semanticAdapter, diagram,
EntitiesVisualIDRegistry.getType(refVisualID), ViewUtil.APPEND, true,
PreferencesHint.USE_DEFAULTS);
...
// Set edge's source and target nodes
viewEdge.setSource(viewNode);
viewEdge.setTarget(viewTarget);
One should not forget setting the source and target view nodes to the created edge (this would have been taken care of by the ViewService static method but not by the instance method)
As for the interface and EObjectAndElementTypeAdapter class, I adapted code which you and others published on other web sites, namely:
package gmf.xtext.sample.models.diagram.adapters;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
public interface IElementTypeAwareAdapter extends IAdaptable {
public IElementType getElementType();
public int getVisualID();
public String getSemanticHint();
}
and
package gmf.xtext.sample.models.diagram.adapters;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import entities.diagram.edit.parts.EntityEditPart;
import entities.diagram.edit.parts.ReferenceEditPart;
import entities.diagram.providers.EntitiesElementTypes;
public class EObjectAndElementTypeAdapter extends EObjectAdapter implements
IElementTypeAwareAdapter {
private final IElementType elementType;
private final int visualID;
public EObjectAndElementTypeAdapter(EObject object, int visualID) {
super(object);
this.visualID = visualID;
switch (visualID) {
case EntityEditPart.VISUAL_ID:
elementType = EntitiesElementTypes.getElementType(EntityEditPart.VISUAL_ID);
break;
case ReferenceEditPart.VISUAL_ID:
elementType = EntitiesElementTypes.getElementType(ReferenceEditPart.VISUAL_ID);
break;
default:
elementType = null;
}
}
@Override
public Object getAdapter(@SuppressWarnings("rawtypes") Class adapter) {
if (adapter.isInstance(elementType)) {
return elementType;
}
return super.getAdapter(adapter);
}
@Override
public IElementType getElementType() {
return elementType;
}
@Override
public int getVisualID() {
return visualID;
}
@Override
public String getSemanticHint() {
return Integer.toString(visualID);
}
}
|
|
|
Goto Forum:
Current Time: Fri Apr 26 19:17:29 GMT 2024
Powered by FUDForum. Page generated in 0.03409 seconds
|