r22005: Add more code (doesn't work at the moment) preparing a place for
[jra/samba/.git] / webapps / swat / source / class / swat / module / netmgr / Fsm.js
1 /*
2  * Copyright (C)  Rafal Szczesniak 2007
3  */
4
5 /**
6  * Swat Net Manager class finite state machine
7  */
8 qx.OO.defineClass("swat.module.netmgr.Fsm", swat.main.AbstractModuleFsm,
9 function()
10 {
11   swat.main.AbstractModuleFsm.call(this);
12 });
13
14
15 qx.Proto.buildFsm = function(module)
16 {
17   var fsm = module.fsm;
18   var _this = this;
19
20   /*
21    * State: Idle
22    *
23    * Actions upon entry
24    *   - if returning from RPC, display the result
25    *
26    * Transition on:
27    *   "changeselection" on tree
28    */
29   var state = new qx.util.fsm.State(
30     "State_Idle",
31     {
32       "onentry" :
33         function(fsm, event)
34         {
35           if (fsm.getPreviousState() == "State_AwaitRpcResult")
36           {
37             var rpcRequest = _this.popRpcRequest();
38             var result = rpcRequest.getUserData("result");
39             var origins = swat.main.AbstractModuleFsm.JsonRpc_Origin;
40             var serverErrors = swat.main.AbstractModuleFsm.JsonRpc_ServerError;
41
42             if (result.type == "failed" &&
43                 result.data.origin == origins.Server &&
44                 result.data.code == serverErrors.ResourceError)
45             {
46               alert("Error when receiving rpc: '" + result.id + "'" + " exception: " + result.data);
47             }
48             else
49             {
50               // get the result of the call and apply it
51               var gui = swat.module.netmgr.Gui.getInstance();
52               gui.displayData(module, rpcRequest);
53             }
54             
55             rpcRequest.request.dispose();
56             rpcRequest.request = null;
57           }
58         },
59
60       "events" :
61         {
62           "appear" :
63           {
64             "swat.main.canvas" :
65               "Transition_Idle_to_AwaitRpcResult_via_canvas_appear",
66             "vlayout":
67               "Transition_Idle_to_AwaitRpcResult_via_vlayout_appear"
68           },
69
70           "changeSelection" :
71           {
72             "tree" :
73               "Transition_Idle_to_AwaitRpcResult_via_tree_selection_changed"
74           }
75         }
76     });
77
78   // Replace the initial Idle state with this one
79   fsm.replaceState(state, true);
80
81   var trans = new qx.util.fsm.Transition(
82     "Transition_Idle_to_AwaitRpcResult_via_canvas_appear",
83     {
84       "nextState" : "State_AwaitRpcResult",
85
86       "ontransition" : function(fsm, event)
87       {
88         var request = _this.callRpc(fsm, "samba.ejsnet", "NetContext", []);
89         request.setUserData("requestType", "NetContext");
90       }
91     });
92
93   // Add the new transition
94   state.addTransition(trans);
95
96   var trans = new qx.util.fsm.Transition(
97     "Transition_Idle_to_AwaitRpcResult_via_vlayout_appear",
98     {
99       "nextState" : "State_AwaitRpcResult",
100
101       "ontransition" :
102         function(fsm, event)
103         {
104           // Request our netbios name to add proper node to the tree
105           var request = _this.callRpc(fsm, "samba.config", "lp_get", [ "netbios name" ]);
106           request.setUserData("requestType", "hostname");
107         }
108     });
109
110   // Add the new transition
111   state.addTransition(trans);
112
113   var trans = new qx.util.fsm.Transition(
114     "Transition_Idle_to_AwaitRpcResult_via_tree_selection_changed",
115     {
116       "nextState" : "State_AwaitRpcResult",
117
118       "ontransition" : function(fsm, event)
119       {
120         var nodes = event.getData();
121         var selectedNode = nodes[0];
122
123         var gui = swat.module.netmgr.Gui.getInstance();
124         var parentNode = gui.getParentNode(module, selectedNode);
125         
126         var params = (parentNode.credentials == undefined) ? [] : [ parentNode.credentials ];
127       }
128       
129     });
130
131   // Add the new transition
132   state.addTransition(trans);
133   
134   blockedEvents =
135   {
136     "appear":
137     {
138       "tree" : qx.util.fsm.FiniteStateMachine.EventHandling.BLOCKED,
139       "vlayout" : qx.util.fsm.FiniteStateMachine.EventHandling.BLOCKED
140     }
141   }
142
143   // Add blocked events
144   this.addAwaitRpcResultState(module, blockedEvents);
145   
146 };
147
148
149 /**
150  * Singleton Instance Getter
151  */
152 qx.Class.getInstance = qx.lang.Function.returnInstance;