Chapter 11 Links
Introduction to Links
Link components are symbols that have a head and tail port. By default, the link component contains a single line as a child component and keeps the head and tail ports aligned with the endpoints of the line. However, other types of components can be used within the link component to render different shapes for the link.
The link component maintains a pointer to an interface called IODLinkShape. This interface provides the link with methods for positioning the head and tail ports to match the shape. It also provides methods for adjusting the shape when the head and tail ports are moved. This interface is mixed into classes derived from CODComponent. The implementation of the interface depends on the type of component with which it is combined. The following link shape classes are included with the library: CODLineLinkShape and CODOrthogonalLinkShape. The CODLineLinkShape class implements IODLinkShape for straight lines. The CODOrthogonalLinkShape implements IODLinkShape for orthogonal lines.
To implement your own custom link shapes, derive a class from an existing component class and mix in the IODLinkShape interface. Example 16 shows the IODLinkShape interface.
Example 16 – The IODLinkShape interface
class IODLinkShape : public IODObject
{
public:
/* Return a pointer to the component that implements the shape. */
virtual CODComponent* GetComponent() = 0;
/* Return the point that represents the tail of the link. */
virtual CPoint GetTailPoint() const = 0;
/* Return the point that represents the head of the link. */
virtual CPoint GetHeadPoint() const = 0;
/* Adjusts the shape so that the head and tail dock with ports. */
virtual BOOL Dock(CODPortComponent* pSourcePort,
CODPortComponent* pTargetPort) = 0;
/* Adjust the shape when a vertex changes. */
virtual BOOL UpdateVertex(const int nVertexIdx,
CODPortComponent* pSourcePort,
CODPortComponent* pTargetPort) = 0;
};
The GetTailPoint() and GetHeadPoint() methods return the tail and head points of the link shape. For a line, the tail and head points are the endpoints. The Dock() method adjusts the component so that it docks with the given ports. When the symbols that the link is connected to move, the Dock() method is called to adjust the shape so that it follows the symbols. The UpdateVertex() method is called when a vertex in the shape is moved or deleted. Updating a vertex in the shape may affect how the shape is docked with the connected symbols.
Use CODLineLinkShape and CODOrthogonalLinkShape as a guide to implementing the IODLinkShape interface. Example 17 shows the declaration of the CODLineLinkShape class.
Example 17 – CODLineLinkShape class
class CODLineLinkShape : public CODLineComponent,
public IODLinkShape
{
VIEWS_DECLARE_SERIAL(CODLineLinkShape)
public:
/* Constructor. */
CODLineLinkShape();
/* Copy constructor. */
CODLineLinkShape(const CODLineComponent& src);
 
/* Return a pointer to the component that implements the shape. */
virtual CODComponent* GetComponent();
/* Return the point that represents the tail of the link. */
virtual CPoint GetTailPoint() const;
/* Return the point that represents the head of the link. */
virtual CPoint GetHeadPoint() const;
/* Adjusts the shape so that the head and tail dock with ports. */
virtual BOOL Dock(CODPortComponent* pSourcePort,
CODPortComponent* pTargetPort);
/* Adjust the shape when a vertex changes. */
virtual BOOL UpdateVertex(const int nVertexIdx,
CODPortComponent* pSourcePort,
CODPortComponent* pTargetPort);
. . .
};
 
The method CODLinkComponent::AssignShape() is used to assign a link shape to a link component. If you want to assign a custom link shape to the link component, call AssignShape() before calling the CODLinkComponent::Create() method. If a link shape has not been assigned by the time Create() is called, Create() assigns a CODLineLinkShape (a straight line) to the link. To use an orthogonal line for the link shape, you can call CODLinkComponent::CreateOrthogonal().