boot/src/main/java/org/apidesign/html/boot/spi/Fn.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 10 Oct 2013 14:02:18 +0200
changeset 309 7025177bd67e
parent 289 5fe8b52d74f9
child 315 b3a78c2e5c33
permissions -rw-r--r--
FnUtils are bloated and contain references to Asm classes. Separate the code needed by generated JsCallbacks into own class
     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.Reader;
    24 import java.net.URL;
    25 import org.apidesign.html.boot.impl.FnContext;
    26 import org.apidesign.html.boot.impl.FnUtils;
    27 
    28 /** Represents single JavaScript function that can be invoked. 
    29  * Created via {@link Presenter#defineFn(java.lang.String, java.lang.String...)}.
    30  *
    31  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    32  */
    33 public abstract class Fn {
    34     private final Presenter presenter;
    35     
    36     /**
    37      * @deprecated Ineffective as of 0.6. 
    38      * Provide a presenter via {@link #Fn(org.apidesign.html.boot.spi.Fn.Presenter)}
    39      * constructor
    40      */
    41     @Deprecated
    42     protected Fn() {
    43         this(null);
    44     }
    45     
    46     /** Creates new function object and associates it with given presenter.
    47      * 
    48      * @param presenter the browser presenter associated with this function
    49      * @since 0.6 
    50      */
    51     protected Fn(Presenter presenter) {
    52         this.presenter = presenter;
    53     }
    54 
    55     /** True, if currently active presenter is the same as presenter this
    56      * function has been created for via {@link #Fn(org.apidesign.html.boot.spi.Fn.Presenter)}.
    57      * 
    58      * @return true, if proper presenter is used
    59      */
    60     public final boolean isValid() {
    61         return FnContext.currentPresenter() == presenter;
    62     }
    63     
    64     /** Invokes the defined function with specified <code>this</code> and
    65      * appropriate arguments.
    66      * 
    67      * @param thiz the meaning of <code>this</code> inside of the JavaScript
    68      *   function - can be <code>null</code>
    69      * @param args arguments for the function
    70      * @return return value from the function
    71      * @throws Exception if something goes wrong, as exception may be thrown
    72      */
    73     public abstract Object invoke(Object thiz, Object... args) throws Exception;
    74 
    75     /** The representation of a <em>presenter</em> - usually a browser window.
    76      */
    77     public interface Presenter {
    78         /** Creates new function with given parameter names and provided body.
    79          * 
    80          * @param code the body of the function. Can refer to variables named
    81          *   as <code>names</code>
    82          * @param names names of parameters of the function - these will be 
    83          *   available when the <code>code</code> body executes
    84          * 
    85          * @return function that can be later invoked
    86          */
    87         public Fn defineFn(String code, String... names);
    88         
    89         /** Opens the browser, loads provided page and when the
    90          * page is ready, it calls back to the provider runnable.
    91          * 
    92          * @param page the URL for the page to display
    93          * @param onPageLoad callback when the page is ready
    94          */
    95         public void displayPage(URL page, Runnable onPageLoad);
    96         
    97         /** Loads a script into the browser JavaScript interpreter and 
    98          * executes it.
    99          * @param code the script to execute
   100          * @throws Exception if something goes wrong, throw an exception
   101          */
   102         public void loadScript(Reader code) throws Exception;
   103     }
   104 }