ko4j/src/main/java/org/netbeans/html/ko4j/KOTech.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 04 Oct 2015 14:55:01 +0200
changeset 1007 2ea65c6d3a8b
parent 984 3dfb8f1fd2f5
child 1027 5af9bfe37601
permissions -rw-r--r--
#255677: Keep reference to Knockout object until its model object is GCed
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@908
    60
implements Technology.BatchInit<Object>, Technology.ValueMutated<Object>, Technology.ApplyId<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) {
jtulach@1007
    69
        return createKO(model, propArr, funcArr, null);
jtulach@1007
    70
    }
jtulach@1007
    71
jtulach@1007
    72
    final Object createKO(Object model, PropertyBinding[] propArr, FunctionBinding[] funcArr, Knockout[] ko) {
jaroslav@275
    73
        String[] propNames = new String[propArr.length];
jaroslav@275
    74
        boolean[] propReadOnly = new boolean[propArr.length];
jaroslav@279
    75
        Object[] propValues = new Object[propArr.length];
jaroslav@275
    76
        for (int i = 0; i < propNames.length; i++) {
jaroslav@275
    77
            propNames[i] = propArr[i].getPropertyName();
jaroslav@275
    78
            propReadOnly[i] = propArr[i].isReadOnly();
jtulach@984
    79
            Object value = propArr[i].getValue();
jtulach@984
    80
            if (value instanceof Enum) {
jtulach@984
    81
                value = value.toString();
jtulach@984
    82
            }
jtulach@984
    83
            propValues[i] = value;
jaroslav@275
    84
        }
jaroslav@275
    85
        String[] funcNames = new String[funcArr.length];
jaroslav@275
    86
        for (int i = 0; i < funcNames.length; i++) {
jaroslav@275
    87
            funcNames[i] = funcArr[i].getFunctionName();
jaroslav@275
    88
        }
jaroslav@574
    89
        Object ret = getJSObject();
jtulach@1007
    90
        Knockout newKO = new Knockout(model, ret, propArr, funcArr);
jtulach@1007
    91
        if (ko != null) {
jtulach@1007
    92
            ko[0] = newKO;
jtulach@1007
    93
        }
jtulach@1007
    94
        newKO.wrapModel(
jtulach@1007
    95
            ret,
jtulach@868
    96
            propNames, propReadOnly, propValues,
jtulach@868
    97
            funcNames
jaroslav@279
    98
        );
jaroslav@430
    99
        return ret;
jaroslav@275
   100
    }
jaroslav@275
   101
    
jaroslav@574
   102
    private Object getJSObject() {
jaroslav@574
   103
        int len = 64;
jaroslav@574
   104
        if (jsObjects != null && jsIndex < (len = jsObjects.length)) {
jtulach@877
   105
            Object ret = jsObjects[jsIndex];
jtulach@877
   106
            jsObjects[jsIndex] = null;
jtulach@877
   107
            jsIndex++;
jtulach@877
   108
            return ret;
jaroslav@574
   109
        }
jaroslav@574
   110
        jsObjects = Knockout.allocJS(len * 2);
jaroslav@574
   111
        jsIndex = 1;
jtulach@877
   112
        Object ret = jsObjects[0];
jtulach@877
   113
        jsObjects[0] = null;
jtulach@877
   114
        return ret;
jaroslav@574
   115
    }
jaroslav@574
   116
    
jaroslav@37
   117
    @Override
jaroslav@430
   118
    public Object wrapModel(Object model) {
jaroslav@276
   119
        throw new UnsupportedOperationException();
jaroslav@37
   120
    }
jaroslav@37
   121
jaroslav@37
   122
    @Override
jaroslav@430
   123
    public void bind(PropertyBinding b, Object model, Object data) {
jaroslav@276
   124
        throw new UnsupportedOperationException();
jaroslav@37
   125
    }
jaroslav@37
   126
jaroslav@37
   127
    @Override
jaroslav@430
   128
    public void valueHasMutated(Object data, String propertyName) {
jtulach@877
   129
        Knockout.cleanUp();
jaroslav@567
   130
        Knockout.valueHasMutated(data, propertyName, null, null);
jaroslav@567
   131
    }
jaroslav@567
   132
    
jaroslav@567
   133
    @Override
jaroslav@567
   134
    public void valueHasMutated(Object data, String propertyName, Object oldValue, Object newValue) {
jtulach@877
   135
        Knockout.cleanUp();
jtulach@984
   136
        if (newValue instanceof Enum) {
jtulach@984
   137
            newValue = newValue.toString();
jtulach@984
   138
        }
jaroslav@567
   139
        Knockout.valueHasMutated(data, propertyName, oldValue, newValue);
jaroslav@37
   140
    }
jaroslav@37
   141
jaroslav@37
   142
    @Override
jaroslav@430
   143
    public void expose(FunctionBinding fb, Object model, Object d) {
jaroslav@276
   144
        throw new UnsupportedOperationException();
jaroslav@37
   145
    }
jaroslav@37
   146
jaroslav@37
   147
    @Override
jaroslav@430
   148
    public void applyBindings(Object data) {
jtulach@908
   149
        applyBindings(null, data);
jtulach@908
   150
    }
jtulach@908
   151
    @Override
jtulach@908
   152
    public void applyBindings(String id, Object data) {
jtulach@908
   153
        Object ko = Knockout.applyBindings(id, data);
jtulach@871
   154
        if (ko instanceof Knockout) {
jtulach@871
   155
            ((Knockout)ko).hold();
jtulach@871
   156
        }
jaroslav@37
   157
    }
jaroslav@37
   158
jaroslav@37
   159
    @Override
jaroslav@37
   160
    public Object wrapArray(Object[] arr) {
jaroslav@430
   161
        return arr;
jaroslav@37
   162
    }
jtulach@886
   163
    
jaroslav@37
   164
    @Override
jtulach@886
   165
    public void runSafe(final Runnable r) {
jtulach@886
   166
        LOG.warning("Technology.runSafe has been deprecated. Use BrwsrCtx.execute!");
jtulach@886
   167
        r.run();
jtulach@886
   168
    }    
jaroslav@38
   169
jaroslav@38
   170
    @Override
jaroslav@38
   171
    public <M> M toModel(Class<M> modelClass, Object data) {
jaroslav@430
   172
        return modelClass.cast(Knockout.toModel(data));
jaroslav@38
   173
    }
jaroslav@37
   174
}