[seam-commits] Seam SVN: r9676 - in trunk: examples/ui/src/org/jboss/seam/example/ui and 5 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Sat Nov 29 19:44:50 EST 2008


Author: danielc.roth
Date: 2008-11-29 19:44:50 -0500 (Sat, 29 Nov 2008)
New Revision: 9676

Modified:
   trunk/doc/Seam_Reference_Guide/en-US/Controls.xml
   trunk/examples/ui/src/org/jboss/seam/example/ui/EqualityValidatorBean.java
   trunk/examples/ui/view/equalityValidator.xhtml
   trunk/ui/src/main/config/component/equalityValidator.xml
   trunk/ui/src/main/java/org/jboss/seam/ui/component/UIEqualityValidator.java
   trunk/ui/src/main/java/org/jboss/seam/ui/renderkit/EqualityValidatorRendererBase.java
   trunk/ui/src/main/java/org/jboss/seam/ui/validator/EqualityValidator.java
Log:
JBSEAM-3720

Modified: trunk/doc/Seam_Reference_Guide/en-US/Controls.xml
===================================================================
--- trunk/doc/Seam_Reference_Guide/en-US/Controls.xml	2008-11-29 17:12:07 UTC (rev 9675)
+++ trunk/doc/Seam_Reference_Guide/en-US/Controls.xml	2008-11-30 00:44:50 UTC (rev 9676)
@@ -481,7 +481,7 @@
             <para><emphasis>Description</emphasis></para>
             <para>
 				Tag to nest inside an input control to validate that its parent's
-				value is the same as the referenced control's value.
+				value is equal (or not equal!) to the referenced control's value.
             </para>
             
             <para><emphasis>Attributes</emphasis></para>
@@ -501,6 +501,44 @@
                      <literal>messageId</literal> &#8212; Message id to show on failure.
                   </para>
                </listitem>
+               <listitem>
+                  <para>
+                     <literal>operator</literal> &#8212; What operator to use when comparing the values
+					 Valid operators are:
+					 <itemizedlist>
+                        <listitem>
+                           <para>
+                              <literal>equal</literal> &#8212; Validates that value.equals(forValue)
+						   </para>
+						 </listitem>
+                        <listitem>
+                           <para>
+                              <literal>not_equal</literal> &#8212; Validates that !value.equals(forValue)
+						   </para>
+						 </listitem>
+                        <listitem>
+                           <para>
+                              <literal>greater</literal> &#8212; <![CDATA[Validates that ((Comparable)value).compareTo(forValue) > 0]]>
+						   </para>
+						 </listitem>
+                        <listitem>
+                           <para>
+                              <literal>greater_or_equal</literal> &#8212; <![CDATA[Validates that ((Comparable)value).compareTo(forValue) >= 0]]>
+						   </para>
+						 </listitem>
+                        <listitem>
+                           <para>
+                              <literal>less</literal> &#8212; <![CDATA[Validates that ((Comparable)value).compareTo(forValue) < 0]]>
+						   </para>
+						 </listitem>
+                        <listitem>
+                           <para>
+                              <literal>less_or_equal</literal> &#8212; <![CDATA[Validates that ((Comparable)value).compareTo(forValue) <= 0]]>
+						   </para>
+						 </listitem>
+					 </itemizedlist>
+                  </para>
+               </listitem>			   
             </itemizedlist>
             
             <para><emphasis>Usage</emphasis></para>

Modified: trunk/examples/ui/src/org/jboss/seam/example/ui/EqualityValidatorBean.java
===================================================================
--- trunk/examples/ui/src/org/jboss/seam/example/ui/EqualityValidatorBean.java	2008-11-29 17:12:07 UTC (rev 9675)
+++ trunk/examples/ui/src/org/jboss/seam/example/ui/EqualityValidatorBean.java	2008-11-30 00:44:50 UTC (rev 9676)
@@ -20,6 +20,8 @@
 
    private String name;
 
+   private Long age;
+
    private Date date;
 
    public void check()
@@ -28,32 +30,34 @@
       {
          statusMessages.addToControl("name", Severity.WARN, "Enter a name!");
       }
-      else {
+      else
+      {
          statusMessages.addToControl("name", Severity.INFO, "OK!");
       }
    }
-   
+
    public void checkDate()
    {
-      if (date==null)
+      if (date == null)
       {
          statusMessages.addToControl("date", Severity.WARN, "Enter a date!");
       }
-      else {
+      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;
@@ -64,4 +68,14 @@
       this.name = name;
    }
 
+   public Long getAge()
+   {
+      return age;
+   }
+
+   public void setAge(Long age)
+   {
+      this.age = age;
+   }
+
 }

