Function binding
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 22 Apr 2013 12:37:43 +0200
changeset 11767609afd29a
parent 10 2df9abc1dbda
child 12 92f3b7a3b975
Function binding
json/src/main/java/net/java/html/json/Context.java
json/src/main/java/org/apidesign/html/json/impl/Bindings.java
json/src/main/java/org/apidesign/html/json/impl/PropertyBindingAccessor.java
json/src/main/java/org/apidesign/html/json/spi/FunctionBinding.java
json/src/main/java/org/apidesign/html/json/spi/PropertyBinding.java
json/src/main/java/org/apidesign/html/json/spi/Technology.java
json/src/test/java/net/java/html/json/MapModelTest.java
json/src/test/java/net/java/html/json/ModelTest.java
     1.1 --- a/json/src/main/java/net/java/html/json/Context.java	Sat Apr 20 07:15:16 2013 +0200
     1.2 +++ b/json/src/main/java/net/java/html/json/Context.java	Mon Apr 22 12:37:43 2013 +0200
     1.3 @@ -21,6 +21,7 @@
     1.4  package net.java.html.json;
     1.5  
     1.6  import org.apidesign.html.json.impl.ContextAccessor;
     1.7 +import org.apidesign.html.json.spi.FunctionBinding;
     1.8  import org.apidesign.html.json.spi.PropertyBinding;
     1.9  import org.apidesign.html.json.spi.Technology;
    1.10  
    1.11 @@ -71,5 +72,9 @@
    1.12          @Override
    1.13          public void bind(PropertyBinding b, Object model, Object data) {
    1.14          }
    1.15 +
    1.16 +        @Override
    1.17 +        public void expose(FunctionBinding fb, Object model, Object d) {
    1.18 +        }
    1.19      }
    1.20  }
     2.1 --- a/json/src/main/java/org/apidesign/html/json/impl/Bindings.java	Sat Apr 20 07:15:16 2013 +0200
     2.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/Bindings.java	Mon Apr 22 12:37:43 2013 +0200
     2.3 @@ -24,6 +24,7 @@
     2.4  import java.util.List;
     2.5  import org.apidesign.html.json.spi.PropertyBinding;
     2.6  import net.java.html.json.Context;
     2.7 +import org.apidesign.html.json.spi.FunctionBinding;
     2.8  import org.apidesign.html.json.spi.Technology;
     2.9  
    2.10  /**
    2.11 @@ -46,7 +47,7 @@
    2.12      
    2.13      private static <Data> Bindings<Data> apply(
    2.14          Technology<Data> bp, Object model, 
    2.15 -        String[] propsAndGetters, String[] functions
    2.16 +        String[] propsAndGetters, String[] methodsAndSignatures
    2.17      ) {
    2.18          Data d = bp.wrapModel(model);
    2.19          
    2.20 @@ -57,6 +58,12 @@
    2.21              );
    2.22              bp.bind(pb, model, d);
    2.23          }
    2.24 +
    2.25 +        arr = Arrays.asList(methodsAndSignatures);
    2.26 +        for (int i = 0; i < methodsAndSignatures.length; i += 2) {
    2.27 +            FunctionBinding fb = PropertyBindingAccessor.createFunction(arr.subList(i, i + 2));
    2.28 +            bp.expose(fb, model, d);
    2.29 +        }
    2.30          
    2.31          return new Bindings<>(d, bp);
    2.32      }
     3.1 --- a/json/src/main/java/org/apidesign/html/json/impl/PropertyBindingAccessor.java	Sat Apr 20 07:15:16 2013 +0200
     3.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/PropertyBindingAccessor.java	Mon Apr 22 12:37:43 2013 +0200
     3.3 @@ -21,6 +21,7 @@
     3.4  package org.apidesign.html.json.impl;
     3.5  
     3.6  import java.util.List;
     3.7 +import org.apidesign.html.json.spi.FunctionBinding;
     3.8  import org.apidesign.html.json.spi.PropertyBinding;
     3.9  
    3.10  /**
    3.11 @@ -47,10 +48,15 @@
    3.12      }
    3.13  
    3.14      protected abstract PropertyBinding newBinding(List<String> params);
    3.15 +    protected abstract FunctionBinding newFunction(List<String> params);
    3.16      
    3.17 -    public static PropertyBinding create(
    3.18 +    static PropertyBinding create(
    3.19          List<String> params
    3.20      ) {
    3.21          return DEFAULT.newBinding(params);
    3.22      }
    3.23 +    static FunctionBinding createFunction(List<String> subList) {
    3.24 +        return DEFAULT.newFunction(subList);
    3.25 +    }
    3.26 +
    3.27  }
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/json/src/main/java/org/apidesign/html/json/spi/FunctionBinding.java	Mon Apr 22 12:37:43 2013 +0200
     4.3 @@ -0,0 +1,42 @@
     4.4 +/**
     4.5 + * HTML via Java(tm) Language Bindings
     4.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4.7 + *
     4.8 + * This program is free software: you can redistribute it and/or modify
     4.9 + * it under the terms of the GNU General Public License as published by
    4.10 + * the Free Software Foundation, version 2 of the License.
    4.11 + *
    4.12 + * This program is distributed in the hope that it will be useful,
    4.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    4.15 + * GNU General Public License for more details. apidesign.org
    4.16 + * designates this particular file as subject to the
    4.17 + * "Classpath" exception as provided by apidesign.org
    4.18 + * in the License file that accompanied this code.
    4.19 + *
    4.20 + * You should have received a copy of the GNU General Public License
    4.21 + * along with this program. Look for COPYING file in the top folder.
    4.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    4.23 + */
    4.24 +package org.apidesign.html.json.spi;
    4.25 +
    4.26 +import java.util.List;
    4.27 +import net.java.html.json.Function;
    4.28 +import net.java.html.json.Model;
    4.29 +
    4.30 +/** Describes a function provided by the {@link Model} and 
    4.31 + * annotated by {@link Function} annotation.
    4.32 + *
    4.33 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.34 + */
    4.35 +public final class FunctionBinding {
    4.36 +    private final List<String> params;
    4.37 +    
    4.38 +    FunctionBinding(List<String> p) {
    4.39 +        this.params = p;
    4.40 +    }
    4.41 +
    4.42 +    public String getFunctionName() {
    4.43 +        return params.get(0);
    4.44 +    }
    4.45 +}
     5.1 --- a/json/src/main/java/org/apidesign/html/json/spi/PropertyBinding.java	Sat Apr 20 07:15:16 2013 +0200
     5.2 +++ b/json/src/main/java/org/apidesign/html/json/spi/PropertyBinding.java	Mon Apr 22 12:37:43 2013 +0200
     5.3 @@ -41,6 +41,11 @@
     5.4              protected PropertyBinding newBinding(List<String> params) {
     5.5                  return new PropertyBinding(params);
     5.6              }
     5.7 +
     5.8 +            @Override
     5.9 +            protected FunctionBinding newFunction(List<String> params) {
    5.10 +                return new FunctionBinding(params);
    5.11 +            }
    5.12          };
    5.13      }
    5.14  
     6.1 --- a/json/src/main/java/org/apidesign/html/json/spi/Technology.java	Sat Apr 20 07:15:16 2013 +0200
     6.2 +++ b/json/src/main/java/org/apidesign/html/json/spi/Technology.java	Mon Apr 22 12:37:43 2013 +0200
     6.3 @@ -54,4 +54,6 @@
     6.4       * @param propertyName name of the model property that changed
     6.5       */
     6.6      public void valueHasMutated(Data data, String propertyName);
     6.7 +
     6.8 +    public void expose(FunctionBinding fb, Object model, Data d);
     6.9  }
     7.1 --- a/json/src/test/java/net/java/html/json/MapModelTest.java	Sat Apr 20 07:15:16 2013 +0200
     7.2 +++ b/json/src/test/java/net/java/html/json/MapModelTest.java	Mon Apr 22 12:37:43 2013 +0200
     7.3 @@ -25,6 +25,7 @@
     7.4  import java.util.HashMap;
     7.5  import java.util.Map;
     7.6  import org.apidesign.html.json.spi.ContextBuilder;
     7.7 +import org.apidesign.html.json.spi.FunctionBinding;
     7.8  import org.apidesign.html.json.spi.PropertyBinding;
     7.9  import org.apidesign.html.json.spi.Technology;
    7.10  import org.testng.annotations.BeforeMethod;
    7.11 @@ -63,7 +64,17 @@
    7.12          assertEquals(o.changes, 2, "Snd change");
    7.13      }
    7.14      
    7.15 -    
    7.16 +    @Test public void changeSex() {
    7.17 +        Person p = new Person(c);
    7.18 +        p.setFirstName("Trans");
    7.19 +        p.setSex(Sex.MALE);
    7.20 +        
    7.21 +        Map m = (Map)p.koData();
    7.22 +        Object o = m.get("changeSex");
    7.23 +        assertNotNull(o, "Function registered in the model");
    7.24 +
    7.25 +        // TBD: invoke
    7.26 +    }
    7.27  
    7.28      private static final class One {
    7.29          int changes;
    7.30 @@ -73,7 +84,11 @@
    7.31      
    7.32          One(Object m, String get, String set) throws NoSuchMethodException {
    7.33              this.model = m;
    7.34 -            this.getter = m.getClass().getMethod(get);
    7.35 +            if (get != null) {
    7.36 +                this.getter = m.getClass().getMethod(get);
    7.37 +            } else {
    7.38 +                this.getter = null;
    7.39 +            }
    7.40              if (set != null) {
    7.41                  this.setter = m.getClass().getMethod(set, this.getter.getReturnType());
    7.42              } else {
    7.43 @@ -114,6 +129,15 @@
    7.44                  throw new IllegalStateException(ex);
    7.45              }
    7.46          }
    7.47 +
    7.48 +        @Override
    7.49 +        public void expose(FunctionBinding fb, Object model, Map<String, One> data) {
    7.50 +            try {
    7.51 +                data.put(fb.getFunctionName(), new One(model, null, null));
    7.52 +            } catch (NoSuchMethodException ex) {
    7.53 +                throw new IllegalStateException(ex);
    7.54 +            }
    7.55 +        }
    7.56      
    7.57      }
    7.58  }
     8.1 --- a/json/src/test/java/net/java/html/json/ModelTest.java	Sat Apr 20 07:15:16 2013 +0200
     8.2 +++ b/json/src/test/java/net/java/html/json/ModelTest.java	Mon Apr 22 12:37:43 2013 +0200
     8.3 @@ -26,6 +26,7 @@
     8.4  import java.util.List;
     8.5  import java.util.ListIterator;
     8.6  import org.apidesign.html.json.spi.ContextBuilder;
     8.7 +import org.apidesign.html.json.spi.FunctionBinding;
     8.8  import org.apidesign.html.json.spi.PropertyBinding;
     8.9  import org.apidesign.html.json.spi.Technology;
    8.10  import static org.testng.Assert.*;
    8.11 @@ -240,5 +241,9 @@
    8.12          @Override
    8.13          public void bind(PropertyBinding b, Object model, Object data) {
    8.14          }
    8.15 +
    8.16 +        @Override
    8.17 +        public void expose(FunctionBinding fb, Object model, Object d) {
    8.18 +        }
    8.19      }
    8.20  }