Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Papyrus/Papyrus Developer Guide/CreateProgrammatically

< Papyrus‎ | Papyrus Developer Guide
Revision as of 06:37, 19 November 2013 by Ansgar.radermacher.cea.fr (Talk | contribs) (Created page with "=== Create graphical elements programmatically === It is possible to create graphical elements programmatically. You need to obtain first a reference to an existing graphica...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Create graphical elements programmatically

It is possible to create graphical elements programmatically. You need to obtain first a reference to an existing graphical view, for instance to obtain a reference to the current diagram in an Eclipse handler.

// event is an ExecutionEvent passed to an Eclipse handler
ISashWindowsContainer windowsContainer = ServiceUtilsForHandlers.getInstance().getISashWindowsContainer(event);
Object model = windowsContainer.getActiveSashWindowsPage().getRawModel();
if(model instanceof PageRef) {
    EObject diagramEObj = ((PageRef)model).getEmfPageIdentifier();
    if(diagramEObj instanceof Diagram) {
	Diagram diagram = (Diagram)diagramEObj;
	...

The Interfaces Diagram, Edge, View and Node can be found in the package org.eclipse.gmf.runtime.notation.

Once the diagram is recovered, you can use the UMLViewProvider of a specific diagram to create new elements within. In general, you need to create a UML element first and then use the provider to create the associated view. For instance, if you want to create a view for a lifeLine within a sequence diagram, you can use the following code. Note that the lifeline is not placed directly into the passed interaction view. Instead, the interaction view has a child corresponding to its compartment.

/**
 * Add a lifeline view for a given UML lifeline within an interaction view
 * @param lifeline the UML lifeline
 * @param interactionView the view associated with the interaction	
 * @param x layout hint
 * @param y layout hint
 * @return the created view
 */
public View addLifeline(Lifeline lifeline, View interactionView, int x, int y)
{
	// get first compartment of view
	Object compartment = interactionView.getChildren().get(1);
	Node lifelineView = sequenceDiagViewProvider.createLifeline_3001(lifeline, (View) compartment, -1, true,
		UMLDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
	Bounds location = NotationFactory.eINSTANCE.createBounds();
	location.setX(x);
	location.setY(40);
	if(lifelineView instanceof Node) {
		((Node)lifelineView).setLayoutConstraint(location);
	}
	return lifelineView;
}

The interaction has a single compartment. Other graphical elements like classes have severals (you can find out via the filters->show/hide compartment dialog). In this case, you need to make sure to obtain the right one.

Edges (lines) are created in a similar way, for instance a message betweeen two lifelines can be added with the following function. It is typically required to set source and target.

/**
 * Add a message view for a given UML message within a given diagram
 * @param message UML message
 * @param diagram Sequence diagram
 * @param lifelineSrcV view of source lifeline
 * @param lifelineDstV view of destination lifeline
 */
public void addMessage(Message message, Diagram diagram, View lifelineSrcV, View lifelineDstV)
{
	View messageView = sequenceDiagViewProvider.createMessage_4004(message, diagram, -1, true,
			UMLDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
	if(messageView instanceof Edge) {
		((Edge)messageView).setSource(lifelineSrcV);
		((Edge)messageView).setTarget(lifelineDstV);
	}
}

Back to the top