You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by er...@novartis.com on 2007/05/22 17:53:32 UTC

enumerated type and f:selectItems

Hi,

What's the best way generate UISelectItems from an enumerated type? I'd 
like to populate a UISelectOne-based component with enumerated type values 
and back the component with an enumerated type variable instance. This 
seems like it should be a common pattern in JSF with JDK 1.5+. In fact, 
I'm a little surprised we can't specify an enumerated type (or variable of 
an enumerated type) for f:selectItems.

Thanks for any replies,
Eric

_________________________

CONFIDENTIALITY NOTICE

The information contained in this e-mail message is intended only for the 
exclusive use of the individual or entity named above and may contain 
information that is privileged, confidential or exempt from disclosure 
under applicable law. If the reader of this message is not the intended 
recipient, or the employee or agent responsible for delivery of the 
message to the intended recipient, you are hereby notified that any 
dissemination, distribution or copying of this communication is strictly 
prohibited. If you have received this communication in error, please 
notify the sender immediately by e-mail and delete the material from any 
computer.  Thank you.

RE: enumerated type and f:selectItems

Posted by "Beelen, Marco" <ma...@merck.com>.
Hello all,

I'm using a slighty different alternative.
I added the method getLabel to my Enum, which returns the key of the
property for that value of Enum.

My page contains a the following scrap:

<h:selectOneRadio 
	layout="pageDirection"
	value="#{createRequestBean.category}">

	<t:selectItems 
		value="#{createRequestBean.availableCategories}" 
		var="category" 
		itemLabel="#{labels[category.label]}" 
		itemValue="#{category}" />

</h:selectOneRadio>


In my managed-bean I just return a List<Enum> with all values from my
Enum like this:

	/**
	 * Gets the available categories.
	 * 
	 * @return An array with all the available categories.
	 */
	public List<Category> getAvailableCategories() {

		Category[] categories = Category.values();
		return Arrays.asList(categories);
	}
 
Combined with a generic converter for Enums and a getter and setter for
the Enum on my managed-bean it works like a charm for me.

With kind regards,
  Marco Beelen



-----Original Message-----
From: Erlend Hamnaberg [mailto:erlenha@tihlde.org] 
Sent: dinsdag 22 mei 2007 20:17
To: MyFaces Discussion
Subject: Re: enumerated type and f:selectItems

Here is a custom localized selectItem creator from enums.
Assumes you have a resource bundle configured in faces config.

It also expects values for each Enum Value.

If you have this enum:

package com.exampe;

public enum Example {
    EXAMPLE
}

Then the appropriate entry in the message bundle would be:

com.example.Example.EXAMPLE=example


    public static List<SelectItem> 
createSelectItemsFromEnum(FacesContext context, Class<? extends Enum> 
value) {
        List<SelectItem> list = new ArrayList<SelectItem>();
        Application a = context.getApplication();
        String msgBundle = a.getMessageBundle();
        Locale locale = context.getViewRoot().getLocale();
        ResourceBundle rb = ResourceBundle.getBundle(msgBundle, locale);

        Set<? extends Enum> set = EnumSet.allOf(value);
        if (set != null && set.size() > 0) {
            for (Enum e : set) {
                String name = value.getName() + "." + e.name();
                list.add(new SelectItem(e, rb.getString(name)));
            }
        }

        return list;
    }

I have also a Converter that uses the same method from and to Enums if 
you'd like that. That is needed if you use standard components.

- Erlend

