[seam-commits] Seam SVN: r12520 - in modules/faces/trunk: impl/src/main/java/org/jboss/seam/faces/event and 1 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Fri Apr 16 17:52:01 EDT 2010


Author: nickarls
Date: 2010-04-16 17:52:00 -0400 (Fri, 16 Apr 2010)
New Revision: 12520

Added:
   modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/event/qualifier/View.java
Modified:
   modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/event/SystemEventBridge.java
   modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/event/SystemEventBridgeTest.java
   modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/event/SystemEventObserver.java
Log:
@View

Added: modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/event/qualifier/View.java
===================================================================
--- modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/event/qualifier/View.java	                        (rev 0)
+++ modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/event/qualifier/View.java	2010-04-16 21:52:00 UTC (rev 12520)
@@ -0,0 +1,44 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.seam.faces.event.qualifier;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.PARAMETER;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import javax.inject.Qualifier;
+
+/**
+ * Qualifies observer method parameters to select events for a particular JSF view
+ * 
+ * @author Nicklas Karlsson
+ */
+ at Qualifier
+ at Target( { FIELD, PARAMETER })
+ at Retention(RUNTIME)
+public @interface View
+{
+   String value() default "";
+}
\ No newline at end of file

Modified: modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/event/SystemEventBridge.java
===================================================================
--- modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/event/SystemEventBridge.java	2010-04-16 21:16:14 UTC (rev 12519)
+++ modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/event/SystemEventBridge.java	2010-04-16 21:52:00 UTC (rev 12520)
@@ -21,17 +21,24 @@
  */
 package org.jboss.seam.faces.event;
 
+import java.lang.annotation.Annotation;
+
 import javax.enterprise.context.ApplicationScoped;
 import javax.enterprise.event.Observes;
 import javax.enterprise.util.AnnotationLiteral;
+import javax.faces.component.UIViewRoot;
 import javax.faces.event.AbortProcessingException;
 import javax.faces.event.ComponentSystemEvent;
 import javax.faces.event.PostConstructApplicationEvent;
+import javax.faces.event.PostConstructViewMapEvent;
+import javax.faces.event.PreDestroyViewMapEvent;
+import javax.faces.event.PreRenderViewEvent;
 import javax.faces.event.SystemEvent;
 import javax.faces.event.SystemEventListener;
 
 import org.jboss.seam.faces.cdi.BeanManagerAware;
 import org.jboss.seam.faces.event.qualifier.Component;
