org.apache.wicket.model
Class LoadableDetachableModel<T>

java.lang.Object
  extended by org.apache.wicket.model.LoadableDetachableModel<T>
Type Parameters:
T - The Model Object type
All Implemented Interfaces:
Serializable, IClusterable, IDetachable, IModel<T>
Direct Known Subclasses:
ContactDetachableModel, DetachableContactModel, DetachableContactModel, SessionSizeModel, SessionTotalSizeModel, StringResourceModel

public abstract class LoadableDetachableModel<T>
extends Object
implements IModel<T>

Model that makes working with detachable models a breeze. LoadableDetachableModel holds a temporary, transient model object, that is set when #getObject(Component) is called by calling abstract method 'load', and that will be reset/ set to null on detach(). A usage example:

 LoadableDetachableModel venueListModel = new LoadableDetachableModel()
 {
        protected Object load()
        {
                return getVenueDao().findVenues();
        }
 };
 

Though you can override methods onAttach() and onDetach() for additional attach/ detach behavior, the point of this class is to hide as much of the attaching/ detaching as possible. So you should rarely need to override those methods, if ever.

Author:
Eelco Hillenius, Igor Vaynberg
See Also:
Serialized Form

Constructor Summary
LoadableDetachableModel()
          Construct.
LoadableDetachableModel(T object)
          This constructor is used if you already have the object retrieved and want to wrap it with a detachable model.
 
Method Summary
 void detach()
          Detaches model after use.
 T getObject()
          Gets the model object.
 boolean isAttached()
          Gets the attached status of this model instance
protected abstract  T load()
          Loads and returns the (temporary) model object.
protected  void onAttach()
          Attaches to the current request.
protected  void onDetach()
          Detaches from the current request.
 void setObject(T object)
          Manually loads the model with the specified object.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LoadableDetachableModel

public LoadableDetachableModel()
Construct.


LoadableDetachableModel

public LoadableDetachableModel(T object)
This constructor is used if you already have the object retrieved and want to wrap it with a detachable model.

Parameters:
object - retrieved instance of the detachable object
Method Detail

detach

public void detach()
Description copied from interface: IDetachable
Detaches model after use. This is generally used to null out transient references that can be re-attached later.

Specified by:
detach in interface IDetachable
See Also:
IDetachable.detach()

getObject

public T getObject()
Description copied from interface: IModel
Gets the model object.

Specified by:
getObject in interface IModel<T>
Returns:
The model object
See Also:
IModel.getObject()

isAttached

public final boolean isAttached()
Gets the attached status of this model instance

Returns:
true if the model is attached, false otherwise

toString

public String toString()
Overrides:
toString in class Object
See Also:
Object.toString()

load

protected abstract T load()
Loads and returns the (temporary) model object.

Returns:
the (temporary) model object

onAttach

protected void onAttach()
Attaches to the current request. Implement this method with custom behavior, such as loading the model object.


onDetach

protected void onDetach()
Detaches from the current request. Implement this method with custom behavior, such as setting the model object to null.


setObject

public void setObject(T object)
Manually loads the model with the specified object. Subsequent calls to getObject() will return object until detach() is called.

Specified by:
setObject in interface IModel<T>
Parameters:
object - The object to set into the model


Copyright © 2004-2011 Apache Software Foundation. All Rights Reserved.