Cagatay Civici wrote:
> Hi,
>
> Sandbox15 has an enumconverter too.
>
>
http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox15/core/src/m
ain/java/org/apache/myfaces/custom/converter/enumeration/EnumConverter.j
ava?view=markup
>
> Cagatay
>
> On 5/22/07, *Andrew Robinson* <andrew.rw.robinson@gmail.com 
> <ma...@gmail.com>> wrote:
>
>     If you use jboss seam there is support for an enum converter and
>     enum-select item components that do just what you are looking for.
>
>     On 5/22/07, eric.jung@novartis.com <ma...@novartis.com>
>     < eric.jung@novartis.com <ma...@novartis.com>> wrote:
>     >
>     > Hi,
>     >
>     > What's the best way generate UISelectItems from an enumerated
>     type? I'd like
>     > to populate a UISelectOne-based component with enumerated type
>     values and
>     > back the component with an enumerated type variable instance.
>     This seems
>     > like it should be a common pattern in JSF with JDK 1.5+. In
>     fact, I'm a
>     > little surprised we can't specify an enumerated type (or
>     variable of an
>     > enumerated type) for f:selectItems.
>     >
>     > Thanks for any replies,
>     > Eric
>     >
>     >  _________________________
>     >
>     >  CONFIDENTIALITY NOTICE
>     >
>     >  The information contained in this e-mail message is intended
>     only for the
>     > exclusive use of the individual or entity named above and may
>     contain
>     > information that is privileged, confidential or exempt from
>     disclosure under
>     > applicable law. If the reader of this message is not the
>     intended recipient,
>     > or the employee or agent responsible for delivery of the message
>     to the
>     > intended recipient, you are hereby notified that any
dissemination,
>     > distribution or copying of this communication is strictly
>     prohibited. If you
>     > have received this communication in error, please notify the
sender
>     > immediately by e-mail and delete the material from any
>     computer.  Thank you.
>     >
>
>




------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachments, contains
information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station,
New Jersey, USA 08889), and/or its affiliates (which may be known
outside the United States as Merck Frosst, Merck Sharp & Dohme or MSD
and in Japan, as Banyu - direct contact information for affiliates is 
available at http://www.merck.com/contact/contacts.html) that may be 
confidential, proprietary copyrighted and/or legally privileged. It is 
intended solely for the use of the individual or entity named on this 
message. If you are not the intended recipient, and have received this 
message in error, please notify us immediately by reply e-mail and then 
delete it from your system.

------------------------------------------------------------------------------

Re: enumerated type and f:selectItems

Posted by Erlend Hamnaberg <er...@tihlde.org>.
Here is a custom localized selectItem creator from enums.
Assumes you have a resource bundle configured in faces config.

It also expects values for each Enum Value.

If you have this enum:

package com.exampe;

public enum Example {
    EXAMPLE
}

Then the appropriate entry in the message bundle would be:

com.example.Example.EXAMPLE=example


    public static List<SelectItem> 
createSelectItemsFromEnum(FacesContext context, Class<? extends Enum> 
value) {
        List<SelectItem> list = new ArrayList<SelectItem>();
        Application a = context.getApplication();
        String msgBundle = a.getMessageBundle();
        Locale locale = context.getViewRoot().getLocale();
        ResourceBundle rb = ResourceBundle.getBundle(msgBundle, locale);

        Set<? extends Enum> set = EnumSet.allOf(value);
        if (set != null && set.size() > 0) {
            for (Enum e : set) {
                String name = value.getName() + "." + e.name();
                list.add(new SelectItem(e, rb.getString(name)));
            }
        }

        return list;
    }

I have also a Converter that uses the same method from and to Enums if 
you'd like that. That is needed if you use standard components.

- Erlend

Cagatay Civici wrote:
> Hi,
>
> Sandbox15 has an enumconverter too.
>
> http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox15/core/src/main/java/org/apache/myfaces/custom/converter/enumeration/EnumConverter.java?view=markup
>
> Cagatay
>
> On 5/22/07, *Andrew Robinson* <andrew.rw.robinson@gmail.com 
> <ma...@gmail.com>> wrote:
>
>     If you use jboss seam there is support for an enum converter and
>     enum-select item components that do just what you are looking for.
>
>     On 5/22/07, eric.jung@novartis.com <ma...@novartis.com>
>     < eric.jung@novartis.com <ma...@novartis.com>> wrote:
>     >
>     > Hi,
>     >
>     > What's the best way generate UISelectItems from an enumerated
>     type? I'd like
>     > to populate a UISelectOne-based component with enumerated type
>     values and
>     > back the component with an enumerated type variable instance.
>     This seems
>     > like it should be a common pattern in JSF with JDK 1.5+. In
>     fact, I'm a
>     > little surprised we can't specify an enumerated type (or
>     variable of an
>     > enumerated type) for f:selectItems.
>     >
>     > Thanks for any replies,
>     > Eric
>     >
>     >  _________________________
>     >
>     >  CONFIDENTIALITY NOTICE
>     >
>     >  The information contained in this e-mail message is intended
>     only for the
>     > exclusive use of the individual or entity named above and may
>     contain
>     > information that is privileged, confidential or exempt from
>     disclosure under
>     > applicable law. If the reader of this message is not the
>     intended recipient,
>     > or the employee or agent responsible for delivery of the message
>     to the
>     > intended recipient, you are hereby notified that any dissemination,
>     > distribution or copying of this communication is strictly
>     prohibited. If you
>     > have received this communication in error, please notify the sender
>     > immediately by e-mail and delete the material from any
>     computer.  Thank you.
>     >
>
>


