01: // File Bind.java
02:
03: package tests.javabind;
04:
05: // This class implements a bindable object that can
06: // be used to test the java::bind command without
07: // loading the awt.
08:
09: public class Bind implements Runnable {
10: private ActionListener actionListener = null;
11: private Thread t = null;
12: private boolean started;
13: private boolean stopped;
14: private int tnum = 0;
15:
16: private final boolean debug = false;
17:
18: public void addActionListener(ActionListener l) {
19: actionListener = l;
20: }
21:
22: public void removeActionListener(ActionListener l) {
23: actionListener = null;
24: }
25:
26: public void doLater() {
27: // Create new thread that will invoke the call to
28: // actionListener.actionPerformed(ActionEvent)
29: // in another thread at a later time
30:
31: started = false;
32: stopped = false;
33:
34: tnum++;
35: t = new Thread(this );
36: t.setDaemon(true);
37: t.setName("Bind delay thread " + tnum);
38: t.start();
39:
40: if (debug) {
41: System.err.println("Waiting to invoke action");
42: }
43: }
44:
45: public void run() {
46: started = true;
47:
48: // Go to sleep for some time
49: try {
50: Thread.sleep(1000);
51: } catch (InterruptedException ie) {
52: }
53:
54: if (debug) {
55: System.err.println("Now to invoke action");
56: }
57:
58: // Now invoke the callback (from a different thread)
59: if (actionListener != null)
60: actionListener.actionPerformed(new ActionEvent(this ));
61:
62: stopped = true;
63: t = null;
64: }
65:
66: public boolean wasStarted() {
67: return started;
68: }
69:
70: public boolean wasStopped() {
71: return stopped;
72: }
73: }
|