1.1 --- a/json/src/main/java/org/apidesign/html/json/spi/PropertyBinding.java Mon Jan 06 08:55:10 2014 +0100
1.2 +++ b/json/src/main/java/org/apidesign/html/json/spi/PropertyBinding.java Mon Jan 06 09:24:49 2014 +0100
1.3 @@ -46,7 +46,6 @@
1.4 import org.netbeans.html.json.impl.Bindings;
1.5 import org.netbeans.html.json.impl.JSON;
1.6 import org.netbeans.html.json.impl.PropertyBindingAccessor;
1.7 -import org.netbeans.html.json.impl.PropertyBindingAccessor.PBData;
1.8 import org.netbeans.html.json.impl.RcvrJSON;
1.9
1.10 /** Describes a property when one is asked to
1.11 @@ -54,21 +53,13 @@
1.12 *
1.13 * @author Jaroslav Tulach <jtulach@netbeans.org>
1.14 */
1.15 -public final class PropertyBinding {
1.16 - private final PBData<?> data;
1.17 -
1.18 - private PropertyBinding(PBData<?> p) {
1.19 - this.data = p;
1.20 +public abstract class PropertyBinding {
1.21 + PropertyBinding() {
1.22 }
1.23
1.24 static {
1.25 new PropertyBindingAccessor() {
1.26 @Override
1.27 - protected <M> PropertyBinding newBinding(PBData<M> d) {
1.28 - return new PropertyBinding(d);
1.29 - }
1.30 -
1.31 - @Override
1.32 protected JSONCall newCall(BrwsrCtx ctx, RcvrJSON callback, String urlBefore, String urlAfter, String method, Object data) {
1.33 return new JSONCall(ctx, callback, urlBefore, urlAfter, method, data);
1.34 }
1.35 @@ -84,16 +75,6 @@
1.36 }
1.37
1.38 @Override
1.39 - protected <Model> void setValue(Proto.Type<Model> type, Model model, int index, Object value) {
1.40 - type.setValue(model, index, value);
1.41 - }
1.42 -
1.43 - @Override
1.44 - protected <Model> Object getValue(Proto.Type<Model> type, Model model, int index) {
1.45 - return type.getValue(model, index);
1.46 - }
1.47 -
1.48 - @Override
1.49 protected Proto findProto(Proto.Type<?> type, Object object) {
1.50 return type.protoFor(object);
1.51 }
1.52 @@ -107,24 +88,81 @@
1.53 protected Object read(Proto.Type<?> from, BrwsrCtx c, Object data) {
1.54 return from.read(c, data);
1.55 }
1.56 +
1.57 + @Override
1.58 + protected <M> PropertyBinding newBinding(
1.59 + Proto.Type<M> access, Bindings<?> bindings, String name,
1.60 + int index, M model, boolean readOnly
1.61 + ) {
1.62 + return new Impl(bindings, name, index, model, access, readOnly);
1.63 + }
1.64 };
1.65 }
1.66
1.67 - public String getPropertyName() {
1.68 - return data.name;
1.69 - }
1.70 + /** Name of the property this binding represents.
1.71 + * @return name of the property
1.72 + */
1.73 + public abstract String getPropertyName();
1.74
1.75 - public void setValue(Object v) {
1.76 - data.setValue(v);
1.77 - }
1.78 + /** Changes value of the property. Can be called only on dedicated
1.79 + * thread. See {@link Technology#runSafe(java.lang.Runnable)}.
1.80 + *
1.81 + * @param v new value of the property
1.82 + */
1.83 + public abstract void setValue(Object v);
1.84
1.85 - public Object getValue() {
1.86 - Object v = data.getValue();
1.87 - Object r = JSON.find(v, data.getBindings());
1.88 - return r == null ? v : r;
1.89 - }
1.90 + /** Obtains current value of the property this binding represents.
1.91 + * Can be called only on dedicated
1.92 + * thread. See {@link Technology#runSafe(java.lang.Runnable)}.
1.93 + *
1.94 + * @return the value or <code>null</code>
1.95 + */
1.96 + public abstract Object getValue();
1.97
1.98 - public boolean isReadOnly() {
1.99 - return data.isReadOnly();
1.100 - }
1.101 + /** Is this property read only? Or can one call {@link #setValue(java.lang.Object)}?
1.102 + *
1.103 + * @return true, if this property is read only
1.104 + */
1.105 + public abstract boolean isReadOnly();
1.106 +
1.107 + private static final class Impl<M> extends PropertyBinding {
1.108 + public final String name;
1.109 + public final boolean readOnly;
1.110 + private final M model;
1.111 + private final Proto.Type<M> access;
1.112 + private final Bindings<?> bindings;
1.113 + private final int index;
1.114 +
1.115 + public Impl(Bindings<?> bindings, String name, int index, M model, Proto.Type<M> access, boolean readOnly) {
1.116 + this.bindings = bindings;
1.117 + this.name = name;
1.118 + this.index = index;
1.119 + this.model = model;
1.120 + this.access = access;
1.121 + this.readOnly = readOnly;
1.122 + }
1.123 +
1.124 + @Override
1.125 + public void setValue(Object v) {
1.126 + access.setValue(model, index, v);
1.127 + }
1.128 +
1.129 + @Override
1.130 + public Object getValue() {
1.131 + Object v = access.getValue(model, index);
1.132 + Object r = JSON.find(v, bindings);
1.133 + return r == null ? v : r;
1.134 + }
1.135 +
1.136 + @Override
1.137 + public boolean isReadOnly() {
1.138 + return readOnly;
1.139 + }
1.140 +
1.141 + @Override
1.142 + public String getPropertyName() {
1.143 + return name;
1.144 + }
1.145 + } // end of PBData
1.146 +
1.147 }
2.1 --- a/json/src/main/java/org/netbeans/html/json/impl/Bindings.java Mon Jan 06 08:55:10 2014 +0100
2.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/Bindings.java Mon Jan 06 09:24:49 2014 +0100
2.3 @@ -47,7 +47,6 @@
2.4 import org.apidesign.html.json.spi.PropertyBinding;
2.5 import org.apidesign.html.json.spi.Proto;
2.6 import org.apidesign.html.json.spi.Technology;
2.7 -import org.netbeans.html.json.impl.PropertyBindingAccessor.PBData;
2.8
2.9 /**
2.10 *
2.11 @@ -62,7 +61,7 @@
2.12 }
2.13
2.14 public <M> PropertyBinding registerProperty(String propName, int index, M model, Proto.Type<M> access, boolean readOnly) {
2.15 - return PropertyBindingAccessor.create(new PBData<M>(this, propName, index, model, access, readOnly));
2.16 + return PropertyBindingAccessor.create(access, this, propName, index, model, readOnly);
2.17 }
2.18
2.19 public static Bindings<?> apply(BrwsrCtx c, Object model) {
3.1 --- a/json/src/main/java/org/netbeans/html/json/impl/PropertyBindingAccessor.java Mon Jan 06 08:55:10 2014 +0100
3.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/PropertyBindingAccessor.java Mon Jan 06 09:24:49 2014 +0100
3.3 @@ -64,7 +64,9 @@
3.4 JSON.initClass(PropertyBinding.class);
3.5 }
3.6
3.7 - protected abstract <M> PropertyBinding newBinding(PBData<M> d);
3.8 + protected abstract <M> PropertyBinding newBinding(
3.9 + Proto.Type<M> access, Bindings<?> bindings, String name, int index, M model, boolean readOnly
3.10 + );
3.11 protected abstract JSONCall newCall(
3.12 BrwsrCtx ctx, RcvrJSON callback, String urlBefore, String urlAfter,
3.13 String method, Object data
3.14 @@ -72,8 +74,6 @@
3.15
3.16 protected abstract Bindings bindings(Proto proto, boolean initialize);
3.17 protected abstract void notifyChange(Proto proto, int propIndex);
3.18 - protected abstract <Model> void setValue(Proto.Type<Model> type, Model model, int index, Object value);
3.19 - protected abstract <Model> Object getValue(Proto.Type<Model> type, Model model, int index);
3.20 protected abstract Proto findProto(Proto.Type<?> type, Object object);
3.21 protected abstract <Model> Model cloneTo(Proto.Type<Model> type, Model model, BrwsrCtx c);
3.22 protected abstract Object read(Proto.Type<?> from, BrwsrCtx c, Object data);
3.23 @@ -86,8 +86,10 @@
3.24 DEFAULT.notifyChange(proto, propIndex);
3.25 }
3.26
3.27 - static <M> PropertyBinding create(PBData<M> d) {
3.28 - return DEFAULT.newBinding(d);
3.29 + static <M> PropertyBinding create(
3.30 + Proto.Type<M> access, Bindings<?> bindings, String name, int index, M model , boolean readOnly
3.31 + ) {
3.32 + return DEFAULT.newBinding(access, bindings, name, index, model, readOnly);
3.33 }
3.34 static JSONCall createCall(
3.35 BrwsrCtx ctx, RcvrJSON callback, String urlBefore, String urlAfter,
3.36 @@ -104,38 +106,4 @@
3.37 static Object readFrom(Proto.Type<?> from, BrwsrCtx c, Object data) {
3.38 return DEFAULT.read(from, c, data);
3.39 }
3.40 -
3.41 - public static final class PBData<M> {
3.42 - public final String name;
3.43 - public final boolean readOnly;
3.44 - private final M model;
3.45 - private final Proto.Type<M> access;
3.46 - private final Bindings<?> bindings;
3.47 - private final int index;
3.48 -
3.49 - public PBData(Bindings<?> bindings, String name, int index, M model, Proto.Type<M> access, boolean readOnly) {
3.50 - this.bindings = bindings;
3.51 - this.name = name;
3.52 - this.index = index;
3.53 - this.model = model;
3.54 - this.access = access;
3.55 - this.readOnly = readOnly;
3.56 - }
3.57 -
3.58 - public void setValue(Object v) {
3.59 - DEFAULT.setValue(access, model, index, v);
3.60 - }
3.61 -
3.62 - public Object getValue() {
3.63 - return DEFAULT.getValue(access, model, index);
3.64 - }
3.65 -
3.66 - public boolean isReadOnly() {
3.67 - return readOnly;
3.68 - }
3.69 -
3.70 - public Bindings getBindings() {
3.71 - return bindings;
3.72 - }
3.73 - } // end of PBData
3.74 }