wicket.util.convert
Class SimpleConverterAdapter

java.lang.Object
  extended by wicket.util.convert.LocalizableAdapter
      extended by wicket.util.convert.SimpleConverterAdapter
All Implemented Interfaces:
java.io.Serializable, IConverter, ILocalizable
Direct Known Subclasses:
MaskConverter

public abstract class SimpleConverterAdapter
extends LocalizableAdapter
implements IConverter

Adapter class to simplify implementing custom IConverters. If the requested type to convert to is a string type (or more precice, assignable from CharSequence), this converter will delegate type conversion to toString(Object). Otherwise, it will delegate type conversion to toObject(String) using the object's Object.toString() value to convert it to a string first (or passing in null in case the value is null).

Note, this class is specifically meant for providing custom converters per component by overriding Component.getConverter(). It is less usefull for application scoped converters; registering ITypeConverters with an instance of Converter is a better choice for that.

WARNING. Due to a current limitation as a result of how IConverter works, classes that extend this adapter will not be much use with string values. If you want to use a custom converter for string values, consider wrapping the values in another class so that conversion will be triggered. See the form input example of wicket-examples for how this can be done.

An example of the use of this class is the following:

 add(new TextField("urlProperty", URL.class)
 {
        public IConverter getConverter()
        {
                return new SimpleConverterAdapter()
                {
                        public String toString(Object value)
                        {
                                return value != null ? value.toString() : null;
                        }
 
                        public Object toObject(String value)
                        {
                                try
                                {
                                        return new URL(value.toString());
                                }
                                catch (MalformedURLException e)
                                {
                                        throw new ConversionException("'" + value + "' is not a valid URL");
                                }
                        }
                };
        }
 });
 

Author:
Eelco Hillenius
See Also:
Serialized Form

Constructor Summary
SimpleConverterAdapter()
           
 
Method Summary
 java.lang.Object convert(java.lang.Object value, java.lang.Class c)
          If class c is a string type (or more precise, assignable from CharSequence), this method will delegate type conversion to toString(Object).
abstract  java.lang.Object toObject(java.lang.String value)
          Convert the given string to an object of choice.
abstract  java.lang.String toString(java.lang.Object value)
          Convert the given value to a string.
 
Methods inherited from class wicket.util.convert.LocalizableAdapter
getLocale, setLocale
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface wicket.util.convert.ILocalizable
getLocale, setLocale
 

Constructor Detail

SimpleConverterAdapter

public SimpleConverterAdapter()
Method Detail

convert

public final java.lang.Object convert(java.lang.Object value,
                                      java.lang.Class c)
If class c is a string type (or more precise, assignable from CharSequence), this method will delegate type conversion to toString(Object). Otherwise, it will delegate type conversion to toObject(String) using the object's Object.toString() value to convert it to a string first (or passing in null in case the value is null).

Specified by:
convert in interface IConverter
Parameters:
value - The value to convert
c - The class of object to convert to
Returns:
The converted value
See Also:
IConverter.convert(java.lang.Object, java.lang.Class)

toString

public abstract java.lang.String toString(java.lang.Object value)
Convert the given value to a string.

Parameters:
value - The value to convert, may be null
Returns:
The value as a string

toObject

public abstract java.lang.Object toObject(java.lang.String value)
Convert the given string to an object of choice.

Parameters:
value - The string to convert, may be null
Returns:
The string value converted to an object of choice


Copyright © 2004-2007 Wicket developers. All Rights Reserved.