org.apache.wicket.model
Class StringResourceModel

java.lang.Object
  extended by org.apache.wicket.model.LoadableDetachableModel<java.lang.String>
      extended by org.apache.wicket.model.StringResourceModel
All Implemented Interfaces:
java.io.Serializable, IClusterable, IComponentAssignedModel<java.lang.String>, IDetachable, IModel<java.lang.String>

public class StringResourceModel
extends LoadableDetachableModel<java.lang.String>
implements IComponentAssignedModel<java.lang.String>

This model class encapsulates the full power of localization support within the Wicket framework. It combines the flexible Wicket resource loading mechanism with property expressions, property models and standard Java MessageFormat substitutions. This combination should be able to solve any dynamic localization requirement that a project has.

The model should be created with four parameters, which are described in detail below:

As well as the supplied parameters, the found string resource can contain formatting information. It may contain property expressions in which case these are evaluated using the model object supplied when the string resource model is created. The string resource may also contain java.text.MessageFormat style markup for replacement of parameters. Where a string resource contains both types of formatting information then the property expression will be applied first.

Example 1

In its simplest form, the model can be used as follows:

 public MyPage extends WebPage<Void>
 {
    public MyPage(final PageParameters parameters)
    {
        add(new Label("username", new StringResourceModel("label.username", this, null)));
    }
 }
 
Where the resource bundle for the page contains the entry label.username=Username

Example 2

