blob: 0e409d2e4164caa5eb0f821ddb818f9d7b4dec81 [file] [log] [blame]
/*******************************************************************************
* <copyright>
*
* Copyright (c) 2013, 2013 SAP AG.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* SAP AG - initial API, implementation and documentation
*
* </copyright>
*
*******************************************************************************/
package org.eclipse.fmc.blockdiagram.editor.features;
import org.eclipse.fmc.blockdiagram.editor.util.FMCUtil;
import org.eclipse.graphiti.datatypes.ILocation;
import org.eclipse.graphiti.features.ICreateConnectionFeature;
import org.eclipse.graphiti.features.ICreateFeature;
import org.eclipse.graphiti.features.context.IContext;
import org.eclipse.graphiti.features.context.ICreateConnectionContext;
import org.eclipse.graphiti.features.context.ICreateContext;
import org.eclipse.graphiti.features.context.impl.CreateConnectionContext;
import org.eclipse.graphiti.features.context.impl.CreateContext;
import org.eclipse.graphiti.mm.algorithms.AbstractText;
import org.eclipse.graphiti.mm.pictograms.Connection;
import org.eclipse.graphiti.mm.pictograms.ContainerShape;
import org.eclipse.graphiti.mm.pictograms.Shape;
/**
* This class is capable of executing two features: one for creating a shape and
* one for creating a connection. The new Shape will be the target of the
* connection.
*
* @author Benjamin Schmeling
*
*/
public class CreateShapeAndConnectionFeature extends CompositeFeature implements
ICreateConnectionFeature {
private final int width;
private final int height;
/**
*
* @param createFeature
* @param createConnectionFeature
* @param width
* The width of the new shape.
* @param height
* The height of the new shape.
*/
public CreateShapeAndConnectionFeature(ICreateFeature createFeature,
ICreateConnectionFeature createConnectionFeature, int width,
int height) {
super(createFeature, createConnectionFeature);
this.width = width;
this.height = height;
}
private ICreateFeature getCreateFeature() {
return (ICreateFeature) getFeature(0);
}
private ICreateConnectionFeature getCreateConnectionFeature() {
return (ICreateConnectionFeature) getFeature(1);
}
@Override
public boolean canExecute(IContext context) {
return true;
}
@Override
public void execute(IContext context) {
CreateConnectionContext ctx = (CreateConnectionContext) context;
getCreateFeature().execute(createContext(ctx));
// NOT Look up the location again and determine the new shape that has
// been added to the diagram
// ILocationInfo locationInfo =
// Graphiti.getPeLayoutService().getLocationInfo((Shape)
// ctx.getTargetPictogramElement(), ctx.getTargetLocation().getX(),
// ctx.getTargetLocation().getY());
// Set this new shape create through the create feature to the context.
ContainerShape container = ((ContainerShape) ctx
.getTargetPictogramElement());
// The element that we have added is the last child of the target
// pictogram.
Shape shape = getShapeBehind(container.getChildren().get(
container.getChildren().size() - 1));
// Shape shape = );
ctx.setTargetPictogramElement(shape);
ctx.setTargetAnchor(shape.getAnchors().get(0));
getCreateConnectionFeature().execute(ctx);
}
/**
* For the case that there is a text on top of this shape.
*
* @param shape
* @return
*/
private Shape getShapeBehind(Shape shape) {
if (shape.getGraphicsAlgorithm() instanceof AbstractText) {
return shape.getContainer();
} else
return shape;
}
private ICreateContext createContext(ICreateConnectionContext ctx) {
CreateContext cctx = new CreateContext();
ILocation relativePosition = FMCUtil.getRelativePosition(
ctx.getTargetPictogramElement(), ctx.getTargetLocation());
cctx.setX(relativePosition.getX());
cctx.setY(relativePosition.getY());
cctx.setSize(width, height);
cctx.setTargetContainer((ContainerShape) ctx
.getTargetPictogramElement());
return cctx;
}
@Override
public boolean canCreate(ICreateConnectionContext context) {
return getCreateConnectionFeature().canCreate(context);
}
@Override
public Connection create(ICreateConnectionContext context) {
return getCreateConnectionFeature().create(context);
}
@Override
public boolean canStartConnection(ICreateConnectionContext context) {
return getCreateConnectionFeature().canStartConnection(context);
}
@Override
public String getCreateName() {
return getCreateConnectionFeature().getCreateName();
}
@Override
public String getCreateDescription() {
return getCreateConnectionFeature().getCreateDescription();
}
@Override
public String getCreateImageId() {
return getCreateConnectionFeature().getCreateImageId();
}
@Override
public String getCreateLargeImageId() {
return getCreateConnectionFeature().getCreateLargeImageId();
}
@Override
public void startConnecting() {
getCreateConnectionFeature().startConnecting();
}
@Override
public void endConnecting() {
getCreateConnectionFeature().endConnecting();
}
@Override
public void attachedToSource(ICreateConnectionContext context) {
getCreateConnectionFeature().attachedToSource(context);
}
@Override
public void canceledAttaching(ICreateConnectionContext context) {
getCreateConnectionFeature().canceledAttaching(context);
}
}