ilog.views.util.event
Class IlvAbstractWeakEventListener

java.lang.Object
  extended by ilog.views.util.event.IlvAbstractWeakEventListener

public abstract class IlvAbstractWeakEventListener
extends Object

This is the last resort for memory leak problems related to event listeners. It can be used as base class for implementing any event listener if there are problems to decide when to remove the event listener.

The typical use case: a listener gets installed at an source object. The listener is needed as long as a reference target object exists, but there is no notification when the reference target object disappears. The problem is to deinstall the listener from the source object to avoid memory leaks.

Let's put the use case more concrete: An application has a javax.swing.Action that might get triggered from various Swing buttons. The swing buttons need to install a property change listener at the action to be informed when the action is enabled etc. Lets assume new buttons for the action are created over time (e.g. when opening documents), and old buttons of the action disappear (e.g. when closing documents). When a button disappears, it should remove the property change listener from the action to avoid memory leaks, but if there is no callback method that allows to do this cleanup, it is difficult to solve. In this case, the solution is to create an IlvAbstractWeakEventListener which makes sure the property change listener is removed automatically some time after the button was garbage collected. Example code:

 class ButtonPropertyChangeListener extends IlvAbstractWeakEventListener
     implements PropertyChangeListener {

     ButtonPropertyChangeListener(Action action, JComponent button) {
         super(action, button);
         action.addPropertyChangeListener(this);
     }

     protected void cleanup(Object action) {
         ((Action)action).removePropertyChangeListener(this);
     }

     public void propertyChange(PropertyChangeEvent evt) {
         ...
     }
 }
 

Since:
JViews 7.5

Constructor Summary
IlvAbstractWeakEventListener(Object source, Object target)
          Creates a new weak event listener.
 
Method Summary
protected abstract  void cleanup(Object source)
          Cleans when the target gets garbage collected.
 Object getSource()
          Returns the source of the listener.
 Object getTarget()
          Returns the target of the listener.
 void setTarget(Object target)
          Sets the target of the listener.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IlvAbstractWeakEventListener

public IlvAbstractWeakEventListener(Object source,
                                    Object target)
Creates a new weak event listener.

Parameters:
source - The source object the listener should be added to.
target - The target object that needs the listener. The goal is that the listener is removed from the source object when the target object is scheduled for garbage collection.
Method Detail

getSource

public Object getSource()
Returns the source of the listener. This is the object the listener should be added to.


getTarget

public Object getTarget()
Returns the target of the listener. This is the object that needs the listener. The listener is automatically removed from the source object when the target object gets garbage collected. The time point when the listener is removed is unspecified. Returns null if the target is already garbage collected.


setTarget

public void setTarget(Object target)
Sets the target of the listener. The listener lives as long as the target exists. When the target is garbage collected, cleanup(java.lang.Object) is eventually called.


cleanup

protected abstract void cleanup(Object source)
Cleans when the target gets garbage collected. The typical implementation is to remove this listener from the source. As long as the target of the listener exists, cleanup is not called. Some time after the target got garbage collected, cleanup is called, but the exact time point is undefined.

Parameters:
source - The source object the listener should be removed from.
See Also:
getTarget()


Copyright © 1996-2007 ILOG S.A. All rights reserved.   Documentation homepage.