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