Class EnumConverter

  • All Implemented Interfaces:
    ObjectConverter

    public class EnumConverter
    extends java.lang.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

      Constructors 
      Constructor Description
      EnumConverter​(java.lang.Class<? extends java.lang.Enum> enumType)
      The constructor to convert a enum type class.
      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)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method 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.Object[] getObjects​(int[] indices)
      Gets a partial object array from the complete enum 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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • EnumConverter

        public EnumConverter​(java.lang.Class<? extends java.lang.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​(java.lang.String name,
                             java.lang.Object[] values,
                             java.lang.String[] strings)
      • EnumConverter

        public EnumConverter​(java.lang.String name,
                             java.lang.Class<?> type,
                             java.lang.Object[] values,
                             java.lang.String[] strings)
      • EnumConverter

        public EnumConverter​(java.lang.String name,
                             java.lang.Class<?> type,
                             java.lang.Object[] objects,
                             java.lang.String[] strings,
                             java.lang.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 java.lang.String toString​(java.lang.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​(java.lang.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 java.lang.Object fromString​(java.lang.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​(java.lang.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 java.lang.String getName()
        Gets the name of the converter.
        Returns:
        the name of the converter.
      • getType

        public java.lang.Class<?> getType()
        Gets the type of the converter.
        Returns:
        the type of the converter.
      • getDefault

        public java.lang.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 java.lang.Object[] getObjects()
        Gets the objects array.
        Returns:
        the objects array.
      • getObjects

        public java.lang.Object[] getObjects​(int[] indices)
        Gets a partial object array from the complete enum objects array.
        Parameters:
        indices - of the partial enum values.
        Returns:
        the objects array.
      • getStrings

        public java.lang.String[] getStrings()
        Gets the strings array.
        Returns:
        the strings array.
      • toStrings

        public static java.lang.String[] toStrings​(java.lang.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 java.lang.String[] toStrings​(java.lang.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.