Class ConnectionManager

java.lang.Object
edu.cnu.mdi.graphics.connection.ConnectionManager
All Implemented Interfaces:
ItemChangeListener, EventListener

public class ConnectionManager extends Object implements ItemChangeListener
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 Details

    • getInstance

      public static ConnectionManager getInstance()
      Get the singleton instance.
      Returns:
      the connection manager
    • canConnect

      public boolean canConnect(AItem item1, AItem item2)
      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 endpoint
      item2 - second endpoint
      Returns:
      true if a new connection is allowed
    • connect

      public ConnectorItem connect(Layer layer, AItem item1, AItem item2)
      Create and register a new ConnectorItem if 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 endpoint
      item2 - second endpoint
      Returns:
      the created connector, or null if connection is not allowed
    • hasConnection

      public boolean hasConnection(AItem item1, AItem item2)
      Check whether a connection already exists between the two items. (Direction does not matter.)
    • getConnectors

      public Set<ConnectorItem> getConnectors()
      Returns:
      an unmodifiable snapshot of known connectors.
    • itemChanged

      public void itemChanged(Layer layer, AItem item, ItemChangeType type)
      Drawable list change callback.
      Specified by:
      itemChanged in interface ItemChangeListener
      Parameters:
      layer - the z layer containing the chanhed item.
      item - the AItem in question.
      type - the type of change.
    • getConnectedItems

      public Set<AItem> getConnectedItems(AItem item)
      Get all items directly connected to the given item.

      The returned set contains the opposite endpoint of every ConnectorItem that 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)