|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.wicket.model.LoadableDetachableModel<String>
org.apache.wicket.model.StringResourceModel
public class StringResourceModel
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:
ComponentStringResourceLoader
. The relative component
may actually be null
when all resource loading is to be done from a global resource
loader. However, we recommend that a relative component is still supplied even in these cases in
order to 'future proof' your application with regards to changing resource loading strategies.
null
java.text.MessageFormat
object. Each parameter may be an ordinary Object, in which
case it will be processed by the standard formatting rules associated with
java.text.MessageFormat
. Alternatively, the parameter may be an instance of
IModel
in which case the getObject()
method will be applied prior to
the parameter being passed to the java.text.MessageFormat
. This allows such features
dynamic parameters that are obtained using a PropertyModel
object or even nested
string resource models.
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}
for additional information especially on the component search
order
,
Serialized FormConstructor Summary | |
---|---|
StringResourceModel(String resourceKey,
Component component,
IModel<?> model)
Construct. |
|
StringResourceModel(String resourceKey,
Component component,
IModel<?> model,
Object[] parameters)
Creates a new string resource model using the supplied parameters. |
|
StringResourceModel(String resourceKey,
Component component,
IModel<?> model,
Object[] parameters,
String defaultValue)
Creates a new string resource model using the supplied parameters. |
|
StringResourceModel(String resourceKey,
Component component,
IModel<?> model,
String defaultValue)
Construct. |
|
StringResourceModel(String resourceKey,
IModel<?> model)
Construct. |
|
StringResourceModel(String resourceKey,
IModel<?> model,
Object[] parameters)
Creates a new string resource model using the supplied parameters. |
|
StringResourceModel(String resourceKey,
IModel<?> model,
Object[] parameters,
String defaultValue)
Creates a new string resource model using the supplied parameters. |
|
StringResourceModel(String resourceKey,
IModel<?> model,
String defaultValue)
Construct. |
Method Summary | |
---|---|
Localizer |
getLocalizer()
Gets the localizer that is being used by this string resource model. |
protected Object[] |
getParameters()
Gets the Java MessageFormat substitution parameters. |
protected String |
getResourceKey()
Gets the resource key for this string resource. |
String |
getString()
Gets the string currently represented by this string resource model. |
protected String |
load()
Gets the string that this string resource model currently represents. |
protected void |
onDetach()
Detaches from the given session |
void |
setLocalizer(Localizer localizer)
Sets the localizer that is being used by this string resource model. |
void |
setObject(String object)
Manually loads the model with the specified object. |
String |
toString()
This method just returns debug information, so it won't return the localized string. |
IWrapModel<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 |
---|
public StringResourceModel(String resourceKey, Component component, IModel<?> model)
resourceKey
- The resource key for this string resourcecomponent
- The component that the resource is relative tomodel
- The model to use for property substitutionsStringResourceModel(String, Component, IModel, Object[])
public StringResourceModel(String resourceKey, Component component, IModel<?> model, String defaultValue)
resourceKey
- The resource key for this string resourcecomponent
- The component that the resource is relative tomodel
- The model to use for property substitutionsdefaultValue
- The default value if the resource key is not found.StringResourceModel(String, Component, IModel, Object[])
public StringResourceModel(String resourceKey, Component component, IModel<?> model, Object[] 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.
resourceKey
- The resource key for this string resourcecomponent
- The component that the resource is relative tomodel
- The model to use for property substitutionsparameters
- The parameters to substitute using a Java MessageFormat objectpublic StringResourceModel(String resourceKey, Component component, IModel<?> model, Object[] parameters, String defaultValue)
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.
resourceKey
- The resource key for this string resourcecomponent
- The component that the resource is relative tomodel
- The model to use for property substitutionsparameters
- The parameters to substitute using a Java MessageFormat objectdefaultValue
- The default value if the resource key is not found.public StringResourceModel(String resourceKey, IModel<?> model)
resourceKey
- The resource key for this string resourcemodel
- The model to use for property substitutionsStringResourceModel(String, Component, IModel, Object[])
public StringResourceModel(String resourceKey, IModel<?> model, String defaultValue)
resourceKey
- The resource key for this string resourcemodel
- The model to use for property substitutionsdefaultValue
- The default value if the resource key is not found.StringResourceModel(String, Component, IModel, Object[])
public StringResourceModel(String resourceKey, IModel<?> model, Object[] 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.
resourceKey
- The resource key for this string resourcemodel
- The model to use for property substitutionsparameters
- The parameters to substitute using a Java MessageFormat objectpublic StringResourceModel(String resourceKey, IModel<?> model, Object[] parameters, String defaultValue)
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.
resourceKey
- The resource key for this string resourcemodel
- The model to use for property substitutionsparameters
- The parameters to substitute using a Java MessageFormat objectdefaultValue
- The default value if the resource key is not found.Method Detail |
---|
public IWrapModel<String> wrapOnAssignment(Component component)
IComponentAssignedModel
wrapOnAssignment
in interface IComponentAssignedModel<String>
public Localizer getLocalizer()
public final String getString()
public final void setLocalizer(Localizer localizer)
localizer
- The localizer to usepublic String toString()
toString
in class LoadableDetachableModel<String>
Object.toString()
protected Object[] getParameters()
protected final String getResourceKey()
protected String load()
load
in class LoadableDetachableModel<String>
protected final void onDetach()
onDetach
in class LoadableDetachableModel<String>
public void setObject(String object)
LoadableDetachableModel
LoadableDetachableModel.getObject()
will return object
until LoadableDetachableModel.detach()
is called.
setObject
in interface IModel<String>
setObject
in class LoadableDetachableModel<String>
object
- The object to set into the model
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |