boot-fx/src/main/java/org/netbeans/html/boot/fx/AbstractFXPresenter.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 16 Dec 2013 16:59:43 +0100
branchnetbeans
changeset 362 92fb71afdc0e
parent 358 boot-fx/src/main/java/org/apidesign/html/boot/fx/AbstractFXPresenter.java@80702021b851
child 365 5c93ad8c7a15
permissions -rw-r--r--
Moving implementation classes into org.netbeans.html namespace
jaroslav@288
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@288
     3
 *
jaroslav@358
     4
 * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
jaroslav@288
     5
 *
jaroslav@358
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@358
     7
 * Other names may be trademarks of their respective owners.
jaroslav@288
     8
 *
jaroslav@358
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@358
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@358
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@358
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@358
    13
 * License. You can obtain a copy of the License at
jaroslav@358
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@358
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@358
    16
 * specific language governing permissions and limitations under the
jaroslav@358
    17
 * License.  When distributing the software, include this License Header
jaroslav@358
    18
 * Notice in each file and include the License file at
jaroslav@358
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@358
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@358
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@358
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@358
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@358
    24
 * your own identifying information:
jaroslav@358
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@358
    26
 *
jaroslav@358
    27
 * Contributor(s):
jaroslav@358
    28
 *
jaroslav@358
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@358
    30
 * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
jaroslav@358
    31
 *
jaroslav@358
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@358
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@358
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@358
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@358
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@358
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@358
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@358
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@358
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@358
    41
 * made subject to such option by the copyright holder.
jaroslav@288
    42
 */
jaroslav@362
    43
package org.netbeans.html.boot.fx;
jaroslav@288
    44
jaroslav@288
    45
import java.io.BufferedReader;
jaroslav@288
    46
import java.io.Reader;
jaroslav@288
    47
import java.net.URL;
jaroslav@288
    48
import java.util.ArrayList;
jaroslav@288
    49
import java.util.Arrays;
jaroslav@288
    50
import java.util.List;
jaroslav@288
    51
import java.util.logging.Level;
jaroslav@288
    52
import java.util.logging.Logger;
jaroslav@288
    53
import javafx.application.Platform;
jaroslav@288
    54
import javafx.scene.web.WebEngine;
jaroslav@288
    55
import javafx.scene.web.WebView;
jaroslav@288
    56
import netscape.javascript.JSObject;
jaroslav@288
    57
import org.apidesign.html.boot.spi.Fn;
jaroslav@288
    58
jaroslav@288
    59
/**
jaroslav@288
    60
 *
jaroslav@288
    61
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@288
    62
 */
jaroslav@288
    63
