use of org.apache.wicket.util.convert.IConverter in project wicket by apache.
the class RadioChoice method appendOptionHtml.
/**
* 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();
}
// 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 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(Strings.escapeMarkup(attr.getKey())).append("=\"").append(Strings.escapeMarkup(attr.getValue().toString())).append('"');
}
}
switch(labelPosition) {
case BEFORE:
buffer.append("<label for=\"").append(Strings.escapeMarkup(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()).append('"').append(" type=\"radio\"").append((isSelected(choice, index, selected) ? " checked=\"checked\"" : "")).append((enabled ? "" : " disabled=\"disabled\"")).append(" value=\"").append(Strings.escapeMarkup(id)).append("\" id=\"").append(Strings.escapeMarkup(idAttr)).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(Strings.escapeMarkup(attr.getKey())).append("=\"").append(Strings.escapeMarkup(attr.getValue().toString())).append('"');
}
}
}
DebugSettings debugSettings = getApplication().getDebugSettings();
String componentPathAttributeName = debugSettings.getComponentPathAttributeName();
if (Strings.isEmpty(componentPathAttributeName) == false) {
CharSequence path = getPageRelativePath();
path = Strings.replaceAll(path, "_", "__");
path = Strings.replaceAll(path, ":", "_");
buffer.append(' ').append(componentPathAttributeName).append("=\"").append(path).append("_input_").append(index).append('"');
}
buffer.append("/>");
switch(labelPosition) {
case WRAP_BEFORE:
buffer.append("</label>");
break;
case WRAP_AFTER:
buffer.append(' ').append(escaped).append("</label>");
break;
case AFTER:
buffer.append("<label for=\"").append(Strings.escapeMarkup(idAttr)).append('"').append(extraLabelAttributes).append('>').append(escaped).append("</label>");
break;
}
// Append option suffix
buffer.append(getSuffix(index, choice));
}
}
use of org.apache.wicket.util.convert.IConverter in project wicket by apache.
the class CheckBoxMultipleChoice method appendOptionHtml.
/**
* 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") IConverter converter = getConverter(objectClass);
label = converter.convertToString(displayValue, getLocale());
} else if (displayValue != null) {
label = displayValue.toString();
}
// 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 = 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);
// 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(Strings.escapeMarkup(attr.getKey())).append("=\"").append(Strings.escapeMarkup(attr.getValue().toString())).append('"');
}
}
switch(labelPosition) {
case BEFORE:
buffer.append("<label for=\"").append(Strings.escapeMarkup(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 checkbox element
buffer.append("<input name=\"");
buffer.append(getInputName());
buffer.append('"');
buffer.append(" type=\"checkbox\"");
if (isSelected(choice, index, selected)) {
buffer.append(" checked=\"checked\"");
}
if (isDisabled(choice, index, selected) || !isEnabledInHierarchy()) {
buffer.append(" disabled=\"disabled\"");
}
buffer.append(" value=\"");
buffer.append(Strings.escapeMarkup(id));
buffer.append("\" id=\"");
buffer.append(Strings.escapeMarkup(idAttr));
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(Strings.escapeMarkup(attr.getKey())).append("=\"").append(Strings.escapeMarkup(attr.getValue().toString())).append('"');
}
}
}
DebugSettings debugSettings = getApplication().getDebugSettings();
String componentPathAttributeName = debugSettings.getComponentPathAttributeName();
if (Strings.isEmpty(componentPathAttributeName) == false) {
CharSequence path = getPageRelativePath();
path = Strings.replaceAll(path, "_", "__");
path = Strings.replaceAll(path, ":", "_");
buffer.append(' ').append(componentPathAttributeName).append("=\"").append(path).append("_input_").append(index).append('"');
}
buffer.append("/>");
switch(labelPosition) {
case WRAP_BEFORE:
buffer.append("</label>");
break;
case WRAP_AFTER:
buffer.append(' ').append(escaped).append("</label>");
break;
case AFTER:
buffer.append("<label for=\"").append(Strings.escapeMarkup(idAttr)).append('"').append(extraLabelAttributes).append('>').append(escaped).append("</label>");
break;
}
// Append option suffix
buffer.append(getSuffix(index, choice));
}
}
use of org.apache.wicket.util.convert.IConverter in project wicket by apache.
the class AjaxEditableChoiceLabel method newLabel.
/**
* {@inheritDoc}
*/
@Override
protected WebComponent newLabel(final MarkupContainer parent, final String componentId, final IModel<T> model) {
Label label = new Label(componentId, model) {
private static final long serialVersionUID = 1L;
/**
* {@inheritDoc}
*/
@Override
public <C> IConverter<C> getConverter(final Class<C> type) {
IConverter<C> c = AjaxEditableChoiceLabel.this.getConverter(type);
return c != null ? c : super.getConverter(type);
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) {
String displayValue = getDefaultModelObjectAsString();
if (renderer != null) {
Object displayObject = renderer.getDisplayValue(getModelObject());
Class<?> objectClass = (displayObject == null ? null : displayObject.getClass());
if ((objectClass != null) && (objectClass != String.class)) {
@SuppressWarnings("rawtypes") final IConverter converter = getConverter(objectClass);
displayValue = converter.convertToString(displayObject, getLocale());
} else if (displayObject != null) {
displayValue = displayObject.toString();
}
}
if (Strings.isEmpty(displayValue)) {
replaceComponentTagBody(markupStream, openTag, defaultNullLabel());
} else {
replaceComponentTagBody(markupStream, openTag, displayValue);
}
}
};
label.setOutputMarkupId(true);
label.add(new LabelAjaxBehavior(getLabelAjaxEvent()));
return label;
}
use of org.apache.wicket.util.convert.IConverter in project wicket by apache.
the class AbstractChoice method appendOptionHtml.
/**
* 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")
protected void appendOptionHtml(AppendingStringBuffer buffer, E choice, int index, String selected) {
Object objectValue = renderer.getDisplayValue(choice);
Class<?> objectClass = (objectValue == null ? null : objectValue.getClass());
String displayValue = "";
if (objectClass != null && objectClass != String.class) {
@SuppressWarnings("rawtypes") IConverter converter = getConverter(objectClass);
displayValue = converter.convertToString(objectValue, getLocale());
} else if (objectValue != null) {
displayValue = objectValue.toString();
}
buffer.append("\n<option ");
setOptionAttributes(buffer, choice, index, selected);
buffer.append('>');
String display = displayValue;
if (localizeDisplayValues()) {
display = getLocalizer().getString(displayValue, this, displayValue);
}
CharSequence escaped = display;
if (getEscapeModelStrings()) {
escaped = escapeOptionHtml(display);
}
buffer.append(escaped);
buffer.append("</option>");
}
use of org.apache.wicket.util.convert.IConverter in project wicket by apache.
the class CSVDataExporter method exportData.
@Override
public <T> void exportData(IDataProvider<T> dataProvider, List<IExportableColumn<T, ?>> columns, OutputStream outputStream) throws IOException {
PrintWriter out = new PrintWriter(new OutputStreamWriter(outputStream, Charset.forName(characterSet)));
try {
if (isExportHeadersEnabled()) {
boolean first = true;
for (IExportableColumn<T, ?> col : columns) {
if (first) {
first = false;
} else {
out.print(delimiter);
}
out.print(quoteValue(col.getDisplayModel().getObject()));
}
out.print("\r\n");
}
long numberOfRows = dataProvider.size();
Iterator<? extends T> rowIterator = dataProvider.iterator(0, numberOfRows);
while (rowIterator.hasNext()) {
T row = rowIterator.next();
boolean first = true;
for (IExportableColumn<T, ?> col : columns) {
if (first) {
first = false;
} else {
out.print(delimiter);
}
Object o = col.getDataModel(dataProvider.model(row)).getObject();
if (o != null) {
Class<?> c = o.getClass();
String s;
IConverter converter = Application.get().getConverterLocator().getConverter(c);
if (converter == null) {
s = o.toString();
} else {
s = converter.convertToString(o, Session.get().getLocale());
}
out.print(quoteValue(s));
}
}
out.print("\r\n");
}
} finally {
out.close();
}
}
Aggregations