1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/ko-fx/src/main/java/org/netbeans/html/kofx/FXContext.java Mon Dec 16 16:59:43 2013 +0100
1.3 @@ -0,0 +1,222 @@
1.4 +/**
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 + *
1.7 + * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
1.8 + *
1.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
1.10 + * Other names may be trademarks of their respective owners.
1.11 + *
1.12 + * The contents of this file are subject to the terms of either the GNU
1.13 + * General Public License Version 2 only ("GPL") or the Common
1.14 + * Development and Distribution License("CDDL") (collectively, the
1.15 + * "License"). You may not use this file except in compliance with the
1.16 + * License. You can obtain a copy of the License at
1.17 + * http://www.netbeans.org/cddl-gplv2.html
1.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.19 + * specific language governing permissions and limitations under the
1.20 + * License. When distributing the software, include this License Header
1.21 + * Notice in each file and include the License file at
1.22 + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
1.23 + * particular file as subject to the "Classpath" exception as provided
1.24 + * by Oracle in the GPL Version 2 section of the License file that
1.25 + * accompanied this code. If applicable, add the following below the
1.26 + * License Header, with the fields enclosed by brackets [] replaced by
1.27 + * your own identifying information:
1.28 + * "Portions Copyrighted [year] [name of copyright owner]"
1.29 + *
1.30 + * Contributor(s):
1.31 + *
1.32 + * The Original Software is NetBeans. The Initial Developer of the Original
1.33 + * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
1.34 + *
1.35 + * If you wish your version of this file to be governed by only the CDDL
1.36 + * or only the GPL Version 2, indicate your decision by adding
1.37 + * "[Contributor] elects to include this software in this distribution
1.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.39 + * single choice of license, a recipient has the option to distribute
1.40 + * your version of this file under either the CDDL, the GPL Version 2 or
1.41 + * to extend the choice of license to its licensees as provided above.
1.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
1.43 + * Version 2 license, then the option applies only if the new code is
1.44 + * made subject to such option by the copyright holder.
1.45 + */
1.46 +package org.netbeans.html.kofx;
1.47 +
1.48 +import java.io.Closeable;
1.49 +import java.io.IOException;
1.50 +import java.io.InputStream;
1.51 +import java.util.ServiceLoader;
1.52 +import java.util.logging.Logger;
1.53 +import javafx.application.Platform;
1.54 +import net.java.html.js.JavaScriptBody;
1.55 +import netscape.javascript.JSObject;
1.56 +import org.apidesign.html.boot.spi.Fn;
1.57 +import org.apidesign.html.context.spi.Contexts;
1.58 +import org.apidesign.html.json.spi.FunctionBinding;
1.59 +import org.apidesign.html.json.spi.JSONCall;
1.60 +import org.apidesign.html.json.spi.PropertyBinding;
1.61 +import org.apidesign.html.json.spi.Technology;
1.62 +import org.apidesign.html.json.spi.Transfer;
1.63 +import org.apidesign.html.json.spi.WSTransfer;
1.64 +import org.openide.util.lookup.ServiceProvider;
1.65 +
1.66 +/** This is an implementation package - just
1.67 + * include its JAR on classpath and use official {@link Context} API
1.68 + * to access the functionality.
1.69 + * <p>
1.70 + * Registers {@link ContextProvider}, so {@link ServiceLoader} can find it.
1.71 + *
1.72 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.73 + */
1.74 +public final class FXContext
1.75 +implements Technology.BatchInit<JSObject>, Transfer, WSTransfer<LoadWS> {
1.76 + static final Logger LOG = Logger.getLogger(FXContext.class.getName());
1.77 + private static Boolean javaScriptEnabled;
1.78 + private final Fn.Presenter browserContext;
1.79 +
1.80 + public FXContext(Fn.Presenter browserContext) {
1.81 + this.browserContext = browserContext;
1.82 + }
1.83 +
1.84 + @JavaScriptBody(args = {}, body = "return true;")
1.85 + private static boolean isJavaScriptEnabledJs() {
1.86 + return false;
1.87 + }
1.88 +
1.89 + static boolean isJavaScriptEnabled() {
1.90 + if (javaScriptEnabled != null) {
1.91 + return javaScriptEnabled;
1.92 + }
1.93 + return javaScriptEnabled = isJavaScriptEnabledJs();
1.94 + }
1.95 +
1.96 + final boolean areWebSocketsSupported() {
1.97 + return LoadWS.isSupported();
1.98 + }
1.99 +
1.100 +
1.101 + @Override
1.102 + public JSObject wrapModel(Object model, PropertyBinding[] propArr, FunctionBinding[] funcArr) {
1.103 + String[] propNames = new String[propArr.length];
1.104 + boolean[] propReadOnly = new boolean[propArr.length];
1.105 + Object[] propValues = new Object[propArr.length];
1.106 + for (int i = 0; i < propNames.length; i++) {
1.107 + propNames[i] = propArr[i].getPropertyName();
1.108 + propReadOnly[i] = propArr[i].isReadOnly();
1.109 + propValues[i] = propArr[i].getValue();
1.110 + }
1.111 + String[] funcNames = new String[funcArr.length];
1.112 + for (int i = 0; i < funcNames.length; i++) {
1.113 + funcNames[i] = funcArr[i].getFunctionName();
1.114 + }
1.115 +
1.116 + return Knockout.wrapModel(model,
1.117 + propNames, propReadOnly, Knockout.toArray(propValues), propArr,
1.118 + funcNames, funcArr
1.119 + );
1.120 + }
1.121 +
1.122 + @Override
1.123 + public JSObject wrapModel(Object model) {
1.124 + throw new UnsupportedOperationException();
1.125 + }
1.126 +
1.127 + @Override
1.128 + public void bind(PropertyBinding b, Object model, JSObject data) {
1.129 + throw new UnsupportedOperationException();
1.130 + }
1.131 +
1.132 + @Override
1.133 + public void valueHasMutated(JSObject data, String propertyName) {
1.134 + Knockout.valueHasMutated(data, propertyName);
1.135 + }
1.136 +
1.137 + @Override
1.138 + public void expose(FunctionBinding fb, Object model, JSObject d) {
1.139 + throw new UnsupportedOperationException();
1.140 + }
1.141 +
1.142 + @Override
1.143 + public void applyBindings(JSObject data) {
1.144 + Knockout.applyBindings(data);
1.145 + }
1.146 +
1.147 + @Override
1.148 + public Object wrapArray(Object[] arr) {
1.149 + return Knockout.toArray(arr);
1.150 + }
1.151 +
1.152 + @Override
1.153 + public void extract(Object obj, String[] props, Object[] values) {
1.154 + LoadJSON.extractJSON(obj, props, values);
1.155 + }
1.156 +
1.157 + @Override
1.158 + public void loadJSON(final JSONCall call) {
1.159 + LoadJSON.loadJSON(call);
1.160 + }
1.161 +
1.162 + @Override
1.163 + public <M> M toModel(Class<M> modelClass, Object data) {
1.164 + if (data instanceof JSObject) {
1.165 + data = ((JSObject)data).getMember("ko-fx.model"); // NOI18N
1.166 + }
1.167 + return modelClass.cast(data);
1.168 + }
1.169 +
1.170 + @Override
1.171 + public Object toJSON(InputStream is) throws IOException {
1.172 + return LoadJSON.parse(is);
1.173 + }
1.174 +
1.175 + @Override
1.176 + public void runSafe(final Runnable r) {
1.177 + class Wrap implements Runnable {
1.178 + @Override public void run() {
1.179 + try (Closeable c = Fn.activate(browserContext)) {
1.180 + r.run();
1.181 + } catch (IOException ex) {
1.182 + // cannot be thrown
1.183 + }
1.184 + }
1.185 + }
1.186 + Wrap w = new Wrap();
1.187 +
1.188 + if (Platform.isFxApplicationThread()) {
1.189 + w.run();
1.190 + } else {
1.191 + Platform.runLater(w);
1.192 + }
1.193 + }
1.194 +
1.195 + @Override
1.196 + public LoadWS open(String url, JSONCall onReply) {
1.197 + return new LoadWS(onReply, url);
1.198 + }
1.199 +
1.200 + @Override
1.201 + public void send(LoadWS socket, JSONCall data) {
1.202 + socket.send(data);
1.203 + }
1.204 +
1.205 + @Override
1.206 + public void close(LoadWS socket) {
1.207 + socket.close();
1.208 + }
1.209 +
1.210 + @ServiceProvider(service = Contexts.Provider.class)
1.211 + public static final class Prvdr implements Contexts.Provider {
1.212 + @Override
1.213 + public void fillContext(Contexts.Builder context, Class<?> requestor) {
1.214 + if (isJavaScriptEnabled()) {
1.215 + FXContext c = new FXContext(Fn.activePresenter());
1.216 +
1.217 + context.register(Technology.class, c, 100);
1.218 + context.register(Transfer.class, c, 100);
1.219 + if (c.areWebSocketsSupported()) {
1.220 + context.register(WSTransfer.class, c, 100);
1.221 + }
1.222 + }
1.223 + }
1.224 + }
1.225 +}