Home » Modeling » Graphiti » Create Connection Programatically
| | |
Re: Create Connection Programatically [message #723637 is a reply to message #723533] |
Fri, 09 September 2011 03:19 |
Murthy Bhat 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 |
Michael Wenz Messages: 1931 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 #754538 is a reply to message #730424] |
Thu, 03 November 2011 05:24 |
Murthy Bhat 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
|
|
|
Goto Forum:
Current Time: Thu Sep 26 12:30:32 GMT 2024
Powered by FUDForum. Page generated in 0.04687 seconds
|