boot/src/main/java/org/apidesign/html/boot/spi/Fn.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 05 Nov 2013 23:06:32 +0100
changeset 322 4a93f2679691
parent 315 b3a78c2e5c33
child 323 86aabecda7a3
permissions -rw-r--r--
Exposing Fn.activate and Fn.activePresenter so they are available from exported packages
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@322
    65
    /** The currently active presenter.
jaroslav@322
    66
     * 
jaroslav@322
    67
     * @return the currently active presenter or <code>null</code>
jaroslav@322
    68
     * @since 0.7
jaroslav@322
    69
     */
jaroslav@322
    70
    public static Presenter activePresenter() {
jaroslav@322
    71
        return FnContext.currentPresenter();
jaroslav@322
    72
    }
jaroslav@322
    73
    
jaroslav@322
    74
    /** Activates given presenter. Used by the code generated by 
jaroslav@322
    75
     * {@link JavaScriptBody} annotation: 
jaroslav@322
    76
     * <pre>
jaroslav@322
    77
     * try ({@link Closeable} c = Fn.activate(presenter)) {
jaroslav@322
    78
     *   doCallsInPresenterContext();
jaroslav@322
    79
     * }
jaroslav@322
    80
     * </pre>
jaroslav@322
    81
     * 
jaroslav@322
    82
     * @param p the presenter that should be active until closable is closed
jaroslav@322
    83
     * @return the closable to close
jaroslav@322
    84
     * @since 0.7
jaroslav@322
    85
     */
jaroslav@322
    86
    public static Closeable activate(Presenter p) {
jaroslav@322
    87
        return FnContext.activate(p);
jaroslav@322
    88
    }
jaroslav@322
    89
    
jaroslav@289
    90
    /** Invokes the defined function with specified <code>this</code> and
jaroslav@289
    91
     * appropriate arguments.
jaroslav@289
    92
     * 
jaroslav@289
    93
     * @param thiz the meaning of <code>this</code> inside of the JavaScript
jaroslav@289
    94
     *   function - can be <code>null</code>
jaroslav@289
    95
     * @param args arguments for the function
jaroslav@289
    96
     * @return return value from the function
jaroslav@289
    97
     * @throws Exception if something goes wrong, as exception may be thrown
jaroslav@289
    98
     */
jaroslav@289
    99
    public abstract Object invoke(Object thiz, Object... args) throws Exception;
jaroslav@289
   100
jaroslav@289
   101
    /** The representation of a <em>presenter</em> - usually a browser window.
jaroslav@315
   102
     * Should be provided by a library included in the application and registered
jaroslav@315
   103
     * in <code>META-INF/services</code>, for example with
jaroslav@315
   104
     * <code>@ServiceProvider(service = Fn.Presenter.class)</code> annotation.
jaroslav@289
   105
     */
jaroslav@127
   106
    public interface Presenter {
jaroslav@289
   107
        /** Creates new function with given parameter names and provided body.
jaroslav@289
   108
         * 
jaroslav@289
   109
         * @param code the body of the function. Can refer to variables named
jaroslav@289
   110
         *   as <code>names</code>
jaroslav@289
   111
         * @param names names of parameters of the function - these will be 
jaroslav@289
   112
         *   available when the <code>code</code> body executes
jaroslav@289
   113
         * 
jaroslav@289
   114
         * @return function that can be later invoked
jaroslav@289
   115
         */
jaroslav@127
   116
        public Fn defineFn(String code, String... names);
jaroslav@289
   117
        
jaroslav@289
   118
        /** Opens the browser, loads provided page and when the
jaroslav@289
   119
         * page is ready, it calls back to the provider runnable.
jaroslav@289
   120
         * 
jaroslav@289
   121
         * @param page the URL for the page to display
jaroslav@289
   122
         * @param onPageLoad callback when the page is ready
jaroslav@289
   123
         */
jaroslav@128
   124
        public void displayPage(URL page, Runnable onPageLoad);
jaroslav@289
   125
        
jaroslav@289
   126
        /** Loads a script into the browser JavaScript interpreter and 
jaroslav@289
   127
         * executes it.
jaroslav@289
   128
         * @param code the script to execute
jaroslav@289
   129
         * @throws Exception if something goes wrong, throw an exception
jaroslav@289
   130
         */
jaroslav@163
   131
        public void loadScript(Reader code) throws Exception;
jaroslav@123
   132
    }
jaroslav@123
   133
}