ko4j/src/main/java/org/netbeans/html/ko4j/FXContext.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 26 Aug 2014 18:13:30 +0200
changeset 838 bdc3d696dd4a
parent 790 30f20d9c0986
child 868 e71705af0948
permissions -rw-r--r--
During the API review process (bug 246133) the reviewers decided that in order to include html4j to NetBeans Platform, we need to stop using org.apidesign namespace and switch to NetBeans one. Repackaging all SPI packages into org.netbeans.html.smthng.spi.
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
jaroslav@446
    45
import java.io.ByteArrayOutputStream;
jaroslav@322
    46
import java.io.Closeable;
jaroslav@60
    47
import java.io.IOException;
jaroslav@60
    48
import java.io.InputStream;
jaroslav@446
    49
import java.io.InputStreamReader;
jaroslav@452
    50
import java.io.Reader;
jaroslav@452
    51
import java.net.URL;
jaroslav@37
    52
import java.util.logging.Logger;
jtulach@838
    53
import org.netbeans.html.boot.spi.Fn;
jtulach@838
    54
import org.netbeans.html.json.spi.FunctionBinding;
jtulach@838
    55
import org.netbeans.html.json.spi.JSONCall;
jtulach@838
    56
import org.netbeans.html.json.spi.PropertyBinding;
jtulach@838
    57
import org.netbeans.html.json.spi.Technology;
jtulach@838
    58
import org.netbeans.html.json.spi.Transfer;
jtulach@838
    59
import org.netbeans.html.json.spi.WSTransfer;
jaroslav@37
    60
jaroslav@54
    61
/** This is an implementation package - just
jaroslav@54
    62
 * include its JAR on classpath and use official {@link Context} API
jaroslav@54
    63
 * to access the functionality.
jaroslav@54
    64
 * <p>
jaroslav@37
    65
 *
jtulach@790
    66
 * @author Jaroslav Tulach
jaroslav@37
    67
 */
jaroslav@442
    68
final class FXContext
jaroslav@567
    69
implements Technology.BatchInit<Object>, Technology.ValueMutated<Object>,
jaroslav@575
    70
