Repository: wicket
Updated Branches:
  refs/heads/wicket-6.x e2f8fc98b -> 9f2ca1478


WICKET-6212 CheckBoxMultipleChoice with getAdditionalAttributesForLabel() as 
RadioChoice


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

Branch: refs/heads/wicket-6.x
Commit: 9f2ca1478b5fcc63798ca383bae56891b477c4b7
Parents: e2f8fc9
Author: Sven Meier <svenme...@apache.org>
Authored: Tue Mar 14 10:43:33 2017 +0100
Committer: Sven Meier <svenme...@apache.org>
Committed: Tue Mar 14 11:04:11 2017 +0100

----------------------------------------------------------------------
 .../html/form/CheckBoxMultipleChoice.java       | 68 ++++++++++++++++----
 .../wicket/markup/html/form/RadioChoice.java    | 22 +++----
 .../html/form/CheckBoxMultipleChoiceTest.java   | 62 +++++++++++++++---
 3 files changed, 121 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/9f2ca147/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckBoxMultipleChoice.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckBoxMultipleChoice.java
 
b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckBoxMultipleChoice.java
index 3563779..435c402 100644
--- 
a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckBoxMultipleChoice.java
+++ 
b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckBoxMultipleChoice.java
@@ -420,19 +420,43 @@ public class CheckBoxMultipleChoice<T> extends 
ListMultipleChoice<T>
                        final CharSequence escaped = (getEscapeModelStrings() ? 
Strings.escapeMarkup(display)
                                        : display);
 
+                       // Allows user to add attributes to the <label..> tag
+                       IValueMap labelAttrs = 
getAdditionalAttributesForLabel(index, choice);
+                       StringBuilder extraLabelAttributes = new 
StringBuilder();
+                       if (labelAttrs != null)
+                       {
+                               for (Map.Entry<String, Object> attr : 
labelAttrs.entrySet())
+                               {
+                                       extraLabelAttributes.append(' ')
+                                                       
.append(Strings.escapeMarkup(attr.getKey()))
+                                                       .append("=\"")
+                                                       
.append(Strings.escapeMarkup(attr.getValue().toString()))
+                                                       .append('"');
+                               }
+                       }
+
                        switch (labelPosition)
                        {
                                case BEFORE:
-                                       buffer.append("<label for=\"");
-                                       
buffer.append(Strings.escapeMarkup(idAttr));
-                                       
buffer.append("\">").append(escaped).append("</label>");
-                                       break;
-                               case WRAP_AFTER:
-                                       buffer.append("<label>");
+                                       buffer.append("<label for=\"")
+                                       .append(Strings.escapeMarkup(idAttr))
+                                       .append('"')
+                                       .append(extraLabelAttributes)
+                                       .append('>')
+                                       .append(escaped)
+                                       .append("</label>");
                                        break;
                                case WRAP_BEFORE:
-                                       buffer.append("<label>");
-                                       buffer.append(escaped).append(' ');
+                                       buffer.append("<label")
+                                       .append(extraLabelAttributes)
+                                       .append('>')
+                                       .append(escaped)
+                                       .append(' ');
+                                       break;
+                               case WRAP_AFTER:
+                                       buffer.append("<label")
+                                       .append(extraLabelAttributes)
+                                       .append('>');
                                        break;
                        }
 
@@ -491,12 +515,18 @@ public class CheckBoxMultipleChoice<T> extends 
ListMultipleChoice<T>
                                        buffer.append("</label>");
                                        break;
                                case WRAP_AFTER:
-                                       buffer.append(' 
').append(escaped).append("</label>");
+                                       buffer.append(' ')
+                                               .append(escaped)
+                                               .append("</label>");
                                        break;
                                case AFTER:
-                                       buffer.append("<label for=\"");
-                                       
buffer.append(Strings.escapeMarkup(idAttr));
-                                       
buffer.append("\">").append(escaped).append("</label>");
+                                       buffer.append("<label for=\"")
+                                               
.append(Strings.escapeMarkup(idAttr))
+                                               .append('"')
+                                               .append(extraLabelAttributes)
+                                               .append('>')
+                                               .append(escaped)
+                                               .append("</label>");
                                        break;
                        }
 
