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]

Reply via email to