|
JIDE 3.5.15 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.jidesoft.converter.EnumConverter
public class EnumConverter
A typical way to define a constant is to use int as the value type. For example, in SwingConstants, the following
values are defined.
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.
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;
Constructor Summary | |
---|---|
EnumConverter(Class<? extends Enum> enumType)
The constructor to convert a enum type class. |
|
EnumConverter(String name,
Class<?> type,
Object[] values,
String[] strings)
|
|
EnumConverter(String name,
Class<?> type,
Object[] objects,
String[] strings,
Object defaultValue)
Creates an EnumConverter. |
|
EnumConverter(String name,
Object[] values,
String[] strings)
|
Method Summary | |
---|---|
Object |
fromString(String string,
ConverterContext context)
Converts the string to the object. |
ConverterContext |
getContext()
Gets the converter context of this converter. |
Object |
getDefault()
Gets the default value of the converter if it failed to find the matching object for a particular string. |
String |
getName()
Gets the name of the converter. |
Object[] |
getObjects()
Gets the objects array. |
String[] |
getStrings()
Gets the strings array. |
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(String string,
ConverterContext context)
If it supports fromString. |
boolean |
supportToString(Object object,
ConverterContext context)
If it supports toString method. |
String |
toString(Object object,
ConverterContext context)
Converts the object to string. |
static String[] |
toStrings(Object[] values)
Converts an object array to a String array using ObjectConverterManager. |
static String[] |
toStrings(Object[] values,
ConverterContext converterContext)
Converts an object array to a String array using ObjectConverterManager. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public EnumConverter(Class<? extends Enum> enumType)
enumType
- the enum typepublic EnumConverter(String name, Object[] values, String[] strings)
public EnumConverter(String name, Class<?> type, Object[] values, String[] strings)
public EnumConverter(String name, Class<?> type, Object[] objects, String[] strings, 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 valueMethod Detail |
---|
public ConverterContext getContext()
public String toString(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(Object object, ConverterContext context)
ObjectConverter
supportToString
in interface ObjectConverter
object
- object to be convertedcontext
- converter context to be used
public Object fromString(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(String string, ConverterContext context)
ObjectConverter
supportFromString
in interface ObjectConverter
string
- the stringcontext
- context to be converted
public String getName()
public Class<?> getType()
public Object getDefault()
public Object[] getObjects()
objects
array.
objects
array.public String[] getStrings()
strings
array.
strings
array.public static String[] toStrings(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 String[] toStrings(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.
|
JIDE 3.5.15 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |