Create dynamically anchors on a shape [message #903145] |
Wed, 22 August 2012 08:16 |
Thomas Zöchbauer Messages: 10 Registered: August 2012 |
Junior Member |
|
|
hi, i think there are some similar problems but i couldn't get smart of them.
This is the problem:
I have a Rectangle on my Diagram
@Override
public PictogramElement add(IAddContext context) {
Shape ladder = createLadderGraphic(context);
getDiagram().eResource().getContents()
.add((EObject) context.getNewObject());
link(ladder, context.getNewObject());
return ladder;
}
private Shape createLadderGraphic(IAddContext context) {
Diagram targetDiagram = (Diagram) context.getTargetContainer();
IGaService gaService = Graphiti.getGaService();
IPeCreateService peCreateService = Graphiti.getPeCreateService();
Ladder bo = (Ladder) context.getNewObject();
// 1 containershape
ContainerShape containerShape = peCreateService.createContainerShape(
targetDiagram, true);
Rectangle rectangle = gaService.createRectangle(containerShape);
rectangle.setBackground(manageColor(StyleUtil.COLOR_BLACK));
rectangle.setFilled(true);
gaService.setLocationAndSize(rectangle, context.getX(), context.getY(),
context.getWidth(), context.getHeight());
return containerShape;
}
Works
Now this what i am trying....
Creating a Connection from the location i clicked in the shape to the target.
I hooked into attach
@Override
public void attachedToSource(final ICreateConnectionContext context) {
final Object bo = getBusinessObjectForPictogramElement(context
.getSourcePictogramElement());
// create fix anchor for connection on ladder
if (bo instanceof StartLadder) {
TransactionalEditingDomain editingDomain = getDiagramEditor()
.getEditingDomain();
CommandStack commandStack = editingDomain.getCommandStack();
commandStack.execute(new RecordingCommand(editingDomain) {
@Override
protected void doExecute() {
IPeCreateService peCreateService = Graphiti
.getPeCreateService();
IGaService gaService = Graphiti.getGaService();
ContainerShape container = (ContainerShape) context
.getSourcePictogramElement();
ILocation location = context.getSourceLocation();
int x = location.getX();
int y = location.getY();
// Point p = gaService.createPoint(x, y);
ChopboxAnchor anchor = peCreateService
.createChopboxAnchor(container);
// gaService.setLocation(anchor, x, y);
// anchor.setUseAnchorLocationAsConnectionEndpoint(true);
Rectangle rect = gaService.createRectangle(anchor);
gaService.setLocation(rect, x, y);
anchor.setGraphicsAlgorithm(rect);
anchor.setReferencedGraphicsAlgorithm(rect);
IEC_Anchor obj = FbsFactory.eINSTANCE.createIEC_Anchor();
obj.setIn(false);
obj.setSource((EObject) bo);
getDiagram().eResource().getContents().add(obj);
// link(shape, obj);
link(anchor, bo);
// use anchor
((CreateConnectionContext) context).setSourceAnchor(anchor);
}
});
}
}
looking the attached jpg.
but i dont want chopbox anchor because it starts from the middle of the shape.
any hints?
BR
Thomas
|
|
|
Re: Create dynamically anchors on a shape [message #903153 is a reply to message #903145] |
Wed, 22 August 2012 09:03 |
Thomas Zöchbauer Messages: 10 Registered: August 2012 |
Junior Member |
|
|
When i do this:
IPeCreateService peCreateService = Graphiti
.getPeCreateService();
IGaService gaService = Graphiti.getGaService();
ContainerShape container = (ContainerShape) context
.getSourcePictogramElement();
ILocation location = context.getSourceLocation();
int x = 0;// location.getX();
int y = location.getY()-20;
// Point p = gaService.createPoint(x, y);
FixPointAnchor anchor = peCreateService
.createFixPointAnchor(container);
anchor.setLocation(gaService.createPoint(x, y));
// anchor.setUseAnchorLocationAsConnectionEndpoint(true);
Rectangle rect = gaService.createRectangle(anchor);
gaService.setLocationAndSize(rect, 0, 0, 1, 1);
// anchor.setGraphicsAlgorithm(rect);
// anchor.setReferencedGraphicsAlgorithm(rect);
IEC_Anchor obj = FbsFactory.eINSTANCE.createIEC_Anchor();
obj.setIn(false);
obj.setSource((EObject) bo);
getDiagram().eResource().getContents().add(obj);
// link(shape, obj);
link(anchor, bo);
// use anchor
((CreateConnectionContext) context).setSourceAnchor(anchor);
its working. the next step is when i create the connection i have to look up for the right anchor.
looking something like this
@Override
public Connection create(ICreateConnectionContext context) {
// create the domain object connection here
SignalFlow flow = FbsFactory.eINSTANCE.createSignalFlow();
// check if horizontal
int sx = context.getSourceLocation().getX();
int sy = context.getSourceLocation().getY();
int tx = context.getTargetLocation().getX();
int ty = context.getTargetLocation().getY();
if (Math.abs(tx - sx) >= Math.abs(ty - sy)) {
flow.setHorizontal(true);
} else {
flow.setHorizontal(false);
}
Object bo = getBusinessObjectForPictogramElement(context
.getSourcePictogramElement());
if (bo instanceof StartLadder) {
EList<Anchor> anchors = ((ContainerShape) context
.getSourcePictogramElement()).getAnchors();
Anchor a2use = null;
for (Anchor anchor : anchors) {
Object anchorBO = getBusinessObjectForPictogramElement(anchor);
a2use = anchor;
break;
}
((CreateConnectionContext) context).setSourceAnchor(a2use);
}
It is both done in a #AbstractConnectionPattern
But there is a bug or missing implementation. Starting to create the connection brings result1.jpg
but creates the connection right, see result2.jpg
The reason is that i have no anchor defined on my containershape and i allow to canStartConnecting() (the laddergraphix), a "default chopbox" anchor is used.
I am able to handle this?
BR
Thomas
-
Attachment: result1.jpg
(Size: 109.78KB, Downloaded 342 times) -
Attachment: result2.jpg
(Size: 106.88KB, Downloaded 1268 times)
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03703 seconds