8f3b7caa8074dea9262d2a23571230d5b57bf6e1
[samba.git] / swat / apps / swat / source / class / swat / module / AbstractModuleFsm.js
1 /*
2  * Copyright:
3  *   (C) 2006 by Derrell Lipman
4  *       All rights reserved
5  *
6  * License:
7  *   LGPL 2.1: http://creativecommons.org/licenses/LGPL/2.1/
8  */
9
10 /**
11  * Common facilities for modules' finite state machines.  Each module's FSM
12  * should extend this class.
13  */
14 qx.OO.defineClass("swat.module.AbstractModuleFsm", qx.core.Object, function()
15 {
16   qx.core.Object.call(this);
17 });
18
19
20 qx.Proto.buildFsm = function(module)
21 {
22   throw new Error("Module must overload buildFsm() " +
23                   "to build its custom finite state machine.");
24 };
25
26 qx.Proto.addAwaitRpcResultState = function(module)
27 {
28   var fsm = module.fsm;
29
30   /*
31    * State: AwaitRpcResult
32    *
33    * Actions upon entry:
34    *  - enable any objects in group "swat.module.fsmUtils.enable_during_rpc"
35    *  - disable any objects in group "swat.module.fsmUtils.disable_during_rpc"
36    *
37    * Actions upon exit:
38    *   - disable any objects in group "group_enable_during_rpc"
39    *   - enable any objects in group "group_disable_during_rpc"
40    *
41    * Transition on:
42    *  "completed" (on RPC)
43    *  "failed" (on RPC)
44    *  "execute" on swat.module.fsmUtils.abort_rpc
45    */
46   var state = new qx.util.fsm.State(
47     "State_AwaitRpcResult",
48     {
49       "autoActionsBeforeOnentry" :
50       {
51         // The name of a function.
52         "setEnabled" :
53         [
54           {
55             // We want to enable objects in the group
56             // swat.module.fsmUtils.enable_during_rpc
57             "parameters" : [ true ],
58
59             // Call this.getObject(<object>).setEnabled(true) on
60             // state entry, for each <object> in the group called
61             // "swat.module.fsmUtils.enable_during_rpc".
62             "groups"      : [ "swat.module.fsmUtils.enable_during_rpc" ]
63           },
64
65           {
66             // We want to disable objects in the group
67             // swat.module.fsmUtils.disable_during_rpc
68             "parameters" : [ false ],
69
70             // Call this.getObject(<object>).setEnabled(false) on
71             // state entry, for each <object> in the group called
72             // "swat.module.fsmUtils.disable_during_rpc".
73             "groups"      : [ "swat.module.fsmUtils.disable_during_rpc" ]
74           }
75         ]
76       },
77
78       "autoActionsBeforeOnexit" :
79       {
80         // The name of a function.
81         "setEnabled" :
82         [
83           {
84             // We want to re-disable objects we had enabled, in the group
85             // swat.module.fsmUtils.enable_during_rpc
86             "parameters" : [ false ],
87
88             // Call this.getObject(<object>).setEnabled(false) on
89             // state entry, for each <object> in the group called
90             // "swat.module.fsmUtils.enable_during_rpc".
91             "groups"      : [ "swat.module.fsmUtils.enable_during_rpc" ]
92           },
93
94           {
95             // We want to re-enable objects we had disabled, in the group
96             // swat.module.fsmUtils.disable_during_rpc
97             "parameters" : [ true ],
98
99             // Call this.getObject(<object>).setEnabled(true) on
100             // state entry, for each <object> in the group called
101             // "swat.module.fsmUtils.disable_during_rpc".
102             "groups"      : [ "swat.module.fsmUtils.disable_during_rpc" ]
103           }
104         ]
105       },
106
107       "onentry" :
108         function(fsm, state)
109         {
110           // If we're coming from some other start...
111           if (fsm.getPreviousState() != "State_AwaitRpcResult")
112           {
113             // ... then push the previous state onto the state stack
114             fsm.pushState(false);
115           }
116         },
117
118       "events" :
119       {
120         "execute"  :
121         {
122           "swat.module.fsmUtils.abort_rpc" :
123             "Transition_AwaitRpcResult_to_AwaitRpcResult_via_button_abort"
124         },
125
126         "completed" :
127           "Transition_AwaitRpcResult_to_PopStack_via_complete",
128
129         "failed" :
130           "Transition_AwaitRpcResult_to_PopStack_via_failed"
131       }
132     });
133   fsm.addState(state);
134
135   /*
136    * Transition: AwaitRpcResult to AwaitRpcResult
137    *
138    * Cause: "execute" on swat.module.fsmUtils.abort_rpc
139    */
140   var trans = new qx.util.fsm.Transition(
141     "Transition_AwaitRpcResult_to_AwaitRpcResult_via_button_abort",
142     {
143       "nextState" :
144         "State_AwaitRpcResult",
145
146       "ontransition" :
147         function(fsm, event)
148         {
149           // Get the request object
150           var request = fsm.getObject("swat.module.fsmUtils.request");
151
152           // Issue an abort for the pending request
153           request.abort();
154         }
155     });
156   state.addTransition(trans);
157
158   /*
159    * Transition: AwaitRpcResult to PopStack
160    *
161    * Cause: "complete" (on RPC)
162    */
163   var trans = new qx.util.fsm.Transition(
164     "Transition_AwaitRpcResult_to_PopStack_via_complete",
165     {
166       "nextState" :
167         qx.util.fsm.FiniteStateMachine.StateChange.POP_STATE_STACK,
168
169       "ontransition" :
170         function(fsm, event)
171         {
172           // Get the request object
173           var request = fsm.getObject("swat.module.fsmUtils.request");
174           
175           // Generate the result for a completed request
176           request.result =
177           {
178             type : "complete",
179             data : event.getData()
180           };
181         }
182     });
183   state.addTransition(trans);
184
185   /*
186    * Transition: AwaitRpcResult to PopStack
187    *
188    * Cause: "failed" (on RPC)
189    */
190   var trans = new qx.util.fsm.Transition(
191     "Transition_AwaitRpcResult_to_PopStack_via_failed",
192     {
193       "nextState" :
194         qx.util.fsm.FiniteStateMachine.StateChange.POP_STATE_STACK,
195
196       "ontransition" :
197         function(fsm, event)
198         {
199           // Get the request object
200           var request = fsm.getObject("swat.module.fsmUtils.request");
201           
202           // Generate the result for a completed request
203           request.result =
204           {
205             type : "failed",
206             data : event.getData()
207           };
208         }
209     });
210   state.addTransition(trans);
211 };
212
213
214 /**
215  * Singleton Instance Getter
216  */
217 qx.Class.getInstance = qx.util.Return.returnInstance;