You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by sc...@apache.org on 2005/08/26 19:54:39 UTC

svn commit: r240298 - in /myfaces/sandbox/trunk: conf/ src/java/org/apache/myfaces/custom/convertboolean/ tld/

Author: schof
Date: Fri Aug 26 10:54:36 2005
New Revision: 240298

URL: http://svn.apache.org/viewcvs?rev=240298&view=rev
Log:
MYFACES-451 (Thanks to Ken Weiner for the new sandbox contribution.)

Added:
    myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/convertboolean/
    myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/convertboolean/BooleanConverter.java
    myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/convertboolean/ConvertBooleanTag.java
Modified:
    myfaces/sandbox/trunk/conf/faces-config.xml
    myfaces/sandbox/trunk/tld/myfaces_sandbox.tld

Modified: myfaces/sandbox/trunk/conf/faces-config.xml
URL: http://svn.apache.org/viewcvs/myfaces/sandbox/trunk/conf/faces-config.xml?rev=240298&r1=240297&r2=240298&view=diff
==============================================================================
--- myfaces/sandbox/trunk/conf/faces-config.xml (original)
+++ myfaces/sandbox/trunk/conf/faces-config.xml Fri Aug 26 10:54:36 2005
@@ -49,6 +49,13 @@
     <component-class>org.apache.myfaces.custom.form.HtmlForm</component-class>
   </component>
 
+  <!-- sandbox converters -->
+  
+  <converter>
+    <converter-id>org.apache.myfaces.custom.convertboolean.BooleanConverter</converter-id>
+    <converter-class>org.apache.myfaces.custom.convertboolean.BooleanConverter</converter-class>
+  </converter>
+  
   <!-- sandbox renderkit -->
 
   <render-kit>

Added: myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/convertboolean/BooleanConverter.java
URL: http://svn.apache.org/viewcvs/myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/convertboolean/BooleanConverter.java?rev=240298&view=auto
==============================================================================
--- myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/convertboolean/BooleanConverter.java (added)
+++ myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/convertboolean/BooleanConverter.java Fri Aug 26 10:54:36 2005
@@ -0,0 +1,90 @@
+package org.apache.myfaces.custom.convertboolean;
+
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
+import javax.faces.convert.ConverterException;
+
+/**
+ * Converter that translates between boolean values (true/false)
+ * and alternate versions of those boolean values like
+ * (yes/no), (1/0), and (way/no way).
+ * <p>
+ * To customize the representation of a boolean true and false,
+ * use {@link #setTrueValue(String)}
+ * and {@link #setFalseValue(String)} 
+ * respectively.  If  not configured with these setter methods,
+ * it defaults to <code>true</code> and <code>false</code>.
+ * <p>
+ * The values are case sensitive.
+ * <p>
+ * @author Ken Weiner
+ */
+public class BooleanConverter implements Converter {
+
+    private String trueValue = "true";
+    private String falseValue = "false";
+
+    public static final String CONVERTER_ID = BooleanConverter.class.getName();
+
+    public String getFalseValue() {
+        return falseValue;
+    }
+
+    public void setFalseValue(String falseValue) {
+        this.falseValue = falseValue;
+    }
+
+    public String getTrueValue() {
+        return trueValue;
+    }
+
+    public void setTrueValue(String trueValue) {
+        this.trueValue = trueValue;
+    }
+
+    public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value)
+            throws ConverterException {
+        if (facesContext == null) {
+            throw new NullPointerException("facesContext");
+        }
+        if (uiComponent == null) {
+            throw new NullPointerException("uiComponent");
+        }
+
+        if (value != null) {
+            value = value.trim();
+            if (value.length() > 0) {
+                try {
+                    return Boolean.valueOf(value.equals(trueValue));
+                } catch (Exception e) {
+                    throw new ConverterException(e);
+                }
+            }
+        }
+        return null;
+    }
+
+    public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value)
+            throws ConverterException {
+        if (facesContext == null) {
+            throw new NullPointerException("facesContext");
+        }
+        if (uiComponent == null) {
+            throw new NullPointerException("uiComponent");
+        }
+
+        if (value == null) {
+            return "";
+        }
+        if (value instanceof String) {
+            return (String) value;
+        }
+        try {
+            return ((Boolean) value).booleanValue() ? trueValue : falseValue;
+        } catch (Exception e) {
+            throw new ConverterException(e);
+        }
+    }
+
+}

