1.1 --- a/boot-fx/src/main/java/org/apidesign/html/boot/fx/AbstractFXPresenter.java Mon Dec 16 15:48:09 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,190 +0,0 @@
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.apidesign.html.boot.fx;
1.47 -
1.48 -import java.io.BufferedReader;
1.49 -import java.io.Reader;
1.50 -import java.net.URL;
1.51 -import java.util.ArrayList;
1.52 -import java.util.Arrays;
1.53 -import java.util.List;
1.54 -import java.util.logging.Level;
1.55 -import java.util.logging.Logger;
1.56 -import javafx.application.Platform;
1.57 -import javafx.scene.web.WebEngine;
1.58 -import javafx.scene.web.WebView;
1.59 -import netscape.javascript.JSObject;
1.60 -import org.apidesign.html.boot.spi.Fn;
1.61 -
1.62 -/**
1.63 - *
1.64 - * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.65 - */
1.66 -public abstract class AbstractFXPresenter implements Fn.Presenter {
1.67 - static final Logger LOG = Logger.getLogger(FXPresenter.class.getName());
1.68 - protected static int cnt;
1.69 - protected List<String> scripts;
1.70 - protected Runnable onLoad;
1.71 - protected WebEngine engine;
1.72 -
1.73 - @Override
1.74 - public Fn defineFn(String code, String... names) {
1.75 - StringBuilder sb = new StringBuilder();
1.76 - sb.append("(function() {");
1.77 - sb.append(" return function(");
1.78 - String sep = "";
1.79 - for (String n : names) {
1.80 - sb.append(sep).append(n);
1.81 - sep = ",";
1.82 - }
1.83 - sb.append(") {\n");
1.84 - sb.append(code);
1.85 - sb.append("};");
1.86 - sb.append("})()");
1.87 - if (LOG.isLoggable(Level.FINE)) {
1.88 - LOG.log(Level.FINE,
1.89 - "defining function #{0}:\n{1}\n",
1.90 - new Object[] { ++cnt, code }
1.91 - );
1.92 - }
1.93 - JSObject x = (JSObject) engine.executeScript(sb.toString());
1.94 - return new JSFn(this, x, cnt);
1.95 - }
1.96 -
1.97 - @Override
1.98 - public void loadScript(Reader code) throws Exception {
1.99 - BufferedReader r = new BufferedReader(code);
1.100 - StringBuilder sb = new StringBuilder();
1.101 - for (;;) {
1.102 - String l = r.readLine();
1.103 - if (l == null) {
1.104 - break;
1.105 - }
1.106 - sb.append(l).append('\n');
1.107 - }
1.108 - final String script = sb.toString();
1.109 - if (scripts != null) {
1.110 - scripts.add(script);
1.111 - }
1.112 - engine.executeScript(script);
1.113 - }
1.114 -
1.115 - protected final void onPageLoad() {
1.116 - if (scripts != null) {
1.117 - for (String s : scripts) {
1.118 - engine.executeScript(s);
1.119 - }
1.120 - }
1.121 - onLoad.run();
1.122 - }
1.123 -
1.124 - @Override
1.125 - public void displayPage(final URL resource, final Runnable onLoad) {
1.126 - this.onLoad = onLoad;
1.127 - final WebView view = findView(resource);
1.128 - this.engine = view.getEngine();
1.129 - try {
1.130 - if (FXInspect.initialize(engine)) {
1.131 - scripts = new ArrayList<String>();
1.132 - }
1.133 - } catch (Throwable ex) {
1.134 - ex.printStackTrace();
1.135 - }
1.136 -
1.137 - class Run implements Runnable {
1.138 -
1.139 - @Override
1.140 - public void run() {
1.141 - if (scripts != null) {
1.142 - view.setContextMenuEnabled(true);
1.143 - }
1.144 - engine.load(resource.toExternalForm());
1.145 - }
1.146 - }
1.147 - Run run = new Run();
1.148 - if (Platform.isFxApplicationThread()) {
1.149 - run.run();
1.150 - } else {
1.151 - Platform.runLater(run);
1.152 - }
1.153 - waitFinished();
1.154 - }
1.155 -
1.156 - protected abstract void waitFinished();
1.157 -
1.158 - protected abstract WebView findView(final URL resource);
1.159 -
1.160 - private static final class JSFn extends Fn {
1.161 -
1.162 - private final JSObject fn;
1.163 - private static int call;
1.164 - private final int id;
1.165 -
1.166 - public JSFn(AbstractFXPresenter p, JSObject fn, int id) {
1.167 - super(p);
1.168 - this.fn = fn;
1.169 - this.id = id;
1.170 - }
1.171 -
1.172 - @Override
1.173 - public Object invoke(Object thiz, Object... args) throws Exception {
1.174 - try {
1.175 - if (LOG.isLoggable(Level.FINE)) {
1.176 - LOG.log(Level.FINE, "calling {0} function #{1}", new Object[]{++call, id});
1.177 - }
1.178 - List<Object> all = new ArrayList<Object>(args.length + 1);
1.179 - all.add(thiz == null ? fn : thiz);
1.180 - all.addAll(Arrays.asList(args));
1.181 - Object ret = fn.call("call", all.toArray()); // NOI18N
1.182 - return ret == fn ? null : ret;
1.183 - } catch (Error t) {
1.184 - t.printStackTrace();
1.185 - throw t;
1.186 - } catch (Exception t) {
1.187 - t.printStackTrace();
1.188 - throw t;
1.189 - }
1.190 - }
1.191 - }
1.192 -
1.193 -}