JIDE 3.5.15

com.jidesoft.converter
Class EnumConverter

java.lang.Object
  extended by com.jidesoft.converter.EnumConverter
All Implemented Interfaces:
ObjectConverter

public class EnumConverter
extends Object
implements 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
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

EnumConverter

public EnumConverter(Class<? extends Enum> enumType)
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

public EnumConverter(String name,
                     Object[] values,
                     String[] strings)

EnumConverter

public EnumConverter(String name,
                     Class<?> type,
                     Object[] values,
                     String[] strings)

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 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 value
Method Detail

getContext

public ConverterContext 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

public String toString(Object object,
                       ConverterContext context)
Converts the object to string. It will find the object from the 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.

Specified by:
toString in interface ObjectConverter
Parameters:
object - the object to be converted.
context - the converter context.
Returns:
the string for the object.

supportToString

public boolean supportToString(Object object,
                               ConverterContext context)
Description copied from interface: ObjectConverter
If it supports toString method.

Specified by:
supportToString in interface ObjectConverter
Parameters:
object - object to be converted
context - converter context to be used
Returns:
true if supports toString

fromString

public Object fromString(String string,
                         ConverterContext context)
Converts the string to the object. It will find the string from the 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.

Specified by:
fromString in interface ObjectConverter
Parameters:
string - the string to be converted
context - the converter context.
Returns:
the object of the string.

supportFromString

public boolean supportFromString(String string,
                                 ConverterContext context)
Description copied from interface: ObjectConverter
If it supports fromString.

Specified by:
supportFromString in interface ObjectConverter
Parameters:
string - the string
context - context to be converted
Returns:
true if it supports

getName

public String getName()
Gets the name of the converter.

Returns:
the name of the converter.

getType

public Class<?> getType()
Gets the type of the converter.

Returns:
the type of the converter.

getDefault

public Object 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

public Object[] getObjects()
Gets the objects array.

Returns:
the objects array.

getStrings

public String[] getStrings()
Gets the strings array.

Returns:
the strings array.

toStrings

public static String[] toStrings(Object[] values)
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.

 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.

Parameters:
values - the object array.
Returns:
the string array.

toStrings

public static String[] toStrings(Object[] values,
                                 ConverterContext converterContext)
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.

JIDE 3.5.15