boot-fx/src/main/java/org/netbeans/html/boot/fx/AbstractFXPresenter.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 446 6dce58c06f58
child 557 2911034fe047
permissions -rw-r--r--
Updating copyright headers to mention current year
jaroslav@288
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@288
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 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@551
    30
 * Software is Oracle. Portions Copyright 2013-2014 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.List;
jaroslav@431
    50
import java.util.concurrent.Executor;
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@431
    63
public abstract class AbstractFXPresenter 
jaroslav@446
    64
implements Fn.Presenter, Fn.ToJavaScript, Fn.FromJavaScript, Executor {
jaroslav@288
    65
    static final Logger LOG = Logger.getLogger(FXPresenter.class.getName());
jaroslav@288
    66
    protected static int cnt;
jaroslav@288
    67
    protected List<String> scripts;
jaroslav@288
    68
    protected Runnable onLoad;
jaroslav@288
    69
    protected WebEngine engine;
jaroslav@288
    70
jaroslav@288
    71
    @Override
jaroslav@288
    72
    public Fn defineFn(String code, String... names) {
jaroslav@429
    73
        return defineJSFn(code, names);
jaroslav@429
    74
    }
jaroslav@429
    75
    
jaroslav@429
    76
    final JSFn defineJSFn(String code, String... names) {
jaroslav@288
    77
        StringBuilder sb = new StringBuilder();
jaroslav@288
    78
        sb.append("(function() {");
jaroslav@288
    79
        sb.append("  return function(");
jaroslav@288
    80
        String sep = "";
jaroslav@288
    81
        for (String n : names) {
jaroslav@288
    82
            sb.append(sep).append(n);
jaroslav@288
    83
            sep = ",";
jaroslav@288
    84
        }
jaroslav@288
    85
        sb.append(") {\n");
jaroslav@288
    86
        sb.append(code);
jaroslav@288
    87
        sb.append("};");
jaroslav@288
    88
        sb.append("})()");
jaroslav@288
    89
        if (LOG.isLoggable(Level.FINE)) {
jaroslav@302
    90
            LOG.log(Level.FINE, 
jaroslav@302
    91
                "defining function #{0}:\n{1}\n", 
jaroslav@302
    92
                new Object[] { ++cnt, code }
jaroslav@302
    93
            );
jaroslav@288
    94
        }
jaroslav@288
    95
        JSObject x = (JSObject) engine.executeScript(sb.toString());
jaroslav@288
    96
        return new JSFn(this, x, cnt);
jaroslav@288
    97
    }
jaroslav@288
    98
jaroslav@288
    99
    @Override
jaroslav@288
   100
    public void loadScript(Reader code) throws Exception {
jaroslav@288
   101
        BufferedReader r = new BufferedReader(code);
jaroslav@288
   102
        StringBuilder sb = new StringBuilder();
jaroslav@288
   103
        for (;;) {
jaroslav@288
   104
            String l = r.readLine();
jaroslav@288
   105
            if (l == null) {
jaroslav@288
   106
                break;
jaroslav@288
   107
            }
jaroslav@288
   108
            sb.append(l).append('\n');
jaroslav@288
   109
        }
jaroslav@288
   110
        final String script = sb.toString();
jaroslav@288
   111
        if (scripts != null) {
jaroslav@288
   112
            scripts.add(script);
jaroslav@288
   113
        }
jaroslav@288
   114
        engine.executeScript(script);
jaroslav@288
   115
    }
jaroslav@288
   116
jaroslav@288
   117
    protected final void onPageLoad() {
jaroslav@288
   118
        if (scripts != null) {
jaroslav@288
   119
            for (String s : scripts) {
jaroslav@288
   120
                engine.executeScript(s);
jaroslav@288
   121
            }
jaroslav@288
   122
        }
jaroslav@288
   123
        onLoad.run();
jaroslav@288
   124
    }
jaroslav@288
   125
jaroslav@288
   126
    @Override
jaroslav@288
   127
    public void displayPage(final URL resource, final Runnable onLoad) {
jaroslav@288
   128
        this.onLoad = onLoad;
jaroslav@288
   129
        final WebView view = findView(resource);
jaroslav@288
   130
        this.engine = view.getEngine();
jaroslav@288
   131
        try {
jaroslav@288
   132
            if (FXInspect.initialize(engine)) {
jaroslav@288
   133
                scripts = new ArrayList<String>();
jaroslav@288
   134
            }
jaroslav@288
   135
        } catch (Throwable ex) {
jaroslav@288
   136
            ex.printStackTrace();
jaroslav@288
   137
        }
jaroslav@288
   138
jaroslav@288
   139
        class Run implements Runnable {
jaroslav@288
   140
jaroslav@288
   141
            @Override
jaroslav@288
   142
            public void run() {
jaroslav@288
   143
                if (scripts != null) {
jaroslav@288
   144
                    view.setContextMenuEnabled(true);
jaroslav@288
   145
                }
jaroslav@288
   146
                engine.load(resource.toExternalForm());
jaroslav@288
   147
            }
jaroslav@288
   148
        }
jaroslav@288
   149
        Run run = new Run();
jaroslav@288
   150
        if (Platform.isFxApplicationThread()) {
jaroslav@288
   151
            run.run();
jaroslav@288
   152
        } else {
jaroslav@288
   153
            Platform.runLater(run);
jaroslav@288
   154
        }
jaroslav@288
   155
        waitFinished();
jaroslav@288
   156
    }
jaroslav@288
   157
jaroslav@288
   158
    protected abstract void waitFinished();
jaroslav@288
   159
jaroslav@288
   160
    protected abstract WebView findView(final URL resource);
jaroslav@429
   161
    
jaroslav@429
   162
    final JSObject convertArrays(Object[] arr) {
jaroslav@429
   163
        for (int i = 0; i < arr.length; i++) {
jaroslav@429
   164
            if (arr[i] instanceof Object[]) {
jaroslav@429
   165
                arr[i] = convertArrays((Object[]) arr[i]);
jaroslav@429
   166
            }
jaroslav@429
   167
        }
jaroslav@429
   168
        final JSObject wrapArr = (JSObject)wrapArrFn().call("array", arr); // NOI18N
jaroslav@429
   169
        return wrapArr;
jaroslav@429
   170
    }
jaroslav@429
   171
jaroslav@429
   172
    private JSObject wrapArrImpl;
jaroslav@429
   173
    private final JSObject wrapArrFn() {
jaroslav@429
   174
        if (wrapArrImpl == null) {
jaroslav@429
   175
            try {
jaroslav@429
   176
                wrapArrImpl = (JSObject)defineJSFn("  var k = {};"
jaroslav@429
   177
                    + "  k.array= function() {"
jaroslav@429
   178
                    + "    return Array.prototype.slice.call(arguments);"
jaroslav@429
   179
                    + "  };"
jaroslav@429
   180
                    + "  return k;"
jaroslav@429
   181
                ).invokeImpl(null, false);
jaroslav@429
   182
            } catch (Exception ex) {
jaroslav@429
   183
                throw new IllegalStateException(ex);
jaroslav@429
   184
            }
jaroslav@429
   185
        }
jaroslav@429
   186
        return wrapArrImpl;
jaroslav@429
   187
    }
jaroslav@429
   188
jaroslav@429
   189
    final Object checkArray(Object val) {
jaroslav@429
   190
        int length = ((Number) arraySizeFn().call("array", val, null)).intValue();
jaroslav@429
   191
        if (length == -1) {
jaroslav@429
   192
            return val;
jaroslav@429
   193
        }
jaroslav@429
   194
        Object[] arr = new Object[length];
jaroslav@429
   195
        arraySizeFn().call("array", val, arr);
jaroslav@429
   196
        return arr;
jaroslav@429
   197
    }
jaroslav@429
   198
    private JSObject arraySize;
jaroslav@429
   199
    private final JSObject arraySizeFn() {
jaroslav@429
   200
        if (arraySize == null) {
jaroslav@429
   201
            try {
jaroslav@429
   202
                arraySize = (JSObject)defineJSFn("  var k = {};"
jaroslav@429
   203
                    + "  k.array = function(arr, to) {"
jaroslav@429
   204
                    + "    if (to === null) {"
jaroslav@429
   205
                    + "      if (Object.prototype.toString.call(arr) === '[object Array]') return arr.length;"
jaroslav@429
   206
                    + "      else return -1;"
jaroslav@429
   207
                    + "    } else {"
jaroslav@429
   208
                    + "      var l = arr.length;"
jaroslav@429
   209
                    + "      for (var i = 0; i < l; i++) to[i] = arr[i];"
jaroslav@429
   210
                    + "      return l;"
jaroslav@429
   211
                    + "    }"
jaroslav@429
   212
                    + "  };"
jaroslav@429
   213
                    + "  return k;"
jaroslav@429
   214
                ).invokeImpl(null, false);
jaroslav@429
   215
            } catch (Exception ex) {
jaroslav@429
   216
                throw new IllegalStateException(ex);
jaroslav@429
   217
            }
jaroslav@429
   218
        }
jaroslav@429
   219
        return arraySize;
jaroslav@429
   220
    }
jaroslav@288
   221
jaroslav@430
   222
    @Override
jaroslav@446
   223
    public Object toJava(Object jsArray) {
jaroslav@446
   224
        return checkArray(jsArray);
jaroslav@446
   225
    }
jaroslav@446
   226
    
jaroslav@446
   227
    @Override
jaroslav@430
   228
    public Object toJavaScript(Object toReturn) {
jaroslav@430
   229
        if (toReturn instanceof Object[]) {
jaroslav@430
   230
            return convertArrays((Object[])toReturn);
jaroslav@430
   231
        } else {
jaroslav@430
   232
            return toReturn;
jaroslav@430
   233
        }
jaroslav@430
   234
    }
jaroslav@431
   235
    
jaroslav@431
   236
    @Override public void execute(Runnable r) {
jaroslav@431
   237
        if (Platform.isFxApplicationThread()) {
jaroslav@431
   238
            r.run();
jaroslav@431
   239
        } else {
jaroslav@431
   240
            Platform.runLater(r);
jaroslav@431
   241
        }
jaroslav@431
   242
    }
jaroslav@430
   243
jaroslav@288
   244
    private static final class JSFn extends Fn {
jaroslav@288
   245
jaroslav@288
   246
        private final JSObject fn;
jaroslav@288
   247
        private static int call;
jaroslav@288
   248
        private final int id;
jaroslav@288
   249
jaroslav@288
   250
        public JSFn(AbstractFXPresenter p, JSObject fn, int id) {
jaroslav@288
   251
            super(p);
jaroslav@288
   252
            this.fn = fn;
jaroslav@288
   253
            this.id = id;
jaroslav@288
   254
        }
jaroslav@288
   255
jaroslav@288
   256
        @Override
jaroslav@289
   257
        public Object invoke(Object thiz, Object... args) throws Exception {
jaroslav@429
   258
            return invokeImpl(thiz, true, args);
jaroslav@429
   259
        }
jaroslav@429
   260
        
jaroslav@429
   261
        final Object invokeImpl(Object thiz, boolean arrayChecks, Object... args) throws Exception {
jaroslav@288
   262
            try {
jaroslav@288
   263
                if (LOG.isLoggable(Level.FINE)) {
jaroslav@288
   264
                    LOG.log(Level.FINE, "calling {0} function #{1}", new Object[]{++call, id});
jaroslav@288
   265
                }
jaroslav@288
   266
                List<Object> all = new ArrayList<Object>(args.length + 1);
jaroslav@288
   267
                all.add(thiz == null ? fn : thiz);
jaroslav@429
   268
                for (int i = 0; i < args.length; i++) {
jaroslav@429
   269
                    if (arrayChecks && args[i] instanceof Object[]) {
jaroslav@429
   270
                        Object[] arr = (Object[]) args[i];
jaroslav@429
   271
                        Object conv = ((AbstractFXPresenter)presenter()).convertArrays(arr);
jaroslav@429
   272
                        args[i] = conv;
jaroslav@429
   273
                    }
jaroslav@429
   274
                    all.add(args[i]);
jaroslav@429
   275
                }
jaroslav@288
   276
                Object ret = fn.call("call", all.toArray()); // NOI18N
jaroslav@429
   277
                if (ret == fn) {
jaroslav@429
   278
                    return null;
jaroslav@429
   279
                }
jaroslav@429
   280
                if (!arrayChecks) {
jaroslav@429
   281
                    return ret;
jaroslav@429
   282
                }
jaroslav@429
   283
                return ((AbstractFXPresenter)presenter()).checkArray(ret);
jaroslav@288
   284
            } catch (Error t) {
jaroslav@288
   285
                t.printStackTrace();
jaroslav@288
   286
                throw t;
jaroslav@288
   287
            } catch (Exception t) {
jaroslav@288
   288
                t.printStackTrace();
jaroslav@288
   289
                throw t;
jaroslav@288
   290
            }
jaroslav@288
   291
        }
jaroslav@288
   292
    }
jaroslav@288
   293
    
jaroslav@288
   294
}