Search in sources :

Example 1 with IConverter

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));
    }
}
Also used : IValueMap(org.apache.wicket.util.value.IValueMap) DebugSettings(org.apache.wicket.settings.DebugSettings) IConverter(org.apache.wicket.util.convert.IConverter) Map(java.util.Map) IValueMap(org.apache.wicket.util.value.IValueMap)

Example 2 with IConverter

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));
    }
}
Also used : DebugSettings(org.apache.wicket.settings.DebugSettings) IValueMap(org.apache.wicket.util.value.IValueMap) IConverter(org.apache.wicket.util.convert.IConverter) IValueMap(org.apache.wicket.util.value.IValueMap) Map(java.util.Map)

Example 3 with IConverter

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;
}
Also used : ComponentTag(org.apache.wicket.markup.ComponentTag) Label(org.apache.wicket.markup.html.basic.Label) MarkupStream(org.apache.wicket.markup.MarkupStream) IConverter(org.apache.wicket.util.convert.IConverter)

Example 4 with IConverter

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>");
}
Also used : IConverter(org.apache.wicket.util.convert.IConverter)

Example 5 with IConverter

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();
    }
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) IConverter(org.apache.wicket.util.convert.IConverter) PrintWriter(java.io.PrintWriter)

Aggregations

IConverter (org.apache.wicket.util.convert.IConverter)6 Map (java.util.Map)2 DebugSettings (org.apache.wicket.settings.DebugSettings)2 IValueMap (org.apache.wicket.util.value.IValueMap)2 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 ComponentTag (org.apache.wicket.markup.ComponentTag)1 MarkupStream (org.apache.wicket.markup.MarkupStream)1 Label (org.apache.wicket.markup.html.basic.Label)1