ko-fx/src/main/java/org/netbeans/html/kofx/FXContext.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 16 Dec 2013 16:59:43 +0100
branchnetbeans
changeset 362 92fb71afdc0e
parent 358 ko-fx/src/main/java/org/apidesign/html/kofx/FXContext.java@80702021b851
child 365 5c93ad8c7a15
permissions -rw-r--r--
Moving implementation classes into org.netbeans.html namespace
jaroslav@37
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@37
     3
 *
jaroslav@358
     4
 * Copyright 1997-2010 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@358
    30
 * Software is Oracle. Portions Copyright 2013-2013 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@362
    43
package org.netbeans.html.kofx;
jaroslav@37
    44
jaroslav@322
    45
import java.io.Closeable;
jaroslav@60
    46
import java.io.IOException;
jaroslav@60
    47
import java.io.InputStream;
jaroslav@54
    48
import java.util.ServiceLoader;
jaroslav@37
    49
import java.util.logging.Logger;
jaroslav@240
    50
import javafx.application.Platform;
jaroslav@150
    51
import net.java.html.js.JavaScriptBody;
jaroslav@37
    52
import netscape.javascript.JSObject;
jaroslav@288
    53
import org.apidesign.html.boot.spi.Fn;
jaroslav@110
    54
import org.apidesign.html.context.spi.Contexts;
jaroslav@37
    55
import org.apidesign.html.json.spi.FunctionBinding;
jaroslav@37
    56
import org.apidesign.html.json.spi.JSONCall;
jaroslav@37
    57
import org.apidesign.html.json.spi.PropertyBinding;
jaroslav@37
    58
import org.apidesign.html.json.spi.Technology;
jaroslav@37
    59
import org.apidesign.html.json.spi.Transfer;
jaroslav@258
    60
import org.apidesign.html.json.spi.WSTransfer;
jaroslav@40
    61
import org.openide.util.lookup.ServiceProvider;
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@54
    67
 * Registers {@link ContextProvider}, so {@link ServiceLoader} can find it.
jaroslav@37
    68
 *
jaroslav@37
    69
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@37
    70
 */
jaroslav@40
    71
public final class FXContext
jaroslav@288
    72
