You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2011/03/22 18:50:23 UTC

svn commit: r1084268 - in /myfaces/tomahawk/trunk/core12/src: main/java/org/apache/myfaces/custom/radio/ main/java/org/apache/myfaces/renderkit/html/ext/ test/java/org/apache/myfaces/renderkit/html/ext/ test/java/org/apache/myfaces/test/utils/

Author: lu4242
Date: Tue Mar 22 17:50:23 2011
New Revision: 1084268

URL: http://svn.apache.org/viewvc?rev=1084268&view=rev
Log:
TOMAHAWK-939 t:radio should support standard pass-through attributes

Added:
    myfaces/tomahawk/trunk/core12/src/main/java/org/apache/myfaces/custom/radio/
    myfaces/tomahawk/trunk/core12/src/main/java/org/apache/myfaces/custom/radio/AbstractHtmlRadio.java
Modified:
    myfaces/tomahawk/trunk/core12/src/main/java/org/apache/myfaces/renderkit/html/ext/HtmlRadioRenderer.java
    myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/renderkit/html/ext/HtmlRadioRendererTest.java
    myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/test/utils/HtmlCheckAttributesUtil.java

Added: myfaces/tomahawk/trunk/core12/src/main/java/org/apache/myfaces/custom/radio/AbstractHtmlRadio.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core12/src/main/java/org/apache/myfaces/custom/radio/AbstractHtmlRadio.java?rev=1084268&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/core12/src/main/java/org/apache/myfaces/custom/radio/AbstractHtmlRadio.java (added)
+++ myfaces/tomahawk/trunk/core12/src/main/java/org/apache/myfaces/custom/radio/AbstractHtmlRadio.java Tue Mar 22 17:50:23 2011
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.custom.radio;
+
+import javax.faces.component.UIComponentBase;
+
+import org.apache.myfaces.component.AccesskeyProperty;
+import org.apache.myfaces.component.AlignProperty;
+import org.apache.myfaces.component.AltProperty;
+import org.apache.myfaces.component.ChangeSelectProperties;
+import org.apache.myfaces.component.DisabledClassEnabledClassProperties;
+import org.apache.myfaces.component.EventAware;
+import org.apache.myfaces.component.FocusBlurProperties;
+import org.apache.myfaces.component.StyleAware;
+import org.apache.myfaces.component.TabindexProperty;
+import org.apache.myfaces.component.UniversalProperties;
+import org.apache.myfaces.component.UserRoleAware;
+
+/**
+ * This tag is used in conjunction with the extended selectOneRadio 
+ * tag when the "spread" layout is selected. It specifies the 
+ * position within the document that the radio button corresponding 
+ * to a specific SelectItem should be rendered. All HTML pass-through 
+ * attributes for this input are taken from the associated 
+ * selectOneRadio. 
+ * 
+ * Unless otherwise specified, all attributes accept static values or EL expressions.
+ * 
+ * @JSFComponent
+ *   name = "t:radio"
+ *   class = "org.apache.myfaces.custom.radio.HtmlRadio"
+ *   tagClass = "org.apache.myfaces.custom.radio.HtmlRadioTag"
+ * @since 1.1.7
+ * @author Thomas Spiegl (latest modification by $Author: lu4242 $)
+ * @version $Revision: 691856 $ $Date: 2008-09-03 21:40:30 -0500 (mié, 03 sep 2008) $
+ */
+public abstract class AbstractHtmlRadio
+    extends UIComponentBase implements UserRoleAware, 
+    FocusBlurProperties, ChangeSelectProperties, 
+    UniversalProperties, EventAware, AltProperty, 
+    AlignProperty, StyleAware, AccesskeyProperty, TabindexProperty, DisabledClassEnabledClassProperties
+{
+    //private static final Log log = LogFactory.getLog(HtmlRadio.class);
+
+    public static final String FOR_ATTR = "for".intern();
+    public static final String INDEX_ATTR = "index".intern();
+
+
+    public static final String COMPONENT_TYPE = "org.apache.myfaces.HtmlRadio";
+    public static final String COMPONENT_FAMILY = "org.apache.myfaces.Radio";
+    private static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.Radio";
+
+    /**
+     * The id of the referenced extended selectOneRadio component. 
+     * This value is resolved to the particular component using 
+     * the standard UIComponent.findComponent() searching algorithm.
+     * 
+     * @JSFProperty
+     *   required="true"
+     */
+    public abstract String getFor();
+    
+    /**
+     * The index of the corresponding SelectItem, where 0 represents the first SelectItem.
+     * 
+     * @JSFProperty
+     *   defaultValue = "Integer.MIN_VALUE"
+     *   required="true"
+     */
+    public abstract int getIndex();
+
+}

