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