You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by sv...@apache.org on 2017/03/14 10:35:06 UTC

wicket git commit: WICKET-6212 CheckBoxMultipleChoice with getAdditionalAttributesForLabel() as RadioChoice

Repository: wicket
Updated Branches:
  refs/heads/master e04e92a75 -> fb5a2fa2f


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/fb5a2fa2
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/fb5a2fa2
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/fb5a2fa2

Branch: refs/heads/master
Commit: fb5a2fa2f927dd6f6bc774731b591becbbb65121
Parents: e04e92a
Author: Sven Meier <sv...@apache.org>
Authored: Tue Mar 14 10:43:33 2017 +0100
Committer: Sven Meier <sv...@apache.org>
Committed: Tue Mar 14 11:21:06 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/fb5a2fa2/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 7823b31..f1c3520 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
@@ -421,19 +421,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;
 			}
 
@@ -494,12 +518,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;
 			}
 
@@ -509,6 +539,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/fb5a2fa2/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 bedd3ab..0a2f11b 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
@@ -584,6 +584,14 @@ public class RadioChoice<T> extends AbstractSingleSelectChoice<T> implements IRe
 
 			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))
@@ -593,14 +601,6 @@ public class RadioChoice<T> extends AbstractSingleSelectChoice<T> implements IRe
 							.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
@@ -611,9 +611,9 @@ public class RadioChoice<T> extends AbstractSingleSelectChoice<T> implements IRe
 	/**
 	 * 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/fb5a2fa2/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 81a262f..cbf9a93 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.Markup;
 import org.apache.wicket.markup.html.WebMarkupContainer;
 import org.apache.wicket.markup.html.WebPage;
 import org.apache.wicket.util.tester.WicketTestCase;
+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></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></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\"/></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\"/></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></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></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></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></span>");
 	}
 }