Author: tchemit Date: 2010-02-17 11:58:26 +0100 (Wed, 17 Feb 2010) New Revision: 1744 Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/editor/EnumEditor.java Log: Evolution #320: Can build EnumEditor on restricted values of enum universe Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/editor/EnumEditor.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/editor/EnumEditor.java 2010-02-15 17:09:42 UTC (rev 1743) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/editor/EnumEditor.java 2010-02-17 10:58:26 UTC (rev 1744) @@ -21,6 +21,8 @@ import javax.swing.JComboBox; import java.util.EnumSet; +import java.util.Iterator; + import org.nuiton.util.ReflectUtil; /** @@ -34,18 +36,64 @@ */ public class EnumEditor<E extends Enum<E>> extends JComboBox { - /** serialVersionUID */ - private static final long serialVersionUID = 2693771553067104538L; + private static final long serialVersionUID = 2L; + /** + * Type of enumeration + */ protected Class<E> type; + /** + * Creates a {@link EnumEditor} for the given enumeration {@code type}, with + * all values of enumeration. + * + * @param type type of enumeration + * @param <E> generci type of enumeration + * @return the instanciated editor + */ public static <E extends Enum<E>> EnumEditor<E> newEditor(Class<E> type) { return new EnumEditor<E>(type); } + + /** + * Creates a {@link EnumEditor} for the given enumeration {@code type}, with + * all values of enumeration which {@code ordinal} is strictly lower than + * the given {@code maxOrdinal}. + * + * @param type type of enumeration + * @param maxOrdinal the upper (strict) bound of ordinal values allowed + * @param <E> generic type of enumeration + * @return the instanciated editor + */ + public static <E extends Enum<E>> EnumEditor<E> newEditor(Class<E> type, + int maxOrdinal) { + return new EnumEditor<E>(type, maxOrdinal); + } + + /** + * Creates a {@link EnumEditor} for the given enumeration {@code type}, with + * all given {@code universe} values of enumeration. + * + * @param universe enumerations to put in editor + * @param <E> generci type of enumeration + * @return the instanciated editor + */ + public static <E extends Enum<E>> EnumEditor<E> newEditor(E... universe) { + return new EnumEditor<E>(universe); + } + public EnumEditor(Class<E> type) { super(buildModel(type)); } + public EnumEditor(Class<E> type, int maxOrdinal) { + super(buildModel(type, maxOrdinal)); + } + + public EnumEditor(E... universe) { + super(universe); + } + @Override public E getSelectedItem() { return (E) super.getSelectedItem(); @@ -56,4 +104,19 @@ EnumSet<E> result = EnumSet.allOf(enumClass); return result.toArray(new Object[result.size()]); } + + + protected static <E extends Enum<E>> Object[] buildModel(Class<E> type, + int maxOrdinal) { + Class<E> enumClass = ReflectUtil.getEnumClass(type); + EnumSet<E> result = EnumSet.allOf(enumClass); + Iterator<E> itr = result.iterator(); + while (itr.hasNext()) { + E e = itr.next(); + if (e.ordinal() > maxOrdinal) { + itr.remove(); + } + } + return result.toArray(new Object[result.size()]); + } }