Class HoverManager

java.lang.Object
edu.cnu.mdi.hover.HoverManager

public final class HoverManager extends Object
Centralized hover management (tool-tip-like behavior) for arbitrary Swing components.

Threading

All callbacks (HoverListener.hoverUp(edu.cnu.mdi.hover.HoverEvent) and HoverListener.hoverDown(edu.cnu.mdi.hover.HoverEvent)) are guaranteed to fire on the Swing EDT. Mouse events are already delivered on the EDT; the shared Timer also fires on the EDT, so no explicit dispatch is needed in the normal path. The safeHoverUp/safeHoverDown helpers defensively re-dispatch for any caller that somehow bypasses the EDT.

Usage

  1. Call registerComponent(Component, HoverListener) once per component.
  2. Call unregisterComponent(Component) when the component or view is being disposed.

lastPoint semantics

The HoverEvent passed to HoverListener.hoverDown(edu.cnu.mdi.hover.HoverEvent) always contains the most recent mouse position seen for the component:

  • For timer-driven expirations (the user moves the cursor while a hover is active), lastPoint is updated on every mouseMoved/mouseDragged event, so the position is current.
  • For boundary exits (mouseExited), the actual exit point from the MouseEvent is forwarded directly.
  • For programmatic exits (component hidden via a HierarchyEvent, or unregisterComponent(java.awt.Component) called while hovering), no mouse position is available; the last recorded position is used as a reasonable fallback.
  • Method Details

    • getInstance

      public static HoverManager getInstance()
      Returns the singleton HoverManager.
      Returns:
      the global instance; never null
    • setDelayMs

      public void setDelayMs(int delayMs)
      Set the hover delay.

      Must be called on the EDT or before the UI is shown.

      Parameters:
      delayMs - delay in milliseconds; must be ≥ 0
      Throws:
      IllegalArgumentException - if delayMs is negative
    • getDelayMs

      public int getDelayMs()
      Returns the current hover delay in milliseconds.
      Returns:
      hover delay; always ≥ 0
    • registerComponent

      public void registerComponent(Component comp, HoverListener listener)
      Register a component for hover events.

      If the component is already registered, this method is a no-op.

      Parameters:
      comp - the component to watch; must not be null
      listener - the listener to notify; must not be null
      Throws:
      NullPointerException - if either argument is null
    • unregisterComponent

      public void unregisterComponent(Component comp)
      Unregister a component and remove all listeners installed by registerComponent(Component, HoverListener).

      If a hover is currently active for this component, HoverListener.hoverDown(edu.cnu.mdi.hover.HoverEvent) is not fired — the component is being torn down and no further callbacks are appropriate. Safe to call multiple times.

      Parameters:
      comp - the component to unregister; ignored if null