ko4j/src/main/java/org/netbeans/html/ko4j/FXContext.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 522 e5dc7cf92ce4
child 567 83879118f17e
permissions -rw-r--r--
Updating copyright headers to mention current year
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@431
    52
import java.util.concurrent.Executor;
jaroslav@37
    53
import java.util.logging.Logger;
jaroslav@150
    54
import net.java.html.js.JavaScriptBody;
jaroslav@288
    55
import org.apidesign.html.boot.spi.Fn;
jaroslav@37
    56
import org.apidesign.html.json.spi.FunctionBinding;
jaroslav@37
    57
import org.apidesign.html.json.spi.JSONCall;
jaroslav@37
    58
import org.apidesign.html.json.spi.PropertyBinding;
jaroslav@37
    59
import org.apidesign.html.json.spi.Technology;
jaroslav@37
    60
import org.apidesign.html.json.spi.Transfer;
jaroslav@258
    61
import org.apidesign.html.json.spi.WSTransfer;
jaroslav@37
    62
jaroslav@54
    63
/** This is an implementation package - just
jaroslav@54
    64
 * include its JAR on classpath and use official {@link Context} API
jaroslav@54
    65
 * to access the functionality.
jaroslav@54
    66
 * <p>
jaroslav@37
    67
 *
jaroslav@37
    68
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@37
    69
 */
jaroslav@442
    70
final class FXContext
jaroslav@430
    71
