01: package fr.aliacom.commands;
02:
03: import java.util.ArrayList;
04:
05: import org.apache.log4j.Logger;
06:
07: import fr.aliacom.form.common.events.CommandEvent;
08: import fr.aliacom.form.common.events.ICommandListener;
09:
10: /**
11: * A command models an action.
12: * It is used by menu items, toolbar buttons, buttons, ...
13: *
14: *
15: *
16: * @author tom
17: *
18: * (c) 2001, 2003 Thomas Cataldo
19: */
20: public abstract class Command {
21:
22: public final static int STATE_BEGIN = 1;
23: public final static int STATE_END = 2;
24: public final static int STATE_UNDO = 3;
25: public final static int STATE_CANCEL = 4;
26: public final static int STATE_ENABLED = 5;
27: public final static int STATE_DISABLED = 6;
28:
29: private ArrayList listenerList;
30:
31: protected static final Logger log = Logger
32: .getLogger(ASyncCommand.class);
33:
34: public Command() {
35: listenerList = new ArrayList();
36: }
37:
38: public void setEnabled(boolean b) {
39: fireStateChange(b ? STATE_ENABLED : STATE_DISABLED);
40: }
41:
42: protected abstract void doIt();
43:
44: /**
45: * This might be invoked directly
46: */
47: public abstract void run();
48:
49: public void addCommandListener(ICommandListener listener) {
50: listenerList.add(listener);
51: }
52:
53: public void removeCommandListener(ICommandListener listener) {
54: listenerList.remove(listener);
55: }
56:
57: protected void fireStateChange(int state) {
58: CommandEvent ev = new CommandEvent(this , state);
59: for (int i = listenerList.size() - 1; i >= 0; i--) {
60: ((ICommandListener) listenerList.get(i)).stateChanged(ev);
61: }
62: }
63:
64: }
|