Home » Modeling » Graphiti » Connection Feature and Drop on Canvas
Connection Feature and Drop on Canvas [message #929419] |
Mon, 01 October 2012 08:51  |
Eclipse User |
|
|
|
Hi,
I have a "Task node A" (graphical node in graphiti) with a "Context button". The context button is in tended to create a new "Task node B" and a link connting from A to B.
"Task node A" and context button is shown in the figure below.

When I drag starting from context button and drop on the canvas editor, I get the new task created but the "link" is not created as expected as you can see in the following diagram. The link unexpectedly ends up towards the left top corner of the editor.

I have created a feature class "OrderLinkToActionTaskCreateFeature" as following.
public class OrderLinkToActionTaskCreateFeature extends OrderLinkCreateFeature {
public OrderLinkToActionTaskCreateFeature(IFeatureProvider fp) {
super(fp);
}
@Override
public boolean canCreate(ICreateConnectionContext context) {
return true;
}
@Override
public Connection create(ICreateConnectionContext context) {
try {
Connection newConnection = null;
// get Tasks which should be connected
Task source = super.getTask(context.getSourceAnchor());
Task target = createTargetTask();
//dynamically add new action task to the diagram
AddContext addContext = new AddContext();
addContext.setNewObject(target);
addContext.setTargetContainer(getDiagram());
//set location for PE and location constraints BO
addContext.setX(context.getTargetLocation().getX());
addContext.setY(context.getTargetLocation().getY() );
target.setConstraints(new Rectangle(context.getTargetLocation().getX(), context.getTargetLocation().getY(), 100, 60));
IAddFeature addFeature = getFeatureProvider().getAddFeature(addContext);
if (addFeature.canAdd(addContext)) {
addFeature.add(addContext);
}
// now add connection
if (source != null && target != null) {
// create new business object
OrderLink eReference = super.createOrderLink(source, target);
// add connection for business object
AddConnectionContext addConnContext = new AddConnectionContext(context.getSourceAnchor(), context.getTargetAnchor());
addConnContext.setNewObject(eReference);
addConnContext.setTargetContainer(getDiagram());
newConnection = (Connection) getFeatureProvider().addIfPossible(addConnContext);
}
// set the state of editor to dirty
DiagramUtil.setActiveEditorStateToDirty();
return newConnection;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private Task createTargetTask(){
Task actionTask = IdmmodelFactory.eINSTANCE.createActionTask();
actionTask.setName("action task.." + new Random().nextInt(999));
actionTask.setPrivateTask(1);
actionTask.setHasSubtasks(0);
actionTask.setIsExpanded(true);
actionTask.setGuid(UUID.randomUUID().toString().toUpperCase());
actionTask.setInstanceId(UUID.randomUUID().toString().toUpperCase());
actionTask.setActionType(IConstants.ACTION_TASK);
actionTask.setUiTaskType(0);
return actionTask;
}
}
When I debug down, I noticed that connection context target anchor is null in the corresponding add feature class ("OrderLinkAddFeature" shown at the end of this post) i.e addConContext.getTargetAnchor() in connection.setEnd(addConContext.getTargetAnchor()); is null in OrderLinkAddFeature class.
Any pointers will be highly appreciated.
Best Regards
Surya
Additional code
...............
Super class of the above feature class is following which is used to connect to Tasks nodes that are already present on the canvas which works properly.
public class OrderLinkCreateFeature extends AbstractCreateConnectionFeature {
public OrderLinkCreateFeature (IFeatureProvider fp) {
// provide name and description for the UI, e.g. the palette
super(fp, "Order Link", "Create Order Link");
}
@Override
public String getCreateImageId() {
return ImageUtil.IMG_ORDERLINK;
}
public boolean canCreate(ICreateConnectionContext context) {
try {
// return true if both anchors belong to an EClass
// and those EClasses are not identical
Task source = getTask(context.getSourceAnchor());
Task target = getTask(context.getTargetAnchor());
if (source != null && target != null && source != target) {
return LinkUtils.canLinkCreate(source, target, this);
}
return false;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public boolean canStartConnection(ICreateConnectionContext context) {
try {
// return true if start anchor belongs to a EClass
Task source = getTask(context.getSourceAnchor());
if (source != null) {
return LinkUtils.canLinkStart(source, this);
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public Connection create(ICreateConnectionContext context) {
try {
Connection newConnection = null;
// get EClasses which should be connected
Task source = getTask(context.getSourceAnchor());
Task target = getTask(context.getTargetAnchor());
if (source != null && target != null) {
// create new business object
OrderLink eReference = createOrderLink(source, target);
// add connection for business object
AddConnectionContext addContext = new AddConnectionContext(context.getSourceAnchor(), context.getTargetAnchor());
addContext.setNewObject(eReference);
newConnection = (Connection) getFeatureProvider().addIfPossible(addContext);
//addGraphicalRepresentation(addContext, eReference);
}
// set the state of editor to dirty
DiagramUtil.setActiveEditorStateToDirty();
return newConnection;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Returns the EClass belonging to the anchor, or null if not available.
*/
public Task getTask(Anchor anchor) {
try {
if (anchor != null) {
Object object = getBusinessObjectForPictogramElement(anchor.getParent());
if (object instanceof Task) {
return (Task) object;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Creates a EReference between two EClasses.
*/
public OrderLink createOrderLink(Task source, Task target) {
try {
OrderLink link = IdmmodelFactory.eINSTANCE.createOrderLink();
link.setName("");
link.setSource(source);
link.setTarget(target);
source.getOutgoingLinks().add(link);
target.getIncomingLinks().add(link);
//link.setSource(source);
//target.getIncomingLinks().add(link);
/*eReference.setEType(target);
eReference.setLowerBound(0);
eReference.setUpperBound(1);
source.getEStructuralFeatures().add(eReference);*/
return link;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
The corresponding feature is following
public class OrderLinkAddFeature extends AbstractAddFeature {
// private static final IColorConstant E_REFERENCE_FOREGROUND = new ColorConstant(98, 131, 167);
public OrderLinkAddFeature (IFeatureProvider fp) {
super(fp);
}
public PictogramElement add(IAddContext context) {
IAddConnectionContext addConContext = (IAddConnectionContext) context;
OrderLink orderLink = (OrderLink) context.getNewObject();
IPeCreateService peCreateService = Graphiti.getPeCreateService();
Diagram targetDiagram = LinkUtils.getDiagramFromConnectionAddContext(addConContext);
// CONNECTION WITH POLYLINE
ManhattanConnection connection = peCreateService.createManhattanConnection(getDiagram()); //peCreateService.createFreeFormConnection(getDiagram());
connection.setStart(addConContext.getSourceAnchor());
connection.setEnd(addConContext.getTargetAnchor());
IGaService gaService = Graphiti.getGaService();
Polyline polyline = gaService.createPolyline(connection);
polyline.setLineWidth(2);
polyline.setForeground(manageColor(IdmColors.TASK_BORDER_COLOR));
// create link and wire it
link(connection, orderLink);
// add dynamic text decorator for the association name
ConnectionDecorator textDecorator = peCreateService.createConnectionDecorator(connection, true, 0.1, true);
Text text = gaService.createDefaultText(getDiagram(),textDecorator);
text.setForeground(manageColor(IColorConstant.BLACK));
gaService.setLocation(text, 5, 0);
// set reference name in the text decorator
//text.setValue((orderLink.getGroup()==null)?"":orderLink.getGroup());
// add static graphical decorator (composition and navigable)
ConnectionDecorator cd;
cd = peCreateService.createConnectionDecorator(connection, false, 1.0, true);
createArrow(cd);
// add model
targetDiagram.getLink().getBusinessObjects().add(orderLink);
// set the state of editor to dirty
DiagramUtil.setActiveEditorStateToDirty();
return connection;
}
public boolean canAdd(IAddContext context) {
try {
// return true if given business object is an OrderLink
// note, that the context must be an instance of IAddConnectionContext
if (context instanceof IAddConnectionContext && context.getNewObject() instanceof OrderLink) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
private Polyline createArrow(GraphicsAlgorithmContainer gaContainer) {
try {
IGaService gaService = Graphiti.getGaService();
// Polyline polyline = gaService.createPolyline(gaContainer, new int[] { -15, 10, 0, 0, -15, -10 });
Polyline polyline = gaService.createPolyline(gaContainer, new int[] { -10, 5, 0, 0, -10, -5 });
polyline.setForeground(manageColor(IdmColors.TASK_BORDER_COLOR));
polyline.setLineWidth(2);
return polyline;
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
}
Attachment: before.gif
(Size: 7.05KB, Downloaded 796 times)
Attachment: after.gif
(Size: 12.47KB, Downloaded 826 times)
[Updated on: Mon, 01 October 2012 08:57] by Moderator
|
|
| | |
Goto Forum:
Current Time: Wed Jul 23 15:52:05 EDT 2025
Powered by FUDForum. Page generated in 0.02576 seconds
|