summaryrefslogtreecommitdiff
path: root/webapps/swat/source/class/swat/module/AbstractModule.js
blob: 19bcc88a13e971560973141531bcf056eab89f86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright:
 *   (C) 2006 by Derrell Lipman
 *       All rights reserved
 *
 * License:
 *   LGPL 2.1: http://creativecommons.org/licenses/LGPL/2.1/
 */

/**
 * Abstract Module class.  All modules should extend this class.
 */
qx.OO.defineClass("swat.module.AbstractModule", qx.core.Object,
function()
{
  qx.core.Object.call(this);
  this.debug("AbstractModule constructor");
});


/**
 * Build the initial finite state machine.
 *
 * In order to prevent long load times, as minimal as possible of an initial
 * FSM should be created.  The FSM will receive an "appear" event when the
 * module is first selected (and each subsequent time), and the FSM can use
 * that event to build the complete FSM.
 *
 * @param module {swat.module.Module}
 *    The module descriptor for the module.
 */
qx.Proto.buildInitialFsm = function(module)
{
  // Create a new finite state machine
  var fsm = new qx.util.fsm.FiniteStateMachine(module.name);

  // For this simple example application, show all debug messages.
  qx.Settings.setCustomOfClass(
    "qx.util.fsm.FiniteStateMachine",
    "debugFlags",
    (qx.util.fsm.FiniteStateMachine.DebugFlags.EVENTS |
     qx.util.fsm.FiniteStateMachine.DebugFlags.TRANSITIONS |
     qx.util.fsm.FiniteStateMachine.DebugFlags.FUNCTION_DETAIL |
     qx.util.fsm.FiniteStateMachine.DebugFlags.OBJECT_NOT_FOUND));

  /*
   * State: Idle
   *
   * Transition on:
   *  "appear" on swat.module.canvas
   */
  var state = new qx.util.fsm.State(
    "State_Idle",
    {
      "events" :
        {
          // When we get an appear event the first time, run the transition
          // that will load the module's finite state machine and graphical
          // user interface.
          "appear"  :
          {
            "swat.module.canvas" :
              "Transition_Idle_to_Idle_Load_Gui"
          }
        }
    });
  fsm.addState(state);

  /*
   * Transition: Idle to (replaced) Idle
   *
   * Cause: "appear" on canvas for the first time
   *
   * Action:
   *  Load module's finite state machine and graphical user interface
   */
  var thisModule = this;
  var trans = new qx.util.fsm.Transition(
    "Transition_Idle_to_Idle_Load_Gui",
    {
      "nextState" :
        qx.util.fsm.FiniteStateMachine.StateChange.CURRENT_STATE,

      "ontransition" :
        function(fsm, event)
        {
          // Make the "Loading" message go away.  (We need to learn how to
          // remove it entirely.  Just doing canvas.removeAll() leaves
          // something in the widget queue and we get spurious error
          // messages.)
          var children = module.canvas.getVisibleChildren();
          for (var child in children)
          {
            children[child].hide();
          }

          // Call the module's initialAppear function to build FSM and GUI.
          // That function should *replace* this state, State_Idle, to which
          // we'll transition.
          thisModule.initialAppear(module);
        }
    });
  state.addTransition(trans);

  // Save the finite state machine for this module
  module.fsm = fsm;

  // Save the module descriptor in the finite state machine
  fsm.addObject("swat.module.module", module);

  // Create an RPC object for use by this module
  module.rpc = new qx.io.remote.Rpc();
  module.rpc.setUrl("/services/");
  module.rpc.setTimeout(10000);
  module.rpc.setCrossDomain(false);
  module.rpc.addEventListener("completed", fsm.eventListener, fsm);
  module.rpc.addEventListener("failed", fsm.eventListener, fsm);
  module.rpc.addEventListener("timeout", fsm.eventListener, fsm);
  module.rpc.addEventListener("aborted", fsm.eventListener, fsm);
  fsm.addObject("swat.module.rpc", module.rpc);

  // Start the finite state machine
  fsm.start();
};

/**
 * Build the initial graphical user interface.
 *
 * In order to prevent long load times, as minimal as possible of an initial
 * GUI should be created.  Generally, this is just a "Loading..." message.
 *
 * @param module {Object}
 *   An object containing at least the following properties:
 *     fsm -
 *       The finite state machine for this module.  It should be filled in
 *       by this function.
 *     canvas -
 *       The canvas on which to create the gui for this module
 *     name -
 *       The name of this module
 *     class -
 *       The class for this module
 *
 */
qx.Proto.buildInitialGui = function(module)
{
  // For now, just create the "Loading" text
  var o = new qx.ui.basic.Label("Loading module '" + module.name + "'...");
  o.set({
            top: 12,
            left: 20
        });
  o.setFont("bold");
  module.canvas.add(o);
};

qx.Proto.finalize = function(module)
{
  this.debug("AbstractModule.finalize()");
};


/*
---------------------------------------------------------------------------
  DEFER SINGLETON INSTANCE
---------------------------------------------------------------------------
*/

/**
 * Singleton Instance Getter
 */
qx.Class.getInstance = qx.util.Return.returnInstance;