Hmm, generics soup...
How would this version work in the case where I would normally have a domain
object resolving my model - e.g. Movie, and I don't want to make a 'default
Movie' of title "n/a"?
Hmm, actually I think this high lights an error in my DefaultPropertyModel,
because if it's genereified with String, that's fine for when it's the
default string value, and it's fine for code which simply calls
getObject().toString (as label does), but if I had other code that called
getObject() and expected a String, as the compiler says it will return a
String, where in fact it will return a Movie.
2008/10/27 Jeremy Thomerson <[EMAIL PROTECTED]>
> 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/
> >
>
--
___________________________
http://stubbisms.wordpress.com/