Re: Display a String when null
I would do something like this, because it's very reusable: Change your model from this: new PropertyModel(this, "ipLocationInfo.country") to: new NotNullModel(new PropertyModel(this, "ipLocationInfo.country"), new StringResourceModel("nullValue", null)) Then add nullValue=FOO BAR to your properties (you get the idea) Here's that implementation: public class NotNullModel implements IModel { private static final long serialVersionUID = 1L; private final IModel mNestedModel; private final IModel mNullObjectModel; public NotNullModel(IModel model, IModel nullValueModel) { if (model == null || nullValueModel == null) { throw new IllegalArgumentException("nested model must be non-null"); } mNestedModel = model; mNullObjectModel = nullValueModel; } public Object getObject() { Object obj = mNestedModel.getObject(); return obj == null ? mNullObjectModel.getObject() : obj; } public void setObject(Object object) { mNestedModel.setObject(object); } public void detach() { mNestedModel.detach(); } } Jeremy Thomerson http://www.wickettraining.com On Mon, May 5, 2008 at 9:30 AM, AlexTM <[EMAIL PROTECTED]> wrote: > > Hi! > > I'm quite new to Wicket and help with something easy, i guess. > > I'm displaying some info on my page: > > add(new Label("country", new PropertyModel(this, > "ipLocationInfo.country"))); > > The country information is sometimes null and then i would like to display > "unknown" instead. Is this easily managed or do i have to change the > country > value of the ipLocationInfo object? > > Regards Alex > -- > View this message in context: > http://www.nabble.com/Display-a-String-when-null-tp17062912p17062912.html > Sent from the Wicket - User mailing list archive at Nabble.com. > > > - > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > >
Re: Display a String when null
write a model decorator that chains another model, checks if it returns null, and uses another value class NullSafeModel implements imodel { private final imodel delegate; private final String nullvalue; public nullsafemodel(imodel delegate, string nullvalue) { this.delegate=delegate; ...} object getobject() { object o=delegate.getobject(); return (o!=null)?o:nullValue; } void setobject(object o) { delegate.setobject(o); } void detach() { delegate.detach();} } add(new label("foo", new nullsafemodel(new propertymodel(..), "unknown")); -igor On Mon, May 5, 2008 at 7:49 AM, AlexTM <[EMAIL PROTECTED]> wrote: > > Thanks! > > The problem is that the opLocationInfo has ten (10) different Strings that > may be null so is there a generic solution to it? > > /Alex > > > > > Vit Rozkovec wrote: > > > > Hi! > > Maybe there is a better way, but I would do: > > > > add(new Label("country", new PropertyModel(this, > > "ipLocationInfo.country") { > > @Override > > public Object getObject() { > > String country = (String) super.getObject(); > > country = country == null ? "unknown" : country; > > return country; > > } > > })); > > > > > > Vitek > > > > > > AlexTM wrote: > >> Hi! > >> > >> I'm quite new to Wicket and help with something easy, i guess. > >> > >> I'm displaying some info on my page: > >> > >> add(new Label("country", new PropertyModel(this, > >> "ipLocationInfo.country"))); > >> > >> The country information is sometimes null and then i would like to > >> display > >> "unknown" instead. Is this easily managed or do i have to change the > >> country > >> value of the ipLocationInfo object? > >> > >> Regards Alex > >> > > > > > > - > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > > > > > > > -- > View this message in context: > http://www.nabble.com/Display-a-String-when-null-tp17062912p17063364.html > > Sent from the Wicket - User mailing list archive at Nabble.com. > > > - > > > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Display a String when null
john's example is better because it is refactorable. my 2c. -igor On Mon, May 5, 2008 at 7:44 AM, Vit Rozkovec <[EMAIL PROTECTED]> wrote: > Hi! > Maybe there is a better way, but I would do: > > >add(new Label("country", new PropertyModel(this, >"ipLocationInfo.country") { >@Override >public Object getObject() { >String country = (String) super.getObject(); >country = country == null ? "unknown" : country; >return country; >} >})); > > > Vitek > > > > AlexTM wrote: > > > Hi! > > > > I'm quite new to Wicket and help with something easy, i guess. > > > > I'm displaying some info on my page: > > > > add(new Label("country", new PropertyModel(this, > > "ipLocationInfo.country"))); > > > > The country information is sometimes null and then i would like to display > > "unknown" instead. Is this easily managed or do i have to change the > country > > value of the ipLocationInfo object? > > > > Regards Alex > > > > > > > > - > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Display a String when null
Thanks! The problem is that the opLocationInfo has ten (10) different Strings that may be null so is there a generic solution to it? /Alex Vit Rozkovec wrote: > > Hi! > Maybe there is a better way, but I would do: > > add(new Label("country", new PropertyModel(this, > "ipLocationInfo.country") { > @Override > public Object getObject() { > String country = (String) super.getObject(); > country = country == null ? "unknown" : country; > return country; > } > })); > > > Vitek > > > AlexTM wrote: >> Hi! >> >> I'm quite new to Wicket and help with something easy, i guess. >> >> I'm displaying some info on my page: >> >> add(new Label("country", new PropertyModel(this, >> "ipLocationInfo.country"))); >> >> The country information is sometimes null and then i would like to >> display >> "unknown" instead. Is this easily managed or do i have to change the >> country >> value of the ipLocationInfo object? >> >> Regards Alex >> > > > - > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > > -- View this message in context: http://www.nabble.com/Display-a-String-when-null-tp17062912p17063364.html Sent from the Wicket - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Display a String when null
Hi! Maybe there is a better way, but I would do: add(new Label("country", new PropertyModel(this, "ipLocationInfo.country") { @Override public Object getObject() { String country = (String) super.getObject(); country = country == null ? "unknown" : country; return country; } })); Vitek AlexTM wrote: Hi! I'm quite new to Wicket and help with something easy, i guess. I'm displaying some info on my page: add(new Label("country", new PropertyModel(this, "ipLocationInfo.country"))); The country information is sometimes null and then i would like to display "unknown" instead. Is this easily managed or do i have to change the country value of the ipLocationInfo object? Regards Alex - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Display a String when null
AlexTM wrote: > > add(new Label("country", new PropertyModel(this, > "ipLocationInfo.country"))); > > The country information is sometimes null and then i would like to display > "unknown" instead. > One way of doing this would be to create your own model. add(new Label("country", new AbstractReadOnlyModel() { public Object getObject() { String country = getIpLocationInfo().getCountry(); if (country != null) return country; else return "Unknown"; } })); -- View this message in context: http://www.nabble.com/Display-a-String-when-null-tp17062912p17063240.html Sent from the Wicket - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]