Class WeakEventListener

java.lang.Object
org.fest.swing.listener.WeakEventListener
All Implemented Interfaces:
AWTEventListener, EventListener

public final class WeakEventListener extends Object implements AWTEventListener
Understands an event listener that wraps a given AWTEventListener and:
  • attaches itself to the default toolkit
  • dispatches any given event to the wrapped listener
  • removes itself from the default toolkit when the wrapped listener gets garbage-collected
  • Field Details

  • Constructor Details

  • Method Details

    • attachAsWeakEventListener

      public static WeakEventListener attachAsWeakEventListener(Toolkit toolkit, AWTEventListener listener, long eventMask)
      Creates a new WeakEventListener and adds it to the given Toolkit using the given event mask. The created WeakEventListener simply "decorates" the given AWTEventListener. All events dispatched to the WeakEventListener are re-routed to the underlying AWTEventListener. When the underlying AWTEventListener is garbage-collected, the WeakEventListener will remove itself from the toolkit.
      Parameters:
      toolkit - the given AWT Toolkit.
      listener - the underlying listener to wrap.
      eventMask - the event mask to use to attach the WeakEventListener to the toolkit.
      Returns:
      the created WeakEventListener.
    • underlyingListener

      public AWTEventListener underlyingListener()
      Returns the underlying listener.
      Returns:
      the underlying listener.
    • eventDispatched

      public void eventDispatched(AWTEvent e)
      Dispatches the given event to the wrapped event listener. If the wrapped listener is null (garbage-collected,) this listener will remove itself from the default toolkit.
      Specified by:
      eventDispatched in interface AWTEventListener
      Parameters:
      e - the event dispatched in the AWT.
    • dispose

      public void dispose()
      Removes itself from the Toolkit this listener is attached to.
    • simulateUnderlyingListenerIsGarbageCollected

      void simulateUnderlyingListenerIsGarbageCollected()
      Removes the wrapped listener from the WeakReference (to simulate garbage collection). This method should be used only for testing only.