01 
02 /*
03  *  JScripter Emulation 1.0 - To Script Java
04  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
05  *  
06  *  This program is free software: you can redistribute it and/or modify
07  *  it under the terms of the GNU Affero General Public License as published by
08  *  the Free Software Foundation, either version 3 of the License, or
09  *  (at your option) any later version.
10  *  
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU Affero General Public License for more details.
15  *  
16  *  You should have received a copy of the GNU Affero General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 package org.jscripter.emu;
21 
22 import js.Function;
23 import js.Js;
24 import js.JsApplet;
25 import js.Ref;
26 import jsx.event.Handle;
27 
28 /**
29  * <p>An <tt>internal</tt> abstract class emulating {@link JsApplet} in JavaScript.</p>
30  * <p>This class is only used internally by JS re-compiler implementations.</p>
31  * 
32  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
33  * 
34  * @javascript Re-compilers must redirect resolution of {@link JsApplet} to this class.
35  */
36 public abstract class Scriptlet
37 {
38     /**
39      * <p>Prevents {@link JsApplet#init()} from resolution.</p>
40      * @since 1.0
41      * @javascript Re-compilers must report error on resolving this method.
42      */
43     public abstract void init();
44     /**
45      * <p>Prevents {@link JsApplet#destroy()} from resolution.</p>
46      * @since 1.0
47      * @javascript Re-compilers must report error on resolving this method.
48      */
49     public abstract void destroy();
50     /**
51      * <p>Prevents {@link JsApplet#start()} from resolution.</p>
52      * @since 1.0
53      * @javascript Re-compilers must report error on resolving this method.
54      */
55     public abstract void start();
56     /**
57      * <p>Prevents {@link JsApplet#stop()} from resolution.</p>
58      * @since 1.0
59      * @javascript Re-compilers must report error on resolving this method.
60      */
61     public abstract void stop();
62     /**
63      * <p>Prevents {@link JsApplet#onReady()} from resolution.</p>
64      * @since 1.0
65      * @javascript Re-compilers must report error on resolving this method.
66      */
67     public abstract void onReady();
68 
69     /**
70      * <p>Application entry point.</p>
71      * @param app A {@link JsApplet} object.
72      * @since 1.0
73      * @javascript Re-compilers must get browsers run the codes of this method immediately after 
74      * loading the document.
75      */
76     public static final void onLoad(final JsApplet app) {
77         final Ref<Handle> h = new Ref<Handle>(null);
78         h.value = new Handle(
79                 Js.win(),
80                 "load",
81                 new Function<Void>() {
82                     @Override
83                     protected Void function(Object jsthis, Call<Void> callee) {
84                         app.onReady();
85                         h.value.detach();
86                         return null;
87                     }
88                 }.var(),
89                 false
90         );
91         h.value.attach();
92     }
93 }