Re: enumerated type and f:selectItems

Posted by er...@novartis.com.
Typo below:

  selectItems.put(new Integer(o.ordinal()), o.getName())
should be:
  selectItems.put(o, o.getName()); 
In any case, I'm still trying to figure out how to use the EnumConverter 
with <h:selectOneRadio/> and friends.







eric.jung@novartis.com 
05/22/2007 02:49 PM
Please respond to
"MyFaces Discussion" <us...@myfaces.apache.org>


To
"MyFaces Discussion" <us...@myfaces.apache.org>
cc

Subject
Re: enumerated type and f:selectItems







Thanks! If I understand correctly, this converter is for the variable 
which is value-bound to a UIOutput/UIInput-derived classes (e.g. 
UISelectOne), but not for UISelectItems. In other words, is there a way to 
get <f:selectItems/> to output options for all values of the enumerated 
type? I tried: 

<f:selectItems value="{#MyClass.selectItems"/> 

public Map<MyEnum, String> getSelectItems() { 
  Map<MyEnum, String> selectItems = new EnumMap<MyEnum, 
String>(MyEnum.class); 
  for (MyEnum o : EnumSet.allOf(MyEnum.class)) 
    selectItems.put(new Integer(o.ordinal()), o.getName()); 
  return selectItems; 
} 

and a few variations on that, without any success. 



"Cagatay Civici" <ca...@gmail.com> 
05/22/2007 01:01 PM 

Please respond to
"MyFaces Discussion" <us...@myfaces.apache.org>


To
"MyFaces Discussion" <us...@myfaces.apache.org> 
cc

Subject
Re: enumerated type and f:selectItems








Hi,

Sandbox15 has an enumconverter too.

http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox15/core/src/main/java/org/apache/myfaces/custom/converter/enumeration/EnumConverter.java?view=markup


Cagatay

On 5/22/07, Andrew Robinson <an...@gmail.com> wrote: 
If you use jboss seam there is support for an enum converter and
enum-select item components that do just what you are looking for.

