WICKET-4876 aligned CheckBoxMultipleChoice with RadioChoice
with getAdditionalAttributes() and renderOptionHtml()


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

Branch: refs/heads/master
Commit: bc8fe7b81677b13c7963e6bd7d85091e3192520f
Parents: 6470c3f
Author: svenmeier <[email protected]>
Authored: Thu Nov 22 17:27:55 2012 +0100
Committer: svenmeier <[email protected]>
Committed: Thu Nov 22 17:27:55 2012 +0100

----------------------------------------------------------------------
 .../markup/html/form/CheckBoxMultipleChoice.java   |   45 +++-
 .../wicket/markup/html/form/RadioChoice.java       |  261 +++++++++------
 2 files changed, 197 insertions(+), 109 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/bc8fe7b8/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 6e3b283..0cd678b 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
@@ -18,6 +18,7 @@ package org.apache.wicket.markup.html.form;
 
 import java.util.Collection;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.wicket.Page;
 import org.apache.wicket.markup.ComponentTag;
@@ -26,6 +27,7 @@ import org.apache.wicket.model.IModel;
 import org.apache.wicket.util.convert.IConverter;
 import org.apache.wicket.util.string.AppendingStringBuffer;
 import org.apache.wicket.util.string.Strings;
+import org.apache.wicket.util.value.IValueMap;
 
 
 /**
@@ -408,7 +410,37 @@ public class CheckBoxMultipleChoice<T> extends 
ListMultipleChoice<T>
                        buffer.append(id);
                        buffer.append("\" id=\"");
                        buffer.append(idAttr);
-                       buffer.append("\"/>");
+                       buffer.append("\"");
+
+                       // Allows user to add attributes to the <input..> tag
+                       {
+                               IValueMap attrs = 
getAdditionalAttributes(index, choice);
+                               if (attrs != null)
+                               {
+                                       for (Map.Entry<String, Object> attr : 
attrs.entrySet())
+                                       {
+                                               buffer.append(" ")
+                                                       .append(attr.getKey())
+                                                       .append("=\"")
+                                                       .append(attr.getValue())
+                                                       .append("\"");
+                                       }
+                               }
+                       }
+
+                       if 
(getApplication().getDebugSettings().isOutputComponentPath())
+                       {
+                               String path = getPageRelativePath();
+                               path = path.replace("_", "__");
+                               path = path.replace(":", "_");
+                               buffer.append(" wicketpath=\"")
+                                       .append(path)
+                                       .append("_input_")
+                                       .append(index)
+                                       .append("\"");
+                       }
+
+                       buffer.append("/>");
 
                        // Add label for checkbox
                        String display = label;
@@ -429,6 +461,17 @@ public class CheckBoxMultipleChoice<T> extends 
ListMultipleChoice<T>
                }
        }
 
+       /**
+        * You may subclass this method to provide additional attributes to the 
&lt;input ..&gt; tag.
+        * 
+        * @param index
+        * @param choice
+        * @return tag attribute name/value pairs.
+        */
+       protected IValueMap getAdditionalAttributes(final int index, final T 
choice)
+       {
+               return null;
+       }
 
        /**
         * Creates markup id for the input tag used to generate the checkbox 
for the element with the

http://git-wip-us.apache.org/repos/asf/wicket/blob/bc8fe7b8/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 69aba81..c5f60af 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
@@ -302,6 +302,32 @@ public class RadioChoice<T> extends 
AbstractSingleSelectChoice<T> implements IOn
        }
 
        /**
+        * @param index
+        *            index of the choice
+        * @param choice
+        *            the choice itself
+        * @return Prefix to use before choice. The default implementation just 
returns
+        *         {@link #getPrefix()}. Override to have a prefix dependent on 
the choice item.
+        */
+       protected String getPrefix(int index, T choice)
+       {
+               return getPrefix();
+       }
+
+       /**
+        * @param index
+        *            index of the choice
+        * @param choice
+        *            the choice itself
+        * @return Separator to use between radio options. The default 
implementation just returns
+        *         {@link #getSuffix()}. Override to have a prefix dependent on 
the choice item.
+        */
+       protected String getSuffix(int index, T choice)
+       {
+               return getSuffix();
+       }
+
+       /**
         * @param prefix
         *            Prefix to use before choice
         * @return this
@@ -338,7 +364,6 @@ public class RadioChoice<T> extends 
AbstractSingleSelectChoice<T> implements IOn
        /**
         * @see org.apache.wicket.Component#onComponentTagBody(MarkupStream, 
ComponentTag)
         */
