use of org.eclipse.elk.graph.ElkLabel in project elk by eclipse.
the class GmfLayoutEditPolicy method getCommand.
@Override
public Command getCommand(final Request request) {
if (ApplyLayoutRequest.REQ_APPLY_LAYOUT.equals(request.getType())) {
if (request instanceof ApplyLayoutRequest) {
ApplyLayoutRequest layoutRequest = (ApplyLayoutRequest) request;
IGraphicalEditPart hostEditPart = (IGraphicalEditPart) getHost();
GmfLayoutCommand command = new GmfLayoutCommand(hostEditPart.getEditingDomain(), "Automatic Layout", new EObjectAdapter((View) hostEditPart.getModel()));
double scale = layoutRequest.getScale();
// retrieve layout data from the request and compute layout data for the command
for (Pair<ElkGraphElement, GraphicalEditPart> layoutPair : layoutRequest.getElements()) {
if (layoutPair.getFirst() instanceof ElkNode) {
addShapeLayout(command, (ElkShape) layoutPair.getFirst(), layoutPair.getSecond(), scale);
} else if (layoutPair.getFirst() instanceof ElkPort) {
addShapeLayout(command, (ElkPort) layoutPair.getFirst(), layoutPair.getSecond(), scale);
} else if (layoutPair.getFirst() instanceof ElkEdge) {
addEdgeLayout(command, (ElkEdge) layoutPair.getFirst(), (ConnectionEditPart) layoutPair.getSecond(), scale);
} else if (layoutPair.getFirst() instanceof ElkLabel) {
addLabelLayout(command, (ElkLabel) layoutPair.getFirst(), layoutPair.getSecond(), scale);
}
}
// TODO Make this configurable?
command.setObliqueRouting(true);
pointListMap.clear();
return new ICommandProxy(command);
} else {
return null;
}
} else {
return super.getCommand(request);
}
}
use of org.eclipse.elk.graph.ElkLabel in project elk by eclipse.
the class ElkGraphTransformer method importElkShape.
/**
* Transforms a {@link ElkNode}, {@link ElkLabel} or {@link ElkPort} into a {@link DCElement} without destroying it.
* An offset may be applied, as some of these elements may have coordinates relative to another element, but the
* {@link DCGraph} only works with absolute coordinates.
*
* @param element
* Elemment to be transformed
* @param considerWhenApplyingOffset
* true - a key-value pair consisting of the original element and its {@link DCElement} representation
* will be saved and used later by {@link ElkGraphTransformer#applyLayout()} to set the original element
* to its new position after the layouting process has finished. false - the original element has
* coordinates relative to another element already moved and doesn't need new coordinates after
* layouting.
* @param offsetX
* X-Coordinate of the offset needed to make the given element's coordinates absolute (only needed, when
* the element has coordinates not relative to its parent {@link ElkNode}).
* @param offsetY
* Y-Coordinate of the offset needed to make the given element's coordinates absolute (only needed, when
* the element has coordinates not relative to its parent {@link ElkNode}).
* @return result of transformation
* @throws IllegalArgumentException
* A {@link ElkGraphElement} other than {@link ElkNode}, {@link ElkLabel} or {@link ElkPort} has been
* given as an argument.
*/
private <E extends ElkShape> DCElement importElkShape(final E element, final boolean considerWhenApplyingOffset, final double offsetX, final double offsetY) throws IllegalArgumentException {
if (!(element instanceof ElkNode || element instanceof ElkLabel || element instanceof ElkPort)) {
throw new IllegalArgumentException("Method only works for ElkNode-, ElkLabel and ElkPort-objects.");
}
double halfComponentSpacing = componentSpacing / 2;
double x0 = element.getX() + offsetX - halfComponentSpacing;
double y0 = element.getY() + offsetY - halfComponentSpacing;
double x1 = x0 + element.getWidth() + componentSpacing;
double y1 = y0 + element.getHeight() + componentSpacing;
KVectorChain coords = new KVectorChain();
coords.add(newPoint(x0, y0));
coords.add(newPoint(x0, y1));
coords.add(newPoint(x1, y1));
coords.add(newPoint(x1, y0));
DCElement shape = new DCElement(coords);
// copy all properties of original layout
shape.copyProperties(element);
if (considerWhenApplyingOffset) {
elementMapping.put(element, shape);
}
return shape;
}
use of org.eclipse.elk.graph.ElkLabel in project elk by eclipse.
the class ElkGraphTransformer method applyLayout.
@Override
public void applyLayout() {
KVector graphDimensions = transformedGraph.getDimensions();
double newWidth = graphDimensions.x;
double newHeight = graphDimensions.y;
double oldWidth = parent.getWidth();
double oldHeight = parent.getHeight();
// Adjust size of layout
parent.setDimensions(graphDimensions.x, graphDimensions.y);
double xFactor = newWidth / oldWidth;
double yFactor = newHeight / oldHeight;
for (ElkLabel label : parent.getLabels()) {
label.setX(label.getX() * xFactor);
label.setY(label.getY() * yFactor);
}
for (ElkPort port : parent.getPorts()) {
double px = port.getX();
double py = port.getY();
if (px > 0) {
port.setX(px * xFactor);
}
if (py > 0) {
port.setY(py * yFactor);
}
}
// Apply offsets, whenever necessary.
elementMapping.forEach(new OffsetApplier());
List<ElkPort> adjustedPorts = Lists.newArrayList();
ElkPort portToAdjust;
for (Entry<ElkEdge, DCExtension> inEntry : incomingExtensionsMapping.entrySet()) {
ElkEdge edge = inEntry.getKey();
DCDirection dir = inEntry.getValue().getDirection();
ElkEdgeSection edgeSection = ElkGraphUtil.firstEdgeSection(edge, false, false);
KVectorChain newPoints = adjustFirstSegment(ElkGraphUtil.getSourceNode(edge), ElkUtil.createVectorChain(edgeSection), dir);
ElkUtil.applyVectorChain(newPoints, edgeSection);
portToAdjust = ElkGraphUtil.getSourcePort(edge);
if (portToAdjust != null && !adjustedPorts.contains(portToAdjust)) {
adjustedPorts.add(portToAdjust);
adjustRelatedPort(portToAdjust, newPoints.getFirst(), dir);
}
}
for (Entry<ElkEdge, DCExtension> outEntry : outgoingExtensionsMapping.entrySet()) {
ElkEdge edge = outEntry.getKey();
DCDirection dir = outEntry.getValue().getDirection();
ElkEdgeSection edgeSection = ElkGraphUtil.firstEdgeSection(edge, false, false);
KVectorChain newPoints = adjustFirstSegment(ElkGraphUtil.getTargetNode(edge), KVectorChain.reverse(ElkUtil.createVectorChain(edgeSection)), dir);
newPoints = KVectorChain.reverse(newPoints);
ElkUtil.applyVectorChain(newPoints, edgeSection);
portToAdjust = ElkGraphUtil.getTargetPort(edge);
if (portToAdjust != null && !adjustedPorts.contains(portToAdjust)) {
adjustedPorts.add(portToAdjust);
adjustRelatedPort(portToAdjust, newPoints.getLast(), dir);
}
}
}
use of org.eclipse.elk.graph.ElkLabel in project elk by eclipse.
the class ElkGraphTransformer method importElkEdge.
/**
* Transforms a {@link ElkEdge} into a {@link DCElement} without destroying it. Edges can have their own
* {@link ElkLabel ElkLabels}, so they will be transformed, too.
*
* @param edge
* Edge to be transformed into a {@link DCElement}
* @param newComponent
* Collection representing the component the edge and its associated labels (if any) belong to. Newly
* generated {@link DCElement DCElements} will be added to it
* @return {@link DCElement} resulting from the transformation
*/
private DCElement importElkEdge(final ElkEdge edge, final Collection<DCElement> newComponent) {
ElkEdgeSection edgeSection = ElkGraphUtil.firstEdgeSection(edge, false, false);
List<KVector> points = ElkUtil.createVectorChain(edgeSection);
double thickness = edge.getProperty(DisCoOptions.EDGE_THICKNESS);
KVectorChain contour = getContour(points, thickness + componentSpacing);
DCElement shape = new DCElement(contour);
shape.copyProperties(edge);
elementMapping.put(edge, shape);
newComponent.add(shape);
// ElkEdges can have labels, too!
List<ElkLabel> labels = edge.getLabels();
for (ElkLabel label : labels) {
// "true" - ElkLabels belonging to an ElkEdge have absolute coordinates and have to be considered when
// applying
// changes to the DCGraph back to the original graph.
DCElement componentLabel = importElkShape(label, true, 0.0f, 0.0f);
newComponent.add(componentLabel);
}
return shape;
}
use of org.eclipse.elk.graph.ElkLabel in project elk by eclipse.
the class ElkGraphTransformer method importExtension.
/**
* Transforms a {@link ElkEdge} into a {@link DCElement} without destroying it. Edges can have their own
* {@link ElkLabel ElkLabels}, so they will be transformed, too.
*
* @param edge
* Edge to be transformed into a {@link DCElement}
* @param newComponent
* Collection representing the component the edge and its associated labels (if any) belong to. Newly
* generated {@link DCElement DCElements} will be added to it
* @return {@link DCElement} resulting from the transformation
*/
/**
* Transforms a short hierarchical {@link ElkEdge} into a {@link DCExtension} without destroying it. Edges can have
* their own {@link ElkLabel ElkLabels}, so they will be transformed, too.
*
* @param edge
* Edge to be transformed into a {@link DCExtension}
* @param newComponent
* component the extension will belong to
* @param outgoingExtension
* true, if the edge should be handled as an outgoing extension; false, otherwise
*/
private void importExtension(final ElkEdge edge, final Collection<DCElement> newComponent, final boolean outgoingExtension) {
ElkEdgeSection edgeSection = ElkGraphUtil.firstEdgeSection(edge, false, false);
KVectorChain points = ElkUtil.createVectorChain(edgeSection);
if (outgoingExtension) {
points = KVectorChain.reverse(points);
}
// Is going to hold the extension
DCElement shape;
double thickness = edge.getProperty(DisCoOptions.EDGE_THICKNESS);
KVector outerPoint = points.getFirst();
KVector innerPoint = points.get(1);
if (points.size() > 2) {
List<KVector> fixedEdgePoints = Lists.newArrayList();
fixedEdgePoints.addAll(points.subList(1, points.size()));
KVectorChain contour = getContour(fixedEdgePoints, thickness + componentSpacing);
shape = new DCElement(contour);
shape.copyProperties(edge);
newComponent.add(shape);
} else {
if (outgoingExtension) {
shape = elementMapping.get(ElkGraphUtil.getSourceNode(edge));
} else {
shape = elementMapping.get(ElkGraphUtil.getTargetNode(edge));
}
}
// Construct the extension and add to mapping
ElkNode extParent = ElkGraphUtil.getSourceNode(edge);
if (outgoingExtension) {
extParent = ElkGraphUtil.getTargetNode(edge);
}
DCDirection dir = nearestSide(outerPoint, extParent);
double extensionWidth = thickness + componentSpacing;
KVector middlePos;
if (dir.isHorizontal()) {
// West or east extension
extensionWidth += Math.abs(outerPoint.y - innerPoint.y);
middlePos = new KVector(innerPoint.x, (innerPoint.y + outerPoint.y) / 2);
} else {
extensionWidth += Math.abs(outerPoint.x - innerPoint.x);
middlePos = new KVector((innerPoint.x + outerPoint.x) / 2, innerPoint.y);
}
if (outgoingExtension) {
outgoingExtensionsMapping.put(edge, new DCExtension(shape, dir, middlePos, extensionWidth));
} else {
incomingExtensionsMapping.put(edge, new DCExtension(shape, dir, middlePos, extensionWidth));
}
elementMapping.put(edge, shape);
// ElkEdges can have labels, too!
List<ElkLabel> labels = edge.getLabels();
for (ElkLabel label : labels) {
// "true" - ElkLabels belonging to an ElkEdge have absolute coordinates and have to be considered when
// applying
// changes to the DCGraph back to the original graph.
DCElement componentLabel = importElkShape(label, true, 0.0f, 0.0f);
newComponent.add(componentLabel);
}
}
Aggregations