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
     1 /**
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
     5  *
     6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
     7  * Other names may be trademarks of their respective owners.
     8  *
     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]"
    26  *
    27  * Contributor(s):
    28  *
    29  * The Original Software is NetBeans. The Initial Developer of the Original
    30  * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
    31  *
    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.
    42  */
    43 package org.netbeans.html.boot.fx;
    44 
    45 import java.io.BufferedReader;
    46 import java.io.Reader;
    47 import java.net.URL;
    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;
    58 
    59 /**
    60  *
    61  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    62  */
    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;
    69 
    70     @Override
    71     public Fn defineFn(String code, String... names) {
    72         StringBuilder sb = new StringBuilder();
    73         sb.append("(function() {");
    74         sb.append("  return function(");
    75         String sep = "";
    76         for (String n : names) {
    77             sb.append(sep).append(n);
    78             sep = ",";
    79         }
    80         sb.append(") {\n");
    81         sb.append(code);
    82         sb.append("};");
    83         sb.append("})()");
    84         if (LOG.isLoggable(Level.FINE)) {
    85             LOG.log(Level.FINE, 
    86                 "defining function #{0}:\n{1}\n", 
    87                 new Object[] { ++cnt, code }
    88             );
    89         }
    90         JSObject x = (JSObject) engine.executeScript(sb.toString());
    91         return new JSFn(this, x, cnt);
    92     }
    93 
    94     @Override
    95     public void loadScript(Reader code) throws Exception {
    96         BufferedReader r = new BufferedReader(code);
    97         StringBuilder sb = new StringBuilder();
    98         for (;;) {
    99             String l = r.readLine();
   100             if (l == null) {
   101                 break;
   102             }
   103             sb.append(l).append('\n');
   104         }
   105         final String script = sb.toString();
   106         if (scripts != null) {
   107             scripts.add(script);
   108         }
   109         engine.executeScript(script);
   110     }
   111 
   112     protected final void onPageLoad() {
   113         if (scripts != null) {
   114             for (String s : scripts) {
   115                 engine.executeScript(s);
   116             }
   117         }
   118         onLoad.run();
   119     }
   120 
   121     @Override
   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();
   126         try {
   127             if (FXInspect.initialize(engine)) {
   128                 scripts = new ArrayList<String>();
   129             }
   130         } catch (Throwable ex) {
   131             ex.printStackTrace();
   132         }
   133 
   134         class Run implements Runnable {
   135 
   136             @Override
   137             public void run() {
   138                 if (scripts != null) {
   139                     view.setContextMenuEnabled(true);
   140                 }
   141                 engine.load(resource.toExternalForm());
   142             }
   143         }
   144         Run run = new Run();
   145         if (Platform.isFxApplicationThread()) {
   146             run.run();
   147         } else {
   148             Platform.runLater(run);
   149         }
   150         waitFinished();
   151     }
   152 
   153     protected abstract void waitFinished();
   154 
   155     protected abstract WebView findView(final URL resource);
   156 
   157     private static final class JSFn extends Fn {
   158 
   159         private final JSObject fn;
   160         private static int call;
   161         private final int id;
   162 
   163         public JSFn(AbstractFXPresenter p, JSObject fn, int id) {
   164             super(p);
   165             this.fn = fn;
   166             this.id = id;
   167         }
   168 
   169         @Override
   170         public Object invoke(Object thiz, Object... args) throws Exception {
   171             try {
   172                 if (LOG.isLoggable(Level.FINE)) {
   173                     LOG.log(Level.FINE, "calling {0} function #{1}", new Object[]{++call, id});
   174                 }
   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;
   180             } catch (Error t) {
   181                 t.printStackTrace();
   182                 throw t;
   183             } catch (Exception t) {
   184                 t.printStackTrace();
   185                 throw t;
   186             }
   187         }
   188     }
   189     
   190 }