2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License. When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
32 * If you wish your version of this file to be governed by only the CDDL
33 * or only the GPL Version 2, indicate your decision by adding
34 * "[Contributor] elects to include this software in this distribution
35 * under the [CDDL or GPL Version 2] license." If you do not indicate a
36 * single choice of license, a recipient has the option to distribute
37 * your version of this file under either the CDDL, the GPL Version 2 or
38 * to extend the choice of license to its licensees as provided above.
39 * However, if you add GPL Version 2 code and therefore, elected the GPL
40 * Version 2 license, then the option applies only if the new code is
41 * made subject to such option by the copyright holder.
43 package org.netbeans.html.kofx;
45 import java.io.Closeable;
46 import java.io.IOException;
47 import java.io.InputStream;
48 import java.util.ServiceLoader;
49 import java.util.logging.Logger;
50 import javafx.application.Platform;
51 import net.java.html.js.JavaScriptBody;
52 import netscape.javascript.JSObject;
53 import org.apidesign.html.boot.spi.Fn;
54 import org.apidesign.html.context.spi.Contexts;
55 import org.apidesign.html.json.spi.FunctionBinding;
56 import org.apidesign.html.json.spi.JSONCall;
57 import org.apidesign.html.json.spi.PropertyBinding;
58 import org.apidesign.html.json.spi.Technology;
59 import org.apidesign.html.json.spi.Transfer;
60 import org.apidesign.html.json.spi.WSTransfer;
61 import org.openide.util.lookup.ServiceProvider;
63 /** This is an implementation package - just
64 * include its JAR on classpath and use official {@link Context} API
65 * to access the functionality.
67 * Registers {@link ContextProvider}, so {@link ServiceLoader} can find it.
69 * @author Jaroslav Tulach <jtulach@netbeans.org>
71 public final class FXContext
72 implements Technology.BatchInit<JSObject>, Transfer, WSTransfer<LoadWS> {
73 static final Logger LOG = Logger.getLogger(FXContext.class.getName());
74 private static Boolean javaScriptEnabled;
75 private final Fn.Presenter browserContext;
77 public FXContext(Fn.Presenter browserContext) {
78 this.browserContext = browserContext;
81 @JavaScriptBody(args = {}, body = "return true;")
82 private static boolean isJavaScriptEnabledJs() {
86 static boolean isJavaScriptEnabled() {
87 if (javaScriptEnabled != null) {
88 return javaScriptEnabled;
90 return javaScriptEnabled = isJavaScriptEnabledJs();
93 final boolean areWebSocketsSupported() {
94 return LoadWS.isSupported();
99 public JSObject wrapModel(Object model, PropertyBinding[] propArr, FunctionBinding[] funcArr) {
100 String[] propNames = new String[propArr.length];
101 boolean[] propReadOnly = new boolean[propArr.length];
102 Object[] propValues = new Object[propArr.length];
103 for (int i = 0; i < propNames.length; i++) {
104 propNames[i] = propArr[i].getPropertyName();
105 propReadOnly[i] = propArr[i].isReadOnly();
106 propValues[i] = propArr[i].getValue();
108 String[] funcNames = new String[funcArr.length];
109 for (int i = 0; i < funcNames.length; i++) {
110 funcNames[i] = funcArr[i].getFunctionName();
113 return Knockout.wrapModel(model,
114 propNames, propReadOnly, Knockout.toArray(propValues), propArr,
120 public JSObject wrapModel(Object model) {
121 throw new UnsupportedOperationException();
125 public void bind(PropertyBinding b, Object model, JSObject data) {
126 throw new UnsupportedOperationException();
130 public void valueHasMutated(JSObject data, String propertyName) {
131 Knockout.valueHasMutated(data, propertyName);
135 public void expose(FunctionBinding fb, Object model, JSObject d) {
136 throw new UnsupportedOperationException();
140 public void applyBindings(JSObject data) {
141 Knockout.applyBindings(data);
145 public Object wrapArray(Object[] arr) {
146 return Knockout.toArray(arr);
150 public void extract(Object obj, String[] props, Object[] values) {
151 LoadJSON.extractJSON(obj, props, values);
155 public void loadJSON(final JSONCall call) {
156 LoadJSON.loadJSON(call);
160 public <M> M toModel(Class<M> modelClass, Object data) {
161 if (data instanceof JSObject) {
162 data = ((JSObject)data).getMember("ko-fx.model"); // NOI18N
164 return modelClass.cast(data);
168 public Object toJSON(InputStream is) throws IOException {
169 return LoadJSON.parse(is);
173 public void runSafe(final Runnable r) {
174 class Wrap implements Runnable {
175 @Override public void run() {
176 try (Closeable c = Fn.activate(browserContext)) {
178 } catch (IOException ex) {
185 if (Platform.isFxApplicationThread()) {
188 Platform.runLater(w);
193 public LoadWS open(String url, JSONCall onReply) {
194 return new LoadWS(onReply, url);
198 public void send(LoadWS socket, JSONCall data) {
203 public void close(LoadWS socket) {
207 @ServiceProvider(service = Contexts.Provider.class)
208 public static final class Prvdr implements Contexts.Provider {
210 public void fillContext(Contexts.Builder context, Class<?> requestor) {
211 if (isJavaScriptEnabled()) {
212 FXContext c = new FXContext(Fn.activePresenter());
214 context.register(Technology.class, c, 100);
215 context.register(Transfer.class, c, 100);
216 if (c.areWebSocketsSupported()) {
217 context.register(WSTransfer.class, c, 100);