Globally registered converters are a great way to format values
consistently throughout an application.
But regretfully these converters are not used to format choices of a
DropDownChoice. Setting a custom converter explicitly on the
DropDownChoice doesn't help either.
IMHO ChoiceRenderer could do better than simply using toString() to
produce a display value (see line 152). Why not utilize converters instead?
We could change IChoiceRenderer#getDisplayValue(Object) to return Object
instead of a String:
/**
* Get the value for displaying to an end user.
*
* @param object
* the actual object
* @return the value meant for displaying to an end user
*/
Object getDisplayValue(Object object);
Then converting a choice value to a String can be left to AbstractChoice
(line 319 and similar in subclasses):
protected void appendOptionHtml(StringBuffer buffer, Object choice,
int index)
{
final String displayValue =
convert(renderer.getDisplayValue(choice));
...
}
If we split Component#getModelObjectAsString() into two methods we can
reuse the converting functionality and get escaping for free:
/**
* Gets a model object as a string.
*
* @return Model object for this component as a string
*/
public final String getModelObjectAsString()
{
final Object modelObject = getModelObject();
return convert(modelObject);
}
/**
* Convert the given object into a string.
*
* @param value
* The value to convert
* @return The converted value
*/
protected final String convert(Object object)
{
if (object != null)
{
// Get converter
final IConverter converter = getConverter();
// Model string from property
final String modelString = (String)converter.convert(object,
String.class);
// If we should escape the markup
if (getFlag(FLAG_ESCAPE_MODEL_STRINGS))
{
// Escape it
return Strings.escapeMarkup(modelString);
}
return modelString;
}
return "";
}
Of course custom IChoiceRenderers can continue to format the value by
themself. But in most cases the IChoiceRenderer will only be responsible
to locate the displayed value, formatting is delegated to converters.
What do you think? I could send in a patch but wanted to hear your
opinion on this first.
Sven
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Wicket-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/wicket-user