Modified: myfaces/tomahawk/trunk/core12/src/main/java/org/apache/myfaces/renderkit/html/ext/HtmlRadioRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core12/src/main/java/org/apache/myfaces/renderkit/html/ext/HtmlRadioRenderer.java?rev=1084268&r1=1084267&r2=1084268&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core12/src/main/java/org/apache/myfaces/renderkit/html/ext/HtmlRadioRenderer.java (original)
+++ myfaces/tomahawk/trunk/core12/src/main/java/org/apache/myfaces/renderkit/html/ext/HtmlRadioRenderer.java Tue Mar 22 17:50:23 2011
@@ -22,9 +22,11 @@ import java.io.IOException;
 import java.util.List;
 
 import javax.faces.FacesException;
+import javax.faces.component.NamingContainer;
 import javax.faces.component.UIComponent;
 import javax.faces.component.UIInput;
 import javax.faces.component.UISelectOne;
+import javax.faces.component.UIViewRoot;
 import javax.faces.context.FacesContext;
 import javax.faces.context.ResponseWriter;
 import javax.faces.convert.Converter;
@@ -32,6 +34,7 @@ import javax.faces.model.SelectItem;
 
 import org.apache.myfaces.component.UserRoleUtils;
 import org.apache.myfaces.custom.radio.HtmlRadio;
+import org.apache.myfaces.shared_tomahawk.renderkit.JSFAttr;
 import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
 import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
 import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRadioRendererBase;
@@ -157,6 +160,7 @@ public class HtmlRadioRenderer
         //Render the radio component
         String itemId = renderRadio(facesContext,
                 uiSelectOne,
+                radio,
                 itemStrValue,
                 selectItem.isDisabled(),
                 itemStrValue.equals(currentValue),
@@ -169,9 +173,139 @@ public class HtmlRadioRenderer
         boolean itemDisabled = selectItem.isDisabled();
         boolean disabled = (componentDisabled || itemDisabled);
 
-        HtmlRendererUtils.renderLabel(writer, uiSelectOne, itemId, selectItem, disabled);
+        renderLabel(writer, radio, uiSelectOne, itemId, selectItem, disabled);
     }
+    
+    /**
+     * Renders the input item
+     * @return the 'id' value of the rendered element
+     */
+    protected String renderRadio(FacesContext facesContext,
+                               UIInput uiComponent,
+                               HtmlRadio radio,
+                               String value,
+                               boolean disabled,
+                               boolean checked,
+                               boolean renderId,
+                               Integer itemNum)
+            throws IOException
+    {
+        String clientId = uiComponent.getClientId(facesContext);
+
+        String itemId = (radio.getId()!=null && !radio.getId().startsWith(UIViewRoot.UNIQUE_ID_PREFIX)) ? 
+                radio.getClientId(facesContext) :
+                (itemNum == null)? null : clientId + NamingContainer.SEPARATOR_CHAR + itemNum;
+
+        ResponseWriter writer = facesContext.getResponseWriter();
+
+        writer.startElement(HTML.INPUT_ELEM, uiComponent);
+
+        if (itemId != null)
+        {
+            writer.writeAttribute(HTML.ID_ATTR, itemId, null);
+        }
+        else if (renderId) {
+            writer.writeAttribute(HTML.ID_ATTR, clientId, null);
+        }
+
+        writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_RADIO, null);
+        writer.writeAttribute(HTML.NAME_ATTR, clientId, null);
+
+        if (disabled) {
+            writer.writeAttribute(HTML.DISABLED_ATTR, HTML.DISABLED_ATTR, null);
+        }
+
+        if (checked)
+        {
+            writer.writeAttribute(HTML.CHECKED_ATTR, HTML.CHECKED_ATTR, null);
+        }
 