Modified: trunk/examples/ui/view/equalityValidator.xhtml
===================================================================
--- trunk/examples/ui/view/equalityValidator.xhtml	2008-11-29 17:12:07 UTC (rev 9675)
+++ trunk/examples/ui/view/equalityValidator.xhtml	2008-11-30 00:44:50 UTC (rev 9676)
@@ -1,26 +1,37 @@
 <!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: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.lang.String inputs are equal</p>
 		<h:form>
+			<p>Validates that two java.lang.String inputs are equal</p>
 			<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">
-					<s:validateEquality for="name" message="Strings do not match!" />
+					<s:validateEquality for="name" message="Must be the same as name!" />
 				</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>
+
+			<h:form>
+			<p>Validates that one number is greater than another</p>
+			<h:panelGrid columns="3">
+				<s:label for="min">Minimum age (e.g. 18)</s:label>
+				<h:inputText id="min" value="#{equalityValidatorBean.age}" />
+				<h:message for="min" />
+				<s:label for="minVerification">Maximum age (e.g. 25)</s:label>
+				<h:inputText id="minVerification">
+					<f:convertNumber integerOnly="true" />
+					<s:validateEquality for="min" message="Must be larger than minimum!" operator="greater" />
+				</h:inputText>
+				<h:message for="minVerification" />
+				<h:commandButton action="#{equalityValidatorBean.check}" value="Check ages" />
+			</h:panelGrid>
 		</h:form>
 	</ui:define>
 </ui:composition>

Modified: trunk/ui/src/main/config/component/equalityValidator.xml
===================================================================
--- trunk/ui/src/main/config/component/equalityValidator.xml	2008-11-29 17:12:07 UTC (rev 9675)
+++ trunk/ui/src/main/config/component/equalityValidator.xml	2008-11-30 00:44:50 UTC (rev 9676)
@@ -36,6 +36,11 @@
 			<classname>java.lang.String</classname>
 			<description>Message id to use on failure</description>
 		</property>
+		<property>
+			<name>operator</name>
+			<classname>java.lang.String</classname>
+			<description>Operation to use.</description>
+		</property>		
 
 	</component>
 

Modified: trunk/ui/src/main/java/org/jboss/seam/ui/component/UIEqualityValidator.java
===================================================================
--- trunk/ui/src/main/java/org/jboss/seam/ui/component/UIEqualityValidator.java	2008-11-29 17:12:07 UTC (rev 9675)
+++ trunk/ui/src/main/java/org/jboss/seam/ui/component/UIEqualityValidator.java	2008-11-30 00:44:50 UTC (rev 9676)
@@ -21,5 +21,9 @@
    public abstract String getMessageId();
 
    public abstract void setMessageId(String messageId);
+   
+   public abstract void setOperator(String operator);
+   
+   public abstract String getOperator();
 
 }

Modified: trunk/ui/src/main/java/org/jboss/seam/ui/renderkit/EqualityValidatorRendererBase.java
===================================================================
--- trunk/ui/src/main/java/org/jboss/seam/ui/renderkit/EqualityValidatorRendererBase.java	2008-11-29 17:12:07 UTC (rev 9675)
+++ trunk/ui/src/main/java/org/jboss/seam/ui/renderkit/EqualityValidatorRendererBase.java	2008-11-30 00:44:50 UTC (rev 9676)
@@ -42,7 +42,7 @@
 
       if (!hasEqualityValidator(evh))
       {
-         evh.addValidator(new EqualityValidator(ev.getFor(), ev.getMessage(), ev.getMessageId()));
+         evh.addValidator(new EqualityValidator(ev.getFor(), ev.getMessage(), ev.getMessageId(), ev.getOperator()));
          evh.setRequired(true);
       }
 

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-11-29 17:12:07 UTC (rev 9675)
+++ trunk/ui/src/main/java/org/jboss/seam/ui/validator/EqualityValidator.java	2008-11-30 00:44:50 UTC (rev 9676)
@@ -4,6 +4,7 @@
 import javax.el.ValueExpression;
 import javax.faces.FacesException;
 import javax.faces.application.Application;
+import javax.faces.application.FacesMessage;
 import javax.faces.component.EditableValueHolder;
 import javax.faces.component.StateHolder;
 import javax.faces.component.UIComponent;
