Hi,

Jeremy Thomerson wrote:
> I don't think one was ever created and it fell off my radar.  If you create
> one, can you post yours and post a link to it back on this thread?

Here is my implementation for Wicket 1.4 (with generics). It is a
little bit different than Scott's one.


import org.apache.wicket.model.IChainingModel;
import org.apache.wicket.model.IDetachable;
import org.apache.wicket.model.IModel;

/**
 * Basic implementation of {...@link IChainingModel} interface.
 *
 * @author Daniel Stoch
 *
 */
public class ChainingModel<T> implements IChainingModel<T> {

  /** Any model object (which may or may not implement IModel) */
  private Object target;

  public ChainingModel(final Object modelObject) {
    target = modelObject;
  }

  @Override
  public IModel<?> getChainedModel() {
    if (target instanceof IModel<?>) {
      return (IModel<?>)target;
    }
    return null;
  }

  @Override
  public void setChainedModel(IModel<?> model) {
    target = model;
  }

  @SuppressWarnings("unchecked")
  public T getObject() {
    if (target instanceof IModel) {
      return ((IModel<T>)target).getObject();
    }
    return (T)target;
  }

  @SuppressWarnings("unchecked")
  public void setObject(T object) {
    if (target instanceof IModel) {
      ((IModel<T>)target).setObject(object);
    } else {
      target = object;
    }
  }

  public void detach() {
    // Detach nested object if it's a detachable
    if (target instanceof IDetachable) {
      ((IDetachable)target).detach();
    }
  }

  @Override
  public String toString() {
    StringBuffer sb = new StringBuffer("Model:classname=[");
    sb.append(getClass().getName()).append("]");
    sb.append(":nestedModel=[").append(target).append("]");
    return sb.toString();
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((target == null) ? 0 : target.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) return true;
    if (obj == null) return false;
    if (getClass() != obj.getClass()) return false;
    ChainingModel<?> other = (ChainingModel<?>)obj;
    if (target == null) {
      if (other.target != null) return false;
    } else if (!target.equals(other.target)) return false;
    return true;
  }

}


--
Daniel

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to