Make edges start from the same port using OrthogonalEdgeRouter
Tips & TricksSummary
Description
One of the main objectives of class OrthogonalEdgeRouter is to find non-overlapping routes for edges. This is why by default OrthogonalEdgeRouter will set starting/ending edges side by side at source/target nodes. However, you can make these edges start from one port by setting a strong port constraint for your edges, i.e., you give an explicit coordinate where the edges should start/end.
The following sample code fragment shows how to specify a strong port constraint for all outgoing edges at a given node.
What the sample code does:
- Creates a default EdgeMap with the graph that contains the node. This edge map is used as a data provider later on.
- For each outgoing edge sets explicit coordinates for the source end. The coordinates are specified relative to the node's center.
- Creates a strong port constraint using the appropriate static method of class PortConstraint. The port constraint is then associated with an edge by means of the edge map.
- Properly registers the edge map as a data provider with the graph so that it can be retrieved by OrthogonalEdgeRouter.
Graph graph = node.getGraph();
// Create an edge map that is used as a data provider later on.
EdgeMap pcMap = graph.createEdgeMap();
for (EdgeCursor ec = node.outEdges(); ec.ok(); ec.next())
{
Edge e = ec.edge();
// Set the coordinates for the edge's source port.
graph.setSourcePointRel(e, new YPoint(-10, 20));
// Strong port constraint that determines an edge end to connect to the
// lower (SOUTH) side of its respective node. The actual end point is at a
// fixed coordinate.
PortConstraint pc = PortConstraint.create(PortConstraint.SOUTH, true);
// Establish a mapping from edges to port constraints.
pcMap.set(e, pc);
}
// Register the edge map as a data provider with the graph.
// Use the "well-known" look-up key defined in interface PortConstraintKeys.
// The look-up key specifies that the data provider is to be used for the
// source ports only.
graph.addDataProvider(PortConstraint.SOURCE_PORT_CONSTRAINT_KEY, pcMap);
OrthogonalEdgeRouter supports both "strong" and "weak" port constraints. (A strong port constraint is a combination of an explicit coordinate together with a node side, weak means only a specific node side.)