Package edu.cnu.mdi.graphics.connection
Class ConnectionManager
java.lang.Object
edu.cnu.mdi.graphics.connection.ConnectionManager
- All Implemented Interfaces:
ItemChangeListener,EventListener
Manages
ConnectorItem objects and enforces connection rules.
This manager listens to drawable list change events so it can:
- cache connectors when they are added
- remove connectors when an endpoint item is removed
Duplicate connections are prevented: at most one connector may exist between two endpoint items (in either direction).
-
Method Summary
Modifier and TypeMethodDescriptionbooleancanConnect(AItem item1, AItem item2) Determine whether two items can be connected.Create and register a newConnectorItemif allowed.getConnectedItems(AItem item) Get all items directly connected to the given item.static ConnectionManagerGet the singleton instance.booleanhasConnection(AItem item1, AItem item2) Check whether a connection already exists between the two items.voiditemChanged(Layer layer, AItem item, ItemChangeType type) Drawable list change callback.
-
Method Details
-
getInstance
Get the singleton instance.- Returns:
- the connection manager
-
canConnect
Determine whether two items can be connected.Rules:
- neither item is null
- items are not the same instance
- both items belong to the same container (cross-container connections are always rejected)
- both items are connectable
- no existing connector already connects them (either direction)
- Parameters:
item1- first endpointitem2- second endpoint- Returns:
- true if a new connection is allowed
-
connect
Create and register a newConnectorItemif allowed.Note: the created item will also be discovered via ADDED events if the layer/list notifies listeners; we still add to our cache immediately so callers can rely on the returned item.
- Parameters:
layer- layer to place the connector on (recommended: a connection layer)item1- first endpointitem2- second endpoint- Returns:
- the created connector, or null if connection is not allowed
-
hasConnection
Check whether a connection already exists between the two items. (Direction does not matter.) -
getConnectors
- Returns:
- an unmodifiable snapshot of known connectors.
-
itemChanged
Drawable list change callback.- Specified by:
itemChangedin interfaceItemChangeListener- Parameters:
layer- the z layer containing the chanhed item.item- the AItem in question.type- the type of change.
-
getConnectedItems
Get all items directly connected to the given item.The returned set contains the opposite endpoint of every
ConnectorItemthat touches the given item.The returned set is a snapshot and is not backed by the manager.
- Parameters:
item- the endpoint item- Returns:
- an unmodifiable set of connected items (empty if none)
-