#257154: Simplifying initialization of knockout technology. Use Models.toRaw(null);
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/json/src/main/java/net/java/html/json/FakeModel.java Mon Feb 29 19:54:28 2016 +0100
1.3 @@ -0,0 +1,122 @@
1.4 +/**
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 + *
1.7 + * Copyright 2013-2014 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-2014 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 net.java.html.json;
1.47 +
1.48 +/**
1.49 + * Generated for {@link Models}
1.50 + */
1.51 +final class FakeModel implements Cloneable {
1.52 + private static Class<Models> modelFor() {
1.53 + return Models.class;
1.54 + }
1.55 + private static final Html4JavaType TYPE = new Html4JavaType();
1.56 + private final org.netbeans.html.json.spi.Proto proto;
1.57 +
1.58 + private FakeModel(net.java.html.BrwsrCtx context) {
1.59 + this.proto = TYPE.createProto(this, context);
1.60 + }
1.61 +
1.62 + private FakeModel() {
1.63 + this(net.java.html.BrwsrCtx.findDefault(FakeModel.class));
1.64 + }
1.65 +
1.66 + public static Object create() {
1.67 + return new FakeModel();
1.68 + }
1.69 +
1.70 + private static class Html4JavaType extends org.netbeans.html.json.spi.Proto.Type<FakeModel> {
1.71 +
1.72 + private Html4JavaType() {
1.73 + super(FakeModel.class, Models.class, 0, 0);
1.74 + }
1.75 +
1.76 + @Override
1.77 + public void setValue(FakeModel data, int type, Object value) {
1.78 + throw new UnsupportedOperationException();
1.79 + }
1.80 +
1.81 + @Override
1.82 + public Object getValue(FakeModel data, int type) {
1.83 + throw new UnsupportedOperationException();
1.84 + }
1.85 +
1.86 + @Override
1.87 + public void call(FakeModel model, int type, Object data, Object ev) throws Exception {
1.88 + switch (type) {
1.89 + }
1.90 + throw new UnsupportedOperationException();
1.91 + }
1.92 +
1.93 + @Override
1.94 + public org.netbeans.html.json.spi.Proto protoFor(Object obj) {
1.95 + return ((FakeModel) obj).proto;
1.96 + }
1.97 +
1.98 + @Override
1.99 + public void onChange(FakeModel model, int type) {
1.100 + throw new UnsupportedOperationException();
1.101 + }
1.102 +
1.103 + @Override
1.104 + public void onMessage(FakeModel model, int index, int type, Object data, Object[] params) {
1.105 + throw new UnsupportedOperationException();
1.106 + }
1.107 +
1.108 + @Override
1.109 + public FakeModel read(net.java.html.BrwsrCtx c, Object json) {
1.110 + return new FakeModel(c, json);
1.111 + }
1.112 +
1.113 + @Override
1.114 + public FakeModel cloneTo(FakeModel o, net.java.html.BrwsrCtx c) {
1.115 + return o;
1.116 + }
1.117 + }
1.118 +
1.119 + private FakeModel(net.java.html.BrwsrCtx c, Object json) {
1.120 + this(c);
1.121 + Object[] ret = new Object[0];
1.122 + proto.extract(json, new String[]{}, ret);
1.123 + }
1.124 +
1.125 +}
2.1 --- a/json/src/main/java/net/java/html/json/Models.java Mon Feb 29 19:37:04 2016 +0100
2.2 +++ b/json/src/main/java/net/java/html/json/Models.java Mon Feb 29 19:54:28 2016 +0100
2.3 @@ -47,6 +47,7 @@
2.4 import java.io.InputStream;
2.5 import java.util.Collection;
2.6 import org.netbeans.html.json.impl.JSON;
2.7 +import org.netbeans.html.json.spi.Technology;
2.8
2.9 /** Information about and
2.10 * operations for classes generated by the {@link Model @Model}
2.11 @@ -137,14 +138,20 @@
2.12 * JSON object. The object may, but does not have to, be the same instance
2.13 * as the model object.
2.14 *
2.15 - * @param model the model object (not <code>null</code>)
2.16 + * @param model the model object
2.17 + * (can be <code>null</code> to initialize the associated {@link Technology})
2.18 * @return the raw JSON object associated with the model
2.19 + * (<code>null</code> if the <code>model</code> parameter was null)
2.20 * @throws IllegalArgumentException if the <code>model</code> is
2.21 * not instance of a class
2.22 * generated by {@link Model model annotation} processor.
2.23 * @since 0.7
2.24 */
2.25 public static Object toRaw(Object model) {
2.26 + if (model == null) {
2.27 + toRaw(FakeModel.create());
2.28 + return null;
2.29 + }
2.30 final Class<? extends Object> type = model.getClass();
2.31 if (!isModel(type)) {
2.32 throw new IllegalStateException("Not a model " + type);
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/ko4j/src/test/java/org/netbeans/html/ko4j/InitializeKnockoutTest.java Mon Feb 29 19:54:28 2016 +0100
3.3 @@ -0,0 +1,156 @@
3.4 +/**
3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3.6 + *
3.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
3.8 + *
3.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
3.10 + * Other names may be trademarks of their respective owners.
3.11 + *
3.12 + * The contents of this file are subject to the terms of either the GNU
3.13 + * General Public License Version 2 only ("GPL") or the Common
3.14 + * Development and Distribution License("CDDL") (collectively, the
3.15 + * "License"). You may not use this file except in compliance with the
3.16 + * License. You can obtain a copy of the License at
3.17 + * http://www.netbeans.org/cddl-gplv2.html
3.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
3.19 + * specific language governing permissions and limitations under the
3.20 + * License. When distributing the software, include this License Header
3.21 + * Notice in each file and include the License file at
3.22 + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
3.23 + * particular file as subject to the "Classpath" exception as provided
3.24 + * by Oracle in the GPL Version 2 section of the License file that
3.25 + * accompanied this code. If applicable, add the following below the
3.26 + * License Header, with the fields enclosed by brackets [] replaced by
3.27 + * your own identifying information:
3.28 + * "Portions Copyrighted [year] [name of copyright owner]"
3.29 + *
3.30 + * Contributor(s):
3.31 + *
3.32 + * The Original Software is NetBeans. The Initial Developer of the Original
3.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
3.34 + *
3.35 + * If you wish your version of this file to be governed by only the CDDL
3.36 + * or only the GPL Version 2, indicate your decision by adding
3.37 + * "[Contributor] elects to include this software in this distribution
3.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
3.39 + * single choice of license, a recipient has the option to distribute
3.40 + * your version of this file under either the CDDL, the GPL Version 2 or
3.41 + * to extend the choice of license to its licensees as provided above.
3.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
3.43 + * Version 2 license, then the option applies only if the new code is
3.44 + * made subject to such option by the copyright holder.
3.45 + */
3.46 +package org.netbeans.html.ko4j;
3.47 +
3.48 +import java.net.URL;
3.49 +import java.util.concurrent.CountDownLatch;
3.50 +import javafx.application.Application;
3.51 +import javafx.application.Platform;
3.52 +import javafx.scene.Scene;
3.53 +import javafx.scene.layout.BorderPane;
3.54 +import javafx.scene.web.WebView;
3.55 +import javafx.stage.Stage;
3.56 +import net.java.html.BrwsrCtx;
3.57 +import net.java.html.boot.fx.FXBrowsers;
3.58 +import net.java.html.js.JavaScriptBody;
3.59 +import net.java.html.json.Models;
3.60 +import org.testng.annotations.BeforeClass;
3.61 +import org.testng.annotations.Test;
3.62 +import static org.testng.Assert.assertNull;
3.63 +import static org.testng.Assert.assertNotNull;
3.64 +
3.65 +/**
3.66 + *
3.67 + * @author Jaroslav Tulach
3.68 + */
3.69 +public class InitializeKnockoutTest {
3.70 + public InitializeKnockoutTest() {
3.71 + }
3.72 +
3.73 + @BeforeClass
3.74 + public void initFX() throws Throwable {
3.75 + new Thread("initFX") {
3.76 + @Override
3.77 + public void run() {
3.78 + if (Platform.isFxApplicationThread()) {
3.79 + new App().start(new Stage());
3.80 + } else {
3.81 + try {
3.82 + App.launch(App.class);
3.83 + } catch (IllegalStateException ex) {
3.84 + Platform.runLater(this);
3.85 + }
3.86 + }
3.87 + }
3.88 + }.start();
3.89 + App.CDL.await();
3.90 + }
3.91 +
3.92 + @JavaScriptBody(args = {}, body = "return typeof ko !== 'undefined' ? ko : null;")
3.93 + static native Object ko();
3.94 +
3.95 + @Test
3.96 + public void brwsrCtxExecute() throws Throwable {
3.97 + final CountDownLatch init = new CountDownLatch(1);
3.98 + final BrwsrCtx[] ctx = { null };
3.99 + FXBrowsers.runInBrowser(App.webView(), new Runnable() {
3.100 + @Override
3.101 + public void run() {
3.102 + ctx[0] = BrwsrCtx.findDefault(InitializeKnockoutTest.class);
3.103 + init.countDown();
3.104 + }
3.105 + });
3.106 + init.await();
3.107 +
3.108 + final CountDownLatch cdl = new CountDownLatch(1);
3.109 + FXBrowsers.runInBrowser(App.webView(), new Runnable() {
3.110 + @Override
3.111 + public void run() {
3.112 + assertNull(ko(), "Knockout isn't yet defined");
3.113 + Models.toRaw(null);
3.114 + assertNotNull(ko(), "After call to toRaw, ko is defined");
3.115 +
3.116 + cdl.countDown();
3.117 + }
3.118 + });
3.119 +
3.120 + cdl.await();
3.121 + }
3.122 +
3.123 + public static class App extends Application {
3.124 + static final CountDownLatch CDL = new CountDownLatch(1);
3.125 + private static BorderPane pane;
3.126 +
3.127 + static WebView webView() {
3.128 + try {
3.129 + CDL.await();
3.130 + } catch (InterruptedException ex) {
3.131 + throw new IllegalStateException(ex);
3.132 + }
3.133 + return (WebView)System.getProperties().get("v1");
3.134 + }
3.135 +
3.136 + @Override
3.137 + public void start(Stage stage) {
3.138 + pane= new BorderPane();
3.139 + Scene scene = new Scene(pane, 800, 600);
3.140 + stage.setScene(scene);
3.141 + final WebView w1 = new WebView();
3.142 + System.getProperties().put("v1", w1);
3.143 + pane.setCenter(w1);
3.144 +
3.145 +
3.146 + URL url = InitializeKnockoutTest.class.getResource("test.html");
3.147 + assertNotNull(url);
3.148 + FXBrowsers.load(w1, url, new Runnable() {
3.149 + @Override
3.150 + public void run() {
3.151 + CDL.countDown();
3.152 + }
3.153 + });
3.154 +
3.155 + }
3.156 +
3.157 +
3.158 + }
3.159 +}