In this example, the resource key is selected based on the evaluation of a property expression:

 public MyPage extends WebPage<Void>
 {
     public MyPage(final PageParameters parameters)
     {
         WeatherStation ws = new WeatherStation();
         add(new Label("weatherMessage",
             new StringResourceModel("weather.${currentStatus}", this, new Model<String>(ws)));
     }
 }
 
Which will call the WeatherStation.getCurrentStatus() method each time the string resource model is used and where the resource bundle for the page contains the entries:
 weather.sunny=Don't forget sunscreen!
 weather.raining=You might need an umbrella
 weather.snowing=Got your skis?
 weather.overcast=Best take a coat to be safe
 

Example 3

In this example the found resource string contains a property expression that is substituted via the model:

 public MyPage extends WebPage<Void>
 {
     public MyPage(final PageParameters parameters)
     {
         WeatherStation ws = new WeatherStation();
         add(new Label("weatherMessage",
             new StringResourceModel("weather.message", this, new Model<String>(ws)));
     }
 }
 
Where the resource bundle contains the entry weather.message=Weather station reports that the temperature is ${currentTemperature} ${units}

Example 4

In this example, the use of substitution parameters is employed to format a quite complex message string. This is an example of the most complex and powerful use of the string resource model:

 public MyPage extends WebPage<Void>
 {
     public MyPage(final PageParameters parameters)
     {
         WeatherStation ws = new WeatherStation();
         IModel<WeatherStation> model = new Model<WeatherStation>(ws);
         add(new Label("weatherMessage",
             new StringResourceModel(
                 "weather.detail", this, model,
                     new Object[]
                     {
                         new Date(),
                         new PropertyModel<?>(model, "currentStatus"),
                         new PropertyModel<?>(model, "currentTemperature"),
                         new PropertyModel<?>(model, "units")
         }));
     }
 }
 
And where the resource bundle entry is:
 weather.detail=The report for {0,date}, shows the temperature as {2,number,###.##} {3} \
     and the weather to be {1}
 

Author:
Chris Turner
See Also:
for additional information especially on the component search order, Serialized Form

Constructor Summary
StringResourceModel(java.lang.String resourceKey, Component component, IModel<?> model, java.lang.Object... parameters)
          Creates a new string resource model using the supplied parameters.
StringResourceModel(java.lang.String resourceKey, Component component, IModel<?> model, java.lang.String defaultValue, java.lang.Object... parameters)
          Creates a new string resource model using the supplied parameters.
StringResourceModel(java.lang.String resourceKey, IModel<?> model, java.lang.Object... parameters)
          Creates a new string resource model using the supplied parameters.
StringResourceModel(java.lang.String resourceKey, IModel<?> model, java.lang.String defaultValue, java.lang.Object... parameters)
          Creates a new string resource model using the supplied parameters.
 
Method Summary
 Localizer getLocalizer()
          Gets the localizer that is being used by this string resource model.
protected  java.lang.Object[] getParameters()
          Gets the Java MessageFormat substitution parameters.
protected  java.lang.String getResourceKey()
          Gets the resource key for this string resource.
 java.lang.String getString()
          Gets the string currently represented by this model.
protected  java.lang.String load()
          Gets the string that this string resource model currently represents.
protected  void onDetach()
          Detaches from the current request.
 void setObject(java.lang.String object)
          Manually loads the model with the specified object.
 java.lang.String toString()
          This method just returns debug information, so it won't return the localized string.
 IWrapModel<java.lang.String> wrapOnAssignment(Component component)
          This method is called when the component gets its model assigned.
 
Methods inherited from class org.apache.wicket.model.LoadableDetachableModel
detach, getObject, isAttached, onAttach
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.apache.wicket.model.IModel
getObject
 
Methods inherited from interface org.apache.wicket.model.IDetachable
detach
 

Constructor Detail

StringResourceModel

public StringResourceModel(java.lang.String resourceKey,
                           Component component,
                           IModel<?> model,
                           java.lang.Object... parameters)
Creates a new string resource model using the supplied parameters.

The relative component parameter should generally be supplied, as without it resources can not be obtained from resource bundles that are held relative to a particular component or page. However, for application that use only global resources then this parameter may be null.

The model parameter is also optional and only needs to be supplied if value substitutions are to take place on either the resource key or the actual resource strings.

The parameters parameter is also optional and is used for substitutions.

Parameters:
resourceKey - The resource key for this string resource
component - The component that the resource is relative to
model - The model to use for property substitutions
parameters - The parameters to substitute using a Java MessageFormat object

StringResourceModel

public StringResourceModel(java.lang.String resourceKey,
                           Component component,
                           IModel<?> model,
                           java.lang.String defaultValue,
                           java.lang.Object... parameters)
Creates a new string resource model using the supplied parameters.

The relative component parameter should generally be supplied, as without it resources can not be obtained from resource bundles that are held relative to a particular component or page. However, for application that use only global resources then this parameter may be null.

The model parameter is also optional and only needs to be supplied if value substitutions are to take place on either the resource key or the actual resource strings.

The parameters parameter is also optional and is used for substitutions.

Parameters:
resourceKey - The resource key for this string resource
component - The component that the resource is relative to
model - The model to use for property substitutions
parameters - The parameters to substitute using a Java MessageFormat object
defaultValue - The default value if the resource key is not found.

StringResourceModel

public StringResourceModel(java.lang.String resourceKey,
                           IModel<?> model,
                           java.lang.Object... parameters)
Creates a new string resource model using the supplied parameters.

The model parameter is also optional and only needs to be supplied if value substitutions are to take place on either the resource key or the actual resource strings.

The parameters parameter is also optional and is used for substitutions.

Parameters:
resourceKey - The resource key for this string resource
model - The model to use for property substitutions
parameters - The parameters to substitute using a Java MessageFormat object

StringResourceModel

public StringResourceModel(java.lang.String resourceKey,
                           IModel<?> model,
                           java.lang.String defaultValue,
                           java.lang.Object... parameters)
Creates a new string resource model using the supplied parameters.

The model parameter is also optional and only needs to be supplied if value substitutions are to take place on either the resource key or the actual resource strings.

The parameters parameter is also optional and is used for substitutions.

Parameters:
resourceKey - The resource key for this string resource
model - The model to use for property substitutions
parameters - The parameters to substitute using a Java MessageFormat object
defaultValue - The default value if the resource key is not found.
Method Detail

wrapOnAssignment

public IWrapModel<java.lang.String> wrapOnAssignment(Component component)
Description copied from interface: IComponentAssignedModel
This method is called when the component gets its model assigned. WARNING: Because the model can be assigned in the constructor of component this method can also be called with a 'this' of a component that is not fully constructed yet.

Specified by:
wrapOnAssignment in interface IComponentAssignedModel<java.lang.String>
Returns:
The WrapModel that wraps this model

getLocalizer

public Localizer getLocalizer()
Gets the localizer that is being used by this string resource model.

Returns:
The localizer

getString

public final java.lang.String getString()
Gets the string currently represented by this model. The string that is returned may vary for each call to this method depending on the values contained in the model and an the parameters that were passed when this string resource model was created.

Returns:
The string

toString

public java.lang.String toString()
This method just returns debug information, so it won't return the localized string. Please use getString() for that.

Overrides:
toString in class LoadableDetachableModel<java.lang.String>
Returns:
The string for this model object
See Also:
Object.toString()

getParameters

protected java.lang.Object[] getParameters()
Gets the Java MessageFormat substitution parameters.

Returns:
The substitution parameters

getResourceKey

protected final java.lang.String getResourceKey()
Gets the resource key for this string resource. If the resource key contains property expressions and the model is not null then the returned value is the actual resource key with all substitutions undertaken.

Returns:
The (possibly substituted) resource key

load

protected java.lang.String load()
Gets the string that this string resource model currently represents.

Specified by:
load in class LoadableDetachableModel<java.lang.String>
Returns:
the (temporary) model object

onDetach

protected final void onDetach()
Description copied from class: LoadableDetachableModel
Detaches from the current request. Implement this method with custom behavior, such as setting the model object to null.

Overrides:
onDetach in class LoadableDetachableModel<java.lang.String>
See Also:
IDetachable.detach()

setObject

public void setObject(java.lang.String object)
Description copied from class: LoadableDetachableModel
Manually loads the model with the specified object. Subsequent calls to LoadableDetachableModel.getObject() will return object until LoadableDetachableModel.detach() is called.

Specified by:
setObject in interface IModel<java.lang.String>
Overrides:
setObject in class LoadableDetachableModel<java.lang.String>
Parameters:
object - The object to set into the model


Copyright © 2006-2011 Apache Software Foundation. All Rights Reserved.