[seam-commits] Seam SVN: r9285 - in trunk: examples/ui/view and 5 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Mon Oct 13 17:54:29 EDT 2008


Author: danielc.roth
Date: 2008-10-13 17:54:29 -0400 (Mon, 13 Oct 2008)
New Revision: 9285

Added:
   trunk/examples/ui/view/equalityValidatorWConvert.xhtml
   trunk/ui/src/main/java/org/jboss/seam/ui/component/UIEqualityValidator.java
   trunk/ui/src/main/java/org/jboss/seam/ui/renderkit/EqualityValidatorRendererBase.java
Modified:
   trunk/examples/ui/src/org/jboss/seam/example/ui/EqualityValidatorBean.java
   trunk/examples/ui/view/equalityValidator.xhtml
   trunk/examples/ui/view/template.xhtml
   trunk/ui/src/main/config/component/equalityValidator.xml
   trunk/ui/src/main/java/org/jboss/seam/ui/util/cdk/Messages.java
   trunk/ui/src/main/java/org/jboss/seam/ui/validator/EqualityValidator.java
Log:
JBSEAM-2809

Modified: trunk/examples/ui/src/org/jboss/seam/example/ui/EqualityValidatorBean.java
===================================================================
--- trunk/examples/ui/src/org/jboss/seam/example/ui/EqualityValidatorBean.java	2008-10-13 20:38:08 UTC (rev 9284)
+++ trunk/examples/ui/src/org/jboss/seam/example/ui/EqualityValidatorBean.java	2008-10-13 21:54:29 UTC (rev 9285)
@@ -1,5 +1,7 @@
 package org.jboss.seam.example.ui;
 
+import java.util.Date;
+
 import org.jboss.seam.ScopeType;
 import org.jboss.seam.annotations.In;
 import org.jboss.seam.annotations.Name;
@@ -13,11 +15,45 @@
 public class EqualityValidatorBean
 {
 
-   private String name;
-
    @In
    private StatusMessages statusMessages;
 
+   private String name;
+
+   private Date date;
+
+   public void check()
+   {
+      if (Strings.isEmpty(name))
+      {
+         statusMessages.addToControl("name", Severity.WARN, "Enter a name!");
+      }
+      else {
+         statusMessages.addToControl("name", Severity.INFO, "OK!");
+      }
+   }
+   
+   public void checkDate()
+   {
+      if (date==null)
+      {
+         statusMessages.addToControl("date", Severity.WARN, "Enter a date!");
+      }
+      else {
+         statusMessages.addToControl("date", Severity.INFO, "OK!");
+      }
+   }   
+   
+   public Date getDate()
+   {
+      return date;
+   }
+   
+   public void setDate(Date date)
+   {
+      this.date = date;
+   }
+   
    public String getName()
    {
       return name;
@@ -28,12 +64,4 @@
       this.name = name;
    }
 
-   public void check()
-   {
-      if (Strings.isEmpty(name))
-      {
-         statusMessages.addToControl("name", Severity.WARN, "Enter a name!");
-      }
-   }
-
 }

Modified: trunk/examples/ui/view/equalityValidator.xhtml
===================================================================
--- trunk/examples/ui/view/equalityValidator.xhtml	2008-10-13 20:38:08 UTC (rev 9284)
+++ trunk/examples/ui/view/equalityValidator.xhtml	2008-10-13 21:54:29 UTC (rev 9285)
@@ -7,18 +7,19 @@
 	template="template.xhtml">
 	<ui:param name="tagName" value="s:validateEquality" />
 	<ui:define name="body">
-		<p>Validates that two inputs are equal</p>
+		<p>Validates that two java.lang.String inputs are equal</p>
 		<h:form>
 			<h:panelGrid columns="3">
 				<s:label for="name">Name</s:label>
 				<h:inputText id="name" value="#{equalityValidatorBean.name}" />
 				<h:message for="name" />
 				<s:label for="nameVerification">Name Verification</s:label>
-				<h:inputText id="nameVerification" value=" ">
-					<s:validateEquality for="name" />
+				<h:inputText id="nameVerification">
+					<s:validateEquality for="name" message="Strings do not match!" />
 				</h:inputText>
 				<h:message for="nameVerification" />
