Support for setters
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 20 Apr 2013 07:15:16 +0200
changeset 102df9abc1dbda
parent 9 e77b0d6cee20
child 11 767609afd29a
Support for setters
json/src/main/java/org/apidesign/html/json/spi/PropertyBinding.java
json/src/test/java/net/java/html/json/MapModelTest.java
     1.1 --- a/json/src/main/java/org/apidesign/html/json/spi/PropertyBinding.java	Fri Apr 19 19:00:39 2013 +0200
     1.2 +++ b/json/src/main/java/org/apidesign/html/json/spi/PropertyBinding.java	Sat Apr 20 07:15:16 2013 +0200
     1.3 @@ -56,4 +56,16 @@
     1.4          }
     1.5          return g.substring(0, end);
     1.6      }
     1.7 +
     1.8 +    public String getSetterName() {
     1.9 +        final String g = params.get(2);
    1.10 +        if (g == null) {
    1.11 +            return null;
    1.12 +        }
    1.13 +        int end = g.indexOf("__");
    1.14 +        if (end == -1) {
    1.15 +            end = g.length();
    1.16 +        }
    1.17 +        return g.substring(0, end);
    1.18 +    }
    1.19  }
     2.1 --- a/json/src/test/java/net/java/html/json/MapModelTest.java	Fri Apr 19 19:00:39 2013 +0200
     2.2 +++ b/json/src/test/java/net/java/html/json/MapModelTest.java	Sat Apr 20 07:15:16 2013 +0200
     2.3 @@ -24,8 +24,6 @@
     2.4  import java.lang.reflect.Method;
     2.5  import java.util.HashMap;
     2.6  import java.util.Map;
     2.7 -import java.util.logging.Level;
     2.8 -import java.util.logging.Logger;
     2.9  import org.apidesign.html.json.spi.ContextBuilder;
    2.10  import org.apidesign.html.json.spi.PropertyBinding;
    2.11  import org.apidesign.html.json.spi.Technology;
    2.12 @@ -58,6 +56,11 @@
    2.13          assertEquals(o.changes, 1, "One change so far");
    2.14          
    2.15          assertEquals(o.get(), "Jarda", "Value should be in the map");
    2.16 +        
    2.17 +        o.set("Karle");
    2.18 +        
    2.19 +        assertEquals(p.getFirstName(), "Karle", "Model value updated");
    2.20 +        assertEquals(o.changes, 2, "Snd change");
    2.21      }
    2.22      
    2.23      
    2.24 @@ -66,15 +69,25 @@
    2.25          int changes;
    2.26          private final Method getter;
    2.27          private final Object model;
    2.28 +        private final Method setter;
    2.29      
    2.30 -        One(Object m, String get) throws NoSuchMethodException {
    2.31 +        One(Object m, String get, String set) throws NoSuchMethodException {
    2.32              this.model = m;
    2.33              this.getter = m.getClass().getMethod(get);
    2.34 +            if (set != null) {
    2.35 +                this.setter = m.getClass().getMethod(set, this.getter.getReturnType());
    2.36 +            } else {
    2.37 +                this.setter = null;
    2.38 +            }
    2.39          }
    2.40          
    2.41          Object get() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    2.42              return getter.invoke(model);
    2.43          }
    2.44 +        
    2.45 +        void set(Object v) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    2.46 +            setter.invoke(model, v);
    2.47 +        }
    2.48      }
    2.49      
    2.50      private static final class MapTechnology implements Technology<Map<String,One>> {
    2.51 @@ -95,7 +108,7 @@
    2.52          @Override
    2.53          public void bind(PropertyBinding b, Object model, Map<String, One> data) {
    2.54              try {
    2.55 -                One o = new One(model, b.getGetterName());
    2.56 +                One o = new One(model, b.getGetterName(), b.getSetterName());
    2.57                  data.put(b.getPropertyName(), o);
    2.58              } catch (NoSuchMethodException ex) {
    2.59                  throw new IllegalStateException(ex);