On 5/22/07, eric.jung@novartis.com < eric.jung@novartis.com> wrote:
>
> Hi,
>
> What's the best way generate UISelectItems from an enumerated type? I'd 
like
> to populate a UISelectOne-based component with enumerated type values 
and 
> back the component with an enumerated type variable instance. This seems
> like it should be a common pattern in JSF with JDK 1.5+. In fact, I'm a
> little surprised we can't specify an enumerated type (or variable of an 
> enumerated type) for f:selectItems.
>
> Thanks for any replies,
> Eric
>
>  _________________________
>
>  CONFIDENTIALITY NOTICE
>
>  The information contained in this e-mail message is intended only for 
the 
> exclusive use of the individual or entity named above and may contain
> information that is privileged, confidential or exempt from disclosure 
under
> applicable law. If the reader of this message is not the intended 
recipient, 
> or the employee or agent responsible for delivery of the message to the
> intended recipient, you are hereby notified that any dissemination,
> distribution or copying of this communication is strictly prohibited. If 
you 
> have received this communication in error, please notify the sender
> immediately by e-mail and delete the material from any computer.  Thank 
you.
> 


_________________________

CONFIDENTIALITY NOTICE

The information contained in this e-mail message is intended only for the 
exclusive use of the individual or entity named above and may contain 
information that is privileged, confidential or exempt from disclosure 
under applicable law. If the reader of this message is not the intended 
recipient, or the employee or agent responsible for delivery of the 
message to the intended recipient, you are hereby notified that any 
dissemination, distribution or copying of this communication is strictly 
prohibited. If you have received this communication in error, please 
notify the sender immediately by e-mail and delete the material from any 
computer.  Thank you.

_________________________

CONFIDENTIALITY NOTICE

The information contained in this e-mail message is intended only for the 
exclusive use of the individual or entity named above and may contain 
information that is privileged, confidential or exempt from disclosure 
under applicable law. If the reader of this message is not the intended 
recipient, or the employee or agent responsible for delivery of the 
message to the intended recipient, you are hereby notified that any 
dissemination, distribution or copying of this communication is strictly 
prohibited. If you have received this communication in error, please 
notify the sender immediately by e-mail and delete the material from any 
computer.  Thank you.

Re: enumerated type and f:selectItems

Posted by er...@novartis.com.
Thanks! If I understand correctly, this converter is for the variable 
which is value-bound to a UIOutput/UIInput-derived classes (e.g. 
UISelectOne), but not for UISelectItems. In other words, is there a way to 
get <f:selectItems/> to output options for all values of the enumerated 
type? I tried:

<f:selectItems value="{#MyClass.selectItems"/>

public Map<MyEnum, String> getSelectItems() {
  Map<MyEnum, String> selectItems = new EnumMap<MyEnum, 
String>(MyEnum.class);
  for (MyEnum o : EnumSet.allOf(MyEnum.class))
    selectItems.put(new Integer(o.ordinal()), o.getName());
  return selectItems;
}

and a few variations on that, without any success.




"Cagatay Civici" <ca...@gmail.com> 
05/22/2007 01:01 PM
Please respond to
"MyFaces Discussion" <us...@myfaces.apache.org>


To
"MyFaces Discussion" <us...@myfaces.apache.org>
cc

Subject
Re: enumerated type and f:selectItems






Hi,

Sandbox15 has an enumconverter too.

http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox15/core/src/main/java/org/apache/myfaces/custom/converter/enumeration/EnumConverter.java?view=markup


Cagatay

On 5/22/07, Andrew Robinson <an...@gmail.com> wrote:
If you use jboss seam there is support for an enum converter and
enum-select item components that do just what you are looking for.

On 5/22/07, eric.jung@novartis.com < eric.jung@novartis.com> wrote:
>
> Hi,
>
> What's the best way generate UISelectItems from an enumerated type? I'd 
like
> to populate a UISelectOne-based component with enumerated type values 
and 
> back the component with an enumerated type variable instance. This seems
> like it should be a common pattern in JSF with JDK 1.5+. In fact, I'm a
> little surprised we can't specify an enumerated type (or variable of an 
> enumerated type) for f:selectItems.
>
> Thanks for any replies,
> Eric
>
>  _________________________
>
>  CONFIDENTIALITY NOTICE
>
>  The information contained in this e-mail message is intended only for 
the 
> exclusive use of the individual or entity named above and may contain
> information that is privileged, confidential or exempt from disclosure 
under
> applicable law. If the reader of this message is not the intended 
recipient, 
> or the employee or agent responsible for delivery of the message to the
> intended recipient, you are hereby notified that any dissemination,
> distribution or copying of this communication is strictly prohibited. If 
you 
> have received this communication in error, please notify the sender
> immediately by e-mail and delete the material from any computer.  Thank 
you.
>


