{
// Iterate through choices
final List<? extends T> choices = getChoices();
// Buffer to hold generated body
final AppendingStringBuffer buffer = new AppendingStringBuffer((choices.size() + 1) * 70);
// The selected value
final String selected = getValue();
// Loop through choices
for (int index = 0; index < choices.size(); index++)
{
// Get next choice
final T choice = choices.get(index);
Object displayValue = getChoiceRenderer().getDisplayValue(choice);
Class<?> objectClass = (displayValue == null ? null : displayValue.getClass());
// Get label for choice
String label = "";
if (objectClass != null && objectClass != String.class)
{
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)
{
// 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("\"");
// Should a roundtrip be made (have onSelectionChanged called)
// when the option is clicked?
if (wantOnSelectionChangedNotifications())
{
CharSequence url = urlFor(IOnChangeListener.INTERFACE);
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("';\"");
}
}
// 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 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("\">")
.append(escaped)
.append("</label>");
// Append option suffix
buffer.append(getSuffix());
}
}
// Replace body
replaceComponentTagBody(markupStream, openTag, buffer);