Skip to main content
Jump to: navigation, search

Difference between revisions of "Sirius/Cookbook"

Line 35: Line 35:
 
<pre>
 
<pre>
 
LayoutData layoutData = SiriusLayoutDataManager.INSTANCE.getData(createdView, true);
 
LayoutData layoutData = SiriusLayoutDataManager.INSTANCE.getData(createdView, true);
 +
// Update the layout data
 
</pre>
 
</pre>
 +
 +
'''Warning''' : this code may not work. The getData() method does not always return layout data as it does not always exist.
 +
See next section for another way of doing this.
  
 
=== How to get the EditPart corresponding to a DDiagramElement? ===
 
=== How to get the EditPart corresponding to a DDiagramElement? ===
 
<pre>
 
<pre>
     DDiagramElement diagramElement = ...
+
     private IGraphicalEditPart getEditPart(DDiagramElement diagramElement) {
    Session session = ...
+
        IEditorPart editor = EclipseUIUtil.getActiveEditor();
    View gmfView = SiriusGMFHelper.getGmfView(diagramElement, View.class, session);
+
        if (editor instanceof DiagramEditor) {
 
+
            Session session = new EObjectQuery(diagramElement).getSession();
    IGraphicalEditPart result = null;
+
            View gmfView = SiriusGMFHelper.getGmfView(diagramElement, session);
    if (gmfView != null && editor instanceof DiagramEditor) {
+
        final Map<?, ?> editPartRegistry = ((DiagramEditor) editor).getDiagramGraphicalViewer().getEditPartRegistry();
+
            IGraphicalEditPart result = null;
        final Object editPart = editPartRegistry.get(gmfView);
+
            if (gmfView != null && editor instanceof DiagramEditor) {
        if (editPart instanceof IGraphicalEditPart) {
+
                final Map<?, ?> editPartRegistry = ((DiagramEditor) editor).getDiagramGraphicalViewer().getEditPartRegistry();
            result = (IGraphicalEditPart) editPart;
+
                final Object editPart = editPartRegistry.get(gmfView);
        }
+
                if (editPart instanceof IGraphicalEditPart) {
    }
+
                    result = (IGraphicalEditPart) editPart;
 +
                    return result;
 +
                }
 +
            }
 +
        }
 +
        return null;
 +
    }
 
</pre>
 
</pre>
  

Revision as of 01:32, 31 May 2018

This page gathers code snippets which can be useful when using and extending Sirius programmatically.

Retrieve a DNode (or another Sirius representation element) from given semantic element

Sirius maintains an session-scoped inverse cross-referencer which can be used to find "who references who" even in the absence of explicit navigable references in the model. Sirius representation elements have a reference to the semantic model they represents, so you can use it for this kind of task. The most convenient way is to use the org.eclipse.sirius.business.api.query.EObjectQuery:

 Collection<EObject> result = new EObjectQuery(mySemanticElement).getInverseReferences(ViewpointPackage.Literals.DSEMANTIC_DECORATOR__TARGET);

Note that this will find all the DSemanticDecorators which represent mySemanticElement in the whole session (all representations included), which may or may not be what you want. If you need more precision, you will have to filter the result (using EObjectQuery#getRepresentation(DRepresentation) for example to restrict to a specific diagram).

Have editor/properties view in read-only

To control the permission on object edition, Sirius use an implementation of IPermissionAuthority. A default one named ReadOnlyPermissionAuthority is used by Sirius, to enable the read-only mode you can the following code snippet :

IPermissionAuthority permissionAuthority = IPermissionAuthorityRegistry.getPermissionAuthority(session.getTransactionalEditingDomain().getResourceSet());
if (permissionAuthority instanceof ReadOnlyPermissionAuthority) {
   ((ReadOnlyPermissionAuthority) permissionAuthority).activate();
}

To better control permissions, you can provide your own IPermissionAuthority implementation using the PermissionProvider extension point.

Enable direct edit on begin/end labels

In addition to its "main" label, an edge on a diagram can also have optional labels at the beginning (source-side) and/or end (target-side). However direct edit tools associate with the edge only allow to edit the main label, not the begin/end ones. Currently (as of Sirius 3.1) the only way to overcome this limitation is to programmatically contribute additional edit policies on the corresponding GMF Edit Parts. UML Designer does this for UML associations: see the code here for how this can be implemented.

Set Coordinates/Position at node creation using a Node Creation Tool

You can define the location and size of additional created nodes/edges by using the "CreateView/CreateEdgeView" operation in the creation tool and calling a java service to use org.eclipse.sirius.diagram.ui.business.api.view.SiriusLayoutDataManager API. Sirius will use this singleton to know the location and size with which to create the nodes/edges. After having called CreateView operation to get the DDiagramElement created, you can call a java service as sub operation (a ChangeContext for example). And in this java service call :

LayoutData layoutData = SiriusLayoutDataManager.INSTANCE.getData(createdView, true);
// Update the layout data

Warning : this code may not work. The getData() method does not always return layout data as it does not always exist. See next section for another way of doing this.

How to get the EditPart corresponding to a DDiagramElement?

    private IGraphicalEditPart getEditPart(DDiagramElement diagramElement) {
        IEditorPart editor = EclipseUIUtil.getActiveEditor();
         if (editor instanceof DiagramEditor) {
             Session session = new EObjectQuery(diagramElement).getSession();
             View gmfView = SiriusGMFHelper.getGmfView(diagramElement, session);
	
             IGraphicalEditPart result = null;
             if (gmfView != null && editor instanceof DiagramEditor) {
                 final Map<?, ?> editPartRegistry = ((DiagramEditor) editor).getDiagramGraphicalViewer().getEditPartRegistry();
                 final Object editPart = editPartRegistry.get(gmfView);
                 if (editPart instanceof IGraphicalEditPart) {
                     result = (IGraphicalEditPart) editPart;
                     return result;
                 }
             }
         }
         return null;
     }

How to get all Diagrams in a session

This is easy, but I always forget, DialectManager is your friend:

DialectManager.INSTANCE.getAllRepresentations(session);

Also, if you only need the Name/Description of all diagrams, it might be better to use

DialectManager.INSTANCE.getAllRepresentationDescriptors(session);

Back to the top