-       @SuppressWarnings("unchecked")
        @Override
        public final void onComponentTagBody(final MarkupStream markupStream, 
final ComponentTag openTag)
        {
@@ -357,132 +382,152 @@ public class RadioChoice<T> extends 
AbstractSingleSelectChoice<T> implements IOn
                        // Get next choice
                        final T choice = choices.get(index);
 
-                       Object displayValue = 
getChoiceRenderer().getDisplayValue(choice);
-                       Class<?> objectClass = (displayValue == null ? null : 
displayValue.getClass());
+                       appendOptionHtml(buffer, choice, index, selected);
+               }
 
-                       // Get label for choice
-                       String label = "";
+               // Replace body
+               replaceComponentTagBody(markupStream, openTag, buffer);
+       }
 
-                       if (objectClass != null && objectClass != String.class)
-                       {
-                               @SuppressWarnings("rawtypes")
-                               final IConverter converter = 
getConverter(objectClass);
-                               label = converter.convertToString(displayValue, 
getLocale());
-                       }
-                       else if (displayValue != null)
-                       {
-                               label = displayValue.toString();
-                       }
+       /**
+        * Generates and appends html for a single choice into the provided 
buffer
+        * 
+        * @param buffer
+        *            Appending string buffer that will have the generated html 
appended
+        * @param choice
+        *            Choice object
+        * @param index
+        *            The index of this option
+        * @param selected
+        *            The currently selected string value
+        */
+       @SuppressWarnings("unchecked")
+       @Override
+       protected void appendOptionHtml(final AppendingStringBuffer buffer, 
final T choice, int index,
+               final String selected)
+       {
+               Object displayValue = 
getChoiceRenderer().getDisplayValue(choice);
+               Class<?> objectClass = (displayValue == null ? null : 
displayValue.getClass());
+
+               // Get label for choice
+               String label = "";
+
+               if (objectClass != null && objectClass != String.class)
+               {
+                       @SuppressWarnings("rawtypes")
+                       final IConverter converter = getConverter(objectClass);
+                       label = converter.convertToString(displayValue, 
getLocale());
+               }
+               else if (displayValue != null)
+               {
+                       label = displayValue.toString();
+               }
 
-                       // If there is a display value for the choice, then we 
know that the
-                       // choice is automatic in some way. If label is /null/ 
then we know
-                       // that the choice is a manually created radio tag at 
some random
-                       // location in the page markup!
-                       if (label != null)
+               // If there is a display value for the choice, then we know 
that the
+               // choice is automatic in some way. If label is /null/ then we 
know
+               // that the choice is a manually created radio tag at some 
random
+               // location in the page markup!
+               if (label != null)
+               {
+                       // Append option suffix
+                       buffer.append(getPrefix(index, choice));
+
+                       String id = getChoiceRenderer().getIdValue(choice, 
index);
+                       final String idAttr = getMarkupId() + "-" + id;
+
+                       boolean enabled = isEnabledInHierarchy() && 
!isDisabled(choice, index, selected);
+
+                       // Add radio tag
+                       buffer.append("<input name=\"")
+                               .append(getInputName())
+                               .append("\"")
+                               .append(" type=\"radio\"")
+                               .append((isSelected(choice, index, selected) ? 
" checked=\"checked\"" : ""))
+                               .append((enabled ? "" : " 
disabled=\"disabled\""))
+                               .append(" value=\"")
+                               .append(id)
+                               .append("\" id=\"")
+                               .append(idAttr)
+                               .append("\"");
+
+                       // Should a roundtrip be made (have onSelectionChanged 
called)
+                       // when the option is clicked?
+                       if (wantOnSelectionChangedNotifications())
                        {
-                               // Append option suffix
-                               buffer.append(getPrefix());
-
-                               String id = 
getChoiceRenderer().getIdValue(choice, index);
-                               final String idAttr = getMarkupId() + "-" + id;
-
-                               boolean enabled = isEnabledInHierarchy() && 
!isDisabled(choice, index, selected);
-
-                               // Add radio tag
-                               buffer.append("<input name=\"")
-                                       .append(getInputName())
-                                       .append("\"")
-                                       .append(" type=\"radio\"")
-                                       .append((isSelected(choice, index, 
selected) ? " checked=\"checked\"" : ""))
-                                       .append((enabled ? "" : " 
disabled=\"disabled\""))
-                                       .append(" value=\"")
-                                       .append(id)
-                                       .append("\" id=\"")
-                                       .append(idAttr)
-                                       .append("\"");
+                               CharSequence url = 
urlFor(IOnChangeListener.INTERFACE, new PageParameters());
 
-                               // Should a roundtrip be made (have 
onSelectionChanged called)
-                               // when the option is clicked?
-                               if (wantOnSelectionChangedNotifications())
+                               Form<?> form = findParent(Form.class);
+                               if (form != null)
                                {
-                                       CharSequence url = 
urlFor(IOnChangeListener.INTERFACE, new PageParameters());
-
-                                       Form<?> form = findParent(Form.class);
-                                       if (form != null)
-                                       {
-                                               buffer.append(" onclick=\"")
-                                                       
.append(form.getJsForInterfaceUrl(url))
-                                                       .append(";\"");
-                                       }
-                                       else
-                                       {
-                                               // NOTE: do not encode the url 
as that would give
-                                               // invalid JavaScript
-                                               buffer.append(" 
onclick=\"window.location.href='")
-                                                       .append(url)
-                                                       
.append((url.toString().indexOf('?') > -1 ? "&" : "?") + getInputName())
-                                                       .append("=")
-                                                       .append(id)
-                                                       .append("';\"");
-                                       }
+                                       buffer.append(" onclick=\"")
+                                               
.append(form.getJsForInterfaceUrl(url))
+                                               .append(";\"");
+                               }
+                               else
+                               {
+                                       // NOTE: do not encode the url as that 
would give
+                                       // invalid JavaScript
+                                       buffer.append(" 
onclick=\"window.location.href='")
+                                               .append(url)
+                                               
.append((url.toString().indexOf('?') > -1 ? "&" : "?") + getInputName())
+                                               .append("=")
+                                               .append(id)
+                                               .append("';\"");
                                }
+                       }
 