Added: myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/convertboolean/ConvertBooleanTag.java
URL: http://svn.apache.org/viewcvs/myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/convertboolean/ConvertBooleanTag.java?rev=240298&view=auto
==============================================================================
--- myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/convertboolean/ConvertBooleanTag.java (added)
+++ myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/convertboolean/ConvertBooleanTag.java Fri Aug 26 10:54:36 2005
@@ -0,0 +1,89 @@
+package org.apache.myfaces.custom.convertboolean;
+
+import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
+import javax.faces.el.ValueBinding;
+import javax.faces.webapp.ConverterTag;
+import javax.faces.webapp.UIComponentTag;
+import javax.servlet.jsp.JspException;
+import javax.servlet.jsp.PageContext;
+
+/**
+ * A tag that invokes the {@link BooleanConverter} and lets the developer
+ * specify the desired values for a boolean true or false.
+ * <p>
+ * Example:
+ * <code>
+ * <h:outputText value="#{backingBean.customer.enjoysJazz}">
+ *     <t:convertBoolean trueValue="Yes" falseValue="No"/>
+ * </h:outputText>
+ * </code>
+ * <p>
+ * @author Ken Weiner
+ */
+public class ConvertBooleanTag extends ConverterTag {
+    private String trueValue;
+    private String falseValue;
+
+    public ConvertBooleanTag() {
+        setConverterId(BooleanConverter.CONVERTER_ID);
+    }
+
+    public String getFalseValue() {
+        return falseValue;
+    }
+
+    public void setFalseValue(String falseValue) {
+        this.falseValue = falseValue;
+    }
+
+    public String getTrueValue() {
+        return trueValue;
+    }
+
+    public void setTrueValue(String trueValue) {
+        this.trueValue = trueValue;
+    }
+
+    public void setPageContext(PageContext context) {
+        super.setPageContext(context);
+        setConverterId(BooleanConverter.CONVERTER_ID);
+    }
+
+    protected Converter createConverter() throws JspException {
+        BooleanConverter converter = (BooleanConverter) super.createConverter();
+
+        FacesContext facesContext = FacesContext.getCurrentInstance();
+        setConverterTrueValue(facesContext, converter, this.trueValue);
+        setConverterFalseValue(facesContext, converter, this.falseValue);
+
+        return converter;
+    }
+
+    private static void setConverterTrueValue(FacesContext facesContext,
+            BooleanConverter converter, String value) {
+        if (value == null) {
+            return;
+        }
+        if (UIComponentTag.isValueReference(value)) {
+            ValueBinding vb = facesContext.getApplication().createValueBinding(value);
+            converter.setTrueValue((String) vb.getValue(facesContext));
+        } else {
+            converter.setTrueValue(value);
+        }
+    }
+
+    private static void setConverterFalseValue(FacesContext facesContext,
+            BooleanConverter converter, String value) {
+        if (value == null) {
+            return;
+        }
+        if (UIComponentTag.isValueReference(value)) {
+            ValueBinding vb = facesContext.getApplication().createValueBinding(value);
+            converter.setFalseValue((String) vb.getValue(facesContext));
+        } else {
+            converter.setFalseValue(value);
+        }
+    }
+
+}

Modified: myfaces/sandbox/trunk/tld/myfaces_sandbox.tld
URL: http://svn.apache.org/viewcvs/myfaces/sandbox/trunk/tld/myfaces_sandbox.tld?rev=240298&r1=240297&r2=240298&view=diff
==============================================================================
--- myfaces/sandbox/trunk/tld/myfaces_sandbox.tld (original)
+++ myfaces/sandbox/trunk/tld/myfaces_sandbox.tld Fri Aug 26 10:54:36 2005
@@ -112,7 +112,7 @@
         <description>
             Provides an input textbox with "suggest" functionality.
         </description>