+        if (value != null)
+        {
+            writer.writeAttribute(HTML.VALUE_ATTR, value, null);
+        }
+
+        renderHTMLAttributes(writer, radio ,uiComponent, HTML.INPUT_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED);
+        
+        if (isDisabled(facesContext, uiComponent))
+        {
+            writer.writeAttribute(org.apache.myfaces.shared_tomahawk.renderkit.html.HTML.DISABLED_ATTR, Boolean.TRUE, null);
+        }
+
+        writer.endElement(HTML.INPUT_ELEM);
+
+        return itemId;
+    }
+    
+    public static void renderLabel(ResponseWriter writer, UIComponent radio,
+            UIComponent component, String forClientId, SelectItem item,
+            boolean disabled) throws IOException
+    {
+        writer.startElement(HTML.LABEL_ELEM, component);
+        writer.writeAttribute(HTML.FOR_ATTR, forClientId, null);
+
+        String labelClass = null;
+
+        if (disabled)
+        {
+            labelClass = (String) radio.getAttributes().get(
+                    JSFAttr.DISABLED_CLASS_ATTR);
+            if (labelClass == null)
+            {
+                labelClass = (String) component.getAttributes().get(
+                        JSFAttr.DISABLED_CLASS_ATTR);
+            }
+        }
+        else
+        {
+            labelClass = (String) radio.getAttributes().get(
+                    JSFAttr.ENABLED_CLASS_ATTR);
+            if (labelClass == null)
+            {
+                labelClass = (String) component.getAttributes().get(
+                        JSFAttr.ENABLED_CLASS_ATTR);
+            }
+        }
+        if (labelClass != null)
+        {
+            writer.writeAttribute("class", labelClass, "labelClass");
+        }
+
+        if ((item.getLabel() != null) && (item.getLabel().length() > 0))
+        {
+            writer.write(HTML.NBSP_ENTITY);
+            if(item.isEscape())
+            {
+                writer.writeText(item.getLabel(), null);
+            }
+            else
+            {
+                writer.write(item.getLabel());
+            }
+        }
+
+        writer.endElement(HTML.LABEL_ELEM);
+    }
+
+    private static boolean renderHTMLAttributes(ResponseWriter writer,
+            UIComponent radio, UIComponent selectOne, String[] attributes) throws IOException
+    {
+        boolean somethingDone = false;
+        for (int i = 0, len = attributes.length; i < len; i++)
+        {
+            String attrName = attributes[i];
+            Object value = radio.getAttributes().get(attrName);
+            if (value == null)
+            {
+                value = selectOne.getAttributes().get(attrName);
+            }
+            if (HtmlRendererUtils.renderHTMLAttribute(writer, attrName, attrName, value ))
+            {
+                somethingDone = true;
+            }
+        }
+        return somethingDone;
+    }
 
     protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent)
     {

Modified: myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/renderkit/html/ext/HtmlRadioRendererTest.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/renderkit/html/ext/HtmlRadioRendererTest.java?rev=1084268&r1=1084267&r2=1084268&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/renderkit/html/ext/HtmlRadioRendererTest.java (original)
+++ myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/renderkit/html/ext/HtmlRadioRendererTest.java Tue Mar 22 17:50:23 2011
@@ -28,6 +28,7 @@ import junit.framework.Test;
 import junit.framework.TestSuite;
 
 import org.apache.myfaces.component.html.ext.HtmlSelectOneRadio;
+import org.apache.myfaces.custom.radio.HtmlRadio;
 import org.apache.myfaces.test.AbstractTomahawkViewControllerTestCase;
 import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
 import org.apache.myfaces.test.utils.HtmlRenderedAttr;
@@ -106,4 +107,137 @@ public class HtmlRadioRendererTest exten
             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
         }
     }
