Repository: wicket Updated Branches: refs/heads/5640-less-mangling-of-html [created] 1218c1113
WICKET-5640 Reduce the mangling of HTML markup in the Java code as much as possible Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/1218c111 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/1218c111 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/1218c111 Branch: refs/heads/5640-less-mangling-of-html Commit: 1218c11130dee32d42c5aaa94bf1ed5cf753932e Parents: 6f50403 Author: Martin Tzvetanov Grigorov <[email protected]> Authored: Mon Jul 21 17:26:14 2014 +0300 Committer: Martin Tzvetanov Grigorov <[email protected]> Committed: Mon Jul 21 17:26:14 2014 +0300 ---------------------------------------------------------------------- .../wicket/markup/html/form/AbstractChoice.java | 27 +++++++++ .../html/form/CheckBoxMultipleChoice.java | 60 ++++++++++++++++---- .../CheckBoxMultipleChoiceWicket6Listener.java | 29 ++++++++++ 3 files changed, 104 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/1218c111/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/1218c111/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..2e5a4d0 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; @@ -68,7 +69,9 @@ public class CheckBoxMultipleChoice<T> extends ListMultipleChoice<T> private static final long serialVersionUID = 1L; private String prefix = ""; - private String suffix = "<br/>\n"; + private String suffix = ""; + + private LabelPosition labelPosition = LabelPosition.AFTER; /** * Constructor @@ -308,6 +311,13 @@ public class CheckBoxMultipleChoice<T> extends ListMultipleChoice<T> return this; } + 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) */ @@ -394,6 +404,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 +485,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/1218c111/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckBoxMultipleChoiceWicket6Listener.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckBoxMultipleChoiceWicket6Listener.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckBoxMultipleChoiceWicket6Listener.java new file mode 100644 index 0000000..4ab4019 --- /dev/null +++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckBoxMultipleChoiceWicket6Listener.java @@ -0,0 +1,29 @@ +package org.apache.wicket.markup.html.form; + +import org.apache.wicket.Component; +import org.apache.wicket.application.IComponentInstantiationListener; + +/** + * Restores the rendering behavior of CheckBoxMultipleChoice until Wicket 6.x. + * That means: + * <ul> + * <li>Uses <xmp><br/></xmp> as a suffix</li> + * <li>renders the label after the checkbox</li> + * </ul> + * + * @deprecated + */ +@Deprecated +public class CheckBoxMultipleChoiceWicket6Listener implements IComponentInstantiationListener +{ + @Override + public void onInstantiation(Component component) + { + if (component instanceof CheckBoxMultipleChoice<?>) + { + CheckBoxMultipleChoice<?> checkBoxMultipleChoice = (CheckBoxMultipleChoice<?>) component; + checkBoxMultipleChoice.setSuffix("<br/>\n"); + checkBoxMultipleChoice.setLabelPosition(AbstractChoice.LabelPosition.AFTER); + } + } +}