-        &standard_input_text_attributes;
+        &standard_input_text_attributes;
         &ext_forceId_attribute;
         &alt_location_attributes;
         &html_input_suggest_attributes;
@@ -158,18 +158,18 @@
             Provides an input textbox with "suggest" functionality, using a remote connection to the server.
 	    The difference to inputSuggest is that a custom drop down is rendered.
         </description>
-        &standard_input_text_attributes;
+        &standard_input_text_attributes;
         &ext_forceId_attribute;
         &alt_location_attributes;
-        &html_input_suggest_attributes;
-        &display_value_only_attributes;
-        &user_role_attributes;
-        
-        <attribute>
-        	<name>size</name>
-        	<required>false</required>
-        	<rtexprvalue>false</rtexprvalue>
-        	<type>java.lang.String</type>
+        &html_input_suggest_attributes;
+        &display_value_only_attributes;
+        &user_role_attributes;
+        
+        <attribute>
+        	<name>size</name>
+        	<required>false</required>
+        	<rtexprvalue>false</rtexprvalue>
+        	<type>java.lang.String</type>
         </attribute>
 
         <attribute>
@@ -178,14 +178,14 @@
             <rtexprvalue>false</rtexprvalue>
             <type>java.lang.String</type>
             <description>Reference to the method which returns the suggested items</description>
-        </attribute>
-        
-        <attribute>
-            <name>maxSuggestedItems</name>
-            <required>false</required>
-            <rtexprvalue>false</rtexprvalue>
-            <type>java.lang.String</type>
-            <description>optional attribute to identify the max size of suggested Values</description>
+        </attribute>
+        
+        <attribute>
+            <name>maxSuggestedItems</name>
+            <required>false</required>
+            <rtexprvalue>false</rtexprvalue>
+            <type>java.lang.String</type>
+            <description>optional attribute to identify the max size of suggested Values</description>
         </attribute>
 
         <attribute>
@@ -295,17 +295,17 @@
             <rtexprvalue>false</rtexprvalue>
             <description>This attribute can be used to set the port for the action attribute</description>
         </attribute>        
-    </tag>
-    
-    <!-- Validators -->
-    <!-- Validator for Url -->
-    <tag>
-       <name>validateUrl</name>
-       <tag-class>org.apache.myfaces.custom.emailvalidator.ValidateUrlTag</tag-class>
-       <body-content>JSP</body-content>
-       <description>
- 			A custom validator for url format, based upons Jakarta Commons.
-        </description>
+    </tag>
+    
+    <!-- Validators -->
+    <!-- Validator for Url -->
+    <tag>
+       <name>validateUrl</name>
+       <tag-class>org.apache.myfaces.custom.emailvalidator.ValidateUrlTag</tag-class>
+       <body-content>JSP</body-content>
+       <description>
+ 			A custom validator for url format, based upons Jakarta Commons.
+        </description>
     </tag>
     
     <!-- autoUpdateDataTable -->
@@ -586,6 +586,28 @@
             </description>
         </attribute>
      </tag>
-    	
+
+    <!-- Converter for Boolean values -->
+	<tag>
+		<name>convertBoolean</name>
+		<tag-class>org.apache.myfaces.custom.convertboolean.ConvertBooleanTag</tag-class>
+		<display-name>Boolean Converter</display-name>
+		<description>
+			Converts a boolean to custom format (yes/no), (1/0), etc.
+		</description>
+
+        <attribute>
+            <name>trueValue</name>
+            <required>false</required>
+            <description>Value representing a boolean true, e.g. TRUE, yes, 1, etc.</description>
+        </attribute>
+
+        <attribute>
+            <name>falseValue</name>
+            <required>false</required>
+            <description>Value representing a boolean false, e.g. FALSE, no, 0, etc.</description>
+        </attribute>
+	</tag>
+
     
 </taglib>