ko4j/src/main/java/org/netbeans/html/ko4j/KOTech.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 09 Dec 2014 08:33:22 +0100
branchgc
changeset 892 de02b7c13379
parent 877 ko4j/src/main/java/org/netbeans/html/ko4j/FXContext.java@75fff3637abe
parent 886 ko4j/src/main/java/org/netbeans/html/ko4j/FXContext.java@88d62267a0b5
child 898 18e17d0cd066
permissions -rw-r--r--
Bringing the most recent changes in default branch to gc
jaroslav@37
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@37
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@37
     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@37
     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@37
    42
 */
jaroslav@442
    43
package org.netbeans.html.ko4j;
jaroslav@37
    44
jtulach@886
    45
import org.netbeans.html.context.spi.Contexts;
jtulach@838
    46
import org.netbeans.html.json.spi.FunctionBinding;
jtulach@838
    47
import org.netbeans.html.json.spi.PropertyBinding;
jtulach@838
    48
import org.netbeans.html.json.spi.Technology;
jtulach@886
    49
import static org.netbeans.html.ko4j.KO4J.LOG;
jaroslav@37
    50
jaroslav@54
    51
/** This is an implementation package - just
jaroslav@54
    52
 * include its JAR on classpath and use official {@link Context} API
jaroslav@54
    53
 * to access the functionality.
jaroslav@54
    54
 * <p>
jaroslav@37
    55
 *
jtulach@790
    56
 * @author Jaroslav Tulach
jaroslav@37
    57
 */
jtulach@886
    58
@Contexts.Id("ko4j")
jtulach@886
    59
final class KOTech
jtulach@886
    60
implements Technology.BatchInit<Object>, Technology.ValueMutated<Object> {
jaroslav@574
    61
    private Object[] jsObjects;
jaroslav@574
    62
    private int jsIndex;
jaroslav@288
    63
jtulach@886
    64
    public KOTech() {
jaroslav@288
    65
    }
jaroslav@150
    66
    
jaroslav@275
    67
    @Override
jaroslav@430
    68
    public Object wrapModel(Object model, PropertyBinding[] propArr, FunctionBinding[] funcArr) {
jaroslav@275
    69
        String[] propNames = new String[propArr.length];
jaroslav@275
    70
        boolean[] propReadOnly = new boolean[propArr.length];
jaroslav@279
    71
        Object[] propValues = new Object[propArr.length];
jaroslav@275
    72
        for (int i = 0; i < propNames.length; i++) {
jaroslav@275
    73
            propNames[i] = propArr[i].getPropertyName();
jaroslav@275
    74
            propReadOnly[i] = propArr[i].isReadOnly();
jaroslav@279
    75
            propValues[i] = propArr[i].getValue();
jaroslav@275
    76
        }
jaroslav@275
    77
        String[] funcNames = new String[funcArr.length];
jaroslav@275
    78
        for (int i = 0; i < funcNames.length; i++) {
jaroslav@275
    79
            funcNames[i] = funcArr[i].getFunctionName();
jaroslav@275
    80
        }
jaroslav@574
    81
        Object ret = getJSObject();
jtulach@877
    82
        Knockout.wrapModel(new Knockout(model, ret, propArr, funcArr),
jtulach@870
    83
            ret, 
jtulach@868
    84
            propNames, propReadOnly, propValues,
jtulach@868
    85
            funcNames
jaroslav@279
    86
        );
jaroslav@430
    87
        return ret;
jaroslav@275
    88
    }
jaroslav@275
    89
    
jaroslav@574
    90
    private Object getJSObject() {
jaroslav@574
    91
        int len = 64;
jaroslav@574
    92
        if (jsObjects != null && jsIndex < (len = jsObjects.length)) {
jtulach@877
    93
            Object ret = jsObjects[jsIndex];
jtulach@877
    94
            jsObjects[jsIndex] = null;
jtulach@877
    95
            jsIndex++;
jtulach@877
    96
            return ret;
jaroslav@574
    97
        }
jaroslav@574
    98
        jsObjects = Knockout.allocJS(len * 2);
jaroslav@574
    99
        jsIndex = 1;
jtulach@877
   100
        Object ret = jsObjects[0];
jtulach@877
   101
        jsObjects[0] = null;
jtulach@877
   102
        return ret;
jaroslav@574
   103
    }
jaroslav@574
   104
    
jaroslav@37
   105
    @Override
jaroslav@430
   106
    public Object wrapModel(Object model) {
jaroslav@276
   107
        throw new UnsupportedOperationException();
jaroslav@37
   108
    }
jaroslav@37
   109
jaroslav@37
   110
    @Override
jaroslav@430
   111
    public void bind(PropertyBinding b, Object model, Object data) {
jaroslav@276
   112
        throw new UnsupportedOperationException();
jaroslav@37
   113
    }
jaroslav@37
   114
jaroslav@37
   115
    @Override
jaroslav@430
   116
    public void valueHasMutated(Object data, String propertyName) {
jtulach@877
   117
        Knockout.cleanUp();
jaroslav@567
   118
        Knockout.valueHasMutated(data, propertyName, null, null);
jaroslav@567
   119
    }
jaroslav@567
   120
    
jaroslav@567
   121
    @Override
jaroslav@567
   122
    public void valueHasMutated(Object data, String propertyName, Object oldValue, Object newValue) {
jtulach@877
   123
        Knockout.cleanUp();
jaroslav@567
   124
        Knockout.valueHasMutated(data, propertyName, oldValue, newValue);
jaroslav@37
   125
    }
jaroslav@37
   126
jaroslav@37
   127
    @Override
jaroslav@430
   128
    public void expose(FunctionBinding fb, Object model, Object d) {
jaroslav@276
   129
        throw new UnsupportedOperationException();
jaroslav@37
   130
    }
jaroslav@37
   131
jaroslav@37
   132
    @Override
jaroslav@430
   133
    public void applyBindings(Object data) {
jtulach@871
   134
        Object ko = Knockout.applyBindings(data);
jtulach@871
   135
        if (ko instanceof Knockout) {
jtulach@871
   136
            ((Knockout)ko).hold();
jtulach@871
   137
        }
jaroslav@37
   138
    }
jaroslav@37
   139
jaroslav@37
   140
    @Override
jaroslav@37
   141
    public Object wrapArray(Object[] arr) {
jaroslav@430
   142
        return arr;
jaroslav@37
   143
    }
jtulach@886
   144
    
jaroslav@37
   145
    @Override
jtulach@886
   146
    public void runSafe(final Runnable r) {
jtulach@886
   147
        LOG.warning("Technology.runSafe has been deprecated. Use BrwsrCtx.execute!");
jtulach@886
   148
        r.run();
jtulach@886
   149
    }    
jaroslav@38
   150
jaroslav@38
   151
    @Override
jaroslav@38
   152
    public <M> M toModel(Class<M> modelClass, Object data) {
jaroslav@430
   153
        return modelClass.cast(Knockout.toModel(data));
jaroslav@38
   154
    }
jaroslav@37
   155
}