Package com.jidesoft.converter
Class EnumConverter
java.lang.Object
com.jidesoft.converter.EnumConverter
- All Implemented Interfaces:
ObjectConverter
A typical way to define a constant is to use int as the value type. For example, in SwingConstants, the following
values are defined.
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 Summary
ConstructorsConstructorDescriptionEnumConverter
(Class<? extends Enum> enumType) The constructor to convert a enum type class.EnumConverter
(String name, Class<?> type, Object[] values, String[] strings) Creates an EnumConverter.EnumConverter
(String name, Object[] values, String[] strings) -
Method Summary
Modifier and TypeMethodDescriptionfromString
(String string, ConverterContext context) Converts the string to the object.Gets the converter context of this converter.Gets the default value of the converter if it failed to find the matching object for a particular string.getName()
Gets the name of the converter.Object[]
Gets theobjects
array.Object[]
getObjects
(int[] indices) Gets a partial object array from the complete enumobjects
array.String[]
Gets thestrings
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.toString
(Object object, ConverterContext context) Converts the object to string.static String[]
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.
-
Constructor Details
-
EnumConverter
The constructor to convert a enum type class. Reflection is used to invoke Enum#getValues(). Please consider make the enum class protected or public if you want to release your version after obfuscated. Otherwise, this constructor may not be able to find correct class method to work.- Parameters:
enumType
- the enum type- Since:
- 3.4.0
-
EnumConverter
-
EnumConverter
-
EnumConverter
public EnumConverter(String name, Class<?> type, Object[] objects, String[] strings, Object defaultValue) Creates an EnumConverter.- Parameters:
name
- the name of the converter. The name is used to create ConverterContext and later on the EditorContext.type
- the type of the element inobjects
array.objects
- theobjects
array. All elements in theobjects
array should have the same type.strings
- thestrings
array. It contains the meaningful names for the elements inobjects
array. They should one to one match with each other. The length ofstrings
array should be the same as that ofobjects
array. Otherwise IllegalArgumentExceptio will be thrown.defaultValue
- the default value
-
-
Method Details
-
getContext
Gets the converter context of this converter. The name of the context is the name of the converter where you pass in to EnumConverter's constructor.- Returns:
- the converter context of this converter.
-
toString
Converts the object to string. It will find the object from theobjects
array and find the matching string fromstrings
array. IfisStrict()
is true, null will be returned if nothing matches. Otherwise, it will return the string value of the object using toString.- Specified by:
toString
in interfaceObjectConverter
- Parameters:
object
- the object to be converted.context
- the converter context.- Returns:
- the string for the object.
-
supportToString
Description copied from interface:ObjectConverter
If it supports toString method.- Specified by:
supportToString
in interfaceObjectConverter
- Parameters:
object
- object to be convertedcontext
- converter context to be used- Returns:
- true if supports toString
-
fromString
Converts the string to the object. It will find the string from thestrings
array and find the matching object fromobjects
array. IfisStrict()
is true, the default value will be returned if nothing matches. Otherwise, it will return the string itself that is passed in.- Specified by:
fromString
in interfaceObjectConverter
- Parameters:
string
- the string to be convertedcontext
- the converter context.- Returns:
- the object of the string.
-
supportFromString
Description copied from interface:ObjectConverter
If it supports fromString.- Specified by:
supportFromString
in interfaceObjectConverter
- Parameters:
string
- the stringcontext
- context to be converted- Returns:
- true if it supports
-
getName
Gets the name of the converter.- Returns:
- the name of the converter.
-
getType
Gets the type of the converter.- Returns:
- the type of the converter.
-
getDefault
Gets the default value of the converter if it failed to find the matching object for a particular string.- Returns:
- the default value.
-
getObjects
Gets theobjects
array.- Returns:
- the
objects
array.
-
getObjects
Gets a partial object array from the complete enumobjects
array.- Parameters:
indices
- of the partial enum values.- Returns:
- the
objects
array.
-
getStrings
Gets thestrings
array.- Returns:
- the
strings
array.
-
toStrings
Converts an object array to a String array using ObjectConverterManager. This method can be used, for example, for Enum type, to provide a default string representation of the enum values.
Of course, you can still define your own string array for the enum values if the default one doesn't work well.ObjectConverter converter = new EnumConverter("Rank", Rank.values(), EnumConverter.toStrings(Rank.values()));
- Parameters:
values
- the object array.- Returns:
- the string array.
-
toStrings
Converts an object array to a String array using ObjectConverterManager.- Parameters:
values
- the object array.converterContext
- the converter context used when calling ObjectConverterManager.toString.- Returns:
- the string array.
-
isStrict
public boolean isStrict()Checks if the EnumConverter is strict about the value that passed to fromString and toString. If true, fromString will convert any String that doesn't match to the default value, toString will return null if the value doesn't match. If false, the string itself will be return from fromString. Default is true.- Returns:
- true or false.
-
setStrict
public void setStrict(boolean strict) Sets if the EnumConverter is strict about the value that passed to fromString and toString. Default is true.- Parameters:
strict
- true or false.
-