3 Replies Latest reply on Jun 30, 2008 12:27 AM by stephen

    How about <s:enumItems>?

    stephen

      <s:selectItems> is great to avoid having to create SelectItems in Java code.
      However for select's based on enums there is no such convenience.You still have to create a method in a seam component that converts the enum values to SelectItems.


      How about this:



      <s:enumItems enumClass="com.acme.OrderType" var="orderType" label="#{msgs[orderType.name()]}"/>




      I think that would fit nicely with Seam.
      If that's ok, I am going to create a Jira issue and try to supply the code.

        • 1. Re: How about <s:enumItems>?
          wrzep

          You can use s:convertEnum tag to achieve this :)


          -Pawel

          • 2. Re: How about <s:enumItems>?
            stephen

            Hi Pawel and thanks for the answer.


            However either you missed the point of my request or I completely misunderstood s:convertEnum.


            AFAICS s:convertEnum is simply a converter (using the enum object's name() as String representation). You still need to specify SelectItems and to do that you need Java code.


            Currently:


            @Name("factories")
            public class Factories
            {
               @Factory("orderTypes")
               public OrderType[] getOrderTypes{
                  return OrderType.values();
               }
            }



            and


            <s:selectItems value="#{orderTypes} ...



            With my proposal you can get rid of the Java code and simply use:


            <s:enumItems class="com.acme.OrderType" ...




            As a side note: I found I so far never needed the s:convertEnum tag. When it is omitted, the index of the enum in the value list is used. I think that's just fine, because the ordering of enum items is stable (if you don't calculate them in a very funny way in java code).


            For me the only open question is whether the s:enumItems approach is susceptible to class loader problems, as it would need to access the enum class using reflection (with the current thread's class loader?).

            • 3. Re: How about <s:enumItems>?
              stephen

              Hm, an alternative would be to add another function to Seam's EL functions that given the FQN would return a reference to a class:



              <s:selectItems value="#{s:clazz(com.acme.OrderType).values()"} ...



              That could be useful in other scenarios, too. Basically you would be able to call any public static method.