OT: How to fire an action and listen in another class [message #453921] |
Wed, 13 April 2005 18:50  |
Eclipse User |
|
|
|
I have the need to create a class which is instantiated in another
class, does some work, and then needs to notify the parent class that it
is done (as well as return some data). I'm not sure how to create an
action method and make sure it is fired when the work is done, thus
notifying the parent class.
Anyone done something like this before? Is there a better way or am I
on the right track?
MP
|
|
|
Re: How to fire an action and listen in another class [message #453924 is a reply to message #453921] |
Wed, 13 April 2005 19:24  |
Eclipse User |
|
|
|
Not related to SWT, but, just create your own listener and have the worker
class notify that interface when it's done.
Here's an example (not tested, wrote from scratch in this message):
public interface WorkerDone {
public void workerFinished(Object data);
}
public class Creator implements WorkerDone {
public Creator() {
Worker worker = new Worker();
worker.addListener(this);
worker.run();
}
public void workerFinished(Object data) {
System.err.println("I'm done! with data: " + data);
}
}
public class Worker {
Vector listeners = new Vector();
public Worker() {}
public void addListener(WorkerListener listener) {
listeners.add(listener);
}
public void run() {
// do some stuff that takes time
// ....
// notify everyone that cares that we finished
Object resultData = null;
notifyListeners(resultData);
}
public void notifyListeners(Object data) {
for (int i = 0; i < listeners.size(); i++) {
((WorkerListener)listeners.get(i)).workerFinished(data);
}
}
}
"MP" <dev1@levelgroundmedia.com> wrote in message
news:d3k7nv$gf4$1@news.eclipse.org...
>I have the need to create a class which is instantiated in another class,
>does some work, and then needs to notify the parent class that it is done
>(as well as return some data). I'm not sure how to create an action method
>and make sure it is fired when the work is done, thus notifying the parent
>class.
>
> Anyone done something like this before? Is there a better way or am I on
> the right track?
>
> MP
|
|
|
Powered by
FUDForum. Page generated in 0.05867 seconds