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:31:04 UTC

svn commit: r683548 - /myfaces/tobago/trunk/extension/facelets/src/main/java/org/apache/myfaces/tobago/facelets/AttributeHandler.java

Author: bommel
Date: Thu Aug  7 01:31:02 2008
New Revision: 683548

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

Modified:
    myfaces/tobago/trunk/extension/facelets/src/main/java/org/apache/myfaces/tobago/facelets/AttributeHandler.java

Modified: myfaces/tobago/trunk/extension/facelets/src/main/java/org/apache/myfaces/tobago/facelets/AttributeHandler.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/extension/facelets/src/main/java/org/apache/myfaces/tobago/facelets/AttributeHandler.java?rev=683548&r1=683547&r2=683548&view=diff
==============================================================================
--- myfaces/tobago/trunk/extension/facelets/src/main/java/org/apache/myfaces/tobago/facelets/AttributeHandler.java (original)
+++ myfaces/tobago/trunk/extension/facelets/src/main/java/org/apache/myfaces/tobago/facelets/AttributeHandler.java Thu Aug  7 01:31:02 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;
@@ -107,6 +109,8 @@
           // TODO jsf 1.2
           ((EditableValueHolder) parent).setValueChangeListener(new LegacyMethodBinding(methodExpression));
         }
+      } else if (parent instanceof ValueHolder && TobagoConstants.ATTR_CONVERTER.equals(nameValue)) {
+        setConverter(faceletContext, parent, nameValue);
       } else if (parent instanceof ActionSource && TobagoConstants.ATTR_ACTION.equals(nameValue)) {
         MethodExpression action = getMethodExpression(faceletContext, String.class, ComponentUtil.ACTION_ARGS);
         if (action != null) {
@@ -146,4 +150,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);
+    }
+  }
 }