Source of EnumEditor.java


  1: import java.beans.*;

  3: /**
  4:    A property editor for enumerated types.
  5: */
  6: public class EnumEditor extends PropertyEditorSupport
  7: {
  8:    /**
  9:       Constructs a property editor for an enumerated type
 10:       @param cl the class object for the enumerated type
 11:    */
 12:    public EnumEditor(Class cl)
 13:    {
 14:       this.cl = cl;
 15:    }

 17:    public String[] getTags()
 18:    {
 19:       try
 20:       {
 21:          Object[] values = (Object[]) cl.getMethod("values").invoke(null);
 22:          String[] result = new String[values.length];
 23:          for (int i = 0; i < values.length; i++)
 24:             result[i] = values[i].toString();
 25:          return result;
 26:       }
 27:       catch (Exception ex)
 28:       {
 29:          return null;
 30:       }
 31:    }

 33:    public String getAsText()
 34:    {
 35:       return getValue().toString();
 36:    }

 38:    public void setAsText(String s)
 39:    {
 40:       setValue(Enum.valueOf(cl, s));
 41:    }

 43:    private Class cl;
 44: }