001package horstmann.ch08_graphed2;
002import java.beans.PropertyEditorSupport;
003
004/**
005   A property editor for enumerated types.
006 */
007@SuppressWarnings("all")
008public class EnumEditor extends PropertyEditorSupport
009{
010        /**
011      Constructs a property editor for an enumerated type
012      @param cl the class object for the enumerated type
013         */
014        public EnumEditor(Class cl)
015        {
016                this.cl = cl;
017        }
018
019        public String[] getTags()
020        {
021                try
022                {
023                        Object[] values = (Object[]) cl.getMethod("values").invoke(null);
024                        String[] result = new String[values.length];
025                        for (int i = 0; i < values.length; i++)
026                                result[i] = values[i].toString();
027                        return result;
028                }
029                catch (Exception ex)
030                {
031                        return null;
032                }
033        }
034
035        public String getAsText()
036        {
037                return getValue().toString();
038        }
039
040        public void setAsText(String s)
041        {
042                setValue(Enum.valueOf(cl, s));
043        }
044
045        private Class cl;
046}