Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.apache.wicket.model;
 
Model that makes working with detachable models a breeze. LoadableDetachableModel holds a temporary, transient model object, that is set when AbstractReadOnlyModel.getObject(org.apache.wicket.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(s):
Eelco Hillenius
Igor Vaynberg
 
 public abstract class LoadableDetachableModel extends AbstractReadOnlyModel
 {
 
 	private static final long serialVersionUID = 1L;

Logger.
 
 	private static final Logger log = LoggerFactory.getLogger(LoadableDetachableModel.class);

keeps track of whether this model is attached or detached
 
 	private transient boolean attached = false;

temporary, transient object.
 
 	private transient Object transientModelObject;

Construct.
 
 	{
 	}

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
 
 	public LoadableDetachableModel(Object object)
 	{
 		this. = object;
 		 = true;
 	}

 
 	public void detach()
 	{
 		if ()
 		{
 			 = false;
 
 			{
 				.debug("removed transient object for " + this + ", requestCycle " +
						RequestCycle.get());
			}
		}
	}

	public Object getObject()
	{
		if (!)
		{
			 = true;
			{
				.debug("loaded transient object " +  + " for " + this +
						", requestCycle " + RequestCycle.get());
			}
		}
	}

Gets the attached status of this model instance

Returns:
true if the model is attached, false otherwise
	public final boolean isAttached()
	{
		return ;
	}

	public String toString()
	{
		StringBuffer sb = new StringBuffer(super.toString());
		sb.append(":attached=").append().append(":tempModelObject=[").append(
		return sb.toString();
	}

Loads and returns the (temporary) model object.

Returns:
the (temporary) model object
	protected abstract Object load();

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

Detaches from the current request. Implement this method with custom behavior, such as setting the model object to null.
	protected void onDetach()
	{
	}
New to GrepCode? Check out our FAQ X