Repository: wicket Updated Branches: refs/heads/master b680969db -> 3c17e8cc0
Updated JavaDoc and user guide for WICKET-5906 Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/3c17e8cc Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/3c17e8cc Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/3c17e8cc Branch: refs/heads/master Commit: 3c17e8cc05e3671fc0962b6c74b252537a45402d Parents: b680969 Author: Andrea Del Bene <â[email protected]â> Authored: Mon May 25 17:51:34 2015 +0200 Committer: Andrea Del Bene <â[email protected]â> Committed: Mon May 25 17:51:34 2015 +0200 ---------------------------------------------------------------------- .../wicket/model/StringResourceModel.java | 30 +++++++++++++++----- .../src/docs/guide/i18n/i18n_5.gdoc | 15 ++++++++-- 2 files changed, 35 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/3c17e8cc/wicket-core/src/main/java/org/apache/wicket/model/StringResourceModel.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/model/StringResourceModel.java b/wicket-core/src/main/java/org/apache/wicket/model/StringResourceModel.java index 59a89c5..04935aa 100644 --- a/wicket-core/src/main/java/org/apache/wicket/model/StringResourceModel.java +++ b/wicket-core/src/main/java/org/apache/wicket/model/StringResourceModel.java @@ -62,7 +62,7 @@ import org.apache.wicket.util.string.Strings; * expressions. Where property expressions are present they will all be evaluated relative to this * model object. If there are no property expressions present then this model parameter may be * <code>null</code> - * <li><b>parameters </b>- The parameters parameter allows an array of objects to be passed for + * <li><b>parameters </b>- This parameter allows an array of objects to be passed for * substitution on the found string resource (see below) using a standard * <code>java.text.MessageFormat</code> object. Each parameter may be an ordinary Object, in which * case it will be processed by the standard formatting rules associated with @@ -70,7 +70,8 @@ import org.apache.wicket.util.string.Strings; * <code>IModel</code> in which case the <code>getObject()</code> method will be applied prior to * the parameter being passed to the <code>java.text.MessageFormat</code>. This allows such features * dynamic parameters that are obtained using a <code>PropertyModel</code> object or even nested - * string resource models. + * string resource models. Unlike the other parameters listed above this one can not be provided + * as constructor parameter but rather using method {@link #setParameters(Object...)}. * </ul> * 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 @@ -79,6 +80,22 @@ import org.apache.wicket.util.string.Strings; * resource contains both types of formatting information then the property expression will be * applied first. * <p> + * <b>Example Bean </b> + * <p> + * In the next examples we will use the following class as bundle model: + * <pre> + * public class WeatherStation implements Serializable + * { + * private final String name = "Europe's main weather station"; + * + * private String currentStatus = "sunny"; + * + * private double currentTemperature = 25.7; + * + * private String units = "\u00B0C"; + * } + * </pre> + * <p> * <b>Example 1 </b> * <p> * In its simplest form, the model can be used as follows: @@ -88,7 +105,7 @@ import org.apache.wicket.util.string.Strings; * { * public MyPage(final PageParameters parameters) * { - * add(new Label("username", new StringResourceModel("label.username", this, null))); + * add(new Label("username", new StringResourceModel("label.username", this))); * } * } * </pre> @@ -155,14 +172,13 @@ import org.apache.wicket.util.string.Strings; * WeatherStation ws = new WeatherStation(); * IModel<WeatherStation> model = new Model<WeatherStation>(ws); * add(new Label("weatherMessage", - * new StringResourceModel( - * "weather.detail", this, model, - * new Object[] - * { + * new StringResourceModel("weather.detail", this) + * .setParameters( * new Date(), * new PropertyModel<?>(model, "currentStatus"), * new PropertyModel<?>(model, "currentTemperature"), * new PropertyModel<?>(model, "units") + * ) * })); * } * } http://git-wip-us.apache.org/repos/asf/wicket/blob/3c17e8cc/wicket-user-guide/src/docs/guide/i18n/i18n_5.gdoc ---------------------------------------------------------------------- diff --git a/wicket-user-guide/src/docs/guide/i18n/i18n_5.gdoc b/wicket-user-guide/src/docs/guide/i18n/i18n_5.gdoc index 3e4b437..4db429d 100644 --- a/wicket-user-guide/src/docs/guide/i18n/i18n_5.gdoc +++ b/wicket-user-guide/src/docs/guide/i18n/i18n_5.gdoc @@ -70,14 +70,16 @@ Order order = new Order(new Date(), ORDER_STATUS.IN_PROGRESS); add(new Label("orderStatus", new StringResourceModel("orderStatus.${status.code}", Model.of(order)))); {code} -As we can see in the code above also the key contains a property expression (${status.code}) which makes its value dynamic. In this way the state of an object (an Order in our example) can determinate which resource will be loaded by StringResourceModel. If we don't use properties expressions we can provide a null value as model and in this case StringResourceModel will behave exactly as a ResourceModel. StringResourceModel supports also the same parameter substitution used by standard class java.text.MessageFormat. Parameters can be generic objects but if we use a model as parameter, StringResourceModel will use the data object inside it as actual value (it will call getObject on the model). Parameters are passed to constructor as a vararg argument. Here is an example of usage of parameter substitution: +As we can see in the code above also the key contains a property expression (${status.code}) which makes its value dynamic. In this way the state of an object (an Order in our example) can determinate which resource will be loaded by StringResourceModel. If we don't use properties expressions we can provide a null value as model and in this case StringResourceModel will behave exactly as a ResourceModel. StringResourceModel supports also the same parameter substitution used by standard class @java.text.MessageFormat@. + +Parameters can be generic objects but if we use a model as parameter, StringResourceModel will use the data object inside it as actual value (it will call getObject on the model). Parameters are passed as a vararg argument with method @setParameters(Object... parameters)@. Here is an example of usage of parameter substitution: Java code: {code} PropertyModel propertyModel = new PropertyModel<Order>(order, "orderDate"); //build a string model with two parameters: a property model and an integer value -StringResourceModel srm = new StringResourceModel("orderStatus.delay", null, propertyModel, 3); +StringResourceModel srm = new StringResourceModel("orderStatus.delay").setParameters(propertyModel, 3); {code} Bundle: @@ -89,5 +91,12 @@ orderStatus.delay=Your order submitted on ${0} has been delayed by {1} days. One further parameter we can specify when we build a StringResourceModel is the component that must be used by the lookup algorithm. Normally this parameter is not relevant, but if we need to use a particular bundle owned by a component not considered by the algorithm, we can specify this component as second parameter. If we pass all possible parameters to StringResourceModel's constructor we obtain something like this: {code} -new StringResourceModel("myKey", myComponent, myModel, param1, param2, param3,...); +new StringResourceModel("myKey", myComponent, myModel); +{code} + +Default value is supported as well, both as string model or as string value: + +{code} +new StringResourceModel("myKey", myComponent, myModel).setDefaultValue("default"); {code} +