implements Technology.BatchInit<Object>, Transfer, WSTransfer<LoadWS> {
jaroslav@37
    72
    static final Logger LOG = Logger.getLogger(FXContext.class.getName());
jaroslav@254
    73
    private static Boolean javaScriptEnabled;
jaroslav@288
    74
    private final Fn.Presenter browserContext;
jaroslav@288
    75
jaroslav@288
    76
    public FXContext(Fn.Presenter browserContext) {
jaroslav@288
    77
        this.browserContext = browserContext;
jaroslav@288
    78
    }
jaroslav@150
    79
    
jaroslav@446
    80
    @JavaScriptBody(args = {}, body = "if (window) return true; else return false;")
jaroslav@254
    81
    private static boolean isJavaScriptEnabledJs() {
jaroslav@254
    82
        return false;
jaroslav@254
    83
    }
jaroslav@254
    84
    
jaroslav@288
    85
    static boolean isJavaScriptEnabled() {
jaroslav@254
    86
        if (javaScriptEnabled != null) {
jaroslav@254
    87
            return javaScriptEnabled;
jaroslav@254
    88
        }
jaroslav@452
    89
        if (!(javaScriptEnabled = isJavaScriptEnabledJs())) {
jaroslav@452
    90
            Closeable c = Fn.activate(new TrueFn());
jaroslav@452
    91
            try {
jaroslav@452
    92
                javaScriptEnabled = isJavaScriptEnabledJs();
jaroslav@452
    93
            } finally {
jaroslav@452
    94
                try {
jaroslav@452
    95
                    c.close();
jaroslav@452
    96
                } catch (IOException ex) {
jaroslav@452
    97
                    // cannot happen
jaroslav@452
    98
                }
jaroslav@452
    99
            }
jaroslav@452
   100
        }
jaroslav@452
   101
        return javaScriptEnabled;
jaroslav@150
   102
    }
jaroslav@40
   103
jaroslav@258
   104
    final boolean areWebSocketsSupported() {
jaroslav@522
   105
        return isWebSocket();
jaroslav@258
   106
    }
jaroslav@258
   107
jaroslav@522
   108
    @JavaScriptBody(args = {}, body = "if (window.WebSocket) return true; else return false;")
jaroslav@522
   109
    private static boolean isWebSocket() {
jaroslav@522
   110
        return false;
jaroslav@522
   111
    }
jaroslav@275
   112
jaroslav@275
   113
    @Override
jaroslav@430
   114
    public Object wrapModel(Object model, PropertyBinding[] propArr, FunctionBinding[] funcArr) {
jaroslav@275
   115
        String[] propNames = new String[propArr.length];
jaroslav@275
   116
        boolean[] propReadOnly = new boolean[propArr.length];
jaroslav@279
   117
        Object[] propValues = new Object[propArr.length];
jaroslav@275
   118
        for (int i = 0; i < propNames.length; i++) {
jaroslav@275
   119
            propNames[i] = propArr[i].getPropertyName();
jaroslav@275
   120
            propReadOnly[i] = propArr[i].isReadOnly();
jaroslav@279
   121
            propValues[i] = propArr[i].getValue();
jaroslav@275
   122
        }
jaroslav@275
   123
        String[] funcNames = new String[funcArr.length];
jaroslav@275
   124
        for (int i = 0; i < funcNames.length; i++) {
jaroslav@275
   125
            funcNames[i] = funcArr[i].getFunctionName();
jaroslav@275
   126
        }
jaroslav@430
   127
        Object ret = Knockout.wrapModel(model, 
jaroslav@430
   128
            propNames, propReadOnly, propValues, propArr,
jaroslav@279
   129
            funcNames, funcArr
jaroslav@279
   130
        );
jaroslav@430
   131
        return ret;
jaroslav@275
   132
    }
jaroslav@275
   133
    
jaroslav@37
   134
    @Override
jaroslav@430
   135
    public Object wrapModel(Object model) {
jaroslav@276
   136
        throw new UnsupportedOperationException();
jaroslav@37
   137
    }
jaroslav@37
   138
jaroslav@37
   139
    @Override
jaroslav@430
   140
    public void bind(PropertyBinding b, Object model, Object data) {
jaroslav@276
   141
        throw new UnsupportedOperationException();
jaroslav@37
   142
    }
jaroslav@37
   143
jaroslav@37
   144
    @Override
jaroslav@430
   145
    public void valueHasMutated(Object data, String propertyName) {
jaroslav@37
   146
        Knockout.valueHasMutated(data, propertyName);
jaroslav@37
   147
    }
jaroslav@37
   148
jaroslav@37
   149
    @Override
jaroslav@430
   150
    public void expose(FunctionBinding fb, Object model, Object d) {
jaroslav@276
   151
        throw new UnsupportedOperationException();
jaroslav@37
   152
    }
jaroslav@37
   153
jaroslav@37
   154
    @Override
jaroslav@430
   155
    public void applyBindings(Object data) {
jaroslav@37
   156
        Knockout.applyBindings(data);
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
    }
jaroslav@37
   163
jaroslav@37
   164
    @Override
jaroslav@37
   165
    public void extract(Object obj, String[] props, Object[] values) {
jaroslav@37
   166
        LoadJSON.extractJSON(obj, props, values);
jaroslav@37
   167
    }
jaroslav@37
   168
jaroslav@37
   169
    @Override
jaroslav@37
   170
    public void loadJSON(final JSONCall call) {
jaroslav@446
   171
        if (call.isJSONP()) {
jaroslav@446
   172
            String me = LoadJSON.createJSONP(call);
jaroslav@446
   173
            LoadJSON.loadJSONP(call.composeURL(me), me);
jaroslav@446
   174
        } else {
jaroslav@446
   175
            String data = null;
jaroslav@446
   176
            if (call.isDoOutput()) {
jaroslav@446
   177
                try {
jaroslav@446
   178
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
jaroslav@446
   179
                    call.writeData(bos);
jaroslav@446
   180
                    data = new String(bos.toByteArray(), "UTF-8");
jaroslav@446
   181
                } catch (IOException ex) {
jaroslav@446
   182
                    call.notifyError(ex);
jaroslav@446
   183
                }
jaroslav@446
   184
            }
jaroslav@446
   185
            LoadJSON.loadJSON(call.composeURL(null), call, call.getMethod(), data);
jaroslav@446
   186
        }
jaroslav@37
   187
    }
jaroslav@38
   188
jaroslav@38
   189
    @Override
jaroslav@38
   190
    public <M> M toModel(Class<M> modelClass, Object data) {
jaroslav@430
   191
        return modelClass.cast(Knockout.toModel(data));
jaroslav@38
   192
    }
jaroslav@60
   193
jaroslav@60
   194
    @Override
jaroslav@60
   195
    public Object toJSON(InputStream is) throws IOException {
jaroslav@446
   196
        StringBuilder sb = new StringBuilder();
jaroslav@446
   197
        InputStreamReader r = new InputStreamReader(is);
jaroslav@446
   198
        for (;;) {
jaroslav@446
   199
            int ch = r.read();
jaroslav@446
   200
            if (ch == -1) {
jaroslav@446
   201
                break;
jaroslav@446
   202
            }
jaroslav@446
   203
            sb.append((char)ch);
jaroslav@446
   204
        }
jaroslav@446
   205
        return LoadJSON.parse(sb.toString());
jaroslav@60
   206
    }
jaroslav@240
   207
jaroslav@240
   208
    @Override
jaroslav@288
   209
    public void runSafe(final Runnable r) {
jaroslav@288
   210
        class Wrap implements Runnable {
jaroslav@288
   211
            @Override public void run() {
jaroslav@430
   212
                Closeable c = Fn.activate(browserContext);
jaroslav@430
   213
                try {
jaroslav@288
   214
                    r.run();
jaroslav@430
   215
                } finally {
jaroslav@430
   216
                    try {
jaroslav@430
   217
                        c.close();
jaroslav@430
   218
                    } catch (IOException ex) {
jaroslav@430
   219
                        // cannot be thrown
jaroslav@430
   220
                    }
jaroslav@288
   221
                }
jaroslav@288
   222
            }
jaroslav@288
   223
        }
jaroslav@288
   224
        Wrap w = new Wrap();
jaroslav@431
   225
        if (browserContext instanceof Executor) {
jaroslav@431
   226
            ((Executor)browserContext).execute(w);
jaroslav@431
   227
        } else {
jaroslav@431
   228
            w.run();
jaroslav@262
   229
        }
jaroslav@240
   230
    }
jaroslav@258
   231
jaroslav@258
   232
    @Override
jaroslav@258
   233
    public LoadWS open(String url, JSONCall onReply) {
jaroslav@258
   234
        return new LoadWS(onReply, url);
jaroslav@258
   235
    }
jaroslav@258
   236
jaroslav@258
   237
    @Override
jaroslav@258
   238
    public void send(LoadWS socket, JSONCall data) {
jaroslav@258
   239
        socket.send(data);
jaroslav@258
   240
    }
jaroslav@258
   241
jaroslav@258
   242
    @Override
jaroslav@258
   243
    public void close(LoadWS socket) {
jaroslav@258
   244
        socket.close();
jaroslav@258
   245
    }
jaroslav@452
   246
jaroslav@452
   247
    private static final class TrueFn extends Fn implements Fn.Presenter {
jaroslav@452
   248
        @Override
jaroslav@452
   249
        public Object invoke(Object thiz, Object... args) throws Exception {
jaroslav@452
   250
            return true;
jaroslav@452
   251
        }
jaroslav@452
   252
jaroslav@452
   253
        @Override
jaroslav@452
   254
        public Fn defineFn(String code, String... names) {
jaroslav@452
   255
            return this;
jaroslav@452
   256
        }
jaroslav@452
   257
jaroslav@452
   258
        @Override
jaroslav@452
   259
        public void displayPage(URL page, Runnable onPageLoad) {
jaroslav@452
   260
        }
jaroslav@452
   261
jaroslav@452
   262
        @Override
jaroslav@452
   263
        public void loadScript(Reader code) throws Exception {
jaroslav@452
   264
        }
jaroslav@452
   265
    } // end of TrueFn
jaroslav@37
   266
}