Author: andyhot
Date: Fri Oct 19 01:16:01 2007
New Revision: 586311

URL: http://svn.apache.org/viewvc?rev=586311&view=rev
Log:
TAPESTRY-1547: Support inherited-binding in annotations

Modified:
    
tapestry/tapestry4/trunk/tapestry-annotations/src/java/org/apache/tapestry/annotations/Component.java
    
tapestry/tapestry4/trunk/tapestry-annotations/src/java/org/apache/tapestry/annotations/ComponentAnnotationWorker.java
    
tapestry/tapestry4/trunk/tapestry-annotations/src/test/org/apache/tapestry/annotations/AnnotatedPage.java
    
tapestry/tapestry4/trunk/tapestry-annotations/src/test/org/apache/tapestry/annotations/TestComponentAnnotationWorker.java

Modified: 
tapestry/tapestry4/trunk/tapestry-annotations/src/java/org/apache/tapestry/annotations/Component.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-annotations/src/java/org/apache/tapestry/annotations/Component.java?rev=586311&r1=586310&r2=586311&view=diff
==============================================================================
--- 
tapestry/tapestry4/trunk/tapestry-annotations/src/java/org/apache/tapestry/annotations/Component.java
 (original)
+++ 
tapestry/tapestry4/trunk/tapestry-annotations/src/java/org/apache/tapestry/annotations/Component.java
 Fri Oct 19 01:16:01 2007
@@ -62,7 +62,7 @@
 
     /**
      * Bindings for the component. Each binding string is of the format
-     * <code><em>name</em>=<em>binding refernce</em></code>, where the binding 
reference is
+     * <code><em>name</em>=<em>binding reference</em></code>, where the 
binding reference is
      * the same kind of string (possibly with a prefix such as "ognl:" or 
"message:" as would appear
      * in a specification.
      * 
@@ -70,4 +70,15 @@
      */
 
     String[] bindings() default {};
+
+    /**
+     * Inherited bindings bind a parameter of the component to a parameter
+     * of the container. Each binding string is of the format
+     * <code><em>parameter-name</em>=<em>container-parameter-name</em></code>, 
where the former is
+     * the name of the component parameter and the latter is the name of the 
container parameter to bind
+     * the parameter to.
+     * In case both names are the same, it's possible to just use 
<code><em>parameter-name</em></code>.
+     */
+
+    String[] inheritedBindings() default {};
 }

Modified: 
tapestry/tapestry4/trunk/tapestry-annotations/src/java/org/apache/tapestry/annotations/ComponentAnnotationWorker.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-annotations/src/java/org/apache/tapestry/annotations/ComponentAnnotationWorker.java?rev=586311&r1=586310&r2=586311&view=diff
==============================================================================
--- 
tapestry/tapestry4/trunk/tapestry-annotations/src/java/org/apache/tapestry/annotations/ComponentAnnotationWorker.java
 (original)
+++ 
tapestry/tapestry4/trunk/tapestry-annotations/src/java/org/apache/tapestry/annotations/ComponentAnnotationWorker.java
 Fri Oct 19 01:16:01 2007
@@ -77,6 +77,11 @@
             addBinding(cc, binding, location);
         }
 
+        for (String binding : component.inheritedBindings())
+        {
+            addInheritedBinding(cc, binding, location);
+        }
+
         String id = component.id();
 
         if (id.equals(""))
@@ -116,7 +121,35 @@
         bs.setLocation(location);
 
         component.setBinding(name, bs);
-    }    
+    }
+
+    /**
+     * @since 4.1.4
+     */
+    void addInheritedBinding(IContainedComponent component, String binding, 
Location location)
+    {
+        int equalsx = binding.indexOf('=');
+        String name;
+        String containerName;
+
+        if (equalsx < 0)
+        {
+            name = binding.trim();
+            containerName = name;
+        }
+        else
+        {
+            name = binding.substring(0, equalsx).trim();
+            containerName = binding.substring(equalsx + 1).trim();
+        }
+
+        IBindingSpecification bs = new BindingSpecification();
+        bs.setType(BindingType.INHERITED);
+        bs.setValue(containerName);
+        bs.setLocation(location);
+
+        component.setBinding(name, bs);
+    }
 
     protected void invalidBinding(String binding)
     {

Modified: 
tapestry/tapestry4/trunk/tapestry-annotations/src/test/org/apache/tapestry/annotations/AnnotatedPage.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-annotations/src/test/org/apache/tapestry/annotations/AnnotatedPage.java?rev=586311&r1=586310&r2=586311&view=diff
==============================================================================
--- 
tapestry/tapestry4/trunk/tapestry-annotations/src/test/org/apache/tapestry/annotations/AnnotatedPage.java
 (original)
+++ 
tapestry/tapestry4/trunk/tapestry-annotations/src/test/org/apache/tapestry/annotations/AnnotatedPage.java
 Fri Oct 19 01:16:01 2007
@@ -103,6 +103,10 @@
     { "condition=message", "element=div" })
     public abstract IComponent getComponentWithBindings();
 
