[jboss-cvs] jboss-seam/src/ui/org/jboss/seam/ui ...

Shane Bryzak sbryzak at redhat.com
Sun May 27 21:22:08 EDT 2007


  User: sbryzak2
  Date: 07/05/27 21:22:08

  Modified:    src/ui/org/jboss/seam/ui  EnumConverter.java
  Log:
  JBSEAM-1351
  
  Revision  Changes    Path
  1.4       +49 -21    jboss-seam/src/ui/org/jboss/seam/ui/EnumConverter.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: EnumConverter.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/src/ui/org/jboss/seam/ui/EnumConverter.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -b -r1.3 -r1.4
  --- EnumConverter.java	13 May 2007 08:33:36 -0000	1.3
  +++ EnumConverter.java	28 May 2007 01:22:08 -0000	1.4
  @@ -1,32 +1,60 @@
   package org.jboss.seam.ui;
   
  +import java.util.Collection;
  +
  +import javax.el.ValueExpression;
   import javax.faces.component.UIComponent;
   import javax.faces.context.FacesContext;
   import javax.faces.convert.Converter;
   import javax.faces.convert.ConverterException;
   
  -public class EnumConverter
  -    implements Converter
  +public class EnumConverter implements Converter
   {
  -    public Object getAsObject(FacesContext context,
  -                              UIComponent comp,
  -                              String value)
  +   public Object getAsObject(FacesContext context, UIComponent comp, String value)
           throws ConverterException
       {
  -        Class enumType = comp.getValueExpression("value").getExpectedType();
  +      ValueExpression expr = comp.getValueExpression("value");
  +
  +      Class enumType = expr.getType(context.getELContext());
  +      if (enumType.isEnum())
  +      {
           return Enum.valueOf(enumType, value);
       }
  +      else
  +      {
  +         for (Object child : comp.getChildren())
  +         {
  +            if (child instanceof UIComponent)
  +            {
  +               UIComponent c = (UIComponent) child;
  +               expr = c.getValueExpression("value");
  +               Object val = expr.getValue(context.getELContext());
  +               if (val == null)
  +               {
  +                  throw new ConverterException("Cannot get items");
  +               }
   
  -    public String getAsString(FacesContext context,
  -                              UIComponent component,
  -                              Object object)
  -        throws ConverterException
  +               Class t = val.getClass();
  +               if (t.isArray() && t.getComponentType().isEnum())
       {
  -        if (object == null) {
  -            return null;
  +                  return Enum.valueOf(t.getComponentType(), value);
  +               }
  +               else if (val instanceof Collection)
  +               {
  +                  t = ((Collection) val).iterator().next().getClass();
  +                  return Enum.valueOf(t, value);
  +               }
  +            }
  +         }
           }
   
  -        return ((Enum) object).name();
  +      throw new ConverterException("Unable to find selectItems with enum values.");
  +   }
  +
  +   public String getAsString(FacesContext context, UIComponent component, Object object)
  +      throws ConverterException
  +   {
  +      return object == null ? null : ((Enum) object).name();
       }
   
   }
  
  
  



More information about the jboss-cvs-commits mailing list