@@ -506,6 +536,20 @@ public class CheckBoxMultipleChoice<T> extends 
ListMultipleChoice<T>
        }
 
        /**
+        * You may subclass this method to provide additional attributes to the 
&lt;label ..&gt; tag.
+        *
+        * @param index
+        *            index of the choice
+        * @param choice
+        *            the choice itself
+        * @return tag attribute name/value pairs.
+        */
+       protected IValueMap getAdditionalAttributesForLabel(int index, T choice)
+       {
+               return null;
+       }
+
+       /**
         * You may subclass this method to provide additional attributes to the 
&lt;input ..&gt; tag.
         * 
         * @param index

http://git-wip-us.apache.org/repos/asf/wicket/blob/9f2ca147/wicket-core/src/main/java/org/apache/wicket/markup/html/form/RadioChoice.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/RadioChoice.java 
b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/RadioChoice.java
index 309fd59..c6c93d3 100644
--- 
a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/RadioChoice.java
+++ 
b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/RadioChoice.java
@@ -579,6 +579,14 @@ public class RadioChoice<T> extends 
AbstractSingleSelectChoice<T> implements IOn
 
                        switch (labelPosition)
                        {
+                               case WRAP_BEFORE:
+                                       buffer.append("</label>");
+                                       break;
+                               case WRAP_AFTER:
+                                       buffer.append(' ')
+                                                       .append(escaped)
+                                                       .append("</label>");
+                                       break;
                                case AFTER:
                                        buffer.append("<label for=\"")
                                                        
.append(Strings.escapeMarkup(idAttr))
@@ -588,14 +596,6 @@ public class RadioChoice<T> extends 
AbstractSingleSelectChoice<T> implements IOn
                                                        .append(escaped)
                                                        .append("</label>");
                                        break;
-                               case WRAP_BEFORE:
-                                       buffer.append("</label>");
-                                       break;
-                               case WRAP_AFTER:
-                                       buffer.append(' ')
-                                                       .append(escaped)
-                                                       .append("</label>");
-                                       break;
                        }
 
                        // Append option suffix
@@ -606,9 +606,9 @@ public class RadioChoice<T> extends 
AbstractSingleSelectChoice<T> implements IOn
        /**
         * You may subclass this method to provide additional attributes to the 
&lt;label ..&gt; tag.
         *
-        @param index
-         *            index of the choice
-         * @param choice
+        * @param index
+        *            index of the choice
+        * @param choice
         *            the choice itself
         * @return tag attribute name/value pairs.
         */

http://git-wip-us.apache.org/repos/asf/wicket/blob/9f2ca147/wicket-core/src/test/java/org/apache/wicket/markup/html/form/CheckBoxMultipleChoiceTest.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/CheckBoxMultipleChoiceTest.java
 
b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/CheckBoxMultipleChoiceTest.java
index 658af02..a7c18a1 100644
--- 
a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/CheckBoxMultipleChoiceTest.java
+++ 
b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/CheckBoxMultipleChoiceTest.java
@@ -24,6 +24,8 @@ import org.apache.wicket.markup.IMarkupFragment;
 import org.apache.wicket.markup.Markup;
 import org.apache.wicket.markup.html.WebMarkupContainer;
 import org.apache.wicket.markup.html.WebPage;
