public class EnumConverter extends java.lang.Object implements ObjectConverter
public static final int CENTER = 0;
public static final int TOP = 1;
public static final int LEFT = 2;
public static final int BOTTOM = 3;
public static final int RIGHT = 4;
Before JDK1.5, there is no enum type, so this is one way to define enumeration. When you use it, you just need to
define a int field say _locaton and the valid value for _location is one of the values above. If you want to display
it in UI and allow user to specify the value of _location, problem comes. You don't want to use 0, 1, 2, 3, 4 as the
value doesn't mean anything from user point of view. You want user to be able to use meaningful names such as
"Center", "Top", "Left", "Bottom", "Right". Obviously you need a converter here to convert from integer in an enum to
string, such as converting from 0 to "Center" and vice verse. That's what EnumConverter for.
Combining with EnumCellConverter, EnumCellEditor, you can easily use combobox to choose value for _location like the
example above using meaningful strings.Constructor and Description |
---|
EnumConverter(java.lang.String name,
java.lang.Class<?> type,
java.lang.Object[] values,
java.lang.String[] strings) |
EnumConverter(java.lang.String name,
java.lang.Class<?> type,
java.lang.Object[] objects,
java.lang.String[] strings,
java.lang.Object defaultValue)
Creates an EnumConverter.
|
EnumConverter(java.lang.String name,
java.lang.Object[] values,
java.lang.String[] strings) |
Modifier and Type | Method and Description |
---|---|
java.lang.Object |
fromString(java.lang.String string,
ConverterContext context)
Converts the string to the object.
|
ConverterContext |
getContext()
Gets the converter context of this converter.
|
java.lang.Object |
getDefault()
Gets the default value of the converter if it failed to find the matching object for a particular string.
|
java.lang.String |
getName()
Gets the name of the converter.
|
java.lang.Object[] |
getObjects()
Gets the
objects array. |
java.lang.String[] |
getStrings()
Gets the
strings array. |
java.lang.Class<?> |
getType()
Gets the type of the converter.
|
boolean |
isStrict()
Checks if the EnumConverter is strict about the value that passed to fromString and toString.
|
void |
setStrict(boolean strict)
Sets if the EnumConverter is strict about the value that passed to fromString and toString.
|
boolean |
supportFromString(java.lang.String string,
ConverterContext context)
If it supports fromString.
|
boolean |
supportToString(java.lang.Object object,
ConverterContext context)
If it supports toString method.
|
java.lang.String |
toString(java.lang.Object object,
ConverterContext context)
Converts the object to string.
|
static java.lang.String[] |
toStrings(java.lang.Object[] values)
Converts an object array to a String array using ObjectConverterManager.
|
static java.lang.String[] |
toStrings(java.lang.Object[] values,
ConverterContext converterContext)
Converts an object array to a String array using ObjectConverterManager.
|
public EnumConverter(java.lang.String name, java.lang.Object[] values, java.lang.String[] strings)
public EnumConverter(java.lang.String name, java.lang.Class<?> type, java.lang.Object[] values, java.lang.String[] strings)
public EnumConverter(java.lang.String name, java.lang.Class<?> type, java.lang.Object[] objects, java.lang.String[] strings, java.lang.Object defaultValue)
name
- the name of the converter. The name is used to create ConverterContext and later on the
EditorContext.type
- the type of the element in objects
array.objects
- the objects
array. All elements in the objects
array should have
the same type.strings
- the strings
array. It contains the meaningful names for the elements in
objects
array. They should one to one match with each other. The length of
strings
array should be the same as that of objects
array.
Otherwise IllegalArgumentExceptio will be thrown.defaultValue
- the default valuepublic ConverterContext getContext()
public java.lang.String toString(java.lang.Object object, ConverterContext context)
objects
array and find the matching
string from strings
array. If isStrict()
is true, null will be returned if nothing matches.
Otherwise, it will return the string value of the object using toString.toString
in interface ObjectConverter
object
- the object to be converted.context
- the converter context.public boolean supportToString(java.lang.Object object, ConverterContext context)
ObjectConverter
supportToString
in interface ObjectConverter
object
- object to be convertedcontext
- converter context to be usedpublic java.lang.Object fromString(java.lang.String string, ConverterContext context)
strings
array and find the
matching object from objects
array. If isStrict()
is true, the default value will be
returned if nothing matches. Otherwise, it will return the string itself that is passed in.fromString
in interface ObjectConverter
string
- the string to be convertedcontext
- the converter context.public boolean supportFromString(java.lang.String string, ConverterContext context)
ObjectConverter
supportFromString
in interface ObjectConverter
string
- the stringcontext
- context to be convertedpublic java.lang.String getName()
public java.lang.Class<?> getType()
public java.lang.Object getDefault()
public java.lang.Object[] getObjects()
objects
array.objects
array.public java.lang.String[] getStrings()
strings
array.strings
array.public static java.lang.String[] toStrings(java.lang.Object[] values)
ObjectConverter converter = new EnumConverter("Rank", Rank.values(),
EnumConverter.toStrings(Rank.values()));
Of course, you can still define your own string array for the enum values if the default one doesn't work well.values
- the object array.public static java.lang.String[] toStrings(java.lang.Object[] values, ConverterContext converterContext)
values
- the object array.converterContext
- the converter context used when calling ObjectConverterManager.toString.public boolean isStrict()
public void setStrict(boolean strict)
strict
- true or false.