0

I'm trying to parameterize a string and set that string as the result of a model:

SomePanel.java

public SomePanel( String id, IModel<Person> personModel)
{
  tallLabel = new Label( "height", new LoadableDetachableModel() {
    pubic String load() {
      Person person = personModel.getObject();
      boolean isTall = apiCallToCheckIfTall( person );

      // 'name' is a property on PersonModel
      String name = person.getName();

      String tallString = MessageFormat.format(getString("Tall.Label"), name );
      String shortString = MessageFormat.format(getString("Short.Label"), name );
  
      return isTall ? tallString : shortString;
    }
  });

  add(tallLabel);
}

Text.properties

Tall.Label = ${name} is tall.
Short.Label = ${name} is short.

I tried implementing a solution but contact.getName() produces an error. My understanding is that personModel.getObject() would give me the actual object (which has getter getName defined) so not sure why this would produce an error.

jjkl
  • 353
  • 6
  • 15
  • 1
    What exactly is the error ? What are `TallModel` and `ShortModel` ? They are not defined earlier and look like classes with static methods. – martin-g May 11 '21 at 06:16
  • @martin-g I revised the question. Sorry about the confusion. – jjkl May 11 '21 at 12:27

2 Answers2

0

MessageFormat uses indexed parameters, so you probably mixed up some technologies here.

Here's the simplest solution using Wicket's resource messages with names parameters:

return getString(isTall ? "Tall.Label" : "Short.Label", personModel)
svenmeier
  • 5,681
  • 17
  • 22
  • Thanks, this works. When the dropdown (https://stackoverflow.com/questions/67452003/api-call-not-triggered-in-onchange-of-dropdown-selection) is unselected; however, `$name` shows in the displayed text (`${name} is tall`). Am I supposed to be handling this case? – jjkl May 11 '21 at 12:22
  • Well, without a selection the person is null. I don't know what you want to display in that case, e.g. you could override #onConfigure and #setVisible(false). – svenmeier May 11 '21 at 13:58
0

I managed to get it to work with:

SomePanel.java

public SomePanel( String id, IModel<Person> personModel)
{
  tallLabel = new Label( "height", new LoadableDetachableModel() {
    public String load() {
      Person person = personModel.getObject();
      boolean isTall = apiCallToCheckIfTall( person );

      PersonGetter getter = new PersonGetter ( personModel );
      String name = getter.getName();

      String RTStringModel = MessageFormat.format( getString("Tall.Label"), person.getName() );
      String StringModel = MessageFormat.format( getString("Short.Label"), person.getName() );
  
      return isTall ? RTStringModel : StringModel;
    }
  });

  add(tallLabel);
}

...

private class NameGetter implements Serializable
{
    private final IModel<Person> model;

    public NameGetter( final IModel<Person> personModel )
    {
        this.model = person;
    }

    public String getName()
    {
        return getFormattedLegalName( this.model.getObject() );
    }
}

public static final String getFormattedLegalName( Person person )
{
    if ( person == null )
    {
        return "";
    }
    else
    {
        return person.getName();
    }
}

Text.properties

Tall.Label = {0} is tall.
Short.Label = {0} is short.

This seems to be a bit too much to extract a value from the model though. I couldn't get the name from the personModel directly (e.g. personModel.getObject().getName()) and went the convoluted route of having to create another class.

jjkl
  • 353
  • 6
  • 15