public abstract class AbstractFXPresenter implements Fn.Presenter {
jaroslav@288
    64
    static final Logger LOG = Logger.getLogger(FXPresenter.class.getName());
jaroslav@288
    65
    protected static int cnt;
jaroslav@288
    66
    protected List<String> scripts;
jaroslav@288
    67
    protected Runnable onLoad;
jaroslav@288
    68
    protected WebEngine engine;
jaroslav@288
    69
jaroslav@288
    70
    @Override
jaroslav@288
    71
    public Fn defineFn(String code, String... names) {
jaroslav@288
    72
        StringBuilder sb = new StringBuilder();
jaroslav@288
    73
        sb.append("(function() {");
jaroslav@288
    74
        sb.append("  return function(");
jaroslav@288
    75
        String sep = "";
jaroslav@288
    76
        for (String n : names) {
jaroslav@288
    77
            sb.append(sep).append(n);
jaroslav@288
    78
            sep = ",";
jaroslav@288
    79
        }
jaroslav@288
    80
        sb.append(") {\n");
jaroslav@288
    81
        sb.append(code);
jaroslav@288
    82
        sb.append("};");
jaroslav@288
    83
        sb.append("})()");
jaroslav@288
    84
        if (LOG.isLoggable(Level.FINE)) {
jaroslav@302
    85
            LOG.log(Level.FINE, 
jaroslav@302
    86
                "defining function #{0}:\n{1}\n", 
jaroslav@302
    87
                new Object[] { ++cnt, code }
jaroslav@302
    88
            );
jaroslav@288
    89
        }
jaroslav@288
    90
        JSObject x = (JSObject) engine.executeScript(sb.toString());
jaroslav@288
    91
        return new JSFn(this, x, cnt);
jaroslav@288
    92
    }
jaroslav@288
    93
jaroslav@288
    94
    @Override
jaroslav@288
    95
    public void loadScript(Reader code) throws Exception {
jaroslav@288
    96
        BufferedReader r = new BufferedReader(code);
jaroslav@288
    97
        StringBuilder sb = new StringBuilder();
jaroslav@288
    98
        for (;;) {
jaroslav@288
    99
            String l = r.readLine();
jaroslav@288
   100
            if (l == null) {
jaroslav@288
   101
                break;
jaroslav@288
   102
            }
jaroslav@288
   103
            sb.append(l).append('\n');
jaroslav@288
   104
        }
jaroslav@288
   105
        final String script = sb.toString();
jaroslav@288
   106
        if (scripts != null) {
jaroslav@288
   107
            scripts.add(script);
jaroslav@288
   108
        }
jaroslav@288
   109
        engine.executeScript(script);
jaroslav@288
   110
    }
jaroslav@288
   111
jaroslav@288
   112
    protected final void onPageLoad() {
jaroslav@288
   113
        if (scripts != null) {
jaroslav@288
   114
            for (String s : scripts) {
jaroslav@288
   115
                engine.executeScript(s);
jaroslav@288
   116
            }
jaroslav@288
   117
        }
jaroslav@288
   118
        onLoad.run();
jaroslav@288
   119
    }
jaroslav@288
   120
jaroslav@288
   121
    @Override
jaroslav@288
   122
    public void displayPage(final URL resource, final Runnable onLoad) {
jaroslav@288
   123
        this.onLoad = onLoad;
jaroslav@288
   124
        final WebView view = findView(resource);
jaroslav@288
   125
        this.engine = view.getEngine();
jaroslav@288
   126
        try {
jaroslav@288
   127
            if (FXInspect.initialize(engine)) {
jaroslav@288
   128
                scripts = new ArrayList<String>();
jaroslav@288
   129
            }
jaroslav@288
   130
        } catch (Throwable ex) {
jaroslav@288
   131
            ex.printStackTrace();
jaroslav@288
   132
        }
jaroslav@288
   133
jaroslav@288
   134
        class Run implements Runnable {
jaroslav@288
   135
jaroslav@288
   136
            @Override
jaroslav@288
   137
            public void run() {
jaroslav@288
   138
                if (scripts != null) {
jaroslav@288
   139
                    view.setContextMenuEnabled(true);
jaroslav@288
   140
                }
jaroslav@288
   141
                engine.load(resource.toExternalForm());
jaroslav@288
   142
            }
jaroslav@288
   143
        }
jaroslav@288
   144
        Run run = new Run();
jaroslav@288
   145
        if (Platform.isFxApplicationThread()) {
jaroslav@288
   146
            run.run();
jaroslav@288
   147
        } else {
jaroslav@288
   148
            Platform.runLater(run);
jaroslav@288
   149
        }
jaroslav@288
   150
        waitFinished();
jaroslav@288
   151
    }
jaroslav@288
   152
jaroslav@288
   153
    protected abstract void waitFinished();
jaroslav@288
   154
jaroslav@288
   155
    protected abstract WebView findView(final URL resource);
jaroslav@288
   156
jaroslav@288
   157
    private static final class JSFn extends Fn {
jaroslav@288
   158
jaroslav@288
   159
        private final JSObject fn;
jaroslav@288
   160
        private static int call;
jaroslav@288
   161
        private final int id;
jaroslav@288
   162
jaroslav@288
   163
        public JSFn(AbstractFXPresenter p, JSObject fn, int id) {
jaroslav@288
   164
            super(p);
jaroslav@288
   165
            this.fn = fn;
jaroslav@288
   166
            this.id = id;
jaroslav@288
   167
        }
jaroslav@288
   168
jaroslav@288
   169
        @Override
jaroslav@289
   170
        public Object invoke(Object thiz, Object... args) throws Exception {
jaroslav@288
   171
            try {
jaroslav@288
   172
                if (LOG.isLoggable(Level.FINE)) {
jaroslav@288
   173
                    LOG.log(Level.FINE, "calling {0} function #{1}", new Object[]{++call, id});
jaroslav@288
   174
                }
jaroslav@288
   175
                List<Object> all = new ArrayList<Object>(args.length + 1);
jaroslav@288
   176
                all.add(thiz == null ? fn : thiz);
jaroslav@288
   177
                all.addAll(Arrays.asList(args));
jaroslav@288
   178
                Object ret = fn.call("call", all.toArray()); // NOI18N
jaroslav@288
   179
                return ret == fn ? null : ret;
jaroslav@288
   180
            } catch (Error t) {
jaroslav@288
   181
                t.printStackTrace();
jaroslav@288
   182
                throw t;
jaroslav@288
   183
            } catch (Exception t) {
jaroslav@288
   184
                t.printStackTrace();
jaroslav@288
   185
                throw t;
jaroslav@288
   186
            }
jaroslav@288
   187
        }
jaroslav@288
   188
    }
jaroslav@288
   189
    
jaroslav@288
   190
}