@@ -34,9 +35,15 @@
 
    public static final String VALIDATOR_ID = "org.jboss.seam.ui.validator.Equality";
 
+   private enum ValidOperation
+   {
+      EQUAL, NOT_EQUAL, GREATER, GREATER_OR_EQUAL, LESS, LESS_OR_EQUAL;
+   }
+
    private String forId;
    private String message;
    private String messageId;
+   private ValidOperation operator = ValidOperation.EQUAL; // Default
 
    public EqualityValidator()
    {
@@ -50,7 +57,7 @@
       setFor(forId);
    }
 
-   public EqualityValidator(String forId, String message, String messageId)
+   public EqualityValidator(String forId, String message, String messageId, String operator)
    {
       this(forId);
       if (message != null)
@@ -61,8 +68,26 @@
       {
          setMessageId(messageId);
       }
+      if (operator != null && !"".equals(operator))
+      {
+         if (ValidOperation.valueOf(operator.toUpperCase()) != null)
+            setOperator(ValidOperation.valueOf(operator.toUpperCase()));
+         else
+            throw new IllegalStateException("Illegal operator. " + "Supported are: " + validOperatorsAsString());
+      }
+
    }
 
+   private String validOperatorsAsString()
+   {
+      StringBuffer buff = new StringBuffer();
+      for (ValidOperation op : ValidOperation.values())
+      {
+         buff.append(op.name()).append(" ");
+      }
+      return buff.toString();
+   }
+
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException
    {
       String forId = getFor();
@@ -78,14 +103,67 @@
       }
       else if (value != null)
       {
-         if (!value.equals(other))
+         switch (operator)
          {
-            String otherComponentId = otherComponent.getId();
-            throw new ValidatorException(FacesMessages.createFacesMessage(javax.faces.application.FacesMessage.SEVERITY_ERROR, getMessageId(), getMessage(), otherComponentId, value, other));
+         case EQUAL:
+            if (!value.equals(other))
+            {
+               throwValidationException(value, otherComponent, other);
+            }
+            break;
+         case NOT_EQUAL:
+            if (value.equals(other))
+            {
+               throwValidationException(value, otherComponent, other);
+            }
+            break;
+         case GREATER:
+            if (!(compare(value, other) > 0))
+            {
+               throwValidationException(value, otherComponent, other);
+            }
+            break;
+         case GREATER_OR_EQUAL:
+            if (!(compare(value, other) >= 0))
+            {
+               throwValidationException(value, otherComponent, other);
+            }
+            break;
+         case LESS:
+            if (!(compare(value, other) < 0))
+            {
+               throwValidationException(value, otherComponent, other);
+            }
+            break;
+         case LESS_OR_EQUAL:
+            if (!(compare(value, other) <= 0))
+            {
+               throwValidationException(value, otherComponent, other);
+            }
+            break;
          }
       }
    }
 
+   private int compare(Object value, Object other) throws IllegalArgumentException
+   {
+      try
+      {
+         Comparable c1 = (Comparable) value;
+         return c1.compareTo(other);
+      }
+      catch (Exception e)
+      {
+         throw new IllegalArgumentException("Values are not comparable", e);
+      }
+
+   }
+
+   private void throwValidationException(Object value, UIComponent otherComponent, Object other)
+   {
+      throw new ValidatorException(FacesMessages.createFacesMessage(FacesMessage.SEVERITY_ERROR, getMessageId(), getMessage(), otherComponent.getId(), value, other));
+   }
+
    public String getFor()
    {
       return forId;
@@ -127,14 +205,16 @@
       forId = (String) fields[0];
       message = (String) fields[1];
       messageId = (String) fields[2];
+      operator = ValidOperation.valueOf((String) fields[3]);
    }
 
    public Object saveState(FacesContext context)
    {
-      Object[] state = new Object[3];
+      Object[] state = new Object[4];
       state[0] = forId;
       state[1] = message;
       state[2] = messageId;
+      state[3] = operator.toString();
       return state;
    }
 
@@ -252,7 +332,8 @@
          }
          else if (newSubmittedValue instanceof String)
          {
-            // If there's no Renderer, and we've got a String, run it through
+            // If there's no Renderer, and we've got a String, run it
+            // through
             // the Converter (if any)
             if (converter != null)
             {
@@ -305,4 +386,14 @@
       }
 
    }
+
+   public ValidOperation getOperator()
+   {
+      return operator;
+   }
+
+   public void setOperator(ValidOperation operator)
+   {
+      this.operator = operator;
+   }
 }




More information about the seam-commits mailing list