001    /*
002     * EnumPropertyEditor.java
003     *
004     * Created on August 18, 2006, 10:43 AM
005     *
006     * To change this template, choose Tools | Template Manager
007     * and open the template in the editor.
008     */
009    
010    package org.jdesktop.swingx.editors;
011    
012    import java.beans.PropertyEditorSupport;
013    import java.util.ArrayList;
014    import java.util.EnumSet;
015    import java.util.List;
016    
017    /**
018     *
019     * @author joshy
020     */
021    public class EnumPropertyEditor<E extends Enum<E>> extends PropertyEditorSupport {
022        private Class<E> en;
023        private EnumSet<E> set;
024        
025        /** Creates a new instance of EnumPropertyEditor */
026        public EnumPropertyEditor(Class<E> en) {
027            this.en = en;
028            set = EnumSet.allOf(en);
029        }
030        
031        public String[] getTags() {
032            List<String> strs = new ArrayList<String>();
033            for(E e : set) {
034                strs.add(e.toString());
035            }
036            return strs.toArray(new String[0]);
037        }
038        
039        public String getAsText() {
040            return getValue().toString();
041        }
042        
043        public void setAsText(String text) throws IllegalArgumentException {
044    //        u.p("setting as text: " + text);
045            Enum<E> e = Enum.valueOf(en, text);
046            setValue(e);
047        }
048    
049        @Override
050        public String getJavaInitializationString() {
051            //org.jdesktop.stuff.Stuff.InsideEnum.VALUE;
052            // the '$' must be converted to '.' to deal with public inner classes
053            return new String(en.getName()+"."+getAsText()).replace("$",".");
054        }
055        
056    }