-				<h:commandButton action="#{equalityValidatorBean.check}" value="Check name" />
+				<h:commandButton action="#{equalityValidatorBean.check}"
+					value="Check name" />
 			</h:panelGrid>
 		</h:form>
 	</ui:define>

Added: trunk/examples/ui/view/equalityValidatorWConvert.xhtml
===================================================================
--- trunk/examples/ui/view/equalityValidatorWConvert.xhtml	                        (rev 0)
+++ trunk/examples/ui/view/equalityValidatorWConvert.xhtml	2008-10-13 21:54:29 UTC (rev 9285)
@@ -0,0 +1,31 @@
+<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<ui:composition xmlns="http://www.w3.org/1999/xhtml"
+	xmlns:ui="http://java.sun.com/jsf/facelets"
+	xmlns:f="http://java.sun.com/jsf/core"
+	xmlns:h="http://java.sun.com/jsf/html"
+	xmlns:s="http://jboss.com/products/seam/taglib"
+	template="template.xhtml">
+	<ui:param name="tagName" value="s:validateEquality" />
+	<ui:define name="body">
+		<p>Validates that two java.util.Date inputs are equal</p>
+		<h:form>
+			<h:panelGrid columns="3">
+				<s:label for="date">Date (yyyy-MM-dd)</s:label>
+				<h:inputText id="date" value="#{equalityValidatorBean.date}">
+					<f:convertDateTime pattern="yyyy-MM-dd" />
+				</h:inputText>
+				<h:message for="date" />
+				<s:label for="dateVerification">Date Verification (yyyy-MM-dd)</s:label>
+				<h:inputText id="dateVerification">
+					<f:convertDateTime pattern="yyyy-MM-dd" />
+					<s:validateEquality for="date" />
+				</h:inputText>
+				<h:message for="dateVerification" />
+				<h:commandButton action="#{equalityValidatorBean.checkDate}"
+					value="Check date" />
+			</h:panelGrid>
+		</h:form>
+
+
+	</ui:define>
+</ui:composition>

Modified: trunk/examples/ui/view/template.xhtml
===================================================================
--- trunk/examples/ui/view/template.xhtml	2008-10-13 20:38:08 UTC (rev 9284)
+++ trunk/examples/ui/view/template.xhtml	2008-10-13 21:54:29 UTC (rev 9285)
@@ -57,7 +57,7 @@
 }
 
 .menu {
-	width: 200px;
+	width: 220px;
 	float: left;
 	padding: 10px;
 }
@@ -102,6 +102,10 @@
 			<code>s:validateEquality</code>
 		</s:link></li>
 		
+		<li><s:link view="/equalityValidatorWConvert.xhtml">
+			<code>s:validateEquality(2)</code>
+		</s:link></li>		
+		
 		<li><s:span title="#{spanTitle}" style="font-weight:bold">
 		    <code>s:span</code>
 		  </s:span>

Modified: trunk/ui/src/main/config/component/equalityValidator.xml
===================================================================
--- trunk/ui/src/main/config/component/equalityValidator.xml	2008-10-13 20:38:08 UTC (rev 9284)
+++ trunk/ui/src/main/config/component/equalityValidator.xml	2008-10-13 21:54:29 UTC (rev 9285)
@@ -1,16 +1,43 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE components PUBLIC "-//AJAX4JSF//CDK Generator config/EN"  "http://jboss.org/jbossrichfaces/component-config.dtd" >
 <components>
-	<validator generate="false">
-		<id>org.jboss.seam.ui.EqualityValidator</id>
-		<classname>org.jboss.seam.ui.validator.EqualityValidator</classname>
+
+	<component>
+		<name>org.jboss.seam.ui.EqualityValidator</name>
+		<family>org.jboss.seam.ui.EqualityValidator</family>
+		<classname>org.jboss.seam.ui.component.html.HtmlEqualityValidator</classname>
+		<superclass>org.jboss.seam.ui.component.UIEqualityValidator</superclass>
 		<description>
-			<![CDATA[Validate that the value of two components are equal]]>
+			<![CDATA[Validate all child JSF input fields against the bound propertys using Hibernate Validator.]]>
 		</description>
