DroidDiagram Programmer's Guide
Anchor Points and Patterns

Anchor Points

For each node you can define one or more points to which links should dock when connecting to that node. Such points are represented by instances of the AnchorPoint class. The position of an anchor point relative to node's bounding rectangle is specified by calling the setX and setY methods. Their values are expressed as percent of the node's width and height to accommodate for moving and resizing. The final location of an anchor point is determined by this simple calculation:

Java  Copy Code

return new PointF(
    nodeRect.x + nodeRect.width * x / 100,
    nodeRect.y + nodeRect.height * y / 100);

A point's position can also be defined as a fixed offset from the node's top-left corner, letting you define anchor positions that do not depend on the node's width or height. This is controlled independently for X and Y coordinates, by means of the XUnit and YUnit properties.

To control whether an anchor point accepts incoming or outgoing links or both, call  the setAllowIncoming and setAllowOutgoing methods. When a link is drawn, its ends are aligned to the nearest anchors allowing docking of the respective end type. Another way to control whether links can be connected to an anchor point is to handle the validateAnchorPoint event.

Anchor Patterns

An AnchorPattern instance defines a set of anchor points to be used together. When creating an anchor pattern, pass to its constructor an array of AnchorPoint objects. The AnchorPattern class exposes several predefined patterns that can be accessed through the static fromId method. An existing pattern can be accessed and assigned as a single entity trough the getAnchorPattern and setAnchorPattern methods of nodes. The following example creates a pattern of four anchor points:

Java  Copy Code

pattern = new AnchorPattern(new AnchorPoint[] {
    new AnchorPoint((short)50, (short)0, true, true),
    new AnchorPoint((short)100, (short)50, true, true),
    new AnchorPoint((short)50, (short)100, true, true),
    new AnchorPoint((short)0, (short)50, true, true) }, "Decision");

The points are located at the middles of bounding rectangle sides of nodes to which the pattern is assigned. All four points accept both incoming and outgoing links.

Displaying Anchor Points

Anchor points can be hidden, always visible on the screen, or displayed automatically. This can be specified through the getShowAnchors and setShowAnchors method. The appearance of anchor point marks can be customized by calling their setMarkStyle and setColor methods.

Anchor Patterns and Tables

A distinct anchor pattern can be assigned to every row of a table. When a link is connected to a row, the respective end of the link is aligned to the nearest AnchorPoint assigned to that row. If a row does not have associated anchor points of the appropriate type (incoming/outgoing), the link will be connected to the nearest row that has such anchors defined. If none of the rows of a table has an associated anchor pattern, links are connected to the middle of the left or right side of the pointed row.