json/src/main/java/org/netbeans/html/json/impl/PropertyBindingAccessor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 16 Dec 2013 16:59:43 +0100
branchnetbeans
changeset 362 92fb71afdc0e
parent 358 json/src/main/java/org/apidesign/html/json/impl/PropertyBindingAccessor.java@80702021b851
child 365 5c93ad8c7a15
permissions -rw-r--r--
Moving implementation classes into org.netbeans.html namespace
jaroslav@9
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@9
     3
 *
jaroslav@358
     4
 * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
jaroslav@9
     5
 *
jaroslav@358
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@358
     7
 * Other names may be trademarks of their respective owners.
jaroslav@9
     8
 *
jaroslav@358
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@358
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@358
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@358
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@358
    13
 * License. You can obtain a copy of the License at
jaroslav@358
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@358
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@358
    16
 * specific language governing permissions and limitations under the
jaroslav@358
    17
 * License.  When distributing the software, include this License Header
jaroslav@358
    18
 * Notice in each file and include the License file at
jaroslav@358
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@358
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@358
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@358
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@358
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@358
    24
 * your own identifying information:
jaroslav@358
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@358
    26
 *
jaroslav@358
    27
 * Contributor(s):
jaroslav@358
    28
 *
jaroslav@358
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@358
    30
 * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
jaroslav@358
    31
 *
jaroslav@358
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@358
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@358
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@358
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@358
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@358
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@358
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@358
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@358
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@358
    41
 * made subject to such option by the copyright holder.
jaroslav@9
    42
 */
jaroslav@362
    43
package org.netbeans.html.json.impl;
jaroslav@9
    44
jaroslav@262
    45
import net.java.html.BrwsrCtx;
jaroslav@11
    46
import org.apidesign.html.json.spi.FunctionBinding;
jaroslav@24
    47
import org.apidesign.html.json.spi.JSONCall;
jaroslav@9
    48
import org.apidesign.html.json.spi.PropertyBinding;
jaroslav@9
    49
jaroslav@9
    50
/**
jaroslav@9
    51
 *
jaroslav@9
    52
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@9
    53
 */
jaroslav@9
    54
public abstract class PropertyBindingAccessor {
jaroslav@9
    55
    private static PropertyBindingAccessor DEFAULT;
jaroslav@9
    56
jaroslav@9
    57
    protected PropertyBindingAccessor() {
jaroslav@9
    58
        if (DEFAULT != null) throw new IllegalStateException();
jaroslav@9
    59
        DEFAULT = this;
jaroslav@9
    60
    }
jaroslav@9
    61
    
jaroslav@9
    62
    static {
jaroslav@30
    63
        JSON.initClass(PropertyBinding.class);
jaroslav@9
    64
    }
jaroslav@9
    65
jaroslav@17
    66
    protected abstract <M> PropertyBinding newBinding(PBData<M> d);
jaroslav@20
    67
    protected abstract <M> FunctionBinding newFunction(FBData<M> d);
jaroslav@24
    68
    protected abstract JSONCall newCall(
jaroslav@262
    69
        BrwsrCtx ctx, RcvrJSON callback, String urlBefore, String urlAfter,
jaroslav@262
    70
        String method, Object data
jaroslav@262
    71
    );
jaroslav@24
    72
jaroslav@9
    73
    
jaroslav@17
    74
    static <M> PropertyBinding create(PBData<M> d) {
jaroslav@17
    75
        return DEFAULT.newBinding(d);
jaroslav@9
    76
    }
jaroslav@20
    77
    static <M> FunctionBinding createFunction(FBData<M> d) {
jaroslav@20
    78
        return DEFAULT.newFunction(d);
jaroslav@11
    79
    }
jaroslav@24
    80
    static JSONCall createCall(
jaroslav@262
    81
        BrwsrCtx ctx, RcvrJSON callback, String urlBefore, String urlAfter, 
jaroslav@262
    82
        String method, Object data
jaroslav@262
    83
    ) {
jaroslav@262
    84
        return DEFAULT.newCall(ctx, callback, urlBefore, urlAfter, method, data);
jaroslav@24
    85
    }
jaroslav@11
    86
jaroslav@17
    87
    public static final class PBData<M> {
jaroslav@17
    88
        public final String name;
jaroslav@17
    89
        public final boolean readOnly;
jaroslav@20
    90
        private final M model;
jaroslav@20
    91
        private final SetAndGet<M> access;
jaroslav@235
    92
        private final Bindings<?> bindings;
jaroslav@17
    93
jaroslav@235
    94
        public PBData(Bindings<?> bindings, String name, M model, SetAndGet<M> access, boolean readOnly) {
jaroslav@235
    95
            this.bindings = bindings;
jaroslav@17
    96
            this.name = name;
jaroslav@17
    97
            this.model = model;
jaroslav@17
    98
            this.access = access;
jaroslav@17
    99
            this.readOnly = readOnly;
jaroslav@17
   100
        }
jaroslav@17
   101
jaroslav@17
   102
        public void setValue(Object v) {
jaroslav@17
   103
            access.setValue(model, v);
jaroslav@17
   104
        }
jaroslav@17
   105
jaroslav@17
   106
        public Object getValue() {
jaroslav@17
   107
            return access.getValue(model);
jaroslav@17
   108
        }
jaroslav@17
   109
jaroslav@17
   110
        public boolean isReadOnly() {
jaroslav@17
   111
            return readOnly;
jaroslav@17
   112
        }
jaroslav@235
   113
jaroslav@235
   114
        public Bindings getBindings() {
jaroslav@235
   115
            return bindings;
jaroslav@235
   116
        }
jaroslav@20
   117
    } // end of PBData
jaroslav@20
   118
    
jaroslav@20
   119
    public static final class FBData<M> {
jaroslav@20
   120
        public final String name;
jaroslav@20
   121
        private final M model;
jaroslav@20
   122
        private final Callback<M> access;
jaroslav@20
   123
jaroslav@20
   124
        public FBData(String name, M model, Callback<M> access) {
jaroslav@20
   125
            this.name = name;
jaroslav@20
   126
            this.model = model;
jaroslav@20
   127
            this.access = access;
jaroslav@20
   128
        }
jaroslav@20
   129
jaroslav@20
   130
jaroslav@20
   131
        public void call(Object data, Object ev) {
jaroslav@20
   132
            access.call(model, data, ev);
jaroslav@20
   133
        }
jaroslav@20
   134
    } // end of FBData
jaroslav@9
   135
}