use of org.eclipse.elk.graph.ElkPort in project elk by eclipse.
the class FixedLayoutProvider method layout.
@Override
public void layout(final ElkNode layoutNode, final IElkProgressMonitor progressMonitor) {
progressMonitor.begin("Fixed Layout", 1);
EdgeRouting edgeRouting = layoutNode.getProperty(CoreOptions.EDGE_ROUTING);
double maxx = 0;
double maxy = 0;
for (ElkNode node : layoutNode.getChildren()) {
// set the fixed position of the node, or leave it as it is
KVector pos = node.getProperty(FixedLayouterOptions.POSITION);
if (pos != null) {
node.setLocation(pos.x, pos.y);
// set the fixed size of the node
if (node.getProperty(FixedLayouterOptions.NODE_SIZE_CONSTRAINTS).contains(SizeConstraint.MINIMUM_SIZE)) {
KVector minSize = node.getProperty(FixedLayouterOptions.NODE_SIZE_MINIMUM);
if (minSize.x > 0 && minSize.y > 0) {
ElkUtil.resizeNode(node, minSize.x, minSize.y, true, true);
}
}
}
maxx = Math.max(maxx, node.getX() + node.getWidth());
maxy = Math.max(maxy, node.getY() + node.getHeight());
// set the fixed position of the node labels, or leave them as they are
for (ElkLabel label : node.getLabels()) {
pos = label.getProperty(FixedLayouterOptions.POSITION);
if (pos != null) {
label.setLocation(pos.x, pos.y);
}
maxx = Math.max(maxx, node.getX() + label.getX() + label.getWidth());
maxy = Math.max(maxy, node.getY() + label.getY() + label.getHeight());
}
// set the fixed position of the ports, or leave them as they are
for (ElkPort port : node.getPorts()) {
pos = port.getProperty(FixedLayouterOptions.POSITION);
if (pos != null) {
port.setLocation(pos.x, pos.y);
}
double portx = node.getX() + port.getX();
double porty = node.getY() + port.getY();
maxx = Math.max(maxx, portx + port.getWidth());
maxy = Math.max(maxy, porty + port.getHeight());
// set the fixed position of the port labels, or leave them as they are
for (ElkLabel label : port.getLabels()) {
pos = label.getProperty(FixedLayouterOptions.POSITION);
if (pos != null) {
label.setLocation(pos.x, pos.y);
}
maxx = Math.max(maxx, portx + label.getX() + label.getWidth());
maxy = Math.max(maxy, porty + label.getY() + label.getHeight());
}
}
// set fixed routing for the node's outgoing edges (both simple and hierarchical), or leave them as they are
for (ElkEdge edge : ElkGraphUtil.allOutgoingEdges(node)) {
KVector maxv = processEdge(edge, edgeRouting);
maxx = Math.max(maxx, maxv.x);
maxy = Math.max(maxy, maxv.y);
}
// note that this potentially results in hierarchical edges being handled twice
for (ElkEdge edge : ElkGraphUtil.allIncomingEdges(node)) {
if (ElkGraphUtil.getSourceNode(edge).getParent() != layoutNode) {
KVector maxv = processEdge(edge, edgeRouting);
maxx = Math.max(maxx, maxv.x);
maxy = Math.max(maxy, maxv.y);
}
}
}
// if orthogonal routing is selected, determine the junction points
if (edgeRouting == EdgeRouting.ORTHOGONAL) {
for (ElkNode node : layoutNode.getChildren()) {
for (ElkEdge edge : ElkGraphUtil.allOutgoingEdges(node)) {
generateJunctionPoints(edge);
}
}
}
// set size of the parent node unless its size should be fixed as well
if (!layoutNode.getProperty(FixedLayouterOptions.NODE_SIZE_FIXED_GRAPH_SIZE)) {
ElkPadding padding = layoutNode.getProperty(FixedLayouterOptions.PADDING);
double newWidth = maxx + padding.getLeft() + padding.getRight();
double newHeight = maxy + padding.getTop() + padding.getBottom();
ElkUtil.resizeNode(layoutNode, newWidth, newHeight, true, true);
}
progressMonitor.done();
}
use of org.eclipse.elk.graph.ElkPort in project elk by eclipse.
the class RandomLayoutProvider method randomize.
/**
* Randomize the given edge by adding bend points in the area between the source and target node.
*
* @param edge an edge
* @param source the source node
* @param target the target node
* @param random the random number generator
* @param drawWidth the total width of the drawing
* @param drawHeight the total Height of the drawing
*/
private void randomize(final ElkEdge edge, final Random random, final double drawWidth, final double drawHeight) {
// Determine source coordinate
ElkConnectableShape sourceShape = edge.getSources().get(0);
double sourceX = sourceShape.getX();
double sourceY = sourceShape.getY();
double sourceWidth = sourceShape.getWidth() / 2;
double sourceHeight = sourceShape.getHeight() / 2;
if (sourceShape instanceof ElkPort) {
ElkPort sourcePort = (ElkPort) sourceShape;
sourceX += sourcePort.getParent().getX();
sourceX += sourcePort.getParent().getX();
}
sourceX += sourceWidth;
sourceY += sourceHeight;
// Determine target coordinate
ElkConnectableShape targetShape = edge.getSources().get(0);
double targetX = targetShape.getX();
double targetY = targetShape.getY();
double targetWidth = targetShape.getWidth() / 2;
double targetHeight = targetShape.getHeight() / 2;
if (targetShape instanceof ElkPort) {
ElkPort targetPort = (ElkPort) targetShape;
targetX += targetPort.getParent().getX();
targetX += targetPort.getParent().getX();
}
targetX += targetWidth;
targetY += targetHeight;
// Ensure that the edge has a single edge section
if (edge.getSections().isEmpty()) {
// We need an edge section to apply the bend points to
ElkEdgeSection edgeSection = ElkGraphFactory.eINSTANCE.createElkEdgeSection();
edge.getSections().add(edgeSection);
} else if (edge.getSections().size() > 1) {
// We can only apply bend points to a single edge section, so throw away all except for the last one
ListIterator<ElkEdgeSection> sections = edge.getSections().listIterator();
while (sections.hasNext()) {
sections.remove();
}
}
ElkEdgeSection edgeSection = edge.getSections().get(0);
// set the source point onto the border of the source element
double sourcePX = targetX;
if (targetX > sourceX + sourceWidth) {
sourcePX = sourceX + sourceWidth;
} else if (targetX < sourceX - sourceWidth) {
sourcePX = sourceX - sourceWidth;
}
double sourcePY = targetY;
if (targetY > sourceY + sourceHeight) {
sourcePY = sourceY + sourceHeight;
} else if (targetY < sourceY - sourceHeight) {
sourcePY = sourceY - sourceHeight;
}
if (sourcePX > sourceX - sourceWidth && sourcePX < sourceX + sourceWidth && sourcePY > sourceY - sourceHeight && sourcePY < sourceY + sourceHeight) {
sourcePX = sourceX + sourceWidth;
}
edgeSection.setStartLocation(sourcePX, sourcePY);
// set the target point onto the border of the target element
double targetPX = sourceX;
if (sourceX > targetX + targetWidth) {
targetPX = targetX + targetWidth;
} else if (sourceX < targetX - targetWidth) {
targetPX = targetX - targetWidth;
}
double targetPY = sourceY;
if (sourceY > targetY + targetHeight) {
targetPY = targetY + targetHeight;
} else if (sourceY < targetY - targetHeight) {
targetPY = targetY - targetHeight;
}
if (targetPX > targetX - targetWidth && targetPX < targetX + targetWidth && targetPY > targetY - targetHeight && targetPY < targetY + targetHeight) {
targetPY = targetY + targetHeight;
}
edgeSection.setEndLocation(targetPX, targetPY);
// add a random number of bend points
edgeSection.getBendPoints().clear();
int bendsNum = random.nextInt(MAX_BENDS);
if (sourceShape == targetShape) {
bendsNum++;
}
double xdiff = targetPX - sourcePX;
double ydiff = targetPY - sourcePY;
double totalDist = (double) Math.sqrt(xdiff * xdiff + ydiff * ydiff);
double maxRand = totalDist * RAND_FACT;
double xincr = xdiff / (bendsNum + 1);
double yincr = ydiff / (bendsNum + 1);
double x = sourcePX, y = sourcePY;
for (int i = 0; i < bendsNum; i++) {
// determine coordinates that deviate from the straight connection by a random amount
x += xincr;
y += yincr;
double randx = x + random.nextFloat() * maxRand - maxRand / 2;
if (randx < 0) {
randx = 1;
} else if (randx > drawWidth) {
randx = drawWidth - 1;
}
double randy = y + random.nextFloat() * maxRand - maxRand / 2;
if (randy < 0) {
randy = 1;
} else if (randy > drawHeight) {
randy = drawHeight - 1;
}
ElkBendPoint bendPoint = ElkGraphFactory.eINSTANCE.createElkBendPoint();
bendPoint.set(randx, randy);
edgeSection.getBendPoints().add(bendPoint);
}
}
use of org.eclipse.elk.graph.ElkPort in project elk by eclipse.
the class DisCoGraphRenderer method renderNodeChildren.
// filling alpha added by mic
/**
* Paints all children of the given parent node that fall into the given dirty
* area.
*
* @param parent
* the node whose children to paint
* @param graphics
* the graphics context used to paint
* @param area
* dirty area that needs painting
* @param offset
* offset to be added to relative coordinates
* @param edgeSet
* set to be filled with edges that are found on the way
* @param nodeAlpha
* alpha value for nodes
* @param fillingAlpha
* alpha value for node fillings
*/
private void renderNodeChildren(final ElkNode parent, final GC graphics, final Rectangle area, final KVector offset, final Set<ElkEdge> edgeSet, final int nodeAlpha, final int fillingAlpha) {
for (ElkNode child : parent.getChildren()) {
PaintRectangle rect = boundsMap.get(child);
if (rect == null) {
rect = new PaintRectangle(child, offset, getScale());
boundsMap.put(child, rect);
}
KVector childOffset = new KVector(rect.x, rect.y);
// render the child node and its content
if (!rect.painted && rect.intersects(area)) {
// paint this node
graphics.setAlpha(fillingAlpha);
if (configurator.getNodeFillColor() != null) {
graphics.setBackground(configurator.getNodeFillColor());
graphics.fillRectangle(rect.x, rect.y, rect.width, rect.height);
}
graphics.setAlpha(nodeAlpha);
if (configurator.getNodeBorderColor() != null) {
graphics.setForeground(configurator.getNodeBorderColor());
graphics.drawRectangle(rect.x, rect.y, rect.width, rect.height);
}
rect.painted = true;
}
// render node labels
if (configurator.getNodeLabelFont() != null) {
graphics.setFont(configurator.getNodeLabelFont());
for (ElkLabel label : child.getLabels()) {
renderLabel(label, graphics, area, childOffset, nodeAlpha);
}
}
// render ports
for (ElkPort port : child.getPorts()) {
renderPort(port, graphics, area, childOffset, nodeAlpha);
}
// store all incident edges to render them later
edgeSet.addAll(Sets.newHashSet(ElkGraphUtil.allIncidentEdges(child)));
}
}
use of org.eclipse.elk.graph.ElkPort in project elk by eclipse.
the class GmfLayoutEditPolicy method addLabelLayout.
/**
* Adds an edge label layout to the given command.
*
* @param command
* command to which the edge label layout shall be added
* @param klabel
* label with layout data
* @param labelEditPart
* edit part to which layout is applied
* @param scale
* scale factor for coordinates
*/
private void addLabelLayout(final GmfLayoutCommand command, final ElkLabel klabel, final GraphicalEditPart labelEditPart, final double scale) {
ElkGraphElement parent = klabel.getParent();
// node and port labels are processed separately
if (parent instanceof ElkNode || parent instanceof ElkPort) {
View view = (View) labelEditPart.getModel();
int xpos = (int) (klabel.getX() * scale);
int ypos = (int) (klabel.getY() * scale);
Object oldx = ViewUtil.getStructuralFeatureValue(view, NotationPackage.eINSTANCE.getLocation_X());
Object oldy = ViewUtil.getStructuralFeatureValue(view, NotationPackage.eINSTANCE.getLocation_Y());
if (oldx == null || oldy == null || xpos != (Integer) oldx || ypos != (Integer) oldy) {
command.addShapeLayout(view, new Point(xpos, ypos), null);
}
return;
} else if (parent instanceof ElkEdge) {
// calculate direct new location of the label
Rectangle targetBounds = new Rectangle(labelEditPart.getFigure().getBounds());
targetBounds.x = (int) (klabel.getX() * scale);
targetBounds.y = (int) (klabel.getY() * scale);
ConnectionEditPart connectionEditPart = (ConnectionEditPart) labelEditPart.getParent();
PointList bendPoints = getBendPoints((ElkEdge) parent, connectionEditPart.getFigure(), scale);
EObject modelElement = connectionEditPart.getNotationView().getElement();
EdgeLabelPlacement labelPlacement = klabel.getProperty(CoreOptions.EDGE_LABELS_PLACEMENT);
// the list of bend points must be reversed
if (modelElement instanceof EReference && labelPlacement == EdgeLabelPlacement.TAIL) {
bendPoints = bendPoints.getCopy();
bendPoints.reverse();
}
// get the referencePoint for the label
int fromEnd, keyPoint = ConnectionLocator.MIDDLE;
if (labelEditPart instanceof LabelEditPart) {
keyPoint = ((LabelEditPart) labelEditPart).getKeyPoint();
}
switch(keyPoint) {
case ConnectionLocator.SOURCE:
fromEnd = SOURCE_LOCATION;
break;
case ConnectionLocator.TARGET:
fromEnd = TARGET_LOCATION;
break;
default:
fromEnd = MIDDLE_LOCATION;
break;
}
Point refPoint = PointListUtilities.calculatePointRelativeToLine(bendPoints, 0, fromEnd, true);
// get the new relative location
Point normalPoint = offsetFromRelativeCoordinate(targetBounds, bendPoints, refPoint);
if (normalPoint != null) {
command.addShapeLayout((View) labelEditPart.getModel(), normalPoint, null);
}
}
}
use of org.eclipse.elk.graph.ElkPort in project elk by eclipse.
the class RandomGraphGenerator method connect.
/**
* Connects two nodes with an edge.
*
* @param source
* the source node
* @param target
* the target node
* @param directed
* whether the edge should be directed or undirected
* @return the edge
*/
private ElkEdge connect(final ElkConnectableShape source, final ElkConnectableShape target) {
ElkNode sourceNode = null;
ElkNode targetNode = null;
ElkPort sourcePort = null;
ElkPort targetPort = null;
if (source instanceof ElkNode) {
sourceNode = (ElkNode) source;
}
if (source instanceof ElkPort) {
sourcePort = (ElkPort) source;
sourceNode = sourcePort.getParent();
}
if (target instanceof ElkPort) {
targetPort = (ElkPort) target;
targetNode = targetPort.getParent();
}
if (target instanceof ElkNode) {
targetNode = (ElkNode) target;
}
ElkEdge edge = ElkGraphUtil.createEdge(null);
if (get(GeneratorOptions.ENABLE_PORTS)) {
if (source instanceof ElkNode) {
sourcePort = retrievePort(sourceNode, true);
}
if (!isHypernode(sourceNode)) {
// remove node
if (edge.getSources().contains(sourceNode)) {
edge.getSources().remove(sourceNode);
}
edge.getSources().add(sourcePort);
// edge.setSourcePort(sourcePort);
}
if (!isHypernode(targetNode)) {
if (targetPort == null) {
targetPort = retrievePort(targetNode, false);
}
if (edge.getTargets().contains(targetNode)) {
edge.getTargets().remove(targetNode);
}
edge.getTargets().remove(target);
edge.getTargets().add(targetPort);
// edge.setTargetPort(targetPort);
}
} else {
edge.getSources().add(sourceNode);
edge.getTargets().add(targetNode);
}
if (get(GeneratorOptions.EDGE_LABELS)) {
addEdgeLabel(edge);
}
ElkGraphUtil.updateContainment(edge);
return edge;
}
Aggregations