3 import java.awt.Toolkit;
4 import java.awt.event.InvocationEvent;
5 import swingjs.api.JSFunction;
8 * A class that takes care of simple threading. There are three states: INIT, LOOP, and DONE.
9 * These states are passed into run1
15 public abstract class JSThread extends Thread implements JSFunction {
17 public static final int INIT = 0;
18 public static final int LOOP = 1;
19 public static final int DONE = 2;
21 protected boolean isJS;
23 public JSThread(ThreadGroup group, String name) {
38 public synchronized void start() {
43 * swingjs.JSToolkit.setTimeout(this, 1, 0);
53 * a generic method that loops until done, or in JavaScript, will reenter and
54 * continue at the appropriate spot. Example given here
58 protected abstract void run1(int state);
61 // protected void run1(int state) {
66 // // once-through stuff here
70 // if (isInterrupted()) {
73 // // put the loop code here
75 // dispatchAndReturn(state);
80 // // add more cases as needed
83 // if (isInterrupted())
89 // // stuff here to be executed after each loop in JS or at the end in Java
97 * @return true if we should interrupt (i.e. JavaScript)
98 * @throws InterruptedException
100 protected boolean sleepAndReturn(final int delay, final int state)
101 throws InterruptedException {
107 // in JavaScript, we need to do this through the system event queue,
108 // which in JSToolkit takes care of all the "thread" handling.
110 final JSThread me = this;
111 Runnable r = new Runnable() {
121 * function() {java.awt.Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(new java.awt.event.InvocationEvent(me, r))},
127 // for reference only
128 Toolkit.getDefaultToolkit().getSystemEventQueue()
129 .postEvent(new InvocationEvent(me, r));