+		<renderer generate="false" override="false">
+			<name>org.jboss.seam.ui.EqualityValidatorRenderer</name>
+			<classname>org.jboss.seam.ui.renderkit.EqualityValidatorRendererBase</classname>
+		</renderer>
 		<tag>
 			<name>validateEquality</name>
-			<classname>org.jboss.seam.ui.taglib.ModelValidatorTag</classname>
-			<superclass>javax.faces.webapp.ValidatorELTag</superclass>
+			<classname>org.jboss.seam.ui.taglib.EqualityValidatorTag</classname>
+			<superclass>
+				org.jboss.seam.ui.util.cdk.UIComponentTagBase
+			</superclass>
 		</tag>
-	</validator>
+		<property>
+			<name>for</name>
+			<classname>java.lang.String</classname>
+			<description>Id of component to validate against</description>
+		</property>
+		<property>
+			<name>message</name>
+			<classname>java.lang.String</classname>
+			<description>Error message to show</description>
+		</property>
+		<property>
+			<name>messageId</name>
+			<classname>java.lang.String</classname>
+			<description>Message id to use on failure</description>
+		</property>
+
+	</component>
+
 </components>
+

Added: trunk/ui/src/main/java/org/jboss/seam/ui/component/UIEqualityValidator.java
===================================================================
--- trunk/ui/src/main/java/org/jboss/seam/ui/component/UIEqualityValidator.java	                        (rev 0)
+++ trunk/ui/src/main/java/org/jboss/seam/ui/component/UIEqualityValidator.java	2008-10-13 21:54:29 UTC (rev 9285)
@@ -0,0 +1,25 @@
+package org.jboss.seam.ui.component;
+
+import javax.faces.component.UIComponentBase;
+
+/**
+ * UIComponent for validator 
+ * 
+ * @author Daniel Roth
+ */
+public abstract class UIEqualityValidator extends UIComponentBase
+{
+
+   public abstract String getFor();
+
+   public abstract void setFor(String forId);
+
+   public abstract String getMessage();
+
+   public abstract void setMessage(String message);
+
+   public abstract String getMessageId();
+
+   public abstract void setMessageId(String messageId);
+
+}

Added: trunk/ui/src/main/java/org/jboss/seam/ui/renderkit/EqualityValidatorRendererBase.java
===================================================================
--- trunk/ui/src/main/java/org/jboss/seam/ui/renderkit/EqualityValidatorRendererBase.java	                        (rev 0)
+++ trunk/ui/src/main/java/org/jboss/seam/ui/renderkit/EqualityValidatorRendererBase.java	2008-10-13 21:54:29 UTC (rev 9285)
@@ -0,0 +1,70 @@
+package org.jboss.seam.ui.renderkit;
+
+import java.io.IOException;
+
+import javax.faces.component.EditableValueHolder;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseWriter;
+import javax.faces.validator.Validator;
+
+import org.jboss.seam.ui.component.UIEqualityValidator;
+import org.jboss.seam.ui.util.cdk.RendererBase;
+import org.jboss.seam.ui.validator.EqualityValidator;
+
+/**
+ * This class mainly does some validation "hook-in"
+ * 
+ * @author Daniel Roth
+ * 
+ */
+public class EqualityValidatorRendererBase extends RendererBase
+{
+
+   @Override
+   protected Class getComponentClass()
+   {
+      return UIEqualityValidator.class;
+   }
+
+   @Override
+   protected void doEncodeChildren(ResponseWriter writer, FacesContext context, UIComponent component) throws IOException
+   {
+      UIEqualityValidator ev = (UIEqualityValidator) component;
+      EditableValueHolder evh = null;
+      if (ev != null && ev.getParent() instanceof EditableValueHolder)
+      {
+         evh = (EditableValueHolder) ev.getParent();
+      }
+
+      if (evh == null)
+         throw new IllegalArgumentException("validateEquality tag must be nested in an EditableValueHolder (\"input tag\")");
+
+      if (!hasEqualityValidator(evh))
+      {
+         evh.addValidator(new EqualityValidator(ev.getFor(), ev.getMessage(), ev.getMessageId()));
+         evh.setRequired(true);
+      }
+
+      renderChildren(context, component);
+   }
+
+   private boolean hasEqualityValidator(EditableValueHolder evh)
+   {
+      for (Validator validator : evh.getValidators())
+      {
+         if (validator instanceof EqualityValidator)
+         {
+            return true;
+         }
+      }
+      return false;
+   }
+
+   @Override
+   public boolean getRendersChildren()
+   {
+      return true;
+   }
+
+}