+import org.apache.wicket.util.value.IValueMap;
+import org.apache.wicket.util.value.ValueMap;
 import org.junit.Test;
 
 /**
@@ -149,39 +151,83 @@ public class CheckBoxMultipleChoiceTest extends 
WicketTestCase
        @Test
        public void defaultLabelPositionIsAfter() throws Exception
        {
-               CheckBoxMultipleChoice<Integer> radioChoice = new 
CheckBoxMultipleChoice<Integer>("testid", Arrays.asList(1));
+               CheckBoxMultipleChoice<Integer> radioChoice = new 
CheckBoxMultipleChoice<Integer>("testid", Arrays.asList(1)) {
+                       @Override
+                       protected IValueMap getAdditionalAttributes(int index, 
Integer choice)
+                       {
+                               return new ValueMap("class=input" + index);
+                       }
+                       @Override
+                       protected IValueMap getAdditionalAttributesForLabel(int 
index, Integer choice)
+                       {
+                               return new ValueMap("class=label" + index);
+                       }
+               };
                tester.startComponentInPage(radioChoice);
 
-               tester.assertResultPage("<span wicket:id=\"testid\"><input 
name=\"testid\" type=\"checkbox\" value=\"0\" id=\"testid1-testid_0\"/><label 
for=\"testid1-testid_0\">1</label><br/>\n</span>");
+               tester.assertResultPage("<span wicket:id=\"testid\"><input 
name=\"testid\" type=\"checkbox\" value=\"0\" id=\"testid1-testid_0\" 
class=\"input0\"/><label for=\"testid1-testid_0\" 
class=\"label0\">1</label><br/>\n</span>");
        }
 
        @Test
        public void labelPositionBefore() throws Exception
        {
-               CheckBoxMultipleChoice<Integer> radioChoice = new 
CheckBoxMultipleChoice<Integer>("testid", Arrays.asList(1));
+               CheckBoxMultipleChoice<Integer> radioChoice = new 
CheckBoxMultipleChoice<Integer>("testid", Arrays.asList(1)) {
+                       @Override
+                       protected IValueMap getAdditionalAttributes(int index, 
Integer choice)
+                       {
+                               return new ValueMap("class=input" + index);
+                       }
+                       @Override
+                       protected IValueMap getAdditionalAttributesForLabel(int 
index, Integer choice)
+                       {
+                               return new ValueMap("class=label" + index);
+                       }
+               };
                
radioChoice.setLabelPosition(AbstractChoice.LabelPosition.BEFORE);
                tester.startComponentInPage(radioChoice);
 
-               tester.assertResultPage("<span wicket:id=\"testid\"><label 
for=\"testid1-testid_0\">1</label><input name=\"testid\" type=\"checkbox\" 
value=\"0\" id=\"testid1-testid_0\"/><br/>\n</span>");
+               tester.assertResultPage("<span wicket:id=\"testid\"><label 
for=\"testid1-testid_0\" class=\"label0\">1</label><input name=\"testid\" 
type=\"checkbox\" value=\"0\" id=\"testid1-testid_0\" 
class=\"input0\"/><br/>\n</span>");
        }
 
        @Test
        public void labelPositionWrapBefore() throws Exception
        {
-               CheckBoxMultipleChoice<Integer> radioChoice = new 
CheckBoxMultipleChoice<Integer>("testid", Arrays.asList(1));
+               CheckBoxMultipleChoice<Integer> radioChoice = new 
CheckBoxMultipleChoice<Integer>("testid", Arrays.asList(1)) {
+                       @Override
+                       protected IValueMap getAdditionalAttributes(int index, 
Integer choice)
+                       {
+                               return new ValueMap("class=input" + index);
+                       }
+                       @Override
+                       protected IValueMap getAdditionalAttributesForLabel(int 
index, Integer choice)
+                       {
+                               return new ValueMap("class=label" + index);
+                       }
+               };
                
radioChoice.setLabelPosition(AbstractChoice.LabelPosition.WRAP_BEFORE);
                tester.startComponentInPage(radioChoice);
 
-               tester.assertResultPage("<span wicket:id=\"testid\"><label>1 
<input name=\"testid\" type=\"checkbox\" value=\"0\" 
id=\"testid1-testid_0\"/></label><br/>\n</span>");
+               tester.assertResultPage("<span wicket:id=\"testid\"><label 
class=\"label0\">1 <input name=\"testid\" type=\"checkbox\" value=\"0\" 
id=\"testid1-testid_0\" class=\"input0\"/></label><br/>\n</span>");
        }
 
        @Test
        public void labelPositionWrapAfter() throws Exception
        {
-               CheckBoxMultipleChoice<Integer> radioChoice = new 
CheckBoxMultipleChoice<Integer>("testid", Arrays.asList(1));
+               CheckBoxMultipleChoice<Integer> radioChoice = new 
CheckBoxMultipleChoice<Integer>("testid", Arrays.asList(1)) {
+                       @Override
+                       protected IValueMap getAdditionalAttributes(int index, 
Integer choice)
+                       {
+                               return new ValueMap("class=input" + index);
+                       }
+                       @Override
+                       protected IValueMap getAdditionalAttributesForLabel(int 
index, Integer choice)
+                       {
+                               return new ValueMap("class=label" + index);
+                       }
+               };
                
radioChoice.setLabelPosition(AbstractChoice.LabelPosition.WRAP_AFTER);
                tester.startComponentInPage(radioChoice);
 
-               tester.assertResultPage("<span 
wicket:id=\"testid\"><label><input name=\"testid\" type=\"checkbox\" 
value=\"0\" id=\"testid1-testid_0\"/> 1</label><br/>\n</span>");
+               tester.assertResultPage("<span wicket:id=\"testid\"><label 
class=\"label0\"><input name=\"testid\" type=\"checkbox\" value=\"0\" 
id=\"testid1-testid_0\" class=\"input0\"/> 1</label><br/>\n</span>");
        }
 }

Reply via email to