+    @Component(type = "Conditional", bindings =
+    { "condition=message", "element=div" }, inheritedBindings = 
{"title=pageTitle", "email"})
+    public abstract IComponent getComponentWithInheritedBindings();
+
     @Component(type = "TextField", bindings =
     { "value = email", "displayName = message:email-label" })
     public abstract IComponent getWhitespace();

Modified: 
tapestry/tapestry4/trunk/tapestry-annotations/src/test/org/apache/tapestry/annotations/TestComponentAnnotationWorker.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-annotations/src/test/org/apache/tapestry/annotations/TestComponentAnnotationWorker.java?rev=586311&r1=586310&r2=586311&view=diff
==============================================================================
--- 
tapestry/tapestry4/trunk/tapestry-annotations/src/test/org/apache/tapestry/annotations/TestComponentAnnotationWorker.java
 (original)
+++ 
tapestry/tapestry4/trunk/tapestry-annotations/src/test/org/apache/tapestry/annotations/TestComponentAnnotationWorker.java
 Fri Oct 19 01:16:01 2007
@@ -97,13 +97,8 @@
         Location l = newLocation();
         IContainedComponent cc = run("componentWithBindings", 
"getComponentWithBindings", l);
 
-        IBindingSpecification bs1 = cc.getBinding("condition");
-        assertSame(l, bs1.getLocation());
-        assertEquals(BindingType.PREFIXED, bs1.getType());
-        assertEquals("message", bs1.getValue());
-
-        IBindingSpecification bs2 = cc.getBinding("element");
-        assertEquals("div", bs2.getValue());
+        assertBinding(cc, "condition", l, BindingType.PREFIXED, "message");
+        assertBinding(cc, "element", l, BindingType.PREFIXED, "div");
     }
 
     public void test_Binding_Whitespace_Trimmed()
@@ -112,13 +107,8 @@
 
         IContainedComponent cc = run("whitespace", "getWhitespace", l);
 
-        IBindingSpecification bs1 = cc.getBinding("value");
-        assertSame(l, bs1.getLocation());
-        assertEquals(BindingType.PREFIXED, bs1.getType());
-        assertEquals("email", bs1.getValue());
-
-        IBindingSpecification bs2 = cc.getBinding("displayName");
-        assertEquals("message:email-label", bs2.getValue());
+        assertBinding(cc, "value", l, BindingType.PREFIXED, "email");
+        assertBinding(cc, "displayName", l, BindingType.PREFIXED, 
"message:email-label");
     }
     
     public void test_With_Type_And_CopyOf()
@@ -141,12 +131,27 @@
         run(spec, "componentWithBindings", "getComponentWithBindings", l);
         IContainedComponent cc = run(spec, "aComponentCopy", 
"getComponentWithBindingsCopy", l);
         
-        IBindingSpecification bs1 = cc.getBinding("condition");
-        assertSame(l, bs1.getLocation());
-        assertEquals(BindingType.PREFIXED, bs1.getType());
-        assertEquals("message", bs1.getValue());
-
-        IBindingSpecification bs2 = cc.getBinding("element");
-        assertEquals("div", bs2.getValue());
-    }     
+        assertBinding(cc, "condition", l, BindingType.PREFIXED, "message");
+        assertBinding(cc, "element", l, BindingType.PREFIXED, "div");
+    }
+
+    public void test_With_InheritedBindings()
+    {
+        Location l = newLocation();
+        IContainedComponent cc = run("componentWithInheritedBindings", 
"getComponentWithInheritedBindings", l);
+
+        assertBinding(cc, "condition", l, BindingType.PREFIXED, "message");
+        assertBinding(cc, "element", l, BindingType.PREFIXED, "div");
+        assertBinding(cc, "title", l, BindingType.INHERITED, "pageTitle");
+        assertBinding(cc, "email", l, BindingType.INHERITED, "email");
+    }
+
+    void assertBinding(IContainedComponent cc, String name, Location location, 
BindingType type, String value)
+    {
+        IBindingSpecification spec = cc.getBinding(name);
+        if (location!=null)
+            assertSame(spec.getLocation(), location);
+        assertEquals(spec.getType(), type);
+        assertEquals(spec.getValue(), value);
+    }
 }


Reply via email to