Modified: trunk/ui/src/main/java/org/jboss/seam/ui/util/cdk/Messages.java
===================================================================
--- trunk/ui/src/main/java/org/jboss/seam/ui/util/cdk/Messages.java	2008-10-13 20:38:08 UTC (rev 9284)
+++ trunk/ui/src/main/java/org/jboss/seam/ui/util/cdk/Messages.java	2008-10-13 21:54:29 UTC (rev 9285)
@@ -13,7 +13,7 @@
 	
 	private static synchronized ResourceBundle getBundle() {
 		ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
-		ResourceBundle bundle = (ResourceBundle) bundles.get(contextClassLoader);
+		ResourceBundle bundle = bundles.get(contextClassLoader);
 		if (bundle == null) {
 			bundle = ResourceBundle.getBundle(BUNDLE_NAME, Locale.getDefault(), contextClassLoader);
 			bundles.put(contextClassLoader, bundle);

Modified: trunk/ui/src/main/java/org/jboss/seam/ui/validator/EqualityValidator.java
===================================================================
--- trunk/ui/src/main/java/org/jboss/seam/ui/validator/EqualityValidator.java	2008-10-13 20:38:08 UTC (rev 9284)
+++ trunk/ui/src/main/java/org/jboss/seam/ui/validator/EqualityValidator.java	2008-10-13 21:54:29 UTC (rev 9285)
@@ -22,34 +22,34 @@
  * Validate two fields are equal
  * 
  * @author pmuir
- *
+ * @author Daniel Roth
+ * 
  */
 public class EqualityValidator implements Validator, StateHolder
 {
-   
+
    private static LogProvider log = Logging.getLogProvider(EqualityValidator.class);
-   
+
    public static final String MESSAGE_ID = "org.jboss.seam.ui.validator.NOT_EQUAL";
-   
+
    public static final String VALIDATOR_ID = "org.jboss.seam.ui.validator.Equality";
 
    private String forId;
-   
    private String message;
    private String messageId;
-   
-   public EqualityValidator() 
+
+   public EqualityValidator()
    {
-      this.message = "Value does not equal that in #0";
+      this.message = "Value does not equal that in '#0'";
       this.messageId = MESSAGE_ID;
    }
-   
+
    public EqualityValidator(String forId)
    {
       this();
       setFor(forId);
    }
-   
+
    public EqualityValidator(String forId, String message, String messageId)
    {
       this(forId);
@@ -62,13 +62,9 @@
          setMessageId(messageId);
       }
    }
-   
+
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException
    {
-      if (!(component instanceof EditableValueHolder))
-      {
-         throw new FacesException("Must attach an equality validator to an input component");
-      }
       String forId = getFor();
       if (forId == null)
       {
@@ -89,32 +85,32 @@
          }
       }
    }
-   
+
    public String getFor()
    {
       return forId;
-   } 
-   
+   }
+
    public void setFor(String forId)
    {
       this.forId = forId;
    }
-   
+
    public String getMessage()
    {
       return message;
    }
-   
+
    public void setMessage(String message)
    {
       this.message = message;
    }
-   
+
    public String getMessageId()
    {
       return messageId;
    }
