You might try something like this that could be used with any kind of model,
including a property model:

public class DefaultWhenNullModel<T> implements IModel<T> {

    private static final long serialVersionUID = 1L;

    private final IModel<T> mNestedModel;
    private final T mDefaultValue;

    public DefaultWhenNullModel(IModel<T> nestedModel, T defaultValue) {
        mNestedModel = nestedModel;
        mDefaultValue = defaultValue;
    }

    public T getObject() {
        T val = mNestedModel.getObject();
        return val == null ? mDefaultValue : val;
    }

    public void setObject(T object) {
        mNestedModel.setObject(object);
    }

    public void detach() {
        mNestedModel.detach();
    }

}

It could be used like: new DefaultWhenNullModel<String>(new
PropertyModel<String>(object, "property"), "your message");

Nesting models like this can get you some very effective reusable pieces of
code.

-- 
Jeremy Thomerson
http://www.wickettraining.com

On Sun, Oct 26, 2008 at 6:55 PM, Antony Stubbs <[EMAIL PROTECTED]>wrote:

> Would something as minor as this be considered for the core?
> I find it useful because I often want a component (in may case a Label
> type)
> to render a short message if the model is null e.g. "n/a" or "please select
> a type of cheese". I first tried extending Label to do this, but hit a wall
> because getDefaultModelObjectAsString is final (onComponentTagBody is
> protected, but getDefaultModelObjectAsString returning "" is not the same
> as
> null).
> Let me know if there's a nicer way to achieve the same thing. class
> DefaultPropertyModel<T> extends PropertyModel<T> { private T defaultModel;
> public DefaultPropertyModel(Object modelObject, String expression, T
> defaultObject) { super(modelObject, expression); this.defaultModel =
> defaultObject; } @Override public T getObject() { T o = super.getObject();
> if (o == null) return this.defaultModel; return o; } } --
> ___________________________ http://stubbisms.wordpress.com/
>

Reply via email to