01: package fr.aliacom.commands;
02:
03: import fr.aliacom.form.common.ToolkitManager;
04:
05: /**
06: * This class provides a way to queue actions at the end
07: * the UI thread of the registered toolkit.
08: *
09: * The command will still block the ui, but it will block
10: * it later.
11: *
12: * If you need to do a long operation before update the UI,
13: * use a <code>FormWorker</code>.
14: *
15: */
16: public abstract class ASyncCommand extends Command {
17:
18: private boolean running;
19: private boolean stateAfterExecution;
20:
21: public ASyncCommand() {
22: running = false;
23: stateAfterExecution = true;
24: }
25:
26: public void run() {
27: fireStateChange(Command.STATE_BEGIN);
28: setEnabled(false);
29: ToolkitManager.getToolkit().asyncExec(
30: new AsyncCommandWorker(this ));
31: }
32:
33: private class AsyncCommandWorker implements Runnable {
34:
35: private ASyncCommand asc;
36:
37: AsyncCommandWorker(ASyncCommand asc) {
38: this .asc = asc;
39: }
40:
41: public void run() {
42: running = true;
43: asc.doIt();
44: fireStateChange(Command.STATE_END);
45: /* Do not reenable if a command is disabled while running */
46: setEnabled(stateAfterExecution);
47: stateAfterExecution = true;
48: running = false;
49: }
50: }
51:
52: /**
53: * If the enabled property is changed during command execution,
54: * the command will have the state you asked during execution.
55: * If you don't change the enabled property during execution, the
56: * command will automagically re-enable itself at the end of the execution.
57: */
58: public void setEnabled(boolean b) {
59: super.setEnabled(b);
60: if (running) {
61: stateAfterExecution = b;
62: }
63: }
64:
65: }
|