-   
+
    public void setMessageId(String messageId)
    {
       this.messageId = messageId;
@@ -127,7 +123,7 @@
 
    public void restoreState(FacesContext context, Object state)
    {
-      Object[] fields = (Object []) state;
+      Object[] fields = (Object[]) state;
       forId = (String) fields[0];
       message = (String) fields[1];
       messageId = (String) fields[2];
@@ -147,22 +143,22 @@
       // No-op
    }
 
-   
    /**
-    * Simple data strcuture to hold info on the "other" component
+    * Simple data structure to hold info on the "other" component
+    * 
     * @author pmuir
-    *
+    * 
     */
-   private class OtherComponent 
+   private class OtherComponent
    {
-      
+
       private FacesContext context;
       private UIComponent component;
       private EditableValueHolder editableValueHolder;
-      
+
       private Renderer renderer;
       private Converter converter;
-      
+
       public OtherComponent(FacesContext facesContext, UIComponent component)
       {
          this.component = component;
@@ -175,23 +171,23 @@
          initRenderer();
          initConverter();
       }
-      
-      private void initRenderer() 
+
+      private void initRenderer()
       {
          if (renderer == null)
          {
             String rendererType = component.getRendererType();
-            if (rendererType != null) 
+            if (rendererType != null)
             {
                renderer = context.getRenderKit().getRenderer(component.getFamily(), rendererType);
-               if (null == renderer) 
+               if (null == renderer)
                {
                   log.trace("Can't get Renderer for type " + rendererType);
                }
-            } 
+            }
             else
             {
-               if (log.isTraceEnabled()) 
+               if (log.isTraceEnabled())
                {
                   String id = component.getId();
                   id = (null != id) ? id : component.getClass().getName();
@@ -200,89 +196,106 @@
             }
          }
       }
-      
-      private void initConverter() {
+
+      private void initConverter()
+      {
          converter = editableValueHolder.getConverter();
-         if (converter != null) {
-             return;
+         if (converter != null)
+         {
+            return;
          }
 
          ValueExpression valueExpression = component.getValueExpression("value");
-         if (valueExpression == null) {
-             return;
+         if (valueExpression == null)
+         {
+            return;
          }
 
          Class converterType;
-         try {
-             converterType = valueExpression.getType(context.getELContext());
+         try
+         {
+            converterType = valueExpression.getType(context.getELContext());
          }
-         catch (ELException e) {
-             throw new FacesException(e);
+         catch (ELException e)
+         {
+            throw new FacesException(e);
          }
 
          // if converterType is null, String, or Object, assume
          // no conversion is needed
-         if (converterType == null || converterType == String.class || converterType == Object.class) 
+         if (converterType == null || converterType == String.class || converterType == Object.class)
          {
-             return;
+            return;
          }
 
          // if getType returns a type for which we support a default
          // conversion, acquire an appropriate converter instance.
-         try 
+         try
          {
-             Application application = context.getApplication();
-             converter = application.createConverter(converterType);
+            Application application = context.getApplication();
+            converter = application.createConverter(converterType);
          }
-         catch (Exception e) 
+         catch (Exception e)
          {
             throw new FacesException(e);
          }
       }
-      
-      private Object getConvertedValue(Object newSubmittedValue) throws ConverterException 
+
+      private Object getConvertedValue(Object newSubmittedValue) throws ConverterException
       {
-         
+
          Object newValue;
 
-         if (renderer != null) 
+         if (renderer != null)
          {
             newValue = renderer.getConvertedValue(context, component, newSubmittedValue);
-         } 
-         else if (newSubmittedValue instanceof String) 
+         }
+         else if (newSubmittedValue instanceof String)
          {
-            // If there's no Renderer, and we've got a String, run it through the Converter (if any)
-            if (converter != null) {
-               newValue = converter.getAsObject(context, component,
-                     (String) newSubmittedValue);
-            } 
+            // If there's no Renderer, and we've got a String, run it through
+            // the Converter (if any)
+            if (converter != null)
+            {
+               newValue = converter.getAsObject(context, component, (String) newSubmittedValue);
+            }
             else
             {
                newValue = newSubmittedValue;
             }
-         } 
-         else 
+         }
+         else
          {
             newValue = newSubmittedValue;
          }
          return newValue;
       }
-      
+
       public Object getValue()
       {
+         /**
+          * If conversion already is done, return value
+          */
+         if (editableValueHolder.isLocalValueSet())
+         {
+            return editableValueHolder.getValue();
+         }
+
+         /**
+          * Convert submittet value
+          */
          Object submittedValue = editableValueHolder.getLocalValue();
-         if (submittedValue == null) 
+         if (submittedValue == null)
          {
             return null;
          }
 
          Object newValue = null;
 
-         try 
+         try
          {
             newValue = getConvertedValue(submittedValue);
          }
-         catch (ConverterException ce) 
+         catch (ConverterException ce)
          {
             // Any errors will be attached by JSF
             return null;
@@ -290,6 +303,6 @@
 
          return newValue;
       }
-      
+
    }
 }




More information about the seam-commits mailing list