10 Replies Latest reply on Jan 2, 2008 10:47 AM by Nicolai Abruzzese

    selectItems and enums

    Sander Mak Newbie

      I can edit a value with an enumerated type in the following way:

      <h:selectOneMenu value="#{EditEntryComponent.blogEntry_be.category}">
       <s:selectItems value="#{EditEntryComponent.values}" var="enum" label="#{enum.label}" noSelectionLabel="Please select : " />
       <s:convertEnum />
      </h:selectOneMenu>
      


      EditEntryComponent.values points to:

      public BlogEntry.Enum_category[] getValues() {
       return BlogEntry.Enum_category.values();
      }
      


      However, I would really like to omit this last function, and retrieve the values from the Enum_category enum directly. Does anybody see a way to express this in EL? I see a couple of problems:

      * the Java Enum API exposes values(), which does not conform to the get/set convention: not really a problem, I can generate a getValues() static method in each enum declaration which delegates to values()
      * I cannot use enhanced EL in s:selectItems (so it appears at least)
      * even if I could, how would I make a call to a static method in EL (using possibly a fully qualified type, i.e. some.package.Enum_category.values() )

      If someone sees a possibility or clever solution, please let me know...

      (one solution I played with is to create the getValues() in the enum as mentioned, and to use an EL expr. like 'EditEntryComponent.blogEntry_be.category.values' in s:selectItems, so we retrieve the possible values from an instance. However, in this case I cannot retrieve the possible values if the instance is null...)

        • 1. Re: selectItems and enums
          Sander Mak Newbie

          Hm, nevermind.... the last solution I sketched, actually works. Even for null values of the enum type. Don't know where I screwed up the initial test of this solution, but it serves me well now. I can generate a selectlist for an enumeration, solely based on the type declaration (ok, given a value of that type actually... without repeating parts of the enum declaration in the xhtml code), which is what I was after.

          Maybe it is an idea to incorporate this kind of functionality into s:selectitems (based on the always available values() and name() function on enums)?

          • 2. Re: selectItems and enums
            David Fedchenko Newbie

            Can you post the code you came up with? I tried some things based on your description but am not succeeding.

            • 3. Re: selectItems and enums
              Sander Mak Newbie

              Here's the code for the enum:

               public enum Enum_level
               {
               GOOD("Good"), AVERAGE("Average"), BAD("Baaad!") ;
              
               private String label;
              
               Enum_level (String label)
               {
               this.label = label;
               }
              
               public String getLabel()
               {
               return label;
               }
              
               public Enum_level[] getValues() {
              
               return Enum_level.values();
              
               }
              
               public String toString()
               {
               return label;
               }
               }
              
              


              and here's the accompanying jsf code:

              <h:selectOneMenu id="level" value="#{ViewBlogComponent.reply_r.level}">
               <s:selectItems value="#{ViewBlogComponent.reply_r.level.values}" var="enum" label="#{enum.label}" noSelectionLabel="Please select : "/>
               <s:convertEnum/>
               </h:selectOneMenu>
              


              However, it turns out that the problem of not being able to get enum values from non-initialized references is still there. I think the EL-resolver just refuses to call getValues(), when (in this case) getLevel() returns null. Though it can safely do so, since it is a static accessor. Bummer :-( I really hoped this would work... this should be possible, right??

              • 4. Re: selectItems and enums
                Sander Mak Newbie

                Note to self: getValues() of course is not a static method, which explains the failure. I would like to make it static, but if I do it is not recognized as a valid getter method by the EL-resolver. Back to square one (though it is a nice, working solution for non-null enum values).

                • 5. Re: selectItems and enums
                  David Fedchenko Newbie

                  Yep, that's what I tried and that's how it didn't work. So I just have an external getter for now.

                  It looks like Seam 1.3 EL will allow Enum.values() in a value expression. That will simplify this and a number of things like it.

                  • 6. Re: selectItems and enums
                    Sander Mak Newbie

                    Oh, that would be cool, where did you see that?

                    • 7. Re: selectItems and enums
                      David Fedchenko Newbie

                      It shows up with jboss-el. It was an item in the release notes.

                      • 8. Re: selectItems and enums
                        Andrew Elegante Newbie

                        I have been unable to find any example of using something like #{EnumClass.values()} in the documentation. When I try it seam tries to resolve EnumClass as a managed component and fails. Can anyone help me out here?

                        <s:selectItems value="#{EnumClass.values()}" />
                        <s:convertEnum />
                        


                        I'm using Seam 2.0.0 GA.

                        • 9. Re: selectItems and enums
                          Pete Muir Master

                          AFAIK this isn't possible, you need to expose your Enum values e.g. via an @Factory

                          • 10. Re: selectItems and enums
                            Nicolai Abruzzese Newbie

                            This worked for me:

                            ...
                             @DataModel
                             private List<Pais> paisList;
                            
                             @Factory("paisList")
                             public String listAll() {
                             personList= entityManager.createQuery("from Pais").getResultList();
                             }
                            ...
                            


                            xhtml:
                            ...
                            <h:selectOneMenu value="#{estePais.formaAplicacionRetenciones}">
                             <s:convertEnum></s:convertEnum>
                             <s:enumItem label="#{m['OPCIONIMPUESTOPORDENTRO']}" enumValue="PORDENTRO"></s:enumItem>
                             <s:enumItem label="#{m['OPCIONIMPUESTOPORFUERA']}" enumValue="PORFUERA"></s:enumItem>
                             </h:selectOneMenu>
                            
                            ..
                            


                            Wher Enum Values are: PORDENTRO, PORFUERA
                            Hope to give u some lights!