You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by bo...@apache.org on 2008/08/07 10:32:01 UTC

svn commit: r683549 - /myfaces/tobago/branches/tobago-1.0.x/extension/facelets/src/main/java/org/apache/myfaces/tobago/facelets/AttributeHandler.java

Author: bommel
Date: Thu Aug  7 01:32:01 2008
New Revision: 683549

URL: http://svn.apache.org/viewvc?rev=683549&view=rev
Log:
(TOBAGO-693) Allow setting the converter with tc:attribute in facelets, similar setting the action or actionlistener

Modified:
    myfaces/tobago/branches/tobago-1.0.x/extension/facelets/src/main/java/org/apache/myfaces/tobago/facelets/AttributeHandler.java

Modified: myfaces/tobago/branches/tobago-1.0.x/extension/facelets/src/main/java/org/apache/myfaces/tobago/facelets/AttributeHandler.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/branches/tobago-1.0.x/extension/facelets/src/main/java/org/apache/myfaces/tobago/facelets/AttributeHandler.java?rev=683549&r1=683548&r2=683549&view=diff
==============================================================================
--- myfaces/tobago/branches/tobago-1.0.x/extension/facelets/src/main/java/org/apache/myfaces/tobago/facelets/AttributeHandler.java (original)
+++ myfaces/tobago/branches/tobago-1.0.x/extension/facelets/src/main/java/org/apache/myfaces/tobago/facelets/AttributeHandler.java Thu Aug  7 01:32:01 2008
@@ -22,9 +22,11 @@
 import javax.el.ExpressionFactory;
 import javax.el.ValueExpression;
 import javax.faces.FacesException;
+import javax.faces.convert.Converter;
 import javax.faces.component.UIComponent;
 import javax.faces.component.EditableValueHolder;
 import javax.faces.component.ActionSource;
+import javax.faces.component.ValueHolder;
 
 import com.sun.facelets.FaceletContext;
 import com.sun.facelets.el.ELAdaptor;
@@ -93,6 +95,8 @@
           // TODO jsf 1.2
           ((EditableValueHolder) parent).setValidator(new LegacyMethodBinding(methodExpression));
         }
+      } else if (parent instanceof ValueHolder && TobagoConstants.ATTR_CONVERTER.equals(nameValue)) {
+        setConverter(faceletContext, parent, nameValue);  
       } else if (parent instanceof EditableValueHolder
           && TobagoConstants.ATTR_VALUE_CHANGE_LISTENER.equals(nameValue)) {
         MethodExpression methodExpression =
@@ -140,4 +144,29 @@
     }
     return null;
   }
+
+  private void setConverter(FaceletContext faceletContext, UIComponent parent, String nameValue) {
+    // in a composition may be we get the converter expression string from the current variable mapper
+    // the expression can be empty
+    // in this case return nothing
+    if (value.getValue().startsWith("${")) {
+      String myValue = value.getValue().replaceAll("(\\$\\{)|(\\})", "");
+      ValueExpression expression = faceletContext.getVariableMapper().resolveVariable(myValue);
+      if (expression != null) {
+        setConverter(faceletContext, parent, nameValue, expression);
+      }
+    } else {
+      setConverter(faceletContext, parent, nameValue, value.getValueExpression(faceletContext, Object.class));
+    }
+  }
+
+  private void setConverter(FaceletContext faceletContext, UIComponent parent, String nameValue, ValueExpression expression) {
+    if (expression.isLiteralText()) {
+      Converter converter =
+          faceletContext.getFacesContext().getApplication().createConverter(expression.getExpressionString());
+      ((ValueHolder) parent).setConverter(converter);
+    } else {
+      ELAdaptor.setExpression(parent, nameValue, expression);
+    }
+  }
 }