1 package cz.cuni.amis.pogamut.base.communication.command.react;
2
3 import cz.cuni.amis.pogamut.base.communication.command.IAct;
4 import cz.cuni.amis.pogamut.base.communication.command.ICommandListener;
5 import cz.cuni.amis.pogamut.base.communication.messages.CommandMessage;
6 import cz.cuni.amis.pogamut.base.communication.worldview.event.IWorldEvent;
7 import cz.cuni.amis.pogamut.base.communication.worldview.react.EventReact;
8 import cz.cuni.amis.pogamut.base.communication.worldview.react.EventReactOnce;
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 public abstract class CommandReact<COMMAND extends CommandMessage> {
28
29 protected ICommandListener<COMMAND> reactListener = new ICommandListener<COMMAND>() {
30
31 @Override
32 public void notify(COMMAND event) {
33 preReact(event);
34 react(event);
35 postReact(event);
36 }
37
38 };
39
40 protected IAct reactAct;
41
42 protected Class<COMMAND> reactCommandClass;
43
44 private boolean reactHooked = false;
45
46 public CommandReact(Class<COMMAND> commandClass, IAct worldView) {
47 this.reactAct = worldView;
48 this.reactCommandClass = commandClass;
49 enable();
50 }
51
52
53
54
55 public synchronized void disable() {
56 if (reactHooked) {
57 reactHooked = false;
58 reactAct.removeCommandListener(reactCommandClass, reactListener);
59 }
60 }
61
62
63
64
65 public synchronized void enable() {
66 if (!reactHooked) {
67 reactHooked = true;
68 if (!reactAct.isCommandListening(reactCommandClass, reactListener)) reactAct.addCommandListener(reactCommandClass, reactListener);
69 }
70 }
71
72
73
74
75
76 protected void preReact(COMMAND event) {
77 }
78
79
80
81
82
83 protected abstract void react(COMMAND event);
84
85
86
87
88
89 protected void postReact(COMMAND event) {
90 }
91
92 }