Class HoverManager
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
- Call
registerComponent(Component, HoverListener)once per component. - 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),
lastPointis updated on everymouseMoved/mouseDraggedevent, so the position is current. - For boundary exits (
mouseExited), the actual exit point from theMouseEventis forwarded directly. - For programmatic exits (component hidden via a
HierarchyEvent, orunregisterComponent(java.awt.Component)called while hovering), no mouse position is available; the last recorded position is used as a reasonable fallback.
-
Method Summary
Modifier and TypeMethodDescriptionintReturns the current hover delay in milliseconds.static HoverManagerReturns the singletonHoverManager.voidregisterComponent(Component comp, HoverListener listener) Register a component for hover events.voidsetDelayMs(int delayMs) Set the hover delay.voidunregisterComponent(Component comp) Unregister a component and remove all listeners installed byregisterComponent(Component, HoverListener).
-
Method Details
-
getInstance
Returns the singletonHoverManager.- 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- ifdelayMsis negative
-
getDelayMs
public int getDelayMs()Returns the current hover delay in milliseconds.- Returns:
- hover delay; always ≥ 0
-
registerComponent
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 benulllistener- the listener to notify; must not benull- Throws:
NullPointerException- if either argument isnull
-
unregisterComponent
Unregister a component and remove all listeners installed byregisterComponent(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 ifnull
-