-                               // Allows user to add attributes to the 
<input..> tag
+                       // Allows user to add attributes to the <input..> tag
+                       {
+                               IValueMap attrs = 
getAdditionalAttributes(index, choice);
+                               if (attrs != null)
                                {
-                                       IValueMap attrs = 
getAdditionalAttributes(index, choice);
-                                       if (attrs != null)
+                                       for (Map.Entry<String, Object> attr : 
attrs.entrySet())
                                        {
-                                               for (Map.Entry<String, Object> 
attr : attrs.entrySet())
-                                               {
-                                                       buffer.append(" ")
-                                                               
.append(attr.getKey())
-                                                               .append("=\"")
-                                                               
.append(attr.getValue())
-                                                               .append("\"");
-                                               }
+                                               buffer.append(" ")
+                                                       .append(attr.getKey())
+                                                       .append("=\"")
+                                                       .append(attr.getValue())
+                                                       .append("\"");
                                        }
                                }
+                       }
 
-                               if 
(getApplication().getDebugSettings().isOutputComponentPath())
-                               {
-                                       String path = getPageRelativePath();
-                                       path = path.replace("_", "__");
-                                       path = path.replace(":", "_");
-                                       buffer.append(" wicketpath=\"")
-                                               .append(path)
-                                               .append("_input_")
-                                               .append(index)
-                                               .append("\"");
-                               }
+                       if 
(getApplication().getDebugSettings().isOutputComponentPath())
+                       {
+                               String path = getPageRelativePath();
+                               path = path.replace("_", "__");
+                               path = path.replace(":", "_");
+                               buffer.append(" wicketpath=\"")
+                                       .append(path)
+                                       .append("_input_")
+                                       .append(index)
+                                       .append("\"");
+                       }
 
-                               buffer.append("/>");
+                       buffer.append("/>");
 
-                               // Add label for radio button
-                               String display = label;
-                               if (localizeDisplayValues())
-                               {
-                                       display = 
getLocalizer().getString(label, this, label);
-                               }
+                       // 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);
-                               }
+                       CharSequence escaped = display;
+                       if (getEscapeModelStrings())
+                       {
+                               escaped = Strings.escapeMarkup(display);
+                       }
 
-                               buffer.append("<label for=\"")
-                                       .append(idAttr)
-                                       .append("\">")
-                                       .append(escaped)
-                                       .append("</label>");
+                       buffer.append("<label for=\"")
+                               .append(idAttr)
+                               .append("\">")
+                               .append(escaped)
+                               .append("</label>");
 
-                               // Append option suffix
-                               buffer.append(getSuffix());
-                       }
+                       // Append option suffix
+                       buffer.append(getSuffix(index, choice));
                }
-
-               // Replace body
-               replaceComponentTagBody(markupStream, openTag, buffer);
        }
 
        /**

Reply via email to