+import org.jboss.seam.faces.event.qualifier.View;
 
 /**
  * A SystemEventListener used to bridge JSF system events to the CDI event
@@ -68,18 +75,33 @@
    public void processEvent(final SystemEvent e) throws AbortProcessingException
    {
       Object payload = e.getClass().cast(e);
-      if (e instanceof ComponentSystemEvent)
+      Annotation[] qualifiers = getQualifiers(e);
+      getBeanManager().fireEvent(payload, qualifiers);
+   }
+
+   private Annotation[] getQualifiers(SystemEvent e)
+   {
+      if (isViewEvent(e))
       {
-         ComponentSystemEvent ce = (ComponentSystemEvent) e;
-         String id = ce.getComponent().getId();
-         getBeanManager().fireEvent(e, new ComponentLiteral(id));
+         String id = ((UIViewRoot) e.getSource()).getViewId();
+         return new Annotation[] { new ViewLiteral(id) };
       }
+      else if (e instanceof ComponentSystemEvent)
+      {
+         String id = ((ComponentSystemEvent) e).getComponent().getId();
+         return new Annotation[] { new ComponentLiteral(id) };
+      }
       else
       {
-         getBeanManager().fireEvent(payload);
+         return new Annotation[] {};
       }
    }
 
+   private boolean isViewEvent(SystemEvent e)
+   {
+      return (e instanceof PreRenderViewEvent) || (e instanceof PostConstructViewMapEvent) || (e instanceof PreDestroyViewMapEvent);
+   }
+
    private class ComponentLiteral extends AnnotationLiteral<Component> implements Component
    {
       private final String value;
@@ -93,6 +115,21 @@
       {
          this.value = value;
       }
-   }   
+   }
 
+   private class ViewLiteral extends AnnotationLiteral<View> implements View
+   {
+      private final String value;
+
+      public String value()
+      {
+         return value;
+      }
+
+      public ViewLiteral(String value)
+      {
+         this.value = value;
+      }
+   }
+
 }

Modified: modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/event/SystemEventBridgeTest.java
===================================================================
--- modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/event/SystemEventBridgeTest.java	2010-04-16 21:16:14 UTC (rev 12519)
+++ modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/event/SystemEventBridgeTest.java	2010-04-16 21:52:00 UTC (rev 12520)
@@ -83,10 +83,12 @@
    private final ScopeContext scopeContext = new ScopeContext("foo", new HashMap<String, Object>());
    private final ExceptionQueuedEventContext eventContext = new ExceptionQueuedEventContext(facesContext, new NullPointerException());
    private static final UIComponent component = new UIOutput();
+   private static final UIViewRoot uiViewRoot = new UIViewRoot();
 
    static
    {
       component.setId("foo");
+      uiViewRoot.setViewId("foo.xhtml");
    }
 
    @Test
@@ -170,8 +172,14 @@
    @Test
    public void testObservePostConstructViewMap()
    {
-      fireAndAssert("14", new PostConstructViewMapEvent(new UIViewRoot()));
+      fireAndAssert("14", new PostConstructViewMapEvent(uiViewRoot));
    }
+   
+   @Test
+   public void testObservePostConstructSpecificViewMap()
+   {
+      fireAndAssert("14a", new PostConstructViewMapEvent(uiViewRoot));
+   }   
 
    @Test
    public void testObservePostRestoreState()
@@ -188,10 +196,16 @@
    @Test
    public void testObservePreDestroyViewMap()
    {
-      fireAndAssert("17", new PreDestroyViewMapEvent(new UIViewRoot()));
+      fireAndAssert("17", new PreDestroyViewMapEvent(uiViewRoot));
    }  
    
    @Test
+   public void testObservePreDestroySpecificViewMap()
+   {
+      fireAndAssert("17a", new PreDestroyViewMapEvent(uiViewRoot));
+   }  
+   
+   @Test
    public void testObservePreRemoveFromView()
    {
       fireAndAssert("18", new PreRemoveFromViewEvent(component));
@@ -218,10 +232,16 @@
    @Test
    public void testObservePreRenderView()
    {
-      fireAndAssert("22", new PreRenderViewEvent(new UIViewRoot()));
+      fireAndAssert("22", new PreRenderViewEvent(uiViewRoot));
    }    
    
+   @Test
+   public void testObservePreRenderSpecificView()
+   {
+      fireAndAssert("23", new PreRenderViewEvent(uiViewRoot));
+   }    
    
+   
    private void fireAndAssert(String caseId, SystemEvent... events)
    {
       observer.reset();

Modified: modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/event/SystemEventObserver.java
===================================================================
--- modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/event/SystemEventObserver.java	2010-04-16 21:16:14 UTC (rev 12519)
+++ modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/event/SystemEventObserver.java	2010-04-16 21:52:00 UTC (rev 12520)
@@ -47,6 +47,7 @@
 import javax.faces.event.SystemEvent;
 
 import org.jboss.seam.faces.event.qualifier.Component;
+import org.jboss.seam.faces.event.qualifier.View;
 
 /**
  * 
@@ -146,11 +147,16 @@
       recordObservation("13", e);
    }   
 
-   public void observe2(@Observes PostConstructViewMapEvent e)
+   public void observe(@Observes PostConstructViewMapEvent e)
    {
       recordObservation("14", e);
+   }
+   
+   public void observe2(@Observes @View("foo.xhtml") PostConstructViewMapEvent e)
+   {
+      recordObservation("14a", e);
    } 
-
+   
    public void observe(@Observes PostRestoreStateEvent e)
    {
       recordObservation("15", e);
@@ -161,11 +167,16 @@
       recordObservation("16", e);
    }
    
-   public void observe2(@Observes PreDestroyViewMapEvent e)
+   public void observe(@Observes PreDestroyViewMapEvent e)
    {
       recordObservation("17", e);
    }    
    
+   public void observe2(@Observes @View("foo.xhtml") PreDestroyViewMapEvent e)
+   {
+      recordObservation("17a", e);
+   }    
+   
    public void observe(@Observes PreRemoveFromViewEvent e)
    {
       recordObservation("18", e);
@@ -190,4 +201,9 @@
    {
       recordObservation("22", e);
    }    
+   
+   public void observe2(@Observes @View("foo.xhtml") PreRenderViewEvent e)
+   {
+      recordObservation("23", e);
+   }  
 }



More information about the seam-commits mailing list