JAL-1807 update
[jalviewjs.git] / src / swingjs / JSThread.java
1 package swingjs;
2
3 import java.awt.Toolkit;
4 import java.awt.event.InvocationEvent;
5 import swingjs.api.JSFunction;
6
7 /**
8  * A class that takes care of simple threading. There are three states: INIT, LOOP, and DONE.
9  * These states are passed into run1
10  * 
11  * 
12  * @author Bob Hanson
13  * 
14  */
15 public abstract class JSThread extends Thread implements JSFunction {
16
17         public static final int INIT = 0;
18         public static final int LOOP = 1;
19         public static final int DONE = 2;
20
21         protected boolean isJS;
22         
23         public JSThread(ThreadGroup group, String name) {
24                 super(group, name);
25                 /**
26                  * @j2sNative
27                  * 
28                  * this.isJS = true;
29                  */
30                 {}
31         }
32
33         public void run() {
34                 run1(INIT);
35         }
36
37         @Override
38         public synchronized void start() {
39
40                 /**
41                  * @j2sNative
42                  * 
43                  *            swingjs.JSToolkit.setTimeout(this, 1, 0);
44                  * 
45                  */
46                 {
47                         super.start();
48                 }
49
50         }
51
52         /**
53          * a generic method that loops until done, or in JavaScript, will reenter and
54          * continue at the appropriate spot. Example given here
55          * 
56          * @param state
57          */
58         protected abstract void run1(int state);
59
60         
61         // protected void run1(int state) {
62         // try {
63         // while (true)
64         // switch (state) {
65         // case INIT:
66         // // once-through stuff here
67         // state = LOOP;
68         // break;
69         // case LOOP:
70         // if (isInterrupted()) {
71         // state = DONE;
72         // } else {
73         // // put the loop code here
74         // };
75         // dispatchAndReturn(state);
76         // if (isJS)
77         // return;
78         // }
79         // break;
80         // // add more cases as needed
81         // case DONE:
82         // // finish up here
83         // if (isInterrupted())
84         // return;
85         // // or here
86         // break;
87         // }
88         // } finally {
89         // // stuff here to be executed after each loop in JS or at the end in Java
90         // }
91         // }
92
93         /**
94          * 
95          * @param r
96          * @param state
97          * @return true if we should interrupt (i.e. JavaScript)
98          * @throws InterruptedException
99          */
100         protected boolean sleepAndReturn(final int delay, final int state)
101                         throws InterruptedException {
102                 if (!isJS) {
103                         sleep(delay);
104                         return false;
105                 }
106
107                 // in JavaScript, we need to do this through the system event queue,
108                 // which in JSToolkit takes care of all the "thread" handling.
109
110                 final JSThread me = this;
111                 Runnable r = new Runnable() {
112                         @Override
113                         public void run() {
114                                 me.run1(state);
115                         }
116                 };
117                 /**
118                  * @j2sNative
119                  * 
120                  *            setTimeout(
121                  *              function() {java.awt.Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(new java.awt.event.InvocationEvent(me, r))}, 
122                  *              delay
123                  *             );
124                  * 
125                  */
126                 {
127                         // for reference only
128                         Toolkit.getDefaultToolkit().getSystemEventQueue()
129                                         .postEvent(new InvocationEvent(me, r));
130                 }
131                 return true;
132         }
133
134 }