Hi.
Localized Enums can be a hassle.

Here is my solution:

in your configured messagebundle add your enum.

#messages.properties
example.enums.VisibleStatus.VISIBLE=visible
example.enums.VisibleStatus.HIDDEN=hidden


package example.enums;

public enum VisibleStatus {
   VISIBLE,
   HIDDEN;
}

package example.converter;

@SuppressWarnings("unchecked")
public class EnumConverter implements Converter {

   private Class type = Enum.class;

   public final static String CONVERTER_ID = "example.EnumConverter";

   /**
    *
    * @param facesContext The facescontext
    * @param uiComponent The uiComponent
* @param value The colon separeted string to be mapped to the Enum class
    * @return a enum
    * @throws ConverterException
    */
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) throws ConverterException {
       if (value == null) return null;
       String bundle = facesContext.getApplication().getMessageBundle();
       Locale loc = facesContext.getViewRoot().getLocale();
       ResourceBundle rb = ResourceBundle.getBundle(bundle, loc);
       Enumeration<String> keys = rb.getKeys();
       Enum e = null;
       while (keys.hasMoreElements()) {
           String elem = keys.nextElement();
           String val = rb.getString(elem);
           if (val.equals(value)) {
               int pos = elem.lastIndexOf(".");
               if (pos != -1) {
                   String clazzName = elem.substring(0, pos);
                   String sval = elem.substring(pos + 1);
                   Class clazz = Enum.class;
                   try {
                       clazz = Class.forName(clazzName);
                   } catch (ClassNotFoundException ex) {
                       //ignore
                   }
                   e = Enum.valueOf(clazz, sval);
                   if (e != null) break;
               }
           }
       }
       return e;
   }

   /**
    *
    * @param facesContext The facesContext
    * @param uiComponent The uiComponent
    * @param value The enum to be mapped to a string
    * @return enum as stringrepresentation
    * @throws ConverterException
    */
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) throws ConverterException {
       if (value == null || !type.isInstance(value)) return null;
       Enum en = (Enum) value;
       Class c = en.getDeclaringClass();
       String mb = facesContext.getApplication().getMessageBundle();
       Locale locale = facesContext.getViewRoot().getLocale();
       ResourceBundle rb = ResourceBundle.getBundle(mb, locale);
       String out = c.getName() + "." + en.name();
       return rb.getString(out);
   }
}

There might be a more efficient solution, however i have found that this works quite well.

Regards.

Erlend


Simon Lessard wrote:
Hello,

Also note that, starting with JSF 1.2, enums are natively supported (a default Enum converter is registered).


Regards,

~ Simon

On 8/7/07, * Andrew Robinson* <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote:

    If you use jboss seam, they also have select item and select items
    components as well as converter support for enumerations

    -Andre

    On 8/7/07, Gerald Müllan <[EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]>> wrote:
    > Hi,
    >
    > i am doing it always via using tomahawks t:selectItems:
    >
    > <t:selectOneMenu value="#{bean.type}">
    >             <t:selectItems var="enum" value="#{
    otherBean.enumValues}"
    >                                  itemLabel="#{enum.name
    <http://enum.name>}"
    >                                  itemValue="#{enum.type}"/>
    > </t:selectOneMenu>
    >
    > and the otherBean:
    >
    > public List getEnumValues()
    > {
    >         return Arrays.asList(EnumClass.values());
    > }
    >
    > Hope this helps,
    >
    > cheers - Gerald
    >
    > On 8/7/07, Wolf Benz <[EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]>> wrote:
    > > I just discover there is a class called java.lang.EnumSet.
    > > Sandeep - have you tried that already?
    > > -Wolf
    > >
    > > On 8/7/07, sandeep gururaj <[EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]>> wrote:
    > > >
    > > > Well, I haven't been lucky! If it is possible, do let me
    know so that I
    > > > can re-factor my code.
    > > >
    > > > ~Sandeep
    > > >
    > > > -----Original Message-----
    > > > From: Wolf Benz [mailto: [EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]>]
    > > > Sent: Tuesday, August 07, 2007 5:40 PM
    > > > To: MyFaces Discussion
    > > > Subject: SelectOneMenu & Enums in "items" var possible?
    > > >
    > > > Hi List,
    > > >
    > > > Does anyone know whether you can load an Enum into a
    SelectOneMenu's
    > > > items variable?
    > > > So smth like:
    > > > <h:selectOneMenu value="#{ jsfBean.beanVar.varOfEnumType}">
    > > >         <f:selectItem itemLabel="#{labels.choose_one}"
    > > > itemValue="choose_one"/>
    > > >         <f:selectItems value="#{ jsfBean.model.EnumClass}" />
    > > > </h:selectOneMenu>
    > > >
    > > > -Wolf
    > > >
    > >
    >
    >
    > --
    > http://www.irian.at <http://www.irian.at>
    >
    > Your JSF powerhouse -
    > JSF Consulting, Development and
    > Courses in English and German
    >
    > Professional Support for Apache MyFaces
    >



Reply via email to