r20347: added generic rpc fsm and statistics-specific fsm. next step: statistics gui
[jelmer/samba4-debian.git] / swat / apps / swat / source / class / swat / module / AbstractModule.js
1 /**
2  * Abstract Module class.  All modules should extend this class.
3  */
4 qx.OO.defineClass("swat.module.AbstractModule", qx.core.Object,
5 function()
6 {
7   qx.core.Object.call(this);
8   this.debug("AbstractModule constructor");
9 });
10
11
12 /**
13  * Build the initial finite state machine.
14  *
15  * In order to prevent long load times, as minimal as possible of an initial
16  * FSM should be created.  The FSM will receive a "visible" event when the
17  * module is first selected (and each subsequent time), and the FSM can use
18  * that event to build the complete FSM.
19  *
20  * @param module {Object}
21  *   An object containing at least the following properties:
22  *     fsm -
23  *       The finite state machine for this module.  It should be filled in
24  *       by this function.
25  *     canvas -
26  *       The canvas on which to create the gui for this module
27  *     name -
28  *       The name of this module
29  *     class -
30  *       The class for this module
31  *
32  */
33 qx.Proto.buildInitialFsm = function(module)
34 {
35   // Create a new finite state machine
36   var fsm = new qx.util.fsm.FiniteStateMachine(module.name);
37
38   // For this simple example application, show all debug messages.
39   qx.Settings.setCustomOfClass(
40     "qx.util.fsm.FiniteStateMachine",
41     "debugFlags",
42     (qx.util.fsm.FiniteStateMachine.DebugFlags.EVENTS |
43      qx.util.fsm.FiniteStateMachine.DebugFlags.TRANSITIONS |
44      qx.util.fsm.FiniteStateMachine.DebugFlags.FUNCTION_DETAIL |
45      qx.util.fsm.FiniteStateMachine.DebugFlags.OBJECT_NOT_FOUND));
46
47   /*
48    * State: Idle
49    *
50    * Transition on:
51    *  "appear" on swat.module.canvas
52    */
53   var state = new qx.util.fsm.State(
54     "State_Idle",
55     {
56       "events" :
57         {
58           // When we get an appear event the first time, run the transition
59           // that will load the module's finite state machine and graphical
60           // user interface.
61           "appear"  :
62           {
63             "swat.module.canvas" :
64               "Transition_Idle_to_Idle_Load_Gui"
65           }
66         }
67     });
68   fsm.addState(state);
69
70   /*
71    * Transition: Idle to (replaced) Idle
72    *
73    * Cause: "appear" on canvas for the first time
74    *
75    * Action:
76    *  Load module's finite state machine and graphical user interface
77    */
78   var thisModule = this;
79   var trans = new qx.util.fsm.Transition(
80     "Transition_Idle_to_Idle_Load_Gui",
81     {
82       "nextState" :
83         qx.util.fsm.FiniteStateMachine.StateChange.CURRENT_STATE,
84
85       "ontransition" :
86         function(fsm, event)
87         {
88           // Make the "Loading" message go away.  (We need to learn how to
89           // remove it entirely.  Just doing canvas.removeAll() leaves
90           // something in the widget queue and we get spurious error
91           // messages.)
92           var children = module.canvas.getVisibleChildren();
93           for (var child in children)
94           {
95             children[child].hide();
96           }
97
98           // Call the module's initialAppear function to build FSM and GUI.
99           // That function should *replace* this state, State_Idle, to which
100           // we'll transition.
101           thisModule.initialAppear(module);
102         }
103     });
104   state.addTransition(trans);
105
106   // Save the finite state machine for this module
107   module.fsm = fsm;
108   fsm.addObject("swat.module.fsm", fsm);
109   fsm.start();
110 };
111
112 /**
113  * Build the initial graphical user interface.
114  *
115  * In order to prevent long load times, as minimal as possible of an initial
116  * GUI should be created.  Generally, this is just a "Loading..." message.
117  *
118  * @param module {Object}
119  *   An object containing at least the following properties:
120  *     fsm -
121  *       The finite state machine for this module.  It should be filled in
122  *       by this function.
123  *     canvas -
124  *       The canvas on which to create the gui for this module
125  *     name -
126  *       The name of this module
127  *     class -
128  *       The class for this module
129  *
130  */
131 qx.Proto.buildInitialGui = function(module)
132 {
133   // For now, just create the "Loading" text
134   var o = new qx.ui.basic.Label("Loading module '" + module.name + "'...");
135   o.set({
136             top: 12,
137             left: 20
138         });
139   o.setFont("bold");
140   module.canvas.add(o);
141 };
142
143 qx.Proto.finalize = function(module)
144 {
145   this.debug("AbstractModule.finalize()");
146 };
147
148
149 /*
150 ---------------------------------------------------------------------------
151   DEFER SINGLETON INSTANCE
152 ---------------------------------------------------------------------------
153 */
154
155 /**
156  * Singleton Instance Getter
157  */
158 qx.Class.getInstance = qx.util.Return.returnInstance;