+    
+    
+    public void testHtmlPropertyRadioPassTru() throws Exception
+    {
+        HtmlRenderedAttr[] attrs = {
+            //_AccesskeyProperty
+            new HtmlRenderedAttr("accesskey"),
+            //_UniversalProperties
+            new HtmlRenderedAttr("dir"), 
+            new HtmlRenderedAttr("lang"), 
+            new HtmlRenderedAttr("title"),
+            //_FocusBlurProperties
+            new HtmlRenderedAttr("onfocus"), 
+            new HtmlRenderedAttr("onblur"),
+            //_ChangeSelectProperties
+            new HtmlRenderedAttr("onchange"), 
+            new HtmlRenderedAttr("onselect"),
+            //_EventProperties
+            new HtmlRenderedAttr("onclick"), 
+            new HtmlRenderedAttr("ondblclick"), 
+            new HtmlRenderedAttr("onkeydown"), 
+            new HtmlRenderedAttr("onkeypress"),
+            new HtmlRenderedAttr("onkeyup"), 
+            new HtmlRenderedAttr("onmousedown"), 
+            new HtmlRenderedAttr("onmousemove"), 
+            new HtmlRenderedAttr("onmouseout"),
+            new HtmlRenderedAttr("onmouseover"), 
+            new HtmlRenderedAttr("onmouseup"),
+            //_StyleProperties
+            new HtmlRenderedAttr("style", 1), 
+            new HtmlRenderedAttr("styleClass", "styleClass", "class=\"styleClass\"", 1),
+            //_TabindexProperty
+            new HtmlRenderedAttr("tabindex"),
+            new HtmlRenderedAttr("id","option1", "id=\"option1\"", 1)
+        };
+        
+        List items = new ArrayList();
+        items.add(new SelectItem("mars"));
+
+        UISelectItems selectItems = new UISelectItems();
+        selectItems.setValue(items);
+
+        selectOneRadio = new HtmlSelectOneRadio();
+        
+        selectOneRadio.setId("selectOneRadio");
+        selectOneRadio.setLayout("spread");
+        
+        facesContext.getViewRoot().getChildren().add(selectOneRadio);
+        
+        selectOneRadio.getChildren().add(selectItems);
+        
+        HtmlRadio radio = new HtmlRadio();
+        radio.setId("option1");
+        radio.setIndex(0);
+        radio.setFor("selectOneRadio");
+        
+        facesContext.getViewRoot().getChildren().add(radio);
+        
+        MockResponseWriter writer = (MockResponseWriter)facesContext.getResponseWriter();
+        HtmlCheckAttributesUtil.checkRenderedAttributes(
+                radio, facesContext, writer, attrs);
+        if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
+            fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
+        }
+    }
+    
+    public void testHtmlPropertyRadioSelectOnePassTru() throws Exception
+    {
+        HtmlRenderedAttr[] attrs = {
+            //_AccesskeyProperty
+            new HtmlRenderedAttr("accesskey"),
+            //_UniversalProperties
+            new HtmlRenderedAttr("dir"), 
+            new HtmlRenderedAttr("lang"), 
+            new HtmlRenderedAttr("title"),
+            //_FocusBlurProperties
+            new HtmlRenderedAttr("onfocus"), 
+            new HtmlRenderedAttr("onblur"),
+            //_ChangeSelectProperties
+            new HtmlRenderedAttr("onchange"), 
+            new HtmlRenderedAttr("onselect"),
+            //_EventProperties
+            new HtmlRenderedAttr("onclick"), 
+            new HtmlRenderedAttr("ondblclick"), 
+            new HtmlRenderedAttr("onkeydown"), 
+            new HtmlRenderedAttr("onkeypress"),
+            new HtmlRenderedAttr("onkeyup"), 
+            new HtmlRenderedAttr("onmousedown"), 
+            new HtmlRenderedAttr("onmousemove"), 
+            new HtmlRenderedAttr("onmouseout"),
+            new HtmlRenderedAttr("onmouseover"), 
+            new HtmlRenderedAttr("onmouseup"),
+            //_StyleProperties
+            new HtmlRenderedAttr("style", 1), 
+            new HtmlRenderedAttr("styleClass", "styleClass", "class=\"styleClass\"", 1),
+            //_TabindexProperty
+            new HtmlRenderedAttr("tabindex")
+        };
+        
+        List items = new ArrayList();
+        items.add(new SelectItem("mars"));
+
+        UISelectItems selectItems = new UISelectItems();
+        selectItems.setValue(items);
+
+        selectOneRadio = new HtmlSelectOneRadio();
+        
+        selectOneRadio.setId("selectOneRadio");
+        selectOneRadio.setLayout("spread");
+        
+        facesContext.getViewRoot().getChildren().add(selectOneRadio);
+        
+        selectOneRadio.getChildren().add(selectItems);
+        
+        HtmlRadio radio = new HtmlRadio();
+        radio.setIndex(0);
+        radio.setFor("selectOneRadio");
+        
+        facesContext.getViewRoot().getChildren().add(radio);
+        
+        MockResponseWriter writer = (MockResponseWriter)facesContext.getResponseWriter();
+        HtmlCheckAttributesUtil.addBaseAttributes(selectOneRadio, attrs);
+        radio.encodeBegin(facesContext);
+        radio.encodeChildren(facesContext);
+        radio.encodeEnd(facesContext);
+        facesContext.renderResponse();
+        HtmlCheckAttributesUtil.checkRenderedAttributes(attrs, writer.getWriter().toString());        
+        
+        if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
+            fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
+        }
+    }    
+
 }

