Why is your property model not returning the String version for label
anyway?  It is generally bad form to rely on toString() for a display
value....  So rather than returning Movie, do: new
PropertyModel(ThingThatHoldsMovie, "movie.title") - wrap that in the model I
showed earlier, and you have your solution.

Otherwise, you could just override onComponentTagBody with:
    @Override
    protected void onComponentTagBody(final MarkupStream markupStream, final
ComponentTag openTag)
    {
        Object obj = getDefaultModelObject();
        String val = obj == null ? "your string" : obj;
        replaceComponentTagBody(markupStream, openTag, val);
    }


In other words, any of these options will work, but like you say - you
probably shouldn't have a model that returns a Movie sometimes and a String
other times.  This is contrary to the "generified" contract.

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


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

> 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/
>

Reply via email to