Transfer, WSTransfer<LoadWS> {
jaroslav@37
    71
    static final Logger LOG = Logger.getLogger(FXContext.class.getName());
jaroslav@574
    72
    private Object[] jsObjects;
jaroslav@574
    73
    private int jsIndex;
jaroslav@288
    74
jaroslav@288
    75
    public FXContext(Fn.Presenter browserContext) {
jaroslav@288
    76
    }
jaroslav@150
    77
    
jaroslav@275
    78
    @Override
jaroslav@430
    79
    public Object wrapModel(Object model, PropertyBinding[] propArr, FunctionBinding[] funcArr) {
jaroslav@275
    80
        String[] propNames = new String[propArr.length];
jaroslav@275
    81
        boolean[] propReadOnly = new boolean[propArr.length];
jaroslav@279
    82
        Object[] propValues = new Object[propArr.length];
jaroslav@275
    83
        for (int i = 0; i < propNames.length; i++) {
jaroslav@275
    84
            propNames[i] = propArr[i].getPropertyName();
jaroslav@275
    85
            propReadOnly[i] = propArr[i].isReadOnly();
jaroslav@279
    86
            propValues[i] = propArr[i].getValue();
jaroslav@275
    87
        }
jaroslav@275
    88
        String[] funcNames = new String[funcArr.length];
jaroslav@275
    89
        for (int i = 0; i < funcNames.length; i++) {
jaroslav@275
    90
            funcNames[i] = funcArr[i].getFunctionName();
jaroslav@275
    91
        }
jaroslav@574
    92
        Object ret = getJSObject();
jaroslav@574
    93
        Knockout.wrapModel(ret, model, 
jaroslav@430
    94
            propNames, propReadOnly, propValues, propArr,
jaroslav@279
    95
            funcNames, funcArr
jaroslav@279
    96
        );
jaroslav@430
    97
        return ret;
jaroslav@275
    98
    }
jaroslav@275
    99
    
jaroslav@574
   100
    private Object getJSObject() {
jaroslav@574
   101
        int len = 64;
jaroslav@574
   102
        if (jsObjects != null && jsIndex < (len = jsObjects.length)) {
jaroslav@574
   103
            return jsObjects[jsIndex++];
jaroslav@574
   104
        }
jaroslav@574
   105
        jsObjects = Knockout.allocJS(len * 2);
jaroslav@574
   106
        jsIndex = 1;
jaroslav@574
   107
        return jsObjects[0];
jaroslav@574
   108
    }
jaroslav@574
   109
    
jaroslav@37
   110
    @Override
jaroslav@430
   111
    public Object wrapModel(Object model) {
jaroslav@276
   112
        throw new UnsupportedOperationException();
jaroslav@37
   113
    }
jaroslav@37
   114
jaroslav@37
   115
    @Override
jaroslav@430
   116
    public void bind(PropertyBinding b, Object model, Object data) {
jaroslav@276
   117
        throw new UnsupportedOperationException();
jaroslav@37
   118
    }
jaroslav@37
   119
jaroslav@37
   120
    @Override
jaroslav@430
   121
    public void valueHasMutated(Object data, String propertyName) {
jaroslav@567
   122
        Knockout.valueHasMutated(data, propertyName, null, null);
jaroslav@567
   123
    }
jaroslav@567
   124
    
jaroslav@567
   125
    @Override
jaroslav@567
   126
    public void valueHasMutated(Object data, String propertyName, Object oldValue, Object newValue) {
jaroslav@567
   127
        Knockout.valueHasMutated(data, propertyName, oldValue, newValue);
jaroslav@37
   128
    }
jaroslav@37
   129
jaroslav@37
   130
    @Override
jaroslav@430
   131
    public void expose(FunctionBinding fb, Object model, Object d) {
jaroslav@276
   132
        throw new UnsupportedOperationException();
jaroslav@37
   133
    }
jaroslav@37
   134
jaroslav@37
   135
    @Override
jaroslav@430
   136
    public void applyBindings(Object data) {
jaroslav@37
   137
        Knockout.applyBindings(data);
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
    }
jaroslav@37
   144
jaroslav@37
   145
    @Override
jaroslav@37
   146
    public void extract(Object obj, String[] props, Object[] values) {
jaroslav@37
   147
        LoadJSON.extractJSON(obj, props, values);
jaroslav@37
   148
    }
jaroslav@37
   149
jaroslav@37
   150
    @Override
jaroslav@37
   151
    public void loadJSON(final JSONCall call) {
jaroslav@446
   152
        if (call.isJSONP()) {
jaroslav@446
   153
            String me = LoadJSON.createJSONP(call);
jaroslav@446
   154
            LoadJSON.loadJSONP(call.composeURL(me), me);
jaroslav@446
   155
        } else {
jaroslav@446
   156
            String data = null;
jaroslav@446
   157
            if (call.isDoOutput()) {
jaroslav@446
   158
                try {
jaroslav@446
   159
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
jaroslav@446
   160
                    call.writeData(bos);
jaroslav@446
   161
                    data = new String(bos.toByteArray(), "UTF-8");
jaroslav@446
   162
                } catch (IOException ex) {
jaroslav@446
   163
                    call.notifyError(ex);
jaroslav@446
   164
                }
jaroslav@446
   165
            }
jaroslav@446
   166
            LoadJSON.loadJSON(call.composeURL(null), call, call.getMethod(), data);
jaroslav@446
   167
        }
jaroslav@37
   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@60
   174
jaroslav@60
   175
    @Override
jaroslav@60
   176
    public Object toJSON(InputStream is) throws IOException {
jaroslav@446
   177
        StringBuilder sb = new StringBuilder();
jaroslav@446
   178
        InputStreamReader r = new InputStreamReader(is);
jaroslav@446
   179
        for (;;) {
jaroslav@446
   180
            int ch = r.read();
jaroslav@446
   181
            if (ch == -1) {
jaroslav@446
   182
                break;
jaroslav@446
   183
            }
jaroslav@446
   184
            sb.append((char)ch);
jaroslav@446
   185
        }
jaroslav@446
   186
        return LoadJSON.parse(sb.toString());
jaroslav@60
   187
    }
jaroslav@240
   188
jaroslav@240
   189
    @Override
jaroslav@288
   190
    public void runSafe(final Runnable r) {
jaroslav@575
   191
        LOG.warning("Technology.runSafe has been deprecated. Use BrwsrCtx.execute!");
jaroslav@575
   192
        r.run();
jaroslav@240
   193
    }
jaroslav@258
   194
jaroslav@258
   195
    @Override
jaroslav@258
   196
    public LoadWS open(String url, JSONCall onReply) {
jaroslav@258
   197
        return new LoadWS(onReply, url);
jaroslav@258
   198
    }
jaroslav@258
   199
jaroslav@258
   200
    @Override
jaroslav@258
   201
    public void send(LoadWS socket, JSONCall data) {
jaroslav@258
   202
        socket.send(data);
jaroslav@258
   203
    }
jaroslav@258
   204
jaroslav@258
   205
    @Override
jaroslav@258
   206
    public void close(LoadWS socket) {
jaroslav@258
   207
        socket.close();
jaroslav@258
   208
    }
jaroslav@452
   209
jaroslav@590
   210
    boolean areWebSocketsSupported() {
jaroslav@590
   211
        return Knockout.areWebSocketsSupported();
jaroslav@590
   212
    }
jaroslav@590
   213
jaroslav@452
   214
    private static final class TrueFn extends Fn implements Fn.Presenter {
jaroslav@452
   215
        @Override
jaroslav@452
   216
        public Object invoke(Object thiz, Object... args) throws Exception {
jaroslav@452
   217
            return true;
jaroslav@452
   218
        }
jaroslav@452
   219
jaroslav@452
   220
        @Override
jaroslav@452
   221
        public Fn defineFn(String code, String... names) {
jaroslav@452
   222
            return this;
jaroslav@452
   223
        }
jaroslav@452
   224
jaroslav@452
   225
        @Override
jaroslav@452
   226
        public void displayPage(URL page, Runnable onPageLoad) {
jaroslav@452
   227
        }
jaroslav@452
   228
jaroslav@452
   229
        @Override
jaroslav@452
   230
        public void loadScript(Reader code) throws Exception {
jaroslav@452
   231
        }
jaroslav@452
   232
    } // end of TrueFn
jaroslav@37
   233
}