Example usage for org.apache.wicket Component getConverter

List of usage examples for org.apache.wicket Component getConverter

Introduction

In this page you can find the example usage for org.apache.wicket Component getConverter.

Prototype

@SuppressWarnings("unchecked")
@Override
public <C> IConverter<C> getConverter(Class<C> type) 

Source Link

Document

Get the converter that should be used by this component, delegates to #createConverter(Class) and then to the application's IConverterLocator .

Usage

From source file:name.martingeisse.wicket.util.WicketConverterUtil.java

License:Open Source License

/**
 * Converts a value to a string. The type converter and locale are taken
 * from the specified context component. Null values are converted to the
 * empty string./* ww  w  . j av a 2  s  .co  m*/
 * 
 * @param value the value to convert
 * @param context the context that provides the type converter and the locale
 * @return the converted value
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static String convertValueToString(Object value, Component context) {
    if (value == null) {
        return "";
    } else {
        IConverter converter = context.getConverter(value.getClass());
        return converter.convertToString(value, context.getLocale());
    }
}

From source file:org.opensingular.form.wicket.helpers.STypeTester.java

License:Apache License

@SuppressWarnings("unchecked")
private <ST extends SType<?>, V extends Object> String getWicketConvertedValue(Component value, V o) {
    if (o == null) {
        return null;
    }//from  www  .j av a2s . c  om
    return value.getConverter((Class<V>) o.getClass()).convertToString(o, Locale.getDefault());
}

From source file:org.wicketstuff.calendar.markup.html.form.DatePicker.java

License:Apache License

/**
 * Check that this behavior can get a date format out of the component it is
 * coupled to. if you override this method to allow for other types (such as
 * your own), you should override {@link #getDatePattern()} as well. This
 * method should return normally if the component is accepted or throw a RTE
 * when it is not.//from  w w w  .j  a  v a  2s.c  o  m
 * 
 * @param component
 *            the component this behavior is being coupled to
 * @throws WicketRuntimeException
 *             if the component is not support.
 */
protected void checkComponentProvidesDateFormat(Component component) {

    if (component instanceof ITextFormatProvider) {
        // were ok
        return;
    }

    IConverter converter = component.getConverter(DateTime.class);
    if (converter == null) {
        converter = component.getConverter(Date.class);
    }
    if (converter instanceof DateConverter) {

        return; // This is ok
    }
    throw new WicketRuntimeException("this behavior can only be added to components that either implement "
            + ITextFormatProvider.class.getName() + " or that use " + DateConverter.class.getName()
            + " configured with an instance of " + SimpleDateFormat.class.getName()
            + " (like Wicket's default configuration has)");
}