Modified: myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/test/utils/HtmlCheckAttributesUtil.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/test/utils/HtmlCheckAttributesUtil.java?rev=1084268&r1=1084267&r2=1084268&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/test/utils/HtmlCheckAttributesUtil.java (original)
+++ myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/test/utils/HtmlCheckAttributesUtil.java Tue Mar 22 17:50:23 2011
@@ -36,7 +36,7 @@ public class HtmlCheckAttributesUtil 
      * @param component The component to add the attributes to.
      * @param attrs     The attributes to be added to the component.
      */
-    private static void addBaseAttributes(UIComponent component, HtmlRenderedAttr[] attrs) 
+    public static void addBaseAttributes(UIComponent component, HtmlRenderedAttr[] attrs) 
     {
         Map map = component.getAttributes();
         for(int i = 0; i < attrs.length; i++) 
@@ -52,7 +52,7 @@ public class HtmlCheckAttributesUtil 
      * @param output     The html output of the component's renderer.
      * @throws Exception
      */
-    private static void checkRenderedAttributes(HtmlRenderedAttr[] attrs, String output) throws Exception 
+    public static void checkRenderedAttributes(HtmlRenderedAttr[] attrs, String output) throws Exception 
     {
         for(int i = 0; i < attrs.length; i++) 
         {
@@ -139,7 +139,7 @@ public class HtmlCheckAttributesUtil 
      * @param attr   The attribute to be tested.
      * @param actual The html generated by the renderer.
      */
-    private static void checkAttributeOccurrences(HtmlRenderedAttr attr, String actual)
+    public static void checkAttributeOccurrences(HtmlRenderedAttr attr, String actual)
     {
         String expectedHtml = attr.getExpectedHtml();