You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by AlexTM <al...@bwin.org> on 2008/05/05 16:30:42 UTC

Display a String when null

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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Display a String when null

Posted by Igor Vaynberg <ig...@gmail.com>.
john's example is better because it is refactorable. my 2c.

-igor


On Mon, May 5, 2008 at 7:44 AM, Vit Rozkovec <wi...@rozkovec.info> 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: users-unsubscribe@wicket.apache.org
>  For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Display a String when null

Posted by Igor Vaynberg <ig...@gmail.com>.
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 <al...@bwin.org> 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: users-unsubscribe@wicket.apache.org
>  > For additional commands, e-mail: users-help@wicket.apache.org
>  >
>  >
>  >
>
>  --
>  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: users-unsubscribe@wicket.apache.org
>  For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Display a String when null

Posted by AlexTM <al...@bwin.org>.
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: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
> 
> 
> 

-- 
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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Display a String when null

Posted by Vit Rozkovec <wi...@rozkovec.info>.
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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Display a String when null

Posted by John Ray <jo...@newonic.com>.

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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Display a String when null

Posted by Jeremy Thomerson <je...@wickettraining.com>.
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 <al...@bwin.org> 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: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>