Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Graphiti » Retrieving the business object linked to the container shape
Retrieving the business object linked to the container shape [message #1110748] Tue, 17 September 2013 09:28 Go to next message
Fabio Messina is currently offline Fabio MessinaFriend
Messages: 9
Registered: July 2013
Junior Member
Hi all. I'm developing an UML sequence diagram editor with Graphiti and I'm facing some problems in accessing the underlying business objects in a correct way.

First, I show you my prototype.

index.php/fa/16223/0/

Green rectangles are roles. They're linked to a Role business object.
White rectangles are so-called activation rectangles. They describe the activity of a role during its lifetime.
Arrows are messages. Messages can be drawn between two activation rectangles, they're linked to Message business objects AND they need to refer to the originating roles.

Now, my actual solution consists in linking the activation rectangles to their container role with the following code.

AddActivationRectangleFeature.java
public PictogramElement add(IAddContext context) {
		final ContainerShape targetContainer = (ContainerShape) context
				.getTargetContainer();
		Partecipant partecipant = (Partecipant) getBusinessObjectForPictogramElement(targetContainer);

		final IPeCreateService peCreateService = Graphiti.getPeCreateService();
		final ContainerShape containerShape = peCreateService
				.createContainerShape(targetContainer, true);

                ...CODE...

		{
			rectangle = gaService.createPlainRectangle(containerShape);
			gaService.setLocationAndSize(rectangle, context.getX(),
					context.getY(), width, height);
			rectangle
					.setBackground(manageColor(IColorConstant.LIGHT_LIGHT_GRAY));
			rectangle.setForeground(manageColor(IColorConstant.GRAY));
			link(containerShape,partecipant);
		}

		// add a chopbox anchor to the shape
		peCreateService.createChopboxAnchor(containerShape);
		return containerShape;

	}


Thanks to the link I'm able to retrieve the Role object where I need it, that is when I create a new message.

CreateMessageRRFeature.java
public Connection create(ICreateConnectionContext context) {
		Connection newConnection = null;
		Object source = getObject(context.getSourceAnchor());
		Object target = getObject(context.getTargetAnchor());

		if (source != null && target != null) {
			// create new business object
			Message_RR message = PassiseqdiagFactory.eINSTANCE
					.createMessage_RR();
			
			message.setMessage_RRSource((Role) source);
			message.setMessage_RRTarget((Role) target);
			message.setContent(content);

			((Role) source).getList_RRMessagesOut().add(message);
			((Role) target).getList_RRMessagesIn().add(message);

			...CODE...

		return newConnection;
	}

        private Object getObject(Anchor anchor) {
		if (anchor != null) {
			Object object = getBusinessObjectForPictogramElement(anchor
					.getParent());
			return object;
		}
		return null;
	}


This solution seemed to work well, but there's a problem related to it.
When I delete an activation rectangle Graphiti destroys the linked business object (despite other pictograms are still linked to it)... and the role is gone!

index.php/fa/16224/0/

This is definitively not good (even if there's an easy fix: when deleting an activation rectangle mind to use only the remove button).

Now, I wonder: maybe linking the activation rectangle to the role is not good. Another solution would be to have no business objects linked to activation rectangles and retrieving them from the shapes hierarchy.

To do that I tried to modify the CreateMessageRRFeature.java like this:

private Object getObject(Anchor anchor) {
		if (anchor != null) {
			Object object = getBusinessObjectForPictogramElement(anchor
					.getParent().getGraphicsAlgorithm()
					.getParentGraphicsAlgorithm().getPictogramElement());
			return object;
		}
		return null;
	}


But this code unfortunately does not work. The function getParentGraphicsAlgorithm() does not return what I expected. It returns null, and the call of getPictogramElement() on it throws a NullPointerException.

Now I'm stuck. Any help? Is there a good way of solving this problem?
Thank you.
Re: Retrieving the business object linked to the container shape [message #1110908 is a reply to message #1110748] Tue, 17 September 2013 13:44 Go to previous messageGo to next message
Michael Wenz is currently offline Michael WenzFriend
Messages: 1931
Registered: July 2009
Location: Walldorf, Germany
Senior Member
Fabio,

it seems you climb up the wrong containment hierarchy: both shapes and
graphics algorithms can be nested: you try to get the parent of the graphics
algorithm, if you used shape containment try anchor.getParent().getParent()
instead.

Regarding your first solution: you might also adapt the delete feature to
avoid deleting the role.

Michael
Re: Retrieving the business object linked to the container shape [message #1111468 is a reply to message #1110908] Wed, 18 September 2013 08:56 Go to previous messageGo to next message
Fabio Messina is currently offline Fabio MessinaFriend
Messages: 9
Registered: July 2013
Junior Member
Hi, Michael.

Michael Wenz wrote on Tue, 17 September 2013 09:44
if you used shape containment try anchor.getParent().getParent()


I tried this first, but I cant call getParent() on anchor.getParent(), cause AnchorContainer doesnt provide such a method.

Michael Wenz wrote on Tue, 17 September 2013 09:44

Regarding your first solution: you might also adapt the delete feature to
avoid deleting the role.


This seems viable, but I dont know exactly how to write my implementation of the delete feature. I believe I have to avoid calling the super.delete(context) method in it, but I always have to call the remove method and I just dont know how to pass the appropriate IRemoveContext object.
Is there any example I can follow?

Further, a little complication emerges: feature provider has to provide the DeleteActivationRectangleFeature when needed. I cant simply check the business object type (like I usually do), because both activation rectangles and Role rectangles are linked to Role objects.
I have to distinguish them in some way (How?).

Thank you Michael for your help.

[Updated on: Wed, 18 September 2013 09:02]

Report message to a moderator

Re: Retrieving the business object linked to the container shape [message #1113005 is a reply to message #1111468] Fri, 20 September 2013 14:02 Go to previous messageGo to next message
Michael Wenz is currently offline Michael WenzFriend
Messages: 1931
Registered: July 2009
Location: Walldorf, Germany
Senior Member
Fabio,

see my answers below.

Michael

"Fabio Messina" wrote in message news:l1bprv$acp$1@xxxxxxxxe.org...

>Hi, Michael.

>Michael Wenz wrote on Tue, 17 September 2013 09:44
>> if you used shape containment try anchor.getParent().getParent()


>I tried this first, but I cant call getParent() on anchor.getParent(),
>cause AnchorContainer doesnt provide such a method.

You will need to cast to ContainerShape.

>Michael Wenz wrote on Tue, 17 September 2013 09:44
> Regarding your first solution: you might also adapt the delete feature to
> avoid deleting the role.


>This seems viable, but I dont know exactly how to write my implementation
>of the delete feature. I believe I have to avoid calling the super.delete
>(context) method in it, but I always have to call the remove method and I
>just dont know how to pass the appropriate IRemoveContext object.
>Is there any example I can follow?

Your guess is true. Unfortunatly I don't have an example for this at hand.
Basically I would follow what is implemented in the default feature and omit
to deletion of the BO.

>Further, a little complication emerges: feature provider have to provide
>the DeleteActivationRectangleFeature. I cant simply check the business
>object >type (like I usually do), because both activation rectangles and
>Role rectangles are linked to Role objects.
>I have to distinguish them in some way (How?).

You could use properties (name, value pairs) to identify the right ones.
Those properties can be set on creation of the shapes.

>Thank you Michael for your help.
Re: Retrieving the business object linked to the container shape [message #1117315 is a reply to message #1113005] Thu, 26 September 2013 09:58 Go to previous message
Fabio Messina is currently offline Fabio MessinaFriend
Messages: 9
Registered: July 2013
Junior Member
Michael Wenz wrote on Fri, 20 September 2013 10:02

You will need to cast to ContainerShape.


Perfect. I solved with the following code (I paste it so it can be useful to others).

private Object getObject(Anchor anchor) {
		if (anchor != null) {
			Object object = getBusinessObjectForPictogramElement(((ContainerShape)anchor
					.getParent()).getContainer());
			return object;
		}
		return null;
	}


Regarding shape properties I encountered another problem, but to get things clean I think it's better to open another topic.
Thank you again.
Previous Topic:Opened unchanged editors marked as dirty...
Next Topic:How to define a selection provider for my editor ?
Goto Forum:
  


Current Time: Thu Apr 18 12:39:58 GMT 2024

Powered by FUDForum. Page generated in 0.02251 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top