implements Technology.BatchInit<JSObject>, Transfer, WSTransfer<LoadWS> {
jaroslav@37
    73
    static final Logger LOG = Logger.getLogger(FXContext.class.getName());
jaroslav@254
    74
    private static Boolean javaScriptEnabled;
jaroslav@288
    75
    private final Fn.Presenter browserContext;
jaroslav@288
    76
jaroslav@288
    77
    public FXContext(Fn.Presenter browserContext) {
jaroslav@288
    78
        this.browserContext = browserContext;
jaroslav@288
    79
    }
jaroslav@150
    80
    
jaroslav@153
    81
    @JavaScriptBody(args = {}, body = "return true;")
jaroslav@254
    82
    private static boolean isJavaScriptEnabledJs() {
jaroslav@254
    83
        return false;
jaroslav@254
    84
    }
jaroslav@254
    85
    
jaroslav@288
    86
    static boolean isJavaScriptEnabled() {
jaroslav@254
    87
        if (javaScriptEnabled != null) {
jaroslav@254
    88
            return javaScriptEnabled;
jaroslav@254
    89
        }
jaroslav@254
    90
        return javaScriptEnabled = isJavaScriptEnabledJs();
jaroslav@150
    91
    }
jaroslav@40
    92
jaroslav@258
    93
    final boolean areWebSocketsSupported() {
jaroslav@258
    94
        return LoadWS.isSupported();
jaroslav@258
    95
    }
jaroslav@258
    96
jaroslav@275
    97
jaroslav@275
    98
    @Override
jaroslav@275
    99
    public JSObject wrapModel(Object model, PropertyBinding[] propArr, FunctionBinding[] funcArr) {
jaroslav@275
   100
        String[] propNames = new String[propArr.length];
jaroslav@275
   101
        boolean[] propReadOnly = new boolean[propArr.length];
jaroslav@279
   102
        Object[] propValues = new Object[propArr.length];
jaroslav@275
   103
        for (int i = 0; i < propNames.length; i++) {
jaroslav@275
   104
            propNames[i] = propArr[i].getPropertyName();
jaroslav@275
   105
            propReadOnly[i] = propArr[i].isReadOnly();
jaroslav@279
   106
            propValues[i] = propArr[i].getValue();
jaroslav@275
   107
        }
jaroslav@275
   108
        String[] funcNames = new String[funcArr.length];
jaroslav@275
   109
        for (int i = 0; i < funcNames.length; i++) {
jaroslav@275
   110
            funcNames[i] = funcArr[i].getFunctionName();
jaroslav@275
   111
        }
jaroslav@279
   112
        
jaroslav@279
   113
        return Knockout.wrapModel(model, 
jaroslav@279
   114
            propNames, propReadOnly, Knockout.toArray(propValues), propArr, 
jaroslav@279
   115
            funcNames, funcArr
jaroslav@279
   116
        );
jaroslav@275
   117
    }
jaroslav@275
   118
    
jaroslav@37
   119
    @Override
jaroslav@37
   120
    public JSObject wrapModel(Object model) {
jaroslav@276
   121
        throw new UnsupportedOperationException();
jaroslav@37
   122
    }
jaroslav@37
   123
jaroslav@37
   124
    @Override
jaroslav@37
   125
    public void bind(PropertyBinding b, Object model, JSObject data) {
jaroslav@276
   126
        throw new UnsupportedOperationException();
jaroslav@37
   127
    }
jaroslav@37
   128
jaroslav@37
   129
    @Override
jaroslav@37
   130
    public void valueHasMutated(JSObject data, String propertyName) {
jaroslav@37
   131
        Knockout.valueHasMutated(data, propertyName);
jaroslav@37
   132
    }
jaroslav@37
   133
jaroslav@37
   134
    @Override
jaroslav@37
   135
    public void expose(FunctionBinding fb, Object model, JSObject d) {
jaroslav@276
   136
        throw new UnsupportedOperationException();
jaroslav@37
   137
    }
jaroslav@37
   138
jaroslav@37
   139
    @Override
jaroslav@37
   140
    public void applyBindings(JSObject data) {
jaroslav@37
   141
        Knockout.applyBindings(data);
jaroslav@37
   142
    }
jaroslav@37
   143
jaroslav@37
   144
    @Override
jaroslav@37
   145
    public Object wrapArray(Object[] arr) {
jaroslav@37
   146
        return Knockout.toArray(arr);
jaroslav@37
   147
    }
jaroslav@37
   148
jaroslav@37
   149
    @Override
jaroslav@37
   150
    public void extract(Object obj, String[] props, Object[] values) {
jaroslav@37
   151
        LoadJSON.extractJSON(obj, props, values);
jaroslav@37
   152
    }
jaroslav@37
   153
jaroslav@37
   154
    @Override
jaroslav@37
   155
    public void loadJSON(final JSONCall call) {
jaroslav@37
   156
        LoadJSON.loadJSON(call);
jaroslav@37
   157
    }
jaroslav@38
   158
jaroslav@38
   159
    @Override
jaroslav@38
   160
    public <M> M toModel(Class<M> modelClass, Object data) {
jaroslav@38
   161
        if (data instanceof JSObject) {
jaroslav@38
   162
            data = ((JSObject)data).getMember("ko-fx.model"); // NOI18N
jaroslav@38
   163
        }
jaroslav@38
   164
        return modelClass.cast(data);
jaroslav@38
   165
    }
jaroslav@60
   166
jaroslav@60
   167
    @Override
jaroslav@60
   168
    public Object toJSON(InputStream is) throws IOException {
jaroslav@60
   169
        return LoadJSON.parse(is);
jaroslav@60
   170
    }
jaroslav@240
   171
jaroslav@240
   172
    @Override
jaroslav@288
   173
    public void runSafe(final Runnable r) {
jaroslav@288
   174
        class Wrap implements Runnable {
jaroslav@288
   175
            @Override public void run() {
jaroslav@322
   176
                try (Closeable c = Fn.activate(browserContext)) {
jaroslav@288
   177
                    r.run();
jaroslav@322
   178
                } catch (IOException ex) {
jaroslav@322
   179
                    // cannot be thrown
jaroslav@288
   180
                }
jaroslav@288
   181
            }
jaroslav@288
   182
        }
jaroslav@288
   183
        Wrap w = new Wrap();
jaroslav@288
   184
        
jaroslav@262
   185
        if (Platform.isFxApplicationThread()) {
jaroslav@288
   186
            w.run();
jaroslav@262
   187
        } else {
jaroslav@288
   188
            Platform.runLater(w);
jaroslav@262
   189
        }
jaroslav@240
   190
    }
jaroslav@258
   191
jaroslav@258
   192
    @Override
jaroslav@258
   193
    public LoadWS open(String url, JSONCall onReply) {
jaroslav@258
   194
        return new LoadWS(onReply, url);
jaroslav@258
   195
    }
jaroslav@258
   196
jaroslav@258
   197
    @Override
jaroslav@258
   198
    public void send(LoadWS socket, JSONCall data) {
jaroslav@258
   199
        socket.send(data);
jaroslav@258
   200
    }
jaroslav@258
   201
jaroslav@258
   202
    @Override
jaroslav@258
   203
    public void close(LoadWS socket) {
jaroslav@258
   204
        socket.close();
jaroslav@258
   205
    }
jaroslav@288
   206
    
jaroslav@288
   207
    @ServiceProvider(service = Contexts.Provider.class)
jaroslav@288
   208
    public static final class Prvdr implements Contexts.Provider {
jaroslav@288
   209
        @Override
jaroslav@288
   210
        public void fillContext(Contexts.Builder context, Class<?> requestor) {
jaroslav@288
   211
            if (isJavaScriptEnabled()) {
jaroslav@322
   212
                FXContext c = new FXContext(Fn.activePresenter());
jaroslav@288
   213
                
jaroslav@288
   214
                context.register(Technology.class, c, 100);
jaroslav@288
   215
                context.register(Transfer.class, c, 100);
jaroslav@288
   216
                if (c.areWebSocketsSupported()) {
jaroslav@288
   217
                    context.register(WSTransfer.class, c, 100);
jaroslav@288
   218
                }
jaroslav@288
   219
            }
jaroslav@288
   220
        }
jaroslav@288
   221
    }
jaroslav@37
   222
}