Repository: wicket
Updated Branches:
  refs/heads/wicket-6.x fdb060119 -> e850af675


WICKET-5754 (String)ResourceModel's defaultValue could be an IModel<String>

(cherry picked from commit c4db9222effe80c49c20bd18f3e3c6773b8c8618)


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/e850af67
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/e850af67
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/e850af67

Branch: refs/heads/wicket-6.x
Commit: e850af675ec4cae295f3895166f4846d70f5168d
Parents: fdb0601
Author: Martin Tzvetanov Grigorov <[email protected]>
Authored: Mon Nov 10 13:03:10 2014 +0200
Committer: Martin Tzvetanov Grigorov <[email protected]>
Committed: Mon Nov 10 13:08:30 2014 +0200

----------------------------------------------------------------------
 .../main/java/org/apache/wicket/Localizer.java  | 59 +++++++++++++++-----
 .../org/apache/wicket/model/ResourceModel.java  | 13 +++--
 .../model/ResourceModelTest$TestPage.html       |  3 +-
 .../ResourceModelTest$TestPage_expected.html    |  3 +-
 .../apache/wicket/model/ResourceModelTest.java  |  9 ++-
 5 files changed, 66 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/e850af67/wicket-core/src/main/java/org/apache/wicket/Localizer.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/Localizer.java 
b/wicket-core/src/main/java/org/apache/wicket/Localizer.java
index 0594233..bc83f84 100644
--- a/wicket-core/src/main/java/org/apache/wicket/Localizer.java
+++ b/wicket-core/src/main/java/org/apache/wicket/Localizer.java
@@ -28,6 +28,7 @@ import java.util.concurrent.atomic.AtomicLong;
 import 
org.apache.wicket.core.util.string.interpolator.ConvertingPropertyVariableInterpolator;
 import org.apache.wicket.markup.repeater.AbstractRepeater;
 import org.apache.wicket.model.IModel;
+import org.apache.wicket.model.Model;
 import org.apache.wicket.resource.loader.IStringResourceLoader;
 import org.apache.wicket.settings.IResourceSettings;
 import org.apache.wicket.util.lang.Generics;
