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 theobjectsarray.Object[]getObjects(int[] indices) Gets a partial object array from the complete enumobjectsarray.String[]Gets thestringsarray.Class<?> getType()Gets the type of the converter.booleanisStrict()Checks if the EnumConverter is strict about the value that passed to fromString and toString.voidsetStrict(boolean strict) Sets if the EnumConverter is strict about the value that passed to fromString and toString.booleansupportFromString(String string, ConverterContext context) If it supports fromString.booleansupportToString(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 inobjectsarray.objects- theobjectsarray. All elements in theobjectsarray should have the same type.strings- thestringsarray. It contains the meaningful names for the elements inobjectsarray. They should one to one match with each other. The length ofstringsarray should be the same as that ofobjectsarray. 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 theobjectsarray and find the matching string fromstringsarray. IfisStrict()is true, null will be returned if nothing matches. Otherwise, it will return the string value of the object using toString.- Specified by:
toStringin interfaceObjectConverter- Parameters:
object- the object to be converted.context- the converter context.- Returns:
- the string for the object.
-
supportToString
Description copied from interface:ObjectConverterIf it supports toString method.- Specified by:
supportToStringin 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 thestringsarray and find the matching object fromobjectsarray. 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:
fromStringin interfaceObjectConverter- Parameters:
string- the string to be convertedcontext- the converter context.- Returns:
- the object of the string.
-
supportFromString
Description copied from interface:ObjectConverterIf it supports fromString.- Specified by:
supportFromStringin 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 theobjectsarray.- Returns:
- the
objectsarray.
-
getObjects
Gets a partial object array from the complete enumobjectsarray.- Parameters:
indices- of the partial enum values.- Returns:
- the
objectsarray.
-
getStrings
Gets thestringsarray.- Returns:
- the
stringsarray.
-
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.
-