[seam-commits] Seam SVN: r10704 - modules/trunk/faces/src/main/java/org/jboss/seam/faces.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Wed Apr 29 02:01:29 EDT 2009


Author: dan.j.allen
Date: 2009-04-29 02:01:29 -0400 (Wed, 29 Apr 2009)
New Revision: 10704

Added:
   modules/trunk/faces/src/main/java/org/jboss/seam/faces/FacesStatusMessages.java
Removed:
   modules/trunk/faces/src/main/java/org/jboss/seam/faces/FacesMessages.java
Modified:
   modules/trunk/faces/src/main/java/org/jboss/seam/faces/FacesExpressions.java
Log:
make FacesExpressions and FacesStatusMessages specialize standard components from international package


Modified: modules/trunk/faces/src/main/java/org/jboss/seam/faces/FacesExpressions.java
===================================================================
--- modules/trunk/faces/src/main/java/org/jboss/seam/faces/FacesExpressions.java	2009-04-29 06:00:15 UTC (rev 10703)
+++ modules/trunk/faces/src/main/java/org/jboss/seam/faces/FacesExpressions.java	2009-04-29 06:01:29 UTC (rev 10704)
@@ -1,10 +1,10 @@
 //$Id: FacesExpressions.java 9684 2008-12-01 21:41:20Z dan.j.allen $
 package org.jboss.seam.faces;
 
-import javax.annotation.Named;
 import javax.context.ApplicationScoped;
 import javax.el.ELContext;
 import javax.faces.context.FacesContext;
+import javax.inject.Specializes;
 import org.jboss.seam.el.Expressions;
 
 /**
@@ -13,7 +13,8 @@
  * @author Gavin King
  */
 public
