boot/src/main/java/org/apidesign/html/boot/spi/Fn.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 06 Nov 2013 15:15:54 +0100
changeset 323 86aabecda7a3
parent 322 4a93f2679691
child 335 c74fed864c9c
child 349 53634fd10e30
permissions -rw-r--r--
Introducing Agent-Class to allow java.lang.instrument-like transforms of the @JavaScriptBody annotation
     1 /**
     2  * HTML via Java(tm) Language Bindings
     3  * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details. apidesign.org
    13  * designates this particular file as subject to the
    14  * "Classpath" exception as provided by apidesign.org
    15  * in the License file that accompanied this code.
    16  *
    17  * You should have received a copy of the GNU General Public License
    18  * along with this program. Look for COPYING file in the top folder.
    19  * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    20  */
    21 package org.apidesign.html.boot.spi;
    22 
    23 import java.io.Closeable;
    24 import java.io.Reader;
    25 import java.net.URL;
    26 import net.java.html.js.JavaScriptBody;
    27 import org.apidesign.html.boot.impl.FnContext;
    28 
    29 /** Represents single JavaScript function that can be invoked. 
    30  * Created via {@link Presenter#defineFn(java.lang.String, java.lang.String...)}.
    31  *
    32  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    33  */
    34 public abstract class Fn {
    35     private final Presenter presenter;
    36     
    37     /**
    38      * @deprecated Ineffective as of 0.6. 
    39      * Provide a presenter via {@link #Fn(org.apidesign.html.boot.spi.Fn.Presenter)}
    40      * constructor
    41      */
    42     @Deprecated
    43     protected Fn() {
    44         this(null);
    45     }
    46     
    47     /** Creates new function object and associates it with given presenter.
    48      * 
    49      * @param presenter the browser presenter associated with this function
    50      * @since 0.6 
    51      */
    52     protected Fn(Presenter presenter) {
    53         this.presenter = presenter;
    54     }
    55 
    56     /** True, if currently active presenter is the same as presenter this
    57      * function has been created for via {@link #Fn(org.apidesign.html.boot.spi.Fn.Presenter)}.
    58      * 
    59      * @return true, if proper presenter is used
    60      */
    61     public final boolean isValid() {
    62         return FnContext.currentPresenter() == presenter;
    63     }
    64     
    65     /** Helper method to check if the provided instance is valid function.
    66      * Checks if the parameter is non-null and if so, does {@link #isValid()}
    67      * check.
    68      * 
    69      * @param fnOrNull function or <code>null</code>
    70      * @return true if the parameter is non-null and valid
    71      * @since 0.7
    72      */
    73     public static boolean isValid(Fn fnOrNull) {
    74         return fnOrNull != null && fnOrNull.isValid();
    75     }
    76 
    77     /** Helper method to find current presenter and ask it to define new
    78      * function by calling {@link Presenter#defineFn(java.lang.String, java.lang.String...)}.
    79      * 
    80      * @param caller the class who wishes to define the function
    81      * @param code the body of the function (can reference <code>this</code> and <code>names</code> variables)
    82      * @param names names of individual parameters
    83      * @return the function object that can be {@link Fn#invoke(java.lang.Object, java.lang.Object...) invoked}
    84      * @since 0.7
    85      */
    86     public static Fn define(Class<?> caller, String code, String... names) {
    87         return FnContext.currentPresenter().defineFn(code, names);
    88     }
    89     
    90     /** The currently active presenter.
    91      * 
    92      * @return the currently active presenter or <code>null</code>
    93      * @since 0.7
    94      */
    95     public static Presenter activePresenter() {
    96         return FnContext.currentPresenter();
    97     }
    98     
    99     /** Activates given presenter. Used by the code generated by 
   100      * {@link JavaScriptBody} annotation: 
   101      * <pre>
   102      * try ({@link Closeable} c = Fn.activate(presenter)) {
   103      *   doCallsInPresenterContext();
   104      * }
   105      * </pre>
   106      * 
   107      * @param p the presenter that should be active until closable is closed
   108      * @return the closable to close
   109      * @since 0.7
   110      */
   111     public static Closeable activate(Presenter p) {
   112         return FnContext.activate(p);
   113     }
   114     
   115     /** Invokes the defined function with specified <code>this</code> and
   116      * appropriate arguments.
   117      * 
   118      * @param thiz the meaning of <code>this</code> inside of the JavaScript
   119      *   function - can be <code>null</code>
   120      * @param args arguments for the function
   121      * @return return value from the function
   122      * @throws Exception if something goes wrong, as exception may be thrown
   123      */
   124     public abstract Object invoke(Object thiz, Object... args) throws Exception;
   125 
   126     /** The representation of a <em>presenter</em> - usually a browser window.
   127      * Should be provided by a library included in the application and registered
   128      * in <code>META-INF/services</code>, for example with
   129      * <code>@ServiceProvider(service = Fn.Presenter.class)</code> annotation.
   130      */
   131     public interface Presenter {
   132         /** Creates new function with given parameter names and provided body.
   133          * 
   134          * @param code the body of the function. Can refer to variables named
   135          *   as <code>names</code>
   136          * @param names names of parameters of the function - these will be 
   137          *   available when the <code>code</code> body executes
   138          * 
   139          * @return function that can be later invoked
   140          */
   141         public Fn defineFn(String code, String... names);
   142         
   143         /** Opens the browser, loads provided page and when the
   144          * page is ready, it calls back to the provider runnable.
   145          * 
   146          * @param page the URL for the page to display
   147          * @param onPageLoad callback when the page is ready
   148          */
   149         public void displayPage(URL page, Runnable onPageLoad);
   150         
   151         /** Loads a script into the browser JavaScript interpreter and 
   152          * executes it.
   153          * @param code the script to execute
   154          * @throws Exception if something goes wrong, throw an exception
   155          */
   156         public void loadScript(Reader code) throws Exception;
   157     }
   158 }