2 Replies Latest reply on Sep 20, 2010 10:23 AM by Neil C

    Backing selectmanycheckbox with Enum Type

    Neil C Novice

      I have an Enum type that will be the basis for a selectmanycheckbox in my facelet. Meanwhile, the backing bean needs to be a collection of the Enum values.


      So if I have a Customer with preferred payment types and an Enum PaymentType of {CASH, CHECK, CREDIT, MONEY_ORDER} for example, how can I use
      s:selectItems and s:convertEnum in concert so that the facelet will present all the Enum choices as checkboxes and save a subset of them in some Seam component? I have tried a List<PaymentType> and various combinations of tags in the facelet to no avail.


      Thanks.

        • 1. Re: Backing selectmanycheckbox with Enum Type
          Shervin Asgari Master

          Should be pretty straight forward.



          xhtml:




          <s:decorate template="/layout/edit_block.xhtml">
                  <ui:define name="label">#{messages['rulingType.choose']}</ui:define>
               <h:selectManyCheckbox id="radioRuling" value="#{rulingPage.ruling}" required="true">
                 <s:selectItems var="rul" value="#{rulings}" label="#{rul.label}" />
                    <s:convertEnum />
               </h:selectManyCheckbox>
          </s:decorate>
          
          @Factory(value = "rulings", scope = ScopeType.EVENT)
          @BypassInterceptors
          public List<Ruling> getRulings() {
               Set<Ruling> allRulings = EnumSet.of(Ruling.INNVILGET,Ruling.DELVIS,Ruling.AVSLAG);
               return Collections.unmodifiableList(new ArrayList<Ruling>(allRulings));
          }
          
          
          //In your bean
          @Name("rulingPage")
          public class RulingPage {
          
          List<Ruling> ruling; //Getter and setter
          
          



          • 2. Re: Backing selectmanycheckbox with Enum Type
            Neil C Novice

            I figured it was pretty straightforward, but I just needed a pointer in the right direction. The EnumSet class and the Factory annotation did the trick. 


            The only difference is that I used EnumSet.range() because I am lazy. Otherwise, I followed your guidance precisely, and it worked like a charm.


            Thanks for you help.