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.boot.fx;
45 import java.io.BufferedReader;
46 import java.io.Reader;
48 import java.util.ArrayList;
49 import java.util.Arrays;
50 import java.util.List;
51 import java.util.logging.Level;
52 import java.util.logging.Logger;
53 import javafx.application.Platform;
54 import javafx.scene.web.WebEngine;
55 import javafx.scene.web.WebView;
56 import netscape.javascript.JSObject;
57 import org.apidesign.html.boot.spi.Fn;
61 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
63 public abstract class AbstractFXPresenter implements Fn.Presenter {
64 static final Logger LOG = Logger.getLogger(FXPresenter.class.getName());
65 protected static int cnt;
66 protected List<String> scripts;
67 protected Runnable onLoad;
68 protected WebEngine engine;
71 public Fn defineFn(String code, String... names) {
72 StringBuilder sb = new StringBuilder();
73 sb.append("(function() {");
74 sb.append(" return function(");
76 for (String n : names) {
77 sb.append(sep).append(n);
84 if (LOG.isLoggable(Level.FINE)) {
86 "defining function #{0}:\n{1}\n",
87 new Object[] { ++cnt, code }
90 JSObject x = (JSObject) engine.executeScript(sb.toString());
91 return new JSFn(this, x, cnt);
95 public void loadScript(Reader code) throws Exception {
96 BufferedReader r = new BufferedReader(code);
97 StringBuilder sb = new StringBuilder();
99 String l = r.readLine();
103 sb.append(l).append('\n');
105 final String script = sb.toString();
106 if (scripts != null) {
109 engine.executeScript(script);
112 protected final void onPageLoad() {
113 if (scripts != null) {
114 for (String s : scripts) {
115 engine.executeScript(s);
122 public void displayPage(final URL resource, final Runnable onLoad) {
123 this.onLoad = onLoad;
124 final WebView view = findView(resource);
125 this.engine = view.getEngine();
127 if (FXInspect.initialize(engine)) {
128 scripts = new ArrayList<String>();
130 } catch (Throwable ex) {
131 ex.printStackTrace();
134 class Run implements Runnable {
138 if (scripts != null) {
139 view.setContextMenuEnabled(true);
141 engine.load(resource.toExternalForm());
145 if (Platform.isFxApplicationThread()) {
148 Platform.runLater(run);
153 protected abstract void waitFinished();
155 protected abstract WebView findView(final URL resource);
157 private static final class JSFn extends Fn {
159 private final JSObject fn;
160 private static int call;
161 private final int id;
163 public JSFn(AbstractFXPresenter p, JSObject fn, int id) {
170 public Object invoke(Object thiz, Object... args) throws Exception {
172 if (LOG.isLoggable(Level.FINE)) {
173 LOG.log(Level.FINE, "calling {0} function #{1}", new Object[]{++call, id});
175 List<Object> all = new ArrayList<Object>(args.length + 1);
176 all.add(thiz == null ? fn : thiz);
177 all.addAll(Arrays.asList(args));
178 Object ret = fn.call("call", all.toArray()); // NOI18N
179 return ret == fn ? null : ret;
183 } catch (Exception t) {