Home » Modeling » Papyrus » API for creating and accessing uml diagrams(I am looking for a starting point how to programatically create diagrams in my existing model)
| |
Re: API for creating and accessing uml diagrams [message #1708180 is a reply to message #1708013] |
Mon, 14 September 2015 12:10 |
Camille Letavernier Messages: 952 Registered: February 2011 |
Senior Member |
|
|
Hi,
For displaying existing elements in a Diagram, you have two options:
- GMF ViewService: you need to know the semantic element to display, and the target GMF View (e.g. specific compartment when dropping a Property in a Class). You also need the graphical hint to create (Which might be complex to obtain in a generic way)
Example:
EObject objectToDisplay = ...;
View targetView = ...; //May be the diagram or a diagram element (Compartment...)
ViewService viewService = ViewService.getInstance();
String hint = "..."; //e.g. "2008" for a Class in a Class Diagram
PreferencesHint diagramPrefHint = UMLDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT; //There is one for each kind of diagram
Node newNode = ViewService.createNode(targetView, objectToDisplay, hint, diagramPrefHint);
This approach doesn't require the diagram to be opened (You work directly at the notation model level), but it may be complex to get all the required info (e.g. exact target View and semantic Hint)
- DropObjectsRequest: this one is much simpler to use, as it will behave exactly like it would if you manually dropped an element from the ModelExplorer to the Diagram. You only need to specify the Semantic EObject, the target element (EditPart) and the mouse coordinates (Optional, especially if you rely on the automatic layout later on)
Example:
EditPart targetEditPart = ...;
Element objectToDisplay = ...;
DropObjectsRequest dropObjectsRequest = new DropObjectsRequest();
List<Element> list = Collections.singletonList(elementToShow)
dropObjectsRequest.setObjects(list);
dropObjectsRequest.setLocation(new Point(20, 100 * position));
Command commandDrop = targetEditPart.getCommand(dropObjectsRequest);
This second approach does a little bit more than displaying, as it will go through the entire Drag & Drop mechanism, which is customizable in Papyrus. So in some cases, you may get a graphical popup asking for a choice (When several drop strategies are available). You can disable this popup (and always use the default strategy) by adding a parameter to the drop request:
org.eclipse.papyrus.infra.gmfdiag.common.utils.RequestUtils.setUseGUI(dropObjectsRequest, false)
(Note that all the code given as examples are untested fragments. In any case, you need to wrap execution in a Transaction, using the EditingDomain, CommandStack and Commands)
Regarding the Automatic Layout, we don't provide anything specific in Papyrus. All layout actions are natively provided by either GEF or GMF, or extensions such as Zest or Eclipse Layout Kernel (ELK)
HTH,
Camille
Camille Letavernier
|
|
|
Re: API for creating and accessing uml diagrams [message #1708536 is a reply to message #1708180] |
Thu, 17 September 2015 10:45 |
Hans Huckebein Messages: 15 Registered: January 2015 |
Junior Member |
|
|
Thank you for your answers! That helped me much, I got started but still have problems; probably I am not walking on the intended paths. Here I describe my first results, maybe other users can benefit.
Johans post led me to the CreateClassDiagramCommand class that is the starting point for creating a class diagram.
Comment copied from the superclass:
/**
* Command creating a new GMF diagram in Papyrus. This command is intended to be used in eclipse
* extensions.
* [...]
*/
But: just calling "cmd.createDiagram()" failed. In the debugger I saw that the policyChecker's "applicableViewpoints" contains a Viewport that has icon and name already set:
// just for inspecting:
// ViewPrototype proto = ViewPrototype.get(cmd.getCreatedDiagramType(), myComponent, myComponent);
and this does not match the matching criteria implemented in policyCheckers "matches()" method (bug or wrong usage?).
I finally was able to create a class diagram by taking the prototype from policyCheckers list "getPrototypesFor(myComponent)"; I took that one with "PapyrusUMLClassDiagram" (cmd.getCreatedDiagramType() ) as implementationID, and called
Diagram myDiagram=cmd.createDiagram(modelSet, myComponent, myComponent, proto, name);
that works and creates an inner class diagram within myComponent. For creating and inserting nodes I need, as Camille described above, its (appropriate!?) visual_id, that is what I not yet understand. The code below works for inserting classes in my inner class diagram:
TreeIterator<EObject> objIterator = myComponent.eAllContents();
while (objIterator.hasNext()) {
EObject eachObject = objIterator.next();
int typeHint=XIDLDiagramUtil.getNodeVisualID(myDiagram,eachObject);
PreferencesHint diagramPrefHint = UMLDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT; //There is one for each kind of diagram
TransactionalEditingDomain ted=TransactionUtil.getEditingDomain(myDiagram);
if (typeHint >0 && ted != null){
ted.getCommandStack().execute(new RecordingCommand(ted){
@Override protected void doExecute(){
Node newNode = ViewService.createNode(myDiagram, eachObject,Integer.toString(typeHint), diagramPrefHint);
System.out.println(newNode.getType()+" created and inserted into "+myDiagram.getName());
}
} ); // end inline override
}
else
System.out.println(eachObject+" is not represented.");
}
return myDiagram;
But not for Generalizations. As visual_ID for that I take GeneralizationEditPart.VISUAL_ID, do I need a different target view?
Here is my delegate for retreiving the visual id.
private static int getNodeVisualID(Diagram myDiagram, EObject eObject) {
int hint=org.eclipse.papyrus.uml.diagram.clazz.part.UMLVisualIDRegistry.getNodeVisualID(myDiagram,eObject);
if(hint>0)
return hint;
if (UMLPackage.eINSTANCE.getGeneralization().isSuperTypeOf(eObject.eClass())) {
return GeneralizationEditPart.VISUAL_ID;
}
return -1;
}
That is quite complicated and I needed to search a lot around in the code - is there no api-documentation that I can rely on? How could I know about the command classes and the visual_id registry ... more hints would be helpfull.
best
Hans
PS
there seems to be a little bug that in line 336 of AbstractPapyrusGmfCreateDiagramCommandHandler; it does abort on viewPrototype==null but the callee newer answers null but a special "UNAVAILABLE_VIEW" instead.
[Updated on: Thu, 17 September 2015 11:00] Report message to a moderator
|
|
| | | |
Goto Forum:
Current Time: Fri Mar 29 12:06:12 GMT 2024
Powered by FUDForum. Page generated in 0.04258 seconds
|