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/18 17:58:17 UTC

svn commit: r1082970 - /myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRendererUtils.java

Author: lu4242
Date: Fri Mar 18 16:58:17 2011
New Revision: 1082970

URL: http://svn.apache.org/viewvc?rev=1082970&view=rev
Log:
Add methods to allow render one client behavior of a component with target to other component (required to fix TOMAHAWK-1551 f:ajax doesn't work in t:selectOneRadio layout="spread")

Modified:
    myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRendererUtils.java

Modified: myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRendererUtils.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRendererUtils.java?rev=1082970&r1=1082969&r2=1082970&view=diff
==============================================================================
--- myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRendererUtils.java (original)
+++ myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRendererUtils.java Fri Mar 18 16:58:17 2011
@@ -73,7 +73,6 @@ import org.apache.myfaces.shared.renderk
 import org.apache.myfaces.shared.renderkit.html.util.HTMLEncoder;
 import org.apache.myfaces.shared.renderkit.html.util.JavascriptUtils;
 import org.apache.myfaces.shared.renderkit.html.util.ResourceUtils;
-import org.apache.myfaces.shared.util.StringUtils;
 
 /**
  * @author Manfred Geiler (latest modification by $Author$)
@@ -2025,7 +2024,19 @@ public final class HtmlRendererUtils {
     private static boolean getClientBehaviorScript(FacesContext facesContext,
                                                    UIComponent uiComponent, String eventName,
                                                    Map<String, List<ClientBehavior>> clientBehaviors,
+                                                   ScriptContext target, Collection<ClientBehaviorContext.Parameter> params)
+    {
+        return getClientBehaviorScript(facesContext, 
+                                       uiComponent, uiComponent.getClientId(facesContext), eventName, 
+                                       clientBehaviors, 
+                                       target, params);
+    }
+    
+    private static boolean getClientBehaviorScript(FacesContext facesContext,
+                                                   UIComponent uiComponent, String targetClientId, String eventName,
+                                                   Map<String, List<ClientBehavior>> clientBehaviors,
                                                    ScriptContext target, Collection<ClientBehaviorContext.Parameter> params) {
+    
         if (!(uiComponent instanceof ClientBehaviorHolder)) {
             target.append(STR_EMPTY);
             return false;
@@ -2051,7 +2062,7 @@ public final class HtmlRendererUtils {
 
         ClientBehaviorContext context = ClientBehaviorContext
                 .createClientBehaviorContext(facesContext, uiComponent,
-                        eventName, uiComponent.getClientId(facesContext),
+                        eventName, targetClientId,
                         params);
 
 
@@ -2094,7 +2105,21 @@ public final class HtmlRendererUtils {
                                             UIComponent uiComponent,
                                             String eventName, Collection<ClientBehaviorContext.Parameter> params,
                                             Map<String, List<ClientBehavior>> clientBehaviors,
+                                            String userEventCode, String serverEventCode)
+    {
+        return buildBehaviorChain(facesContext, 
+                                  uiComponent, uiComponent.getClientId(facesContext), 
+                                  eventName, params, 
+                                  clientBehaviors, 
+                                  userEventCode, serverEventCode);
+    }
+    
+    public static String buildBehaviorChain(FacesContext facesContext,
+                                            UIComponent uiComponent, String targetClientId,
+                                            String eventName, Collection<ClientBehaviorContext.Parameter> params,
+                                            Map<String, List<ClientBehavior>> clientBehaviors,
                                             String userEventCode, String serverEventCode) {
+
         ExternalContext externalContext = facesContext.getExternalContext();
         boolean renderCode = JavascriptUtils
                 .isJavascriptAllowed(externalContext);
@@ -2113,7 +2138,7 @@ public final class HtmlRendererUtils {
         ScriptContext behaviorCode = new ScriptContext();
         ScriptContext retVal = new ScriptContext(currentInstance.isPrettyHtml());
 
-        getClientBehaviorScript(facesContext, uiComponent, eventName, clientBehaviors,
+        getClientBehaviorScript(facesContext, uiComponent, targetClientId, eventName, clientBehaviors,
                 behaviorCode, params);
         if (behaviorCode != null
                 && !behaviorCode.toString().trim().equals(STR_EMPTY)) {
@@ -2131,7 +2156,7 @@ public final class HtmlRendererUtils {
         if (it.hasNext()) {
             //according to the spec jsf.util.chain has to be used to build up the behavior and scripts
             retVal.append("jsf.util.chain(document.getElementById('"
-                    + uiComponent.getClientId(facesContext) + "'), event,");
+                    + targetClientId + "'), event,");
             while (it.hasNext()) {
                 retVal.append(it.next());
                 if (it.hasNext()) {
@@ -2163,7 +2188,25 @@ public final class HtmlRendererUtils {
                                             String eventName2, Collection<ClientBehaviorContext.Parameter> params2,
                                             Map<String, List<ClientBehavior>> clientBehaviors,
                                             String userEventCode,
-                                            String serverEventCode) {
+                                            String serverEventCode)
+    {
+        return buildBehaviorChain(facesContext, 
+                                  uiComponent, uiComponent.getClientId(facesContext), 
+                                  eventName1, params, 
+                                  eventName2, params2, 
+                                  clientBehaviors, 
+                                  userEventCode, 
+                                  serverEventCode);
+    }
+    
+    public static String buildBehaviorChain(FacesContext facesContext,
+            UIComponent uiComponent, String targetClientId,
+            String eventName1, Collection<ClientBehaviorContext.Parameter> params,
+            String eventName2, Collection<ClientBehaviorContext.Parameter> params2,
+            Map<String, List<ClientBehavior>> clientBehaviors,
+            String userEventCode,
+            String serverEventCode) {
+
         ExternalContext externalContext = facesContext.getExternalContext();
         boolean renderCode = JavascriptUtils
                 .isJavascriptAllowed(externalContext);
@@ -2180,9 +2223,9 @@ public final class HtmlRendererUtils {
         ScriptContext behaviorCode = new ScriptContext();
         ScriptContext retVal = new ScriptContext(currentInstance.isPrettyHtml());
 
-        boolean submitting1 = getClientBehaviorScript(facesContext, uiComponent, eventName1, clientBehaviors,
+        boolean submitting1 = getClientBehaviorScript(facesContext, uiComponent, targetClientId, eventName1, clientBehaviors,
                 behaviorCode, params);
-        boolean submitting2 = getClientBehaviorScript(facesContext, uiComponent, eventName2, clientBehaviors,
+        boolean submitting2 = getClientBehaviorScript(facesContext, uiComponent, targetClientId, eventName2, clientBehaviors,
                 behaviorCode, params2);
 
         // ClientBehaviors for both events have to be checked for the Submitting hint
@@ -2207,7 +2250,7 @@ public final class HtmlRendererUtils {
             }
             //according to the spec jsf.util.chain has to be used to build up the behavior and scripts
             retVal.append("jsf.util.chain(document.getElementById('"
-                    + uiComponent.getClientId(facesContext) + "'), event,");
+                    +  targetClientId + "'), event,");
             while (it.hasNext()) {
                 retVal.append(it.next());
                 if (it.hasNext()) {
@@ -2408,6 +2451,16 @@ public final class HtmlRendererUtils {
                 componentProperty, component,
                 eventName, clientBehaviors, componentProperty);
     }
+    
+    public static boolean renderBehaviorizedAttribute(
+            FacesContext facesContext, ResponseWriter writer,
+            String componentProperty, UIComponent component, String targetClientId,
+            String eventName, Map<String, List<ClientBehavior>> clientBehaviors) throws IOException {
+        return renderBehaviorizedAttribute(
+                facesContext, writer,
+                componentProperty, component, targetClientId,
+                eventName, clientBehaviors, componentProperty);
+    }
 
 
     /**
@@ -2436,6 +2489,17 @@ public final class HtmlRendererUtils {
                 eventName, null, clientBehaviors,
                 htmlAttrName, (String) component.getAttributes().get(componentProperty));
     }
+    
+    public static boolean renderBehaviorizedAttribute(
+            FacesContext facesContext, ResponseWriter writer,
+            String componentProperty, UIComponent component, String targetClientId,
+            String eventName, Map<String, List<ClientBehavior>> clientBehaviors,
+            String htmlAttrName) throws IOException {
+        return renderBehaviorizedAttribute(facesContext, writer,
+                componentProperty, component, targetClientId,
+                eventName, null, clientBehaviors,
+                htmlAttrName, (String) component.getAttributes().get(componentProperty));
+    }
 
     /**
      * Render an attribute taking into account the passed event,
@@ -2457,7 +2521,22 @@ public final class HtmlRendererUtils {
             FacesContext facesContext, ResponseWriter writer,
             String componentProperty, UIComponent component,
             String eventName, Collection<ClientBehaviorContext.Parameter> eventParameters, Map<String, List<ClientBehavior>> clientBehaviors,
-            String htmlAttrName, String attributeValue) throws IOException {
+            String htmlAttrName, String attributeValue) throws IOException
+    {
+        return renderBehaviorizedAttribute(
+                facesContext, writer,
+                componentProperty, component, component.getClientId(facesContext),
+                eventName, eventParameters, clientBehaviors,
+                htmlAttrName, attributeValue);
+    }
+    
+    public static boolean renderBehaviorizedAttribute(
+            FacesContext facesContext, ResponseWriter writer,
+            String componentProperty, UIComponent component, String targetClientId,
+            String eventName, Collection<ClientBehaviorContext.Parameter> eventParameters, Map<String, List<ClientBehavior>> clientBehaviors,
+            String htmlAttrName, String attributeValue) throws IOException
+    {
+
         List<ClientBehavior> cbl = (clientBehaviors != null) ? clientBehaviors.get(eventName) : null;
 
         if (cbl == null || cbl.size() == 0) {
@@ -2467,7 +2546,7 @@ public final class HtmlRendererUtils {
         if (cbl.size() > 1 || (cbl.size() == 1 && attributeValue != null)) {
             return renderHTMLAttribute(writer, componentProperty, htmlAttrName,
                     HtmlRendererUtils.buildBehaviorChain(facesContext,
-                            component, eventName, eventParameters, clientBehaviors,
+                            component, targetClientId, eventName, eventParameters, clientBehaviors,
                             attributeValue, STR_EMPTY));
         } else {
             //Only 1 behavior and attrValue == null, so just render it directly
@@ -2475,7 +2554,7 @@ public final class HtmlRendererUtils {
                     cbl.get(0).getScript(
                             ClientBehaviorContext
                                     .createClientBehaviorContext(facesContext, component,
-                                            eventName, component.getClientId(facesContext),
+                                            eventName, targetClientId,
                                             eventParameters)));
         }
     }
@@ -2502,7 +2581,21 @@ public final class HtmlRendererUtils {
             FacesContext facesContext, ResponseWriter writer,
             String componentProperty, UIComponent component,
             String eventName, Collection<ClientBehaviorContext.Parameter> eventParameters, Map<String, List<ClientBehavior>> clientBehaviors,
+            String htmlAttrName, String attributeValue, String serverSideScript) throws IOException
+    {
+        return renderBehaviorizedAttribute(
+                facesContext, writer,
+                componentProperty, component, component.getClientId(facesContext),
+                eventName, eventParameters, clientBehaviors,
+                htmlAttrName, attributeValue, serverSideScript);
+    }
+    
+    public static boolean renderBehaviorizedAttribute(
+            FacesContext facesContext, ResponseWriter writer,
+            String componentProperty, UIComponent component, String targetClientId,
+            String eventName, Collection<ClientBehaviorContext.Parameter> eventParameters, Map<String, List<ClientBehavior>> clientBehaviors,
             String htmlAttrName, String attributeValue, String serverSideScript) throws IOException {
+
         List<ClientBehavior> cbl = (clientBehaviors != null) ? clientBehaviors.get(eventName) : null;
 
         if (((cbl != null) ? cbl.size() : 0) + (attributeValue != null ? 1 : 0) + (serverSideScript != null ? 1 : 0) <= 1) {
@@ -2517,13 +2610,13 @@ public final class HtmlRendererUtils {
                         cbl.get(0).getScript(
                                 ClientBehaviorContext
                                         .createClientBehaviorContext(facesContext, component,
-                                                eventName, component.getClientId(facesContext),
+                                                eventName, targetClientId,
                                                 eventParameters)));
             }
         } else {
             return renderHTMLAttribute(writer, componentProperty, htmlAttrName,
                     HtmlRendererUtils.buildBehaviorChain(facesContext,
-                            component, eventName, eventParameters, clientBehaviors,
+                            component, targetClientId, eventName, eventParameters, clientBehaviors,
                             attributeValue, serverSideScript));
         }
     }
@@ -2551,6 +2644,23 @@ public final class HtmlRendererUtils {
             String eventName2, Collection<ClientBehaviorContext.Parameter> eventParameters2,
             Map<String, List<ClientBehavior>> clientBehaviors,
             String htmlAttrName, String attributeValue, String serverSideScript) throws IOException {
+        return renderBehaviorizedAttribute(
+                facesContext, writer,
+                componentProperty, component, component.getClientId(facesContext),
+                eventName, eventParameters,
+                eventName2, eventParameters2,
+                clientBehaviors,
+                htmlAttrName, attributeValue, serverSideScript);
+    }
+    
+    public static boolean renderBehaviorizedAttribute(
+            FacesContext facesContext, ResponseWriter writer,
+            String componentProperty, UIComponent component, String targetClientId,
+            String eventName, Collection<ClientBehaviorContext.Parameter> eventParameters,
+            String eventName2, Collection<ClientBehaviorContext.Parameter> eventParameters2,
+            Map<String, List<ClientBehavior>> clientBehaviors,
+            String htmlAttrName, String attributeValue, String serverSideScript) throws IOException
+    {
         List<ClientBehavior> cb1 = (clientBehaviors != null) ? clientBehaviors.get(eventName) : null;
         List<ClientBehavior> cb2 = (clientBehaviors != null) ? clientBehaviors.get(eventName2) : null;
 
@@ -2564,20 +2674,20 @@ public final class HtmlRendererUtils {
                         cb1.get(0).getScript(
                                 ClientBehaviorContext
                                         .createClientBehaviorContext(facesContext, component,
-                                                eventName, component.getClientId(facesContext),
+                                                eventName, targetClientId,
                                                 eventParameters)));
             } else {
                 return renderHTMLAttribute(writer, componentProperty, htmlAttrName,
                         cb2.get(0).getScript(
                                 ClientBehaviorContext
                                         .createClientBehaviorContext(facesContext, component,
-                                                eventName2, component.getClientId(facesContext),
+                                                eventName2, targetClientId,
                                                 eventParameters2)));
             }
         } else {
             return renderHTMLAttribute(writer, componentProperty, htmlAttrName,
                     HtmlRendererUtils.buildBehaviorChain(
-                            facesContext, component,
+                            facesContext, component, targetClientId,
                             eventName, eventParameters,
                             eventName2, eventParameters2,
                             clientBehaviors, attributeValue, serverSideScript));
@@ -2595,25 +2705,31 @@ public final class HtmlRendererUtils {
     public static void renderBehaviorizedEventHandlers(
             FacesContext facesContext, ResponseWriter writer, UIComponent uiComponent,
             Map<String, List<ClientBehavior>> clientBehaviors) throws IOException {
-        renderBehaviorizedAttribute(facesContext, writer, HTML.ONCLICK_ATTR, uiComponent,
+        renderBehaviorizedEventHandlers(facesContext, writer, uiComponent, uiComponent.getClientId(facesContext), clientBehaviors);
+    }
+    
+    public static void renderBehaviorizedEventHandlers(
+            FacesContext facesContext, ResponseWriter writer, UIComponent uiComponent, String targetClientId,
+            Map<String, List<ClientBehavior>> clientBehaviors) throws IOException {
+        renderBehaviorizedAttribute(facesContext, writer, HTML.ONCLICK_ATTR, uiComponent, targetClientId,
                 ClientBehaviorEvents.CLICK, clientBehaviors, HTML.ONCLICK_ATTR);
-        renderBehaviorizedAttribute(facesContext, writer, HTML.ONDBLCLICK_ATTR, uiComponent,
+        renderBehaviorizedAttribute(facesContext, writer, HTML.ONDBLCLICK_ATTR, uiComponent, targetClientId,
                 ClientBehaviorEvents.DBLCLICK, clientBehaviors, HTML.ONDBLCLICK_ATTR);
-        renderBehaviorizedAttribute(facesContext, writer, HTML.ONMOUSEDOWN_ATTR, uiComponent,
+        renderBehaviorizedAttribute(facesContext, writer, HTML.ONMOUSEDOWN_ATTR, uiComponent, targetClientId,
                 ClientBehaviorEvents.MOUSEDOWN, clientBehaviors, HTML.ONMOUSEDOWN_ATTR);
-        renderBehaviorizedAttribute(facesContext, writer, HTML.ONMOUSEUP_ATTR, uiComponent,
+        renderBehaviorizedAttribute(facesContext, writer, HTML.ONMOUSEUP_ATTR, uiComponent, targetClientId,
                 ClientBehaviorEvents.MOUSEUP, clientBehaviors, HTML.ONMOUSEUP_ATTR);
-        renderBehaviorizedAttribute(facesContext, writer, HTML.ONMOUSEOVER_ATTR, uiComponent,
+        renderBehaviorizedAttribute(facesContext, writer, HTML.ONMOUSEOVER_ATTR, uiComponent, targetClientId,
                 ClientBehaviorEvents.MOUSEOVER, clientBehaviors, HTML.ONMOUSEOVER_ATTR);
-        renderBehaviorizedAttribute(facesContext, writer, HTML.ONMOUSEMOVE_ATTR, uiComponent,
+        renderBehaviorizedAttribute(facesContext, writer, HTML.ONMOUSEMOVE_ATTR, uiComponent, targetClientId,
                 ClientBehaviorEvents.MOUSEMOVE, clientBehaviors, HTML.ONMOUSEMOVE_ATTR);
-        renderBehaviorizedAttribute(facesContext, writer, HTML.ONMOUSEOUT_ATTR, uiComponent,
+        renderBehaviorizedAttribute(facesContext, writer, HTML.ONMOUSEOUT_ATTR, uiComponent, targetClientId,
                 ClientBehaviorEvents.MOUSEOUT, clientBehaviors, HTML.ONMOUSEOUT_ATTR);
-        renderBehaviorizedAttribute(facesContext, writer, HTML.ONKEYPRESS_ATTR, uiComponent,
+        renderBehaviorizedAttribute(facesContext, writer, HTML.ONKEYPRESS_ATTR, uiComponent, targetClientId,
                 ClientBehaviorEvents.KEYPRESS, clientBehaviors, HTML.ONKEYPRESS_ATTR);
-        renderBehaviorizedAttribute(facesContext, writer, HTML.ONKEYDOWN_ATTR, uiComponent,
+        renderBehaviorizedAttribute(facesContext, writer, HTML.ONKEYDOWN_ATTR, uiComponent, targetClientId,
                 ClientBehaviorEvents.KEYDOWN, clientBehaviors, HTML.ONKEYDOWN_ATTR);
-        renderBehaviorizedAttribute(facesContext, writer, HTML.ONKEYUP_ATTR, uiComponent,
+        renderBehaviorizedAttribute(facesContext, writer, HTML.ONKEYUP_ATTR, uiComponent, targetClientId,
                 ClientBehaviorEvents.KEYUP, clientBehaviors, HTML.ONKEYUP_ATTR);
     }
 
@@ -2719,6 +2835,18 @@ public final class HtmlRendererUtils {
         renderBehaviorizedAttribute(facesContext, writer, HTML.ONSELECT_ATTR, uiComponent,
                 ClientBehaviorEvents.SELECT, clientBehaviors, HTML.ONSELECT_ATTR);
     }
+    
+    public static void renderBehaviorizedFieldEventHandlersWithoutOnchange(
+            FacesContext facesContext, ResponseWriter writer, UIComponent uiComponent, String targetClientId,
+            Map<String, List<ClientBehavior>> clientBehaviors) throws IOException {
+        renderBehaviorizedAttribute(facesContext, writer, HTML.ONFOCUS_ATTR, uiComponent, targetClientId,
+                ClientBehaviorEvents.FOCUS, clientBehaviors, HTML.ONFOCUS_ATTR);
+        renderBehaviorizedAttribute(facesContext, writer, HTML.ONBLUR_ATTR, uiComponent, targetClientId,
+                ClientBehaviorEvents.BLUR, clientBehaviors, HTML.ONBLUR_ATTR);
+        renderBehaviorizedAttribute(facesContext, writer, HTML.ONSELECT_ATTR, uiComponent, targetClientId,
+                ClientBehaviorEvents.SELECT, clientBehaviors, HTML.ONSELECT_ATTR);
+    }
+
 
     public static void renderBehaviorizedFieldEventHandlersWithoutOnchangeAndOnselect(
             FacesContext facesContext, ResponseWriter writer, UIComponent uiComponent,
@@ -2760,6 +2888,29 @@ public final class HtmlRendererUtils {
             return HtmlRendererUtils.renderHTMLAttribute(writer, uiComponent, HTML.ONCHANGE_ATTR, HTML.ONCHANGE_ATTR);
         }
     }
+    
+    public static boolean renderBehaviorizedOnchangeEventHandler(
+            FacesContext facesContext, ResponseWriter writer, UIComponent uiComponent, String targetClientId,
+            Map<String, List<ClientBehavior>> clientBehaviors) throws IOException {
+        boolean hasChange = HtmlRendererUtils.hasClientBehavior(ClientBehaviorEvents.CHANGE, clientBehaviors, facesContext);
+        boolean hasValueChange = HtmlRendererUtils.hasClientBehavior(ClientBehaviorEvents.VALUECHANGE, clientBehaviors, facesContext);
+
+        if (hasChange && hasValueChange) {
+            String chain = HtmlRendererUtils.buildBehaviorChain(facesContext,
+                    uiComponent, targetClientId, ClientBehaviorEvents.CHANGE, null, ClientBehaviorEvents.VALUECHANGE, null, clientBehaviors,
+                    (String) uiComponent.getAttributes().get(HTML.ONCHANGE_ATTR), null);
+
+            return HtmlRendererUtils.renderHTMLAttribute(writer, HTML.ONCHANGE_ATTR, HTML.ONCHANGE_ATTR, chain);
+        } else if (hasChange) {
+            return HtmlRendererUtils.renderBehaviorizedAttribute(facesContext, writer, HTML.ONCHANGE_ATTR, uiComponent, targetClientId,
+                    ClientBehaviorEvents.CHANGE, clientBehaviors, HTML.ONCHANGE_ATTR);
+        } else if (hasValueChange) {
+            return HtmlRendererUtils.renderBehaviorizedAttribute(facesContext, writer, HTML.ONCHANGE_ATTR, uiComponent, targetClientId,
+                    ClientBehaviorEvents.VALUECHANGE, clientBehaviors, HTML.ONCHANGE_ATTR);
+        } else {
+            return HtmlRendererUtils.renderHTMLAttribute(writer, uiComponent, HTML.ONCHANGE_ATTR, HTML.ONCHANGE_ATTR);
+        }
+    }    
 
     public static void renderViewStateJavascript(FacesContext facesContext, String hiddenId, String serializedState) throws IOException {
         ResponseWriter writer = facesContext.getResponseWriter();