javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Timer.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 26 Jan 2013 08:47:05 +0100
changeset 592 5e13b1ac2886
child 1787 ea12a3bb4b33
permissions -rw-r--r--
In order to support fields of the same name in subclasses we are now prefixing them with name of the class that defines them. To provide convenient way to access them from generated bytecode and also directly from JavaScript, there is a getter/setter function for each field. It starts with _ followed by the field name. If called with a parameter, it sets the field, with a parameter it just returns it.
jaroslav@428
     1
/**
jaroslav@428
     2
 * Back 2 Browser Bytecode Translator
jaroslav@428
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@428
     4
 *
jaroslav@428
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@428
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@428
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@428
     8
 *
jaroslav@428
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@428
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@428
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@428
    12
 * GNU General Public License for more details.
jaroslav@428
    13
 *
jaroslav@428
    14
 * You should have received a copy of the GNU General Public License
jaroslav@428
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@428
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@428
    17
 */
jaroslav@428
    18
package org.apidesign.bck2brwsr.htmlpage.api;
jaroslav@428
    19
jaroslav@428
    20
import java.io.Closeable;
jaroslav@428
    21
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@428
    22
jaroslav@428
    23
/**
jaroslav@428
    24
 *
jaroslav@428
    25
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@428
    26
 */
jaroslav@428
    27
public class Timer implements Closeable {
jaroslav@428
    28
    private final Object t;
jaroslav@428
    29
    
jaroslav@428
    30
    private Timer(Object t) {
jaroslav@428
    31
        this.t = t;
jaroslav@428
    32
    }
jaroslav@428
    33
    
jaroslav@428
    34
    /** Creates a timer that invokes provided runnable on a fixed interval
jaroslav@428
    35
     * 
jaroslav@428
    36
     * @param r the runnable to execute
jaroslav@428
    37
     * @param time milliseconds to invoke the timer periodically
jaroslav@428
    38
     */
jaroslav@428
    39
    public static Timer create(Runnable r, int time) {
jaroslav@428
    40
        return new Timer(interval(r, time));
jaroslav@428
    41
    }
jaroslav@428
    42
    
jaroslav@428
    43
    @JavaScriptBody(args = { "r", "time" }, body = 
jaroslav@428
    44
        "return window.setInterval(function() { r.run__V(); }, time);"
jaroslav@428
    45
    )
jaroslav@428
    46
    private static native Object interval(Runnable r, int time);
jaroslav@428
    47
jaroslav@428
    48
    @JavaScriptBody(args = { "self" }, body = 
jaroslav@428
    49
        "window.clearInterval(self);"
jaroslav@428
    50
    )
jaroslav@428
    51
    private static native void close(Object self);
jaroslav@428
    52
    
jaroslav@428
    53
    /** Cancels this timer.
jaroslav@428
    54
     */
jaroslav@428
    55
    @Override
jaroslav@428
    56
    public void close() {
jaroslav@428
    57
        close(t);
jaroslav@428
    58
    }
jaroslav@428
    59
}