Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Graphiti » Create Connection Programatically
Create Connection Programatically [message #722948] Wed, 07 September 2011 09:40 Go to next message
Murthy Bhat is currently offline Murthy BhatFriend
Messages: 159
Registered: July 2009
Senior Member
I am trying to create connections <FreeFormConnection> for my model element in my code using the below method. I am however unable to see my connection once it is added.


private PictogramElement createTransition(ContainerShape srcState,ContainerShape destState,MyTransition myTransition) {

		ChopboxAnchor srcAnchor=GraphitiUi.getPeService().createChopboxAnchor(srcState);
		srcAnchor.setReferencedGraphicsAlgorithm(srcState.getGraphicsAlgorithm());
		srcState.getAnchors().add(srcAnchor);

		ChopboxAnchor destAnchor=GraphitiUi.getPeService().createChopboxAnchor(destState);
		destAnchor.setReferencedGraphicsAlgorithm(destState.getGraphicsAlgorithm());
		destState.getAnchors().add(destAnchor);

		AddConnectionContext context = new AddConnectionContext(srcAnchor, destAnchor);
		context.setNewObject(myTransition);
		PictogramElement connection = iFeatureProvider.addIfPossible(context);

		return connection;
	}


Am i missing something here ?

This post is related to the second half of the http://www.eclipse.org/forums/index.php/t/229987/

Thanks in advance for help.

Regards,
Murthy
Re: Create Connection Programatically [message #723344 is a reply to message #722948] Thu, 08 September 2011 11:23 Go to previous messageGo to next message
Michael Wenz is currently offline Michael WenzFriend
Messages: 1640
Registered: July 2009
Location: Walldorf, Germany
Senior Member
Do you have an add feature that gets triggered when you do the call to
addIfPossible? It needs to be returned by the feature provide in
getAddFetaures and needs to return true on canAdd.

Michael


"Murthybhat" schrieb im Newsbeitrag news:j47dgd$kit$1@news.eclipse.org...

I am trying to create connections <FreeFormConnection> for my model element
in my code using the below method. I am however unable to see my connection
once it is added.



private PictogramElement createTransition(ContainerShape
srcState,ContainerShape destState,MyTransition myTransition) {

ChopboxAnchor
srcAnchor=GraphitiUi.getPeService().createChopboxAnchor(srcState);
srcAnchor.setReferencedGraphicsAlgorithm(srcState.getGraphicsAlgorithm());
srcState.getAnchors().add(srcAnchor);

ChopboxAnchor
destAnchor=GraphitiUi.getPeService().createChopboxAnchor(destState);
destAnchor.setReferencedGraphicsAlgorithm(destState.getGraphicsAlgorithm());
destState.getAnchors().add(destAnchor);

AddConnectionContext context = new AddConnectionContext(srcAnchor,
destAnchor);
context.setNewObject(myTransition);
PictogramElement connection = iFeatureProvider.addIfPossible(context);

return connection;
}


Am i missing something here ?

This post is related to the second half of the
http://www.eclipse.org/forums/index.php/t/229987/

Thanks in advance for help.

Regards,
Murthy
Re: Create Connection Programatically [message #723533 is a reply to message #723344] Thu, 08 September 2011 18:33 Go to previous messageGo to next message
Jos Warmer is currently offline Jos WarmerFriend
Messages: 108
Registered: October 2010
Senior Member
You can test Michaels suggestion by adding a check for null after the addIfPossible()
PictogramElement connection = iFeatureProvider.addIfPossible(context);
if( connection == null){
    // Add was not possible,  probably no addFeature available as Michael states
}

Jos
Re: Create Connection Programatically [message #723637 is a reply to message #723533] Fri, 09 September 2011 03:19 Go to previous messageGo to next message
Murthy Bhat is currently offline Murthy BhatFriend
Messages: 159
Registered: July 2009
Senior Member
Yes, I have a corresponding addFeature and a valid connection object(FreeFormConnection) is returned after adding. Do i have to explicitly set the start POINT and end POINT ?


//ADDMyTransitionFeature.java

public boolean canAdd(IAddContext context) {		
			return true;
	}


public PictogramElement add(IAddContext context) {
		IAddConnectionContext addConContext = (IAddConnectionContext) context;
		MyTransition addedTransition = (MyTransition) context.getNewObject();
		final Diagram targetDiagram = (Diagram) context.getTargetContainer();

		IPeCreateService peCreateService = Graphiti.getPeCreateService();
		
		// CONNECTION WITH POLYLINE
		Connection connection = peCreateService.createFreeFormConnection(targetDiagram);
		Anchor sourceAnchor = addConContext.getSourceAnchor();
		connection.setStart(sourceAnchor);
		Anchor targetAnchor = addConContext.getTargetAnchor();
		connection.setEnd(targetAnchor);

		IGaService gaService = Graphiti.getGaService();
		Polyline polyline = gaService.createPolyline(connection);
		polyline.setBackground(manageColor(IColorConstant.BLACK));
		polyline.setForeground(manageColor(IColorConstant.BLACK));
		polyline.setLineWidth(5);

		// creating the link
		link(connection, addedTransition);

		// add Text decorator for the reference name
		ConnectionDecorator textDecorator = peCreateService.createConnectionDecorator(connection, true, 0.5, true);
		Text text = gaService.createDefaultText(targetDiagram,textDecorator);
		StyleUtil.setStyleForTransition(targetDiagram,text);

		MyTransition eReference = (MyTransition) context.getNewObject();
		text.setValue(eReference.getName());

		// add the image decorator for the reference name
		ConnectionDecorator imgDecorator = peCreateService.createConnectionDecorator(connection, true, 0.4, true);
		Image eventImg = Graphiti.getGaService().createImage(imgDecorator,TRANSITION_IMG);
		Graphiti.getGaService().setLocationAndSize(eventImg, 0, 0, 25, 25);

		
		ConnectionDecorator cd;
		cd = peCreateService.createConnectionDecorator(connection, false, 1.0, true);
		createArrow(cd);

		// create link and wire it
		link(cd, addedTransition);
		
		return connection;
	}

The text and image decorators are appearing correctly, except for the polyline. Any thoughts ?

Regards,
Murthy

[Updated on: Fri, 09 September 2011 03:21]

Report message to a moderator

Re: Create Connection Programatically [message #723750 is a reply to message #723637] Fri, 09 September 2011 10:53 Go to previous messageGo to next message
Michael Wenz is currently offline Michael WenzFriend
Messages: 1640
Registered: July 2009
Location: Walldorf, Germany
Senior Member
What you're doing here seems to be correct. Could you additionally post your
addConnectionFeature?

Michael


"Murthybhat" schrieb im Newsbeitrag news:j4bvut$c2t$1@news.eclipse.org...

Yes, I have a corresponding addFeature and a valid connection
object(FreeFormConnection) is returned after adding. Do i have to explicitly
set the start POINT and end POINT ?



//ADDMyTransitionFeature.java

public boolean canAdd(IAddContext context) {
return true;
}


public PictogramElement add(IAddContext context) {
IAddConnectionContext addConContext = (IAddConnectionContext) context;
MyTransition addedTransition = (MyTransition) context.getNewObject();
final Diagram targetDiagram = (Diagram) context.getTargetContainer();

IPeCreateService peCreateService = Graphiti.getPeCreateService();

// CONNECTION WITH POLYLINE
Connection connection =
peCreateService.createFreeFormConnection(targetDiagram);
Anchor sourceAnchor = addConContext.getSourceAnchor();
connection.setStart(sourceAnchor);
Anchor targetAnchor = addConContext.getTargetAnchor();
connection.setEnd(targetAnchor);

IGaService gaService = Graphiti.getGaService();
Polyline polyline = gaService.createPolyline(connection);
polyline.setBackground(manageColor(IColorConstant.BLACK));
polyline.setForeground(manageColor(IColorConstant.BLACK));
polyline.setLineWidth(5);

// creating the link
link(connection, addedTransition);

// add dynamic text decorator for the reference name
ConnectionDecorator textDecorator =
peCreateService.createConnectionDecorator(connection, true, 0.5, true);
Text text = gaService.createDefaultText(targetDiagram,textDecorator);
StyleUtil.setStyleForTransition(targetDiagram,text);

// set reference name in the text decorator
MyTransition eReference = (MyTransition) context.getNewObject();
text.setValue(eReference.getName());

// add dynamic text decorator for the reference name
ConnectionDecorator imgDecorator =
peCreateService.createConnectionDecorator(connection, true, 0.4, true);
Image eventImg =
Graphiti.getGaService().createImage(imgDecorator,TRANSITION_IMG);
Graphiti.getGaService().setLocationAndSize(eventImg, 0, 0, 25, 25);


ConnectionDecorator cd;
cd = peCreateService.createConnectionDecorator(connection, false, 1.0,
true);
createArrow(cd);

// create link and wire it
link(cd, addedTransition);

return connection;
}

The text and image decorators are appearing correctly, except for the
polyline. Any thoughts ?

Regards,
Murthy
Re: Create Connection Programatically [message #724306 is a reply to message #723750] Mon, 12 September 2011 03:22 Go to previous messageGo to next message
Murthy Bhat is currently offline Murthy BhatFriend
Messages: 159
Registered: July 2009
Senior Member
Hi Michael,

I have attached my AddTransitionFeature.java here.

Thanks.

Regards,
Murthy

[Updated on: Mon, 12 September 2011 03:35]

Report message to a moderator

Re: Create Connection Programatically [message #730362 is a reply to message #724306] Wed, 28 September 2011 08:55 Go to previous messageGo to next message
Henning Selt is currently offline Henning SeltFriend
Messages: 1
Registered: April 2010
Junior Member
Hi,

I have the same problem. The Connection is created without an Error but is not shown in the Diagram.

Is there any Solution for this problem

Regards,
Henning
Re: Create Connection Programatically [message #730424 is a reply to message #730362] Wed, 28 September 2011 11:16 Go to previous messageGo to next message
Michael Wenz is currently offline Michael WenzFriend
Messages: 1640
Registered: July 2009
Location: Walldorf, Germany
Senior Member
Henning,

I still doubt that this is a general issue. Could you post your add
connection feature and/or a diagram file for further analysis?

Michael
Re: Create Connection Programatically [message #754538 is a reply to message #730424] Thu, 03 November 2011 05:24 Go to previous message
Murthy Bhat is currently offline Murthy BhatFriend
Messages: 159
Registered: July 2009
Senior Member
Hello,

The connection problem is finally resolved. After a lot of digging around, it was a very small change that got this feature working.

I was creating a chop box anchor every time i wanted a new connection like below

ChopboxAnchor srcAnchor=GraphitiUi.getPeService().createChopboxAnchor(srcState);

But it seems that the anchors are already added. So I call the getAnchor method of the container, it returns the required anchor.

Anchor srcAnchor=getAnchor(srcState);
....
	private Anchor getAnchor(PictogramElement pe) {
		Anchor anchor= null;
		if (pe instanceof Anchor) {
			anchor= (Anchor) pe;
		} else if (pe instanceof AnchorContainer) {
			anchor= Graphiti.getPeService().getChopboxAnchor((AnchorContainer) pe);
		}
		return anchor;
	}



By replacing the former code with latter in my createTransition() method , I was able to resolve the problem. Thanks for all your suggestions.


Best Regards,
Murthy

[Updated on: Thu, 03 November 2011 05:25]

Report message to a moderator

Previous Topic:Using graphiti diagrams without GUI
Next Topic:Tabbed property sections are sometimes not activated.
Goto Forum:
  


Current Time: Thu Dec 18 10:35:17 GMT 2014

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

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