boot-script/src/main/java/net/java/html/boot/script/Scripts.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 05 Jun 2014 21:01:56 +0200
branchenvjs
changeset 700 055a0f3766f5
parent 699 82773e03ff40
child 716 0654466b2273
permissions -rw-r--r--
A bit of documentation
jtulach@699
     1
/**
jtulach@699
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@699
     3
 *
jtulach@699
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jtulach@699
     5
 *
jtulach@699
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jtulach@699
     7
 * Other names may be trademarks of their respective owners.
jtulach@699
     8
 *
jtulach@699
     9
 * The contents of this file are subject to the terms of either the GNU
jtulach@699
    10
 * General Public License Version 2 only ("GPL") or the Common
jtulach@699
    11
 * Development and Distribution License("CDDL") (collectively, the
jtulach@699
    12
 * "License"). You may not use this file except in compliance with the
jtulach@699
    13
 * License. You can obtain a copy of the License at
jtulach@699
    14
 * http://www.netbeans.org/cddl-gplv2.html
jtulach@699
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jtulach@699
    16
 * specific language governing permissions and limitations under the
jtulach@699
    17
 * License.  When distributing the software, include this License Header
jtulach@699
    18
 * Notice in each file and include the License file at
jtulach@699
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jtulach@699
    20
 * particular file as subject to the "Classpath" exception as provided
jtulach@699
    21
 * by Oracle in the GPL Version 2 section of the License file that
jtulach@699
    22
 * accompanied this code. If applicable, add the following below the
jtulach@699
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jtulach@699
    24
 * your own identifying information:
jtulach@699
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jtulach@699
    26
 *
jtulach@699
    27
 * Contributor(s):
jtulach@699
    28
 *
jtulach@699
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jtulach@699
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jtulach@699
    31
 *
jtulach@699
    32
 * If you wish your version of this file to be governed by only the CDDL
jtulach@699
    33
 * or only the GPL Version 2, indicate your decision by adding
jtulach@699
    34
 * "[Contributor] elects to include this software in this distribution
jtulach@699
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jtulach@699
    36
 * single choice of license, a recipient has the option to distribute
jtulach@699
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jtulach@699
    38
 * to extend the choice of license to its licensees as provided above.
jtulach@699
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jtulach@699
    40
 * Version 2 license, then the option applies only if the new code is
jtulach@699
    41
 * made subject to such option by the copyright holder.
jtulach@699
    42
 */
jtulach@699
    43
package net.java.html.boot.script;
jtulach@699
    44
jtulach@699
    45
import java.util.concurrent.Executor;
jtulach@699
    46
import javax.script.ScriptEngine;
jtulach@700
    47
import net.java.html.boot.BrowserBuilder;
jtulach@700
    48
import net.java.html.js.JavaScriptBody;
jtulach@699
    49
import org.apidesign.html.boot.spi.Fn.Presenter;
jtulach@699
    50
jtulach@699
    51
/** Implementations of {@link Presenter}s that delegate
jtulach@700
    52
 * to Java {@link ScriptEngine scripting} API. Initialize your presenter
jtulach@700
    53
 * like this:
jtulach@700
    54
 * 
jtulach@700
    55
 * <pre>
jtulach@700
    56
 * 
jtulach@700
    57
 * {@link Runnable} <em>run</em> = ...; // your own init code
jtulach@700
    58
 * {@link Presenter Fn.Presenter} <em>p</em> = Scripts.{@link Scripts#createPresenter()};
jtulach@700
    59
 * BrowserBuilder.{@link BrowserBuilder#newBrowser(java.lang.Object...) newBrowser(<em>p</em>)}.
jtulach@700
    60
 *      {@link BrowserBuilder#loadFinished(java.lang.Runnable) loadFinished(run)}.
jtulach@700
    61
 *      {@link BrowserBuilder#showAndWait()};
jtulach@700
    62
 * </pre>
jtulach@700
    63
 * 
jtulach@700
    64
 * and your runnable can make extensive use of {@link JavaScriptBody} directly or
jtulach@700
    65
 * indirectly via APIs using {@link JavaScriptBody such annotation} themselves.
jtulach@699
    66
 * 
jtulach@699
    67
 * @author Jaroslav Tulach
jtulach@699
    68
 */
jtulach@699
    69
public final class Scripts {
jtulach@699
    70
    private Scripts() {
jtulach@699
    71
    }
jtulach@699
    72
    
jtulach@699
    73
    /** Simple implementation of {@link Presenter} that delegates
jtulach@699
    74
     * to Java {@link ScriptEngine scripting} API. The presenter runs headless
jtulach@699
    75
     * without appropriate simulation of browser APIs. Its primary usefulness
jtulach@699
    76
     * is inside testing environments. The presenter implements {@link Executor}
jtulach@699
    77
     * interface, but invokes all runnables passed to {@link Executor#execute(java.lang.Runnable)}
jtulach@699
    78
     * immediately.
jtulach@699
    79
     * 
jtulach@699
    80
     * @return new instance of a presenter that is using its own
jtulach@699
    81
     *   {@link ScriptEngine} for <code>text/javascript</code> mimetype
jtulach@699
    82
     */
jtulach@699
    83
    public static Presenter createPresenter() {
jtulach@699
    84
        return new ScriptPresenter(null);
jtulach@699
    85
    }
jtulach@699
    86
jtulach@700
    87
    /** Implementation of {@link Presenter} that delegates
jtulach@700
    88
     * to Java {@link ScriptEngine scripting} API and can control execution
jtulach@700
    89
     * thread. The presenter runs headless
jtulach@699
    90
     * without appropriate simulation of browser APIs. Its primary usefulness
jtulach@699
    91
     * is inside testing environments. The presenter implements {@link Executor}
jtulach@699
    92
     * interface, and passes all runnables from {@link Executor#execute(java.lang.Runnable)}
jtulach@699
    93
     * to here in provided <code>exc</code> instance.
jtulach@699
    94
     * 
jtulach@699
    95
     * @param exc the executor to re-schedule all asynchronous requests to
jtulach@699
    96
     * @return new instance of a presenter that is using its own
jtulach@699
    97
     *   {@link ScriptEngine} for <code>text/javascript</code> mimetype
jtulach@699
    98
     */
jtulach@699
    99
    public static Presenter createPresenter(Executor exc) {
jtulach@699
   100
        return new ScriptPresenter(exc);
jtulach@699
   101
    }
jtulach@699
   102
}