_________________________

CONFIDENTIALITY NOTICE

The information contained in this e-mail message is intended only for the 
exclusive use of the individual or entity named above and may contain 
information that is privileged, confidential or exempt from disclosure 
under applicable law. If the reader of this message is not the intended 
recipient, or the employee or agent responsible for delivery of the 
message to the intended recipient, you are hereby notified that any 
dissemination, distribution or copying of this communication is strictly 
prohibited. If you have received this communication in error, please 
notify the sender immediately by e-mail and delete the material from any 
computer.  Thank you.

Re: enumerated type and f:selectItems

Posted by Cagatay Civici <ca...@gmail.com>.
Hi,

Sandbox15 has an enumconverter too.

http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox15/core/src/main/java/org/apache/myfaces/custom/converter/enumeration/EnumConverter.java?view=markup

Cagatay

On 5/22/07, Andrew Robinson <an...@gmail.com> wrote:
>
> If you use jboss seam there is support for an enum converter and
> enum-select item components that do just what you are looking for.
>
> On 5/22/07, eric.jung@novartis.com <er...@novartis.com> wrote:
> >
> > Hi,
> >
> > What's the best way generate UISelectItems from an enumerated type? I'd
> like
> > to populate a UISelectOne-based component with enumerated type values
> and
> > back the component with an enumerated type variable instance. This seems
> > like it should be a common pattern in JSF with JDK 1.5+. In fact, I'm a
> > little surprised we can't specify an enumerated type (or variable of an
> > enumerated type) for f:selectItems.
> >
> > Thanks for any replies,
> > Eric
> >
> >  _________________________
> >
> >  CONFIDENTIALITY NOTICE
> >
> >  The information contained in this e-mail message is intended only for
> the
> > exclusive use of the individual or entity named above and may contain
> > information that is privileged, confidential or exempt from disclosure
> under
> > applicable law. If the reader of this message is not the intended
> recipient,
> > or the employee or agent responsible for delivery of the message to the
> > intended recipient, you are hereby notified that any dissemination,
> > distribution or copying of this communication is strictly prohibited. If
> you
> > have received this communication in error, please notify the sender
> > immediately by e-mail and delete the material from any computer.  Thank
> you.
> >
>

Re: enumerated type and f:selectItems

Posted by Andrew Robinson <an...@gmail.com>.
If you use jboss seam there is support for an enum converter and
enum-select item components that do just what you are looking for.

On 5/22/07, eric.jung@novartis.com <er...@novartis.com> wrote:
>
> Hi,
>
> What's the best way generate UISelectItems from an enumerated type? I'd like
> to populate a UISelectOne-based component with enumerated type values and
> back the component with an enumerated type variable instance. This seems
> like it should be a common pattern in JSF with JDK 1.5+. In fact, I'm a
> little surprised we can't specify an enumerated type (or variable of an
> enumerated type) for f:selectItems.
>
> Thanks for any replies,
> Eric
>
>  _________________________
>
>  CONFIDENTIALITY NOTICE
>
>  The information contained in this e-mail message is intended only for the
> exclusive use of the individual or entity named above and may contain
> information that is privileged, confidential or exempt from disclosure under
> applicable law. If the reader of this message is not the intended recipient,
> or the employee or agent responsible for delivery of the message to the
> intended recipient, you are hereby notified that any dissemination,
> distribution or copying of this communication is strictly prohibited. If you
> have received this communication in error, please notify the sender
> immediately by e-mail and delete the material from any computer.  Thank you.
>