Search in sources :

Example 1 with IWrapModel

use of org.apache.wicket.model.IWrapModel in project wicket by apache.

the class Component method initModel.

/**
 * Called when a null model is about to be retrieved in order to allow a subclass to provide an
 * initial model.
 * <p>
 * By default this implementation looks components in the parent chain owning a
 * {@link IComponentInheritedModel} to provide a model for this component via
 * {@link IComponentInheritedModel#wrapOnInheritance(Component)}.
 * <p>
 * For example a {@link FormComponent} has the opportunity to instantiate a model on the fly
 * using its {@code id} and the containing {@link Form}'s model, if the form holds a
 * {@link CompoundPropertyModel}.
 *
 * @return The model
 */
protected IModel<?> initModel() {
    IModel<?> foundModel = null;
    // Search parents for IComponentInheritedModel (i.e. CompoundPropertyModel)
    for (Component current = getParent(); current != null; current = current.getParent()) {
        // Get model
        // Don't call the getModel() that could initialize many in between
        // completely useless models.
        // IModel model = current.getDefaultModel();
        IModel<?> model = current.getModelImpl();
        if (model instanceof IWrapModel && !(model instanceof IComponentInheritedModel)) {
            model = ((IWrapModel<?>) model).getWrappedModel();
        }
        if (model instanceof IComponentInheritedModel) {
            // return the shared inherited
            foundModel = ((IComponentInheritedModel<?>) model).wrapOnInheritance(this);
            setFlag(FLAG_INHERITABLE_MODEL, true);
            break;
        }
    }
    // No model for this component!
    return foundModel;
}
Also used : IWrapModel(org.apache.wicket.model.IWrapModel) IRequestableComponent(org.apache.wicket.request.component.IRequestableComponent) FormComponent(org.apache.wicket.markup.html.form.FormComponent) IComponentInheritedModel(org.apache.wicket.model.IComponentInheritedModel)

Example 2 with IWrapModel

use of org.apache.wicket.model.IWrapModel in project webanno by webanno.

the class ModelChangedVisitor method innermostModel.

private IModel<?> innermostModel(IModel<?> aModel) {
    IModel<?> nested = aModel;
    while (nested != null) {
        if (nested instanceof IWrapModel) {
            final IModel<?> next = ((IWrapModel<?>) nested).getWrappedModel();
            if (nested == next) {
                throw new WicketRuntimeException("Model for " + nested + " is self-referential");
            }
            nested = next;
        } else if (nested instanceof ChainingModel) {
            final IModel<?> next = ((ChainingModel<?>) nested).getChainedModel();
            if (nested == next) {
                throw new WicketRuntimeException("Model for " + nested + " is self-referential");
            }
            nested = next;
        } else {
            break;
        }
    }
    return nested;
}
Also used : IModel(org.apache.wicket.model.IModel) IWrapModel(org.apache.wicket.model.IWrapModel) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) ChainingModel(org.apache.wicket.model.ChainingModel)

Aggregations

IWrapModel (org.apache.wicket.model.IWrapModel)2 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)1 FormComponent (org.apache.wicket.markup.html.form.FormComponent)1 ChainingModel (org.apache.wicket.model.ChainingModel)1 IComponentInheritedModel (org.apache.wicket.model.IComponentInheritedModel)1 IModel (org.apache.wicket.model.IModel)1 IRequestableComponent (org.apache.wicket.request.component.IRequestableComponent)1