- at Named
+ at Faces
+ at Specializes
 @ApplicationScoped
 class FacesExpressions extends Expressions
 {

Deleted: modules/trunk/faces/src/main/java/org/jboss/seam/faces/FacesMessages.java
===================================================================
--- modules/trunk/faces/src/main/java/org/jboss/seam/faces/FacesMessages.java	2009-04-29 06:00:15 UTC (rev 10703)
+++ modules/trunk/faces/src/main/java/org/jboss/seam/faces/FacesMessages.java	2009-04-29 06:01:29 UTC (rev 10704)
@@ -1,139 +0,0 @@
-package org.jboss.seam.faces;
-
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-import javax.context.ConversationScoped;
-import javax.faces.application.FacesMessage;
-import javax.faces.component.UIComponent;
-import javax.faces.context.FacesContext;
-
-import org.jboss.seam.international.StatusMessage;
-import org.jboss.seam.international.StatusMessages;
-
-/**
- * A Seam component that propagates FacesMessages across redirects
- * and interpolates EL expressions in the message string.
- * 
- * @author Gavin King
- * @author Pete Muir
- */
- at ConversationScoped
-public class FacesMessages extends StatusMessages
-{
-   /**
-    * Called by Seam to transfer messages from Seam status messages to real JSF messages
-    */
-   public void beforeRenderView()
-   {
-      for (StatusMessage statusMessage : getMessages())
-      {
-         FacesContext.getCurrentInstance().addMessage(null, toFacesMessage(statusMessage));
-      }
-      for (Map.Entry<String, List<StatusMessage>> entry : getKeyedMessages().entrySet())
-      {
-         for (StatusMessage statusMessage : entry.getValue())
-         {
-            String clientId = getClientId(entry.getKey());
-            FacesContext.getCurrentInstance().addMessage(clientId, toFacesMessage(statusMessage));
-         }
-      }
-      clear();
-   }
-
-   /**
-    * Convert a StatusMessage to a FacesMessage
-    */
-   private static FacesMessage toFacesMessage(StatusMessage statusMessage)
-   {
-      if (statusMessage.getSummary() != null && statusMessage.getSummary().length() > 0)
-      {
-         return new FacesMessage(toSeverity(statusMessage.getSeverity()), statusMessage.getSummary(), statusMessage.getDetail());
-      }
-      else
-      {
-         return null;
-      }
-   }
-
-   /**
-    * Convert a StatusMessage.Severity to a FacesMessage.Severity
-    */
-   private static javax.faces.application.FacesMessage.Severity toSeverity(org.jboss.seam.international.StatusMessage.Severity severity)
-   {
-      switch (severity)
-      {
-         case ERROR:
-            return FacesMessage.SEVERITY_ERROR;
-         case FATAL:
-            return FacesMessage.SEVERITY_FATAL;
-         case INFO:
-            return FacesMessage.SEVERITY_INFO;
-         case WARN:
-            return FacesMessage.SEVERITY_WARN;
-         default:
-            return null;
-      }
-   }
-
-   /**
-    * Convert a FacesMessage.Severity to a StatusMessage.Severity
-    */
-   private static org.jboss.seam.international.StatusMessage.Severity toSeverity(javax.faces.application.FacesMessage.Severity severity)
-   {
-      if (FacesMessage.SEVERITY_ERROR.equals(severity))
-      {
-         return org.jboss.seam.international.StatusMessage.Severity.ERROR;
-      }
-      else if (FacesMessage.SEVERITY_FATAL.equals(severity))
-      {
-         return org.jboss.seam.international.StatusMessage.Severity.FATAL;
-      }
-      else if (FacesMessage.SEVERITY_INFO.equals(severity))
-      {
-         return org.jboss.seam.international.StatusMessage.Severity.INFO;
-      }
-      else if (FacesMessage.SEVERITY_WARN.equals(severity))
-      {
-         return org.jboss.seam.international.StatusMessage.Severity.WARN;
-      }
-      else
-      {
-         return null;
-      }
-   }
-
-   /**
-    * Calculate the JSF client ID from the provided widget ID
-    */
-   private String getClientId(String id)
-   {
-      FacesContext facesContext = FacesContext.getCurrentInstance();
-      return getClientId(facesContext.getViewRoot(), id, facesContext);
-   }
-
-   private static String getClientId(UIComponent component, String id, FacesContext facesContext)
-   {
-      String componentId = component.getId();
-      if (componentId != null && componentId.equals(id))
-      {
-         return component.getClientId(facesContext);
-      }
-      else
-      {
-         Iterator iter = component.getFacetsAndChildren();
-         while (iter.hasNext())
-         {
-            UIComponent child = (UIComponent) iter.next();
-            String clientId = getClientId(child, id, facesContext);
-            if (clientId != null)
-            {
-               return clientId;
-            }
-         }
-         return null;
-      }
-   }
-}

Copied: modules/trunk/faces/src/main/java/org/jboss/seam/faces/FacesStatusMessages.java (from rev 10668, modules/trunk/faces/src/main/java/org/jboss/seam/faces/FacesMessages.java)
===================================================================
--- modules/trunk/faces/src/main/java/org/jboss/seam/faces/FacesStatusMessages.java	                        (rev 0)
+++ modules/trunk/faces/src/main/java/org/jboss/seam/faces/FacesStatusMessages.java	2009-04-29 06:01:29 UTC (rev 10704)
@@ -0,0 +1,170 @@
+package org.jboss.seam.faces;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javax.context.ConversationScoped;
+import javax.faces.application.FacesMessage;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+
+import javax.inject.Initializer;
+import javax.inject.Specializes;
+import org.jboss.seam.international.Interpolator;
+import org.jboss.seam.international.StatusMessage;
+import org.jboss.seam.international.StatusMessages;
+import org.jboss.webbeans.log.LogProvider;
+import org.jboss.webbeans.log.Logging;
+
+/**
+ * <p>A bean which specializes StatusMessage to provide support for
+ * JSF. Generic StatusMessage objects are translated into JSF
+ * FacesMessage objects just prior to when the view is rendered.</p>
+ *
+ * <p>To access the JSF FacesMessage objects once they have been registered
+ * with the FacesContext, you should use the methods getMessageList()
+ * and getMessagesList(String), which were added to FacesContext in JSF 2.0.</p>
+ *
+ * <p>For instance, to retrieve the list of global messages, you can use
+ * the following value expression in your page:</p>
+ *
+ * <pre>#{facesContext.getMessageList(null)}</pre>
+ *
+ * <p>To retrieve the list of messages for a "client id", you can use this
+ * expression:</p>
+ *
+ * <pre>#{facesContext.getMessageList('username')}</pre>
+ *
+ * <p>These examples rely, of course, on the JBoss EL.</p>
+ *
+ * QUESTION: should we set Flash#setKeepMessages(boolean)?
+ *
+ * @author Gavin King
+ * @author Pete Muir
+ */
+public
+ at Faces
+ at Specializes
+ at ConversationScoped
+class FacesStatusMessages extends StatusMessages
+{
+   private static final LogProvider log = Logging.getLogProvider(FacesStatusMessages.class);
+
+   public FacesStatusMessages()
+   {
+      super();
+   }
+
+   public @Initializer FacesStatusMessages(Interpolator interpolator)
+   {
+      super(interpolator);
+   }
+
+   /**
+    * Called by a JSF SystemEventListener listening for the PreRenderViewEvent
+    * to transpose Seam status messages to real JSF messages and register them
+    * with the FacesContext.
+    */
+   @Override
+   public void onBeforeRender()
+   {
+      super.onBeforeRender();
+      FacesContext facesContext = FacesContext.getCurrentInstance();
+      for (StatusMessage statusMessage : getGlobalMessages())
+      {
+         facesContext.addMessage(null, toFacesMessage(statusMessage));
+      }
+      for (Map.Entry<String, List<StatusMessage>> messagesForKey : getKeyedMessages().entrySet())
+      {
+         String clientId = getClientId(messagesForKey.getKey(), facesContext);
+         if (clientId == null)
+         {
+            log.warn("Could not locate clientId " + clientId + " when registering JSF messages for control. A global message will be created instead.");
+         }
+         for (StatusMessage statusMessage : messagesForKey.getValue())
+         {
+            facesContext.addMessage(clientId, toFacesMessage(statusMessage));
+         }
+      }
+      clear();
+   }
+
+   /**
+    * Convert a StatusMessage to a FacesMessage
+    */
+   private FacesMessage toFacesMessage(StatusMessage statusMessage)
+   {
+      if (statusMessage.getSummary() != null && statusMessage.getSummary().length() > 0)
+      {
+         return new FacesMessage(toFacesSeverity(statusMessage.getSeverity()), statusMessage.getSummary(), statusMessage.getDetail());
+      }
+      else
+      {
+         return null;
+      }
+   }
+
+   /**
+    * Convert a StatusMessage.Severity to a FacesMessage.Severity
+    */
+   private javax.faces.application.FacesMessage.Severity toFacesSeverity(org.jboss.seam.international.StatusMessage.Severity severity)
+   {
+      switch (severity)
+      {
+         case ERROR:
+            return FacesMessage.SEVERITY_ERROR;
+         case FATAL:
+            return FacesMessage.SEVERITY_FATAL;
+         case WARN:
+            return FacesMessage.SEVERITY_WARN;
+         case INFO:
+         default:
+            return FacesMessage.SEVERITY_INFO;
+      }
+   }
+
+   /**
+    * Calculate the JSF client ID from the provided widget ID.
+    * TODO It would be great if this could do suffix maching.
+    */
+   private String getClientId(String targetId, FacesContext facesContext)
+   {
+      if (isAbsoluteClientIdPresent(targetId, facesContext))
+      {
+         return targetId;
+      }
+      else
+      {
+         return getClientId(facesContext.getViewRoot(), targetId, facesContext);
+      }
+   }
+
+   private boolean isAbsoluteClientIdPresent(String targetId, FacesContext facesContext)
+   {
+      return facesContext.getViewRoot().findComponent(":" + targetId) != null;
+   }
+
+   private String getClientId(UIComponent component, String targetLocalId, FacesContext facesContext)
+   {
+      String currentLocalId = component.getId();
+      if (currentLocalId != null && currentLocalId.equals(targetLocalId))
+      {
+         return component.getClientId(facesContext);
+      }
+      else
+      {
+         Iterator iter = component.getFacetsAndChildren();
+         while (iter.hasNext())
+         {
+            UIComponent child = (UIComponent) iter.next();
+            String clientId = getClientId(child, targetLocalId, facesContext);
+            if (clientId != null)
+            {
+               return clientId;
+            }
+         }
+         return null;
+      }
+   }
+}




More information about the seam-commits mailing list