A ConnectionFigure connecting two ellipse figures
ConnectionFigures are used to create connections between figures in JHotDraw. Connections are typically visual elements although this behaviour may be turned off if required. Constraints are modelled in the framework by the ConnectionFigure interface and its two specialisations LineConnection and ElbowConnection.
The connection between the connected figures and the ConnectionFigure is made via an implementation of the Observer design pattern. The figures act as Observable classes while the connection acts as the Observer. The default behaviour for this relationship is to update the connection in response to changes in the connected figures (maintaining the connection as the figures are repositioned in the drawing).
Figures can refuse to take part in connections by overriding the figures canConnect() method to return false.
The ConnectionFigure interface is an extension of the FigureChangeListener interface. The FigureChangeListener interface describes several different methods that may be invoked upon a state change. The figureChanged(FigureChangedEvent) method that is sent when a figure changes state is the most general and is the most commonly used by connections. Other messages that the interface can react to include figureInvalidated(FigureChangedEvent), figureRemoved(FigureChangedEvent), figureRequestRemove(FigureChangedEvent) and figureRequestUpdate(FigureChangedEvent). The FigureChangeListener interface can be overridden in any subclass of ConnectionFigure to customise the constraint behaviour.
Connections are not strictly speaking made between figures but are actually made between Connectors. Connectors exist on figures and define how connections will appear on the figure.
Connections between figures can be created by passing the figure's connectors into the ConnectionFigure’s connectStart(Connector) and connectEnd(connector) methods. A more common approach is to create a tool to make connections in the application.