You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by bo...@apache.org on 2010/11/17 20:49:15 UTC

svn commit: r1036180 - in /myfaces/tobago/trunk: tobago-core/src/main/java/org/apache/myfaces/tobago/ajax/ tobago-core/src/main/java/org/apache/myfaces/tobago/internal/ajax/ tobago-extension/tobago-deprecation/src/main/java/org/apache/myfaces/tobago/aj...

Author: bommel
Date: Wed Nov 17 19:49:14 2010
New Revision: 1036180

URL: http://svn.apache.org/viewvc?rev=1036180&view=rev
Log:
cleanup AjaxUtils

(TOBAGO-942) Example for adding UIMessages automatic to renderedPartially if the facesContext contains messages

Modified:
    myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/ajax/AjaxUtils.java
    myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/ajax/AjaxInternalUtils.java
    myfaces/tobago/trunk/tobago-extension/tobago-deprecation/src/main/java/org/apache/myfaces/tobago/ajax/api/AjaxUtils.java

Modified: myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/ajax/AjaxUtils.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/ajax/AjaxUtils.java?rev=1036180&r1=1036179&r2=1036180&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/ajax/AjaxUtils.java (original)
+++ myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/ajax/AjaxUtils.java Wed Nov 17 19:49:14 2010
@@ -18,12 +18,15 @@ package org.apache.myfaces.tobago.ajax;
  */
 
 
+import org.apache.myfaces.tobago.util.ComponentUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.apache.myfaces.tobago.internal.ajax.AjaxInternalUtils;
 
 import javax.faces.component.UIComponent;
 import javax.faces.context.FacesContext;
+import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 
 public class AjaxUtils {
@@ -56,30 +59,31 @@ public class AjaxUtils {
     }
   }
 
