Repository: wicket Updated Branches: refs/heads/master 6f504037f -> 112929d1a
WICKET-5650 Make is possible to position the choice label before/after/around the choice (cherry picked from commit f9668b5ea6d4dfc196409dff588a06dd5b9ea571) Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/4ec4a07f Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/4ec4a07f Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/4ec4a07f Branch: refs/heads/master Commit: 4ec4a07fa63ee7540af4352cb9cdb41a85ea1a2f Parents: 6f50403 Author: Martin Tzvetanov Grigorov <[email protected]> Authored: Mon Jul 21 17:26:14 2014 +0300 Committer: Martin Tzvetanov Grigorov <[email protected]> Committed: Tue Jul 22 14:46:04 2014 +0300 ---------------------------------------------------------------------- .../wicket/markup/html/form/AbstractChoice.java | 27 ++++ .../html/form/CheckBoxMultipleChoice.java | 65 ++++++++-- .../wicket/markup/html/form/RadioChoice.java | 124 +++++++++++++------ .../markup/html/form/RadioChoiceTest.java | 64 ++++++++++ 4 files changed, 234 insertions(+), 46 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/4ec4a07f/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractChoice.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractChoice.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractChoice.java index 8716eb5..e29c7fb 100644 --- a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractChoice.java +++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractChoice.java @@ -50,6 +50,32 @@ public abstract class AbstractChoice<T, E> extends FormComponent<T> { private static final long serialVersionUID = 1L; + /** + * An enumeration of possible positions of the label for a choice + */ + public static enum LabelPosition + { + /** + * will render the label before the choice + */ + BEFORE, + + /** + * will render the label after the choice + */ + AFTER, + + /** + * render the label around and the text will be before the the choice + */ + WRAP_BEFORE, + + /** + * render the label around and the text will be after the the choice + */ + WRAP_AFTER + } + /** The list of objects. */ private IModel<? extends List<? extends E>> choices; @@ -270,6 +296,7 @@ public abstract class AbstractChoice<T, E> extends FormComponent<T> * Set the choice renderer to be used. * * @param renderer + * The IChoiceRenderer used for rendering the data objects * @return this for chaining */ public final AbstractChoice<T, E> setChoiceRenderer(IChoiceRenderer<? super E> renderer) http://git-wip-us.apache.org/repos/asf/wicket/blob/4ec4a07f/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 bca4908..700b83c 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 @@ -26,6 +26,7 @@ import org.apache.wicket.markup.MarkupStream; import org.apache.wicket.model.IModel; import org.apache.wicket.settings.DebugSettings; import org.apache.wicket.util.convert.IConverter; +import org.apache.wicket.util.lang.Args; import org.apache.wicket.util.string.AppendingStringBuffer; import org.apache.wicket.util.string.Strings; import org.apache.wicket.util.value.IValueMap; @@ -70,6 +71,8 @@ public class CheckBoxMultipleChoice<T> extends ListMultipleChoice<T> private String prefix = ""; private String suffix = "<br/>\n"; + private LabelPosition labelPosition = LabelPosition.AFTER; + /** * Constructor * @@ -309,6 +312,20 @@ public class CheckBoxMultipleChoice<T> extends ListMultipleChoice<T> } /** + * Sets the preferred position of the <label> for each choice + * + * @param labelPosition + * The preferred position for the label + * @return {@code this} instance, for chaining + */ + public CheckBoxMultipleChoice<T> setLabelPosition(LabelPosition labelPosition) + { + Args.notNull(labelPosition, "labelPosition"); + this.labelPosition = labelPosition; + return this; + } + + /** * @see org.apache.wicket.markup.html.form.ListMultipleChoice#onComponentTag(org.apache.wicket.markup.ComponentTag) */ @Override @@ -394,6 +411,31 @@ public class CheckBoxMultipleChoice<T> extends ListMultipleChoice<T> String id = getChoiceRenderer().getIdValue(choice, index); final String idAttr = getCheckBoxMarkupId(id); + // Add label for checkbox + String display = label; + if (localizeDisplayValues()) + { + display = getLocalizer().getString(label, this, label); + } + + final CharSequence escaped = (getEscapeModelStrings() ? Strings.escapeMarkup(display) + : display); + + switch (labelPosition) + { + case BEFORE: + buffer.append("<label for=\""); + buffer.append(idAttr); + buffer.append("\">").append(escaped).append("</label>"); + break; + case WRAP_AFTER: + buffer.append("<label>"); + case WRAP_BEFORE: + buffer.append("<label>"); + buffer.append(escaped).append(' '); + break; + } + // Add checkbox element buffer.append("<input name=\""); buffer.append(getInputName()); @@ -450,20 +492,21 @@ public class CheckBoxMultipleChoice<T> extends ListMultipleChoice<T> buffer.append("/>"); - // Add label for checkbox - String display = label; - if (localizeDisplayValues()) + switch (labelPosition) { - display = getLocalizer().getString(label, this, label); + case WRAP_BEFORE: + buffer.append("</label>"); + break; + case WRAP_AFTER: + buffer.append(' ').append(escaped).append("</label>"); + break; + case AFTER: + buffer.append("<label for=\""); + buffer.append(idAttr); + buffer.append("\">").append(escaped).append("</label>"); + break; } - final CharSequence escaped = (getEscapeModelStrings() ? Strings.escapeMarkup(display) - : display); - - buffer.append("<label for=\""); - buffer.append(idAttr); - buffer.append("\">").append(escaped).append("</label>"); - // Append option suffix buffer.append(getSuffix(index, choice)); } http://git-wip-us.apache.org/repos/asf/wicket/blob/4ec4a07f/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 62f3b0e..aed9ca6 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 @@ -25,6 +25,7 @@ import org.apache.wicket.model.IModel; import org.apache.wicket.request.mapper.parameter.PageParameters; import org.apache.wicket.settings.DebugSettings; import org.apache.wicket.util.convert.IConverter; +import org.apache.wicket.util.lang.Args; import org.apache.wicket.util.string.AppendingStringBuffer; import org.apache.wicket.util.string.Strings; import org.apache.wicket.util.value.IValueMap; @@ -71,6 +72,8 @@ public class RadioChoice<T> extends AbstractSingleSelectChoice<T> implements IOn private String prefix = ""; private String suffix = "<br />\n"; + private LabelPosition labelPosition = LabelPosition.AFTER; + /** * Constructor * @@ -364,6 +367,20 @@ public class RadioChoice<T> extends AbstractSingleSelectChoice<T> implements IOn } /** + * Sets the preferred position of the <label> for each choice + * + * @param labelPosition + * The preferred position for the label + * @return {@code this} instance, for chaining + */ + public RadioChoice<T> setLabelPosition(LabelPosition labelPosition) + { + Args.notNull(labelPosition, "labelPosition"); + this.labelPosition = labelPosition; + return this; + } + + /** * @see org.apache.wicket.Component#onComponentTagBody(MarkupStream, ComponentTag) */ @Override @@ -439,6 +456,60 @@ public class RadioChoice<T> extends AbstractSingleSelectChoice<T> implements IOn boolean enabled = isEnabledInHierarchy() && !isDisabled(choice, index, selected); + // Add label for radio button + String display = label; + if (localizeDisplayValues()) + { + display = getLocalizer().getString(label, this, label); + } + + CharSequence escaped = display; + if (getEscapeModelStrings()) + { + escaped = Strings.escapeMarkup(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(attr.getKey()) + .append("=\"") + .append(attr.getValue()) + .append('"'); + } + } + + switch (labelPosition) + { + case BEFORE: + + buffer.append("<label for=\"") + .append(idAttr) + .append('"') + .append(extraLabelAttributes) + .append('>') + .append(escaped) + .append("</label>"); + break; + case WRAP_BEFORE: + buffer.append("<label") + .append(extraLabelAttributes) + .append('>') + .append(escaped) + .append(' '); + break; + case WRAP_AFTER: + buffer.append("<label") + .append(extraLabelAttributes) + .append('>'); + break; + } + // Add radio tag buffer.append("<input name=\"") .append(getInputName()) @@ -515,44 +586,27 @@ public class RadioChoice<T> extends AbstractSingleSelectChoice<T> implements IOn buffer.append("/>"); - // Add label for radio button - String display = label; - if (localizeDisplayValues()) - { - display = getLocalizer().getString(label, this, label); - } - - CharSequence escaped = display; - if (getEscapeModelStrings()) - { - escaped = Strings.escapeMarkup(display); - } - - buffer.append("<label for=\"") - .append(idAttr) - .append('"'); - - // Allows user to add attributes to the <label..> tag + switch (labelPosition) { - IValueMap labelAttrs = getAdditionalAttributesForLabel(index, choice); - if (labelAttrs != null) - { - for (Map.Entry<String, Object> attr : labelAttrs.entrySet()) - { - buffer.append(' ') - .append(attr.getKey()) - .append("=\"") - .append(attr.getValue()) - .append('"'); - } - } + case AFTER: + buffer.append("<label for=\"") + .append(idAttr) + .append('"') + .append(extraLabelAttributes) + .append('>') + .append(escaped) + .append("</label>"); + break; + case WRAP_BEFORE: + buffer.append("</label>"); + break; + case WRAP_AFTER: + buffer.append(' ') + .append(escaped) + .append("</label>"); + break; } - buffer - .append('>') - .append(escaped) - .append("</label>"); - // Append option suffix buffer.append(getSuffix(index, choice)); } http://git-wip-us.apache.org/repos/asf/wicket/blob/4ec4a07f/wicket-core/src/test/java/org/apache/wicket/markup/html/form/RadioChoiceTest.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/RadioChoiceTest.java b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/RadioChoiceTest.java new file mode 100644 index 0000000..496a389 --- /dev/null +++ b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/RadioChoiceTest.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.markup.html.form; + +import java.util.Arrays; + +import org.apache.wicket.WicketTestCase; +import org.junit.Test; + +public class RadioChoiceTest extends WicketTestCase +{ + @Test + public void defaultLabelPositionIsAfter() throws Exception + { + RadioChoice<Integer> radioChoice = new RadioChoice<Integer>("id", Arrays.asList(1)); + tester.startComponentInPage(radioChoice); + + tester.assertResultPage("<span wicket:id=\"id\"><input name=\"id\" type=\"radio\" value=\"0\" id=\"id1-0\"/><label for=\"id1-0\">1</label><br />\n</span>"); + } + + @Test + public void labelPositionBefore() throws Exception + { + RadioChoice<Integer> radioChoice = new RadioChoice<Integer>("id", Arrays.asList(1)); + radioChoice.setLabelPosition(AbstractChoice.LabelPosition.BEFORE); + tester.startComponentInPage(radioChoice); + + tester.assertResultPage("<span wicket:id=\"id\"><label for=\"id1-0\">1</label><input name=\"id\" type=\"radio\" value=\"0\" id=\"id1-0\"/><br />\n</span>"); + } + + @Test + public void labelPositionWrapBefore() throws Exception + { + RadioChoice<Integer> radioChoice = new RadioChoice<Integer>("id", Arrays.asList(1)); + radioChoice.setLabelPosition(AbstractChoice.LabelPosition.WRAP_BEFORE); + tester.startComponentInPage(radioChoice); + + tester.assertResultPage("<span wicket:id=\"id\"><label>1 <input name=\"id\" type=\"radio\" value=\"0\" id=\"id1-0\"/></label><br />\n</span>"); + } + + @Test + public void labelPositionWrapAfter() throws Exception + { + RadioChoice<Integer> radioChoice = new RadioChoice<Integer>("id", Arrays.asList(1)); + radioChoice.setLabelPosition(AbstractChoice.LabelPosition.WRAP_AFTER); + tester.startComponentInPage(radioChoice); + + tester.assertResultPage("<span wicket:id=\"id\"><label><input name=\"id\" type=\"radio\" value=\"0\" id=\"id1-0\"/> 1</label><br />\n</span>"); + } +}
