T
- The type of the short-lived objectpublic abstract class ALcdWeakModelListener<T> extends Object implements ILcdModelListener
An ILcdModelListener
implementation which only keeps a WeakReference
to the object it
wants to update when model changes are received. This avoids memory leaks, even without removing the
listener.
As an example, suppose we have a table that shows the contents of some model. Assume the table is created in a dialog that a user can show and dismiss at will. Here's the code:
ILcdModel longLiving = ...;
JTable shortLiving = ...;
longLiving.addModelListener(new ModelContentsListener(shortLiving));
private static final class ModelContentsListener extends ALcdWeakModelListener<JTable> {
public ModelContentsListener(JTable aToModify) { super(aToModify); }
@Override
protected void modelChangedImpl(JTable aToModify, TLcdModelChangedEvent aModelChangedEvent){
ILcdModel model = aModelChangedEvent.getModel();
aToModify.setModel( createTableModel(model) );
}
}
The table can be garbage collected as soon as it is removed from the UI (so the dialog is closed), while the model still exists. There is no need to explicitly remove the listener, which makes it convenient to use.
Rules on using weak listeners:
Modifier | Constructor and Description |
---|---|
protected |
ALcdWeakModelListener(T aObjectToModify)
Creates a new
ALcdWeakModelListener instance which can update aObjectToModify
when it receives events. |
Modifier and Type | Method and Description |
---|---|
void |
modelChanged(TLcdModelChangedEvent aEvent)
Notifies this model listener that a model has changed.
|
protected abstract void |
modelChangedImpl(T aToModify,
TLcdModelChangedEvent aModelChangedEvent)
This method is called each time a
TLcdModelChangedEvent is received by this listener,
and the short-living object to modify passed in the constructor of this listener is not yet GC-ed. |
protected ALcdWeakModelListener(T aObjectToModify)
Creates a new ALcdWeakModelListener
instance which can update aObjectToModify
when it receives events.
aObjectToModify
- The object you want to modify when a TLcdModelChangedEvent
is received.
This is a short-lived object, while the listener will be attached to a
long-living ILcdModel
object.public final void modelChanged(TLcdModelChangedEvent aEvent)
See the template code example
in TLcdModelChangedEvent
on how to handle model change events.
This method will check whether the short-living object is still available.
If it is, this method will pass the short-living object and the event to the modelChangedImpl(Object, TLcdModelChangedEvent)
method.
If the short-living object is already garbage collected, this method will de-register the listener from the source model.
modelChanged
in interface ILcdModelListener
aEvent
- the TLcdModelChangedEvent
that contains relevant information on the change(s).modelChangedImpl(Object, TLcdModelChangedEvent)
protected abstract void modelChangedImpl(T aToModify, TLcdModelChangedEvent aModelChangedEvent)
This method is called each time a TLcdModelChangedEvent
is received by this listener,
and the short-living object to modify passed in the constructor of this listener is not yet GC-ed.
aToModify
- The object to modify, as passed in the constructor of this class.aModelChangedEvent
- The received event