json/src/main/java/org/apidesign/html/json/spi/PropertyBinding.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 415 ead37ae4d202
child 790 30f20d9c0986
permissions -rw-r--r--
Updating copyright headers to mention current year
jaroslav@9
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@9
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 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@551
    30
 * Software is Oracle. Portions Copyright 2013-2014 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@9
    43
package org.apidesign.html.json.spi;
jaroslav@9
    44
jaroslav@262
    45
import net.java.html.BrwsrCtx;
jaroslav@404
    46
import org.netbeans.html.json.impl.Bindings;
jaroslav@414
    47
import org.netbeans.html.json.impl.JSON;
jaroslav@362
    48
import org.netbeans.html.json.impl.PropertyBindingAccessor;
jaroslav@362
    49
import org.netbeans.html.json.impl.RcvrJSON;
jaroslav@9
    50
jaroslav@9
    51
/** Describes a property when one is asked to 
jaroslav@9
    52
 * bind it 
jaroslav@9
    53
 *
jaroslav@9
    54
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@9
    55
 */
jaroslav@415
    56
public abstract class PropertyBinding {
jaroslav@415
    57
    PropertyBinding() {
jaroslav@9
    58
    }
jaroslav@9
    59
jaroslav@9
    60
    static {
jaroslav@9
    61
        new PropertyBindingAccessor() {
jaroslav@9
    62
            @Override
jaroslav@262
    63
            protected JSONCall newCall(BrwsrCtx ctx, RcvrJSON callback, String urlBefore, String urlAfter, String method, Object data) {
jaroslav@262
    64
                return new JSONCall(ctx, callback, urlBefore, urlAfter, method, data);
jaroslav@24
    65
            }
jaroslav@404
    66
jaroslav@404
    67
            @Override
jaroslav@404
    68
            protected Bindings bindings(Proto proto, boolean initialize) {
jaroslav@404
    69
                return initialize ? proto.initBindings() : proto.getBindings();
jaroslav@404
    70
            }
jaroslav@406
    71
jaroslav@406
    72
            @Override
jaroslav@406
    73
            protected void notifyChange(Proto proto, int propIndex) {
jaroslav@406
    74
                proto.onChange(propIndex);
jaroslav@406
    75
            }
jaroslav@412
    76
jaroslav@412
    77
            @Override
jaroslav@412
    78
            protected Proto findProto(Proto.Type<?> type, Object object) {
jaroslav@412
    79
                return type.protoFor(object);
jaroslav@412
    80
            }
jaroslav@412
    81
jaroslav@412
    82
            @Override
jaroslav@412
    83
            protected <Model> Model cloneTo(Proto.Type<Model> type, Model model, BrwsrCtx c) {
jaroslav@412
    84
                return type.cloneTo(model, c);
jaroslav@412
    85
            }
jaroslav@412
    86
jaroslav@412
    87
            @Override
jaroslav@412
    88
            protected Object read(Proto.Type<?> from, BrwsrCtx c, Object data) {
jaroslav@412
    89
                return from.read(c, data);
jaroslav@412
    90
            }
jaroslav@415
    91
jaroslav@415
    92
            @Override
jaroslav@415
    93
            protected <M> PropertyBinding newBinding(
jaroslav@415
    94
                Proto.Type<M> access, Bindings<?> bindings, String name,
jaroslav@415
    95
                int index, M model, boolean readOnly
jaroslav@415
    96
            ) {
jaroslav@415
    97
                return new Impl(bindings, name, index, model, access, readOnly);
jaroslav@415
    98
            }
jaroslav@9
    99
        };
jaroslav@9
   100
    }
jaroslav@9
   101
jaroslav@415
   102
    /** Name of the property this binding represents.
jaroslav@415
   103
     * @return name of the property
jaroslav@415
   104
     */
jaroslav@415
   105
    public abstract String getPropertyName();
jaroslav@17
   106
jaroslav@415
   107
    /** Changes value of the property. Can be called only on dedicated
jaroslav@415
   108
     * thread. See {@link Technology#runSafe(java.lang.Runnable)}.
jaroslav@415
   109
     * 
jaroslav@415
   110
     * @param v new value of the property
jaroslav@415
   111
     */
jaroslav@415
   112
    public abstract void setValue(Object v);
jaroslav@9
   113
    
jaroslav@415
   114
    /** Obtains current value of the property this binding represents.
jaroslav@415
   115
     * Can be called only on dedicated
jaroslav@415
   116
     * thread. See {@link Technology#runSafe(java.lang.Runnable)}.
jaroslav@415
   117
     * 
jaroslav@415
   118
     * @return the value or <code>null</code>
jaroslav@415
   119
     */
jaroslav@415
   120
    public abstract Object getValue();
jaroslav@17
   121
    
jaroslav@415
   122
    /** Is this property read only? Or can one call {@link #setValue(java.lang.Object)}?
jaroslav@415
   123
     * 
jaroslav@415
   124
     * @return true, if this property is read only
jaroslav@415
   125
     */
jaroslav@415
   126
    public abstract boolean isReadOnly();
jaroslav@415
   127
    
jaroslav@415
   128
    private static final class Impl<M> extends PropertyBinding {
jaroslav@415
   129
        public final String name;
jaroslav@415
   130
        public final boolean readOnly;
jaroslav@415
   131
        private final M model;
jaroslav@415
   132
        private final Proto.Type<M> access;
jaroslav@415
   133
        private final Bindings<?> bindings;
jaroslav@415
   134
        private final int index;
jaroslav@415
   135
jaroslav@415
   136
        public Impl(Bindings<?> bindings, String name, int index, M model, Proto.Type<M> access, boolean readOnly) {
jaroslav@415
   137
            this.bindings = bindings;
jaroslav@415
   138
            this.name = name;
jaroslav@415
   139
            this.index = index;
jaroslav@415
   140
            this.model = model;
jaroslav@415
   141
            this.access = access;
jaroslav@415
   142
            this.readOnly = readOnly;
jaroslav@415
   143
        }
jaroslav@415
   144
jaroslav@415
   145
        @Override
jaroslav@415
   146
        public void setValue(Object v) {
jaroslav@415
   147
            access.setValue(model, index, v);
jaroslav@415
   148
        }
jaroslav@415
   149
jaroslav@415
   150
        @Override
jaroslav@415
   151
        public Object getValue() {
jaroslav@415
   152
            Object v = access.getValue(model, index);
jaroslav@415
   153
            Object r = JSON.find(v, bindings);
jaroslav@415
   154
            return r == null ? v : r;
jaroslav@415
   155
        }
jaroslav@415
   156
jaroslav@415
   157
        @Override
jaroslav@415
   158
        public boolean isReadOnly() {
jaroslav@415
   159
            return readOnly;
jaroslav@415
   160
        }
jaroslav@415
   161
jaroslav@415
   162
        @Override
jaroslav@415
   163
        public String getPropertyName() {
jaroslav@415
   164
            return name;
jaroslav@415
   165
        }
jaroslav@415
   166
    } // end of PBData
jaroslav@415
   167
    
jaroslav@9
   168
}