org.jdesktop.application
Class ResourceConverter

java.lang.Object
  extended by org.jdesktop.application.ResourceConverter

public abstract class ResourceConverter
extends java.lang.Object

A base class for converting arbitrary types to and from Strings, as well as a registry of ResourceConverter implementations.

The supportsType method defines what types a ResourceConverter supports. By default it returns true for classes that are equal to the constructor's type argument. The parseType methods converts a string the ResourceConverter's supported type, and the toString does the inverse, it converts a supported type to a String. Concrete ResourceConverter subclasses must override parseType() and, in most cases, the toString method as well.

This class maintains a registry of ResourceConverters. The forType method returns the first ResourceConverter that supports a particular type, new ResourceConverters can be added with register(). A small set of generic ResourceConverters are registered by default. They support the following types:

The Boolean ResourceConverter returns true for "true", "on", "yes", false otherwise. The other primitive type ResourceConverters rely on the corresponding static parseType method, e.g. Integer.parseInt(). The MessageFormat ResourceConverter just creates MessageFormat object with the string as its constructor argument. The URL/URI converters just apply the corresponding constructor to the resource string.

Author:
Hans Muller (Hans.Muller@Sun.COM)
See Also:
ResourceMap

Nested Class Summary
static class ResourceConverter.ResourceConverterException
           
 
Field Summary
protected  java.lang.Class type
           
 
Constructor Summary
protected ResourceConverter(java.lang.Class type)
           
 
Method Summary
static ResourceConverter forType(java.lang.Class type)
          Returns ResourceConverter for the specified type
abstract  java.lang.Object parseString(java.lang.String s, ResourceMap r)
          Convert string to object
static void register(ResourceConverter resourceConverter)
          Registers a ResourceConverter
 boolean supportsType(java.lang.Class testType)
          Checks whether testType can be converted with this converter.
 java.lang.String toString(java.lang.Object obj)
          Null safe toString operation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

type

protected final java.lang.Class type
Constructor Detail

ResourceConverter

protected ResourceConverter(java.lang.Class type)
Method Detail

parseString

public abstract java.lang.Object parseString(java.lang.String s,
                                             ResourceMap r)
                                      throws ResourceConverter.ResourceConverterException
Convert string to object

Parameters:
s - the string to be parsed
r - the ResourceMap
Returns:
the object which was created from the string
Throws:
ResourceConverter.ResourceConverterException

toString

public java.lang.String toString(java.lang.Object obj)
Null safe toString operation.

Parameters:
obj - the object to be converted to String
Returns:
result of obj.toString or "null"

supportsType

public boolean supportsType(java.lang.Class testType)
Checks whether testType can be converted with this converter.

Parameters:
testType -
Returns:
true if testType can be converted with this converter.

register

public static void register(ResourceConverter resourceConverter)
Registers a ResourceConverter

Parameters:
resourceConverter - the resource converter to be registered

forType

public static ResourceConverter forType(java.lang.Class type)
Returns ResourceConverter for the specified type

Parameters:
type - the type converter must be found for
Returns:
the converter for specified type or null if no converter is found for the type