-  public static void ensureDecoded(FacesContext facesContext, String clientId) {
-    ensureDecoded(facesContext, facesContext.getViewRoot().findComponent(clientId));
-  }
-
-  public static void ensureDecoded(FacesContext facesContext, UIComponent component) {
-    if (component == null) {
-      LOG.warn("Ignore AjaxComponent: null");
-      return;
+  /**
+   *
+   * @param context
+   * @return true if a UIMessage component has added to renderedPartially
+   */
+  public static boolean addUIMessagesToRenderedPartially(FacesContext context) {
+    if (!isAjaxRequest(context)) {
+      return false;
     }
-    Map<String, UIComponent> ajaxComponents = AjaxInternalUtils.getAjaxComponents(facesContext);
-    if (ajaxComponents != null) {
-      for (UIComponent uiComponent : ajaxComponents.values()) {
-        // is component or a parent of it in the list?
-        UIComponent parent = component;
-        while (parent != null) {
-          if (component == uiComponent) {
-            // nothing to do, because it was already decoded (in the list)
-            return;
-          }
-          parent = parent.getParent();
+    List<String> list = AjaxInternalUtils.getMessagesComponentIds(context);
+    Iterator clientIds = context.getClientIdsWithMessages();
+    boolean added = false;
+
+    if (clientIds.hasNext()) { // messages in the partial part
+      for (String componentClientId: list) {
+        added = AjaxInternalUtils.addNextPossibleAjaxComponent(context, componentClientId);
+      }
+    } else {  // checking for an existing shown error on page
+      for (String componentClientId: list) {
+        if (context.getExternalContext().getRequestParameterMap().containsKey(
+            componentClientId + ComponentUtils.SUB_SEPARATOR + "messagesExists")) {
+          added = AjaxInternalUtils.addNextPossibleAjaxComponent(context, componentClientId);
         }
       }
-      component.processDecodes(facesContext);
     }
+    return added;
   }
-  
 }

Modified: myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/ajax/AjaxInternalUtils.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/ajax/AjaxInternalUtils.java?rev=1036180&r1=1036179&r2=1036180&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/ajax/AjaxInternalUtils.java (original)
+++ myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/ajax/AjaxInternalUtils.java Wed Nov 17 19:49:14 2010
@@ -20,15 +20,20 @@ package org.apache.myfaces.tobago.intern
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.myfaces.tobago.ajax.AjaxUtils;
+import org.apache.myfaces.tobago.context.TobagoFacesContext;
+import org.apache.myfaces.tobago.internal.component.AbstractUIMessages;
 import org.apache.myfaces.tobago.renderkit.RendererBase;
 import org.apache.myfaces.tobago.util.ComponentUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import javax.faces.component.UIComponent;
+import javax.faces.component.UIPanel;
 import javax.faces.context.FacesContext;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.StringTokenizer;
 
@@ -38,6 +43,8 @@ public class AjaxInternalUtils {
 
   public static final String AJAX_COMPONENTS = AjaxUtils.class.getName() + ".AJAX_COMPONENTS";
 
+  private static final String TOBAGO_MESSAGES_CLIENT_IDS = "tobago.messages.clientIds";
+
   public static void checkParamValidity(FacesContext facesContext, UIComponent uiComponent, Class compClass) {
     if (facesContext == null) {
       throw new NullPointerException("facesContext may not be null");
@@ -67,6 +74,47 @@ public class AjaxInternalUtils {
     }
   }
 
+  public static void setRenderAllComponents(FacesContext facesContext) {
+    Map<String, UIComponent> ajaxComponents = new HashMap<String, UIComponent>();
+    facesContext.getExternalContext().getRequestMap().put(AJAX_COMPONENTS, ajaxComponents);
+    javax.faces.component.UIViewRoot viewRoot = facesContext.getViewRoot();
+    UIComponent page = viewRoot.getChildren().get(0);
+    ajaxComponents.put(page.getClientId(facesContext), page);
+  }
+
+  public static void storeMessagesClientIds(TobagoFacesContext facesContext, AbstractUIMessages messages) {
+    Map<Object, Object> attributes = facesContext.getAttributes();
+    List<String> messageClientIds;
+    if (attributes.containsKey(TOBAGO_MESSAGES_CLIENT_IDS)) {
+      messageClientIds = (List<String>) attributes.get(TOBAGO_MESSAGES_CLIENT_IDS);
+    } else {
+      messageClientIds = new ArrayList<String>();
+      attributes.put(TOBAGO_MESSAGES_CLIENT_IDS, messageClientIds);
+    }
+    messageClientIds.add(messages.getClientId(facesContext));
+  }
+
+  public static List<String> getMessagesClientIds(TobagoFacesContext facesContext) {
+     return (List<String>) facesContext.getAttributes().get(TOBAGO_MESSAGES_CLIENT_IDS);
+  }
+
+  public static List<String> getMessagesComponentIds(FacesContext facesContext) {
+    Map parameterMap = facesContext.getExternalContext().getRequestParameterMap();
+    UIComponent component = facesContext.getViewRoot().getChildren().get(0);
+    String clientId = component.getClientId(facesContext);
+    String ids = (String) parameterMap.get(clientId + ComponentUtils.SUB_SEPARATOR + "messagesClientIds");
+    List<String> list = new ArrayList<String>();
+    if (ids != null) {
+      StringTokenizer tokenizer = new StringTokenizer(ids, ",");
+      while (tokenizer.hasMoreTokens()) {
+        String id = tokenizer.nextToken();
+        list.add(id);
+      }
+    }
+    return list;
+
+  }
+
   public static Map<String, UIComponent> parseAndStoreComponents(FacesContext facesContext) {
     Map parameterMap = facesContext.getExternalContext().getRequestParameterMap();
     String ajaxComponentIds = (String) parameterMap.get("tobago::partialIds");
@@ -100,10 +148,64 @@ public class AjaxInternalUtils {
         facesContext.getExternalContext().getRequestMap().get(AJAX_COMPONENTS);
   }
 
+  public static void addAjaxComponent(FacesContext facesContext, UIComponent component) {
+    Map<String, UIComponent> ajaxComponents =
+        (Map<String, UIComponent>) facesContext.getExternalContext().getRequestMap().get(AJAX_COMPONENTS);
+    if (ajaxComponents != null) {
+      if (!alreadyContained(component, ajaxComponents)) {
+        ajaxComponents.put(component.getClientId(facesContext), component);
+      }
+    }
+  }
+
   public static String encodeJavaScriptString(String value) {
     String result = StringUtils.replace(value, "\\", "\\\\");
     result = StringUtils.replace(result, "\n", "\\n");
     result = StringUtils.replace(result, "\r", "\\r");
     return StringUtils.replace(result, "\"", "\\\"");
   }
+
+  public static boolean alreadyContained(UIComponent component, Map<String, UIComponent> ajaxComponents) {
+    for (UIComponent uiComponent : ajaxComponents.values()) {
+      // is component or a parent of it in the list?
+      UIComponent parent = component;
+      while (parent != null) {
+        if (component == uiComponent) {
+          // nothing to do, because it was already decoded (in the list)
+          return true;
+        }
+        parent = parent.getParent();
+      }
+    }
+    return false;
+  }
+
+  public static boolean addNextPossibleAjaxComponent(FacesContext context, String componentClientId) {
+    UIComponent component = ComponentUtils.findComponent(context.getViewRoot(), componentClientId);
+    component = component.getParent();
+    if (component instanceof UIPanel) {
+      addAjaxComponent(context, component);
+      return true;
+    } else {
+      LOG.error("Ignore adding ajax component (no instance of UIPanel) id: "+ componentClientId + " component: " + component);
+      return false;
+    }
+  }
+
+  public static void ensureDecoded(FacesContext facesContext, String clientId) {
+    ensureDecoded(facesContext, facesContext.getViewRoot().findComponent(clientId));
+  }
+
+  public static void ensureDecoded(FacesContext facesContext, UIComponent component) {
+    if (component == null) {
+      LOG.warn("Ignore AjaxComponent: null");
+      return;
+    }
+    Map<String, UIComponent> ajaxComponents = getAjaxComponents(facesContext);
+    if (ajaxComponents != null) {
+      if (!alreadyContained(component, ajaxComponents)) {
+        component.processDecodes(facesContext);
+      }
+    }
+  }
 }

Modified: myfaces/tobago/trunk/tobago-extension/tobago-deprecation/src/main/java/org/apache/myfaces/tobago/ajax/api/AjaxUtils.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-extension/tobago-deprecation/src/main/java/org/apache/myfaces/tobago/ajax/api/AjaxUtils.java?rev=1036180&r1=1036179&r2=1036180&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-extension/tobago-deprecation/src/main/java/org/apache/myfaces/tobago/ajax/api/AjaxUtils.java (original)
+++ myfaces/tobago/trunk/tobago-extension/tobago-deprecation/src/main/java/org/apache/myfaces/tobago/ajax/api/AjaxUtils.java Wed Nov 17 19:49:14 2010
@@ -102,23 +102,23 @@ public class AjaxUtils {
   }
 
   /**
-   * @deprecated Please use org.apache.myfaces.tobago.ajax.AjaxUtils.ensureDecoded() 
+   * @deprecated Please use org.apache.myfaces.tobago.internal.ajax.AjaxInternalUtils.ensureDecoded()
    */
   @Deprecated
   public static void ensureDecoded(FacesContext facesContext, String clientId) {
-    org.apache.myfaces.tobago.ajax.AjaxUtils.ensureDecoded(facesContext, clientId);
+    AjaxInternalUtils.ensureDecoded(facesContext, clientId);
   }
 
   /**
-   * @deprecated Please use org.apache.myfaces.tobago.ajax.AjaxUtils.ensureDecoded() 
+   * @deprecated Please use org.apache.myfaces.tobago.internal.ajax.AjaxInternalUtils.ensureDecoded()
    */
   @Deprecated
   public static void ensureDecoded(FacesContext facesContext, UIComponent component) {
-    org.apache.myfaces.tobago.ajax.AjaxUtils.ensureDecoded(facesContext, component);
+    AjaxInternalUtils.ensureDecoded(facesContext, component);
   }
 
   /**
-   * @deprecated Please use org.apache.myfaces.tobago.ajax.AjaxInternalUtils.encodeJavaScriptString() 
+   * @deprecated Please use org.apache.myfaces.tobago.internal.ajax.AjaxInternalUtils.encodeJavaScriptString()
    */
   @Deprecated
   public static String encodeJavascriptString(String value) {