@@ -108,7 +109,7 @@ public class Localizer
        public String getString(final String key, final Component component)
                throws MissingResourceException
        {
-               return getString(key, component, null, null, null, null);
+               return getString(key, component, null, null, null, 
(String)null);
        }
 
        /**
@@ -127,7 +128,7 @@ public class Localizer
        public String getString(final String key, final Component component, 
final IModel<?> model)
                throws MissingResourceException
        {
-               return getString(key, component, model, null, null, null);
+               return getString(key, component, model, null, null, 
(String)null);
        }
 
        /**
@@ -196,30 +197,60 @@ public class Localizer
                final Locale locale, final String style, final String 
defaultValue)
                throws MissingResourceException
        {
+               IModel<String> defaultValueModel = defaultValue != null ? 
Model.of(defaultValue) : null;
+               return getString(key, component, model, locale, style, 
defaultValueModel);
+       }
+
+       /**
+        * Get the localized string using all of the supplied parameters. This 
method is left public to
+        * allow developers full control over string resource loading. However, 
it is recommended that
+        * one of the other convenience methods in the class are used as they 
handle all of the work
+        * related to obtaining the current user locale and style information.
+        *
+        * @param key
+        *            The key to obtain the resource for
+        * @param component
+        *            The component to get the resource for (optional)
+        * @param model
+        *            The model to use for substitutions in the strings 
(optional)
+        * @param locale
+        *            If != null, it'll supersede the component's locale
+        * @param style
+        *            If != null, it'll supersede the component's style
+        * @param defaultValue
+        *            The default value (optional)
+        * @return The string resource
+        * @throws MissingResourceException
+        *             If resource not found and configuration dictates that 
exception should be thrown
+        */
+       public String getString(final String key, final Component component, 
final IModel<?> model,
+                               final Locale locale, final String style, final 
IModel<String> defaultValue)
+                       throws MissingResourceException
+       {
                final IResourceSettings resourceSettings = 
Application.get().getResourceSettings();
 
                String value = getStringIgnoreSettings(key, component, model, 
locale, style, null);
-               if ((value == null) && (defaultValue != null))
+
+               // If a property value has been found, or a default value was 
given,
+               // than replace the placeholder and we are done
+               if (value != null)
+               {
+                       return value;
+               }
+               else if (defaultValue != null && 
resourceSettings.getUseDefaultOnMissingResource())
                {
                        // Resource not found, so handle missing resources 
based on
                        // application configuration and try the default value
-                       if (resourceSettings.getUseDefaultOnMissingResource())
-                       {
-                               value = defaultValue;
+                       value = defaultValue.getObject();
 
+                       if (value != null)
+                       {
                                // If a property value has been found, or a 
default value was given,
-                               // than replace the placeholder and we are done
+                               // then replace the placeholder and we are done
                                return substitutePropertyExpressions(component, 
value, model);
                        }
                }
 
-               // If a property value has been found, or a default value was 
given,
-               // than replace the placeholder and we are done
-               if (value != null)
-               {
-                       return value;
-               }
-
                if (resourceSettings.getThrowExceptionOnMissingResource())
                {
                        AppendingStringBuffer message = new 
AppendingStringBuffer("Unable to find property: '");

http://git-wip-us.apache.org/repos/asf/wicket/blob/e850af67/wicket-core/src/main/java/org/apache/wicket/model/ResourceModel.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/model/ResourceModel.java 
b/wicket-core/src/main/java/org/apache/wicket/model/ResourceModel.java
index 6abf065..85ec0db 100644
--- a/wicket-core/src/main/java/org/apache/wicket/model/ResourceModel.java
+++ b/wicket-core/src/main/java/org/apache/wicket/model/ResourceModel.java
@@ -38,7 +38,7 @@ public class ResourceModel extends 
AbstractReadOnlyModel<String>
 
        private final String resourceKey;
 
-       private final String defaultValue;
+       private final IModel<String> defaultValue;
 
        /**
         * Constructor
@@ -48,7 +48,7 @@ public class ResourceModel extends 
AbstractReadOnlyModel<String>
         */
        public ResourceModel(String resourceKey)
        {
-               this(resourceKey, null);
+               this(resourceKey, (IModel<String>)null);
        }
 
        /**
@@ -62,6 +62,11 @@ public class ResourceModel extends 
AbstractReadOnlyModel<String>
         */
        public ResourceModel(String resourceKey, String defaultValue)
        {
+               this(resourceKey, Model.of(defaultValue));
+       }
+
+       public ResourceModel(String resourceKey, IModel<String> defaultValue)
+       {
                this.resourceKey = resourceKey;
                this.defaultValue = defaultValue;
        }
@@ -76,7 +81,7 @@ public class ResourceModel extends 
AbstractReadOnlyModel<String>
                return Application.get()
                        .getResourceSettings()
                        .getLocalizer()
-                       .getString(resourceKey, null, defaultValue);
+                       .getString(resourceKey, null, null, null, null, 
defaultValue);
        }
 
        /**
@@ -124,7 +129,7 @@ public class ResourceModel extends 
AbstractReadOnlyModel<String>
                        return Application.get()
                                .getResourceSettings()
                                .getLocalizer()
-                               .getString(resourceKey, component, 
defaultValue);
+                               .getString(resourceKey, component, null, null, 
null, defaultValue);
                }
 
                @Override

http://git-wip-us.apache.org/repos/asf/wicket/blob/e850af67/wicket-core/src/test/java/org/apache/wicket/model/ResourceModelTest$TestPage.html
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/model/ResourceModelTest$TestPage.html
 
b/wicket-core/src/test/java/org/apache/wicket/model/ResourceModelTest$TestPage.html
index 78a14a3..7fbb19b 100644
--- 
a/wicket-core/src/test/java/org/apache/wicket/model/ResourceModelTest$TestPage.html
+++ 
b/wicket-core/src/test/java/org/apache/wicket/model/ResourceModelTest$TestPage.html
@@ -1,6 +1,7 @@
 <html>
 <body>
 <span wicket:id="testlabel">[a test]</span>
+<span wicket:id="testlabelWithDefault">[a test]</span>
 <span wicket:id="otherlabel">[a test]</span>
 </body>
-</html>
\ No newline at end of file
+</html>

http://git-wip-us.apache.org/repos/asf/wicket/blob/e850af67/wicket-core/src/test/java/org/apache/wicket/model/ResourceModelTest$TestPage_expected.html
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/model/ResourceModelTest$TestPage_expected.html
 
b/wicket-core/src/test/java/org/apache/wicket/model/ResourceModelTest$TestPage_expected.html
index aa85a20..0d0234c 100644
--- 
a/wicket-core/src/test/java/org/apache/wicket/model/ResourceModelTest$TestPage_expected.html
+++ 
b/wicket-core/src/test/java/org/apache/wicket/model/ResourceModelTest$TestPage_expected.html
@@ -1,6 +1,7 @@
 <html>
 <body>
 <span wicket:id="testlabel">A test label from a resource bundle</span>
+<span wicket:id="testlabelWithDefault">A test label from a resource 
bundle</span>
 <span wicket:id="otherlabel">A test label from a resource bundle</span>
 </body>
-</html>
\ No newline at end of file
+</html>

http://git-wip-us.apache.org/repos/asf/wicket/blob/e850af67/wicket-core/src/test/java/org/apache/wicket/model/ResourceModelTest.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/model/ResourceModelTest.java 
b/wicket-core/src/test/java/org/apache/wicket/model/ResourceModelTest.java
index f343e38..42f4ed5 100644
--- a/wicket-core/src/test/java/org/apache/wicket/model/ResourceModelTest.java
+++ b/wicket-core/src/test/java/org/apache/wicket/model/ResourceModelTest.java
@@ -40,7 +40,14 @@ public class ResourceModelTest extends WicketTestCase
                {
                        add(new Label("testlabel", new 
ResourceModel("testlabel")));
 
-                       // another label with a model explicitely assigned to 
the page
+
+                       Label testlabelWithDefault = new 
Label("testlabelWithDefault");
+                       IModel<String> defaultModel = new 
ResourceModel("testlabel").wrapOnAssignment(testlabelWithDefault);
+                       ResourceModel labelWithDefaultModel = new 
ResourceModel("missingKey", defaultModel);
+                       
testlabelWithDefault.setDefaultModel(labelWithDefaultModel);
+                       add(testlabelWithDefault);
+
+                       // another label with a model explicitly assigned to 
the page
                        add(new Label("otherlabel", new 
ResourceModel("testlabel").wrapOnAssignment(this)));
                }
        }

Reply via email to