summaryrefslogtreecommitdiff
path: root/webapps/qooxdoo-0.6.3-sdk/frontend/framework/source/class/qx/core/Object.js
blob: 26453f82a2e6e64987e142d0061c6a9c24082475 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/* ************************************************************************

   qooxdoo - the new era of web development

   http://qooxdoo.org

   Copyright:
     2004-2006 by 1&1 Internet AG, Germany, http://www.1and1.org

   License:
     LGPL 2.1: http://www.gnu.org/licenses/lgpl.html

   Authors:
     * Sebastian Werner (wpbasti)
     * Andreas Ecker (ecker)

************************************************************************ */

/* ************************************************************************

#module(core)
#load(qx.core.Init)

************************************************************************ */

/**
 * The qooxdoo base object. All qooxdoo classes extend this one
 *
 * This class contains functions for:
 * <ul>
 *   <li> logging </li>
 *   <li> common getter/setter </li>
 *   <li> user data </li>
 *   <li> object destruction </li>
 * </ul>
 *
 * @param vAutoDispose {boolean ? true} wether the object should be disposed automatically by qooxdoo
 */
qx.OO.defineClass("qx.core.Object", Object,
function(vAutoDispose)
{
  this._hashCode = qx.core.Object._counter++;

  if (vAutoDispose !== false) {
    qx.core.Object._db.push(this);
  }
});


/*
---------------------------------------------------------------------------
  DEFAULT SETTINGS
---------------------------------------------------------------------------
*/

qx.Settings.setDefault("enableDisposerDebug", false);





/* ************************************************************************
   Class data, properties and methods
************************************************************************ */

qx.Class._counter = 0;
qx.Class._db = [];

/**
 * Generate an unique key for the given object and return it.
 * Sets object._hashCode to the generated key.
 *
 * @param o {Object}
 * @return {int} unique key for the given object
 */
qx.Class.toHashCode = function(o)
{
  if(o._hashCode != null) {
    return o._hashCode;
  }

  return o._hashCode = qx.core.Object._counter++;
}


/**
 * Class function which returns an object given its hash code
 *
 * @param hash {string} hash code of an object
 *
 * @returns {Object} the object whose hash is specified
 */
qx.Class.fromHashCode = function(hash) {
  return qx.core.Object._db[hash];
}


/**
 * Destructor. This method is called by qooxdoo on object destruction.
 *
 * Any class that holds ressources like links to DOM nodes must overwrite
 * this method and free theese ressources.
 */
qx.Class.dispose = function()
{
  // var logger = qx.dev.log.Logger.getClassLogger(qx.core.Object);
  // logger.debug("Disposing Application");

  // var vStart = (new Date).valueOf();
  var vObject;

  for (var i=qx.core.Object._db.length-1; i>=0; i--)
  {
    vObject = qx.core.Object._db[i];

    if (vObject && vObject._disposed === false)
    {
      // logger.debug("Disposing: " + vObject);
      vObject.dispose();
    }
  }

  // logger.debug("Done in: " + ((new Date).valueOf() - vStart) + "ms");
}


/**
 * Summary of allocated objects
 *
 * @return {string} summary of allocated objects.
 */
qx.Class.summary = function()
{
  var vData = {};
  var vCounter = 0;

  for (var i=qx.core.Object._db.length-1; i>=0; i--)
  {
    vObject = qx.core.Object._db[i];

    if (vObject && vObject._disposed === false)
    {
      if (vData[vObject.classname] == null)
      {
        vData[vObject.classname] = 1;
      }
      else
      {
        vData[vObject.classname]++;
      }

      vCounter++;
    }
  }

  var vArrData = [];

  for (var vClassName in vData) {
    vArrData.push({ classname : vClassName, number : vData[vClassName] });
  }

  vArrData.sort(function(a, b) {
    return b.number - a.number;
  });

  var vMsg = "Summary: (" + vCounter + " Objects)\n\n";

  for (var i=0; i<vArrData.length; i++) {
    vMsg += vArrData[i].number + ": " + vArrData[i].classname + "\n";
  }

  alert(vMsg);
}

/**
 * Enable or disable the Object.
 *
 * The actual semantic of this property depends on concrete subclass of qx.core.Object.
 */
qx.OO.addProperty({ name : "enabled", type : "boolean", defaultValue : true, getAlias : "isEnabled" });






/* ************************************************************************
   Instance data, properties and methods
************************************************************************ */

/*
---------------------------------------------------------------------------
  UTILITIES
---------------------------------------------------------------------------
*/

/**
 * Returns a string represantation of the qooxdoo object.
 *
 * @returns {string} string representation of the object
 */
qx.Proto.toString = function()
{
  if(this.classname) {
    return "[object " + this.classname + "]";
  }

  return "[object Object]";
}


/**
 * Return unique hash code of object
 *
 * @return {int} unique hash code of the object
 */
qx.Proto.toHashCode = function() {
  return this._hashCode;
}


/**
 * Returns true if the object is disposed.
 *
 * @return {boolean} wether the object has been disposed
 */
qx.Proto.getDisposed = function() {
  return this._disposed;
}


/**
 * Returns true if the object is disposed.
 *
 * @return {boolean} wether the object has been disposed
 */
qx.Proto.isDisposed = function() {
  return this._disposed;
}


/**
 * Returns a settings from global setting definition
 *
 * @param vKey {string}
 * @return {Object} value of the global setting
 */
qx.Proto.getSetting = function(vKey) {
  return qx.Settings.getValueOfClass(this.classname, vKey);
}


/*
---------------------------------------------------------------------------
  LOGGING INTERFACE
---------------------------------------------------------------------------
*/

/**
 * Returns the logger of this class.
 *
 * @return {qx.dev.log.Logger} the logger of this class.
 */
qx.Proto.getLogger = function() {
  return qx.dev.log.Logger.getClassLogger(this.constructor);
}


/**
 * Logs a debug message.
 *
 * @param msg {var} the message to log. If this is not a string, the
 *        object dump will be logged.
 * @param exc {var ? null} the exception to log.
 */
qx.Proto.debug = function(msg, exc) {
  this.getLogger().debug(msg, this._hashCode, exc);
}


/**
 * Logs an info message.
 *
 * @param msg {var} the message to log. If this is not a string, the
 *    object dump will be logged.
 * @param exc {var ? null} the exception to log.
 */
qx.Proto.info = function(msg, exc) {
  this.getLogger().info(msg, this._hashCode, exc);
}


/**
 * Logs a warning message.
 *
 * @param msg {var} the message to log. If this is not a string, the
 *    object dump will be logged.
 * @param exc {var ? null} the exception to log.
 */
qx.Proto.warn = function(msg, exc) {
  this.getLogger().warn(msg, this._hashCode, exc);
}


/**
 * Logs an error message.
 *
 * @param msg {var} the message to log. If this is not a string, the
 *    object dump will be logged.
 * @param exc {var ? null} the exception to log.
 */
qx.Proto.error = function(msg, exc) {
  this.getLogger().error(msg, this._hashCode, exc);
}




/*
---------------------------------------------------------------------------
  COMMON SETTER/GETTER SUPPORT
---------------------------------------------------------------------------
*/

/**
 * Sets multiple properties at once by using a property list
 *
 * @param propertyValues {Object} A hash of key-value pairs.
 */
qx.Proto.set = function(propertyValues)
{
  if (typeof propertyValues !== "object") {
    throw new Error("Please use a valid hash of property key-values pairs.");
  }

  for (var prop in propertyValues)
  {
    try
    {
      this[qx.OO.setter[prop]](propertyValues[prop]);
    }
    catch(ex)
    {
      this.error("Setter of property " + prop + " returned with an error", ex);
    }
  }

  return this;
}

/**
 * Gets multiple properties at once by using a property list
 *
 * @param propertyNames {string | array | hash} list of the properties to get
 * @param outputHint {string ? "array"} how should the values be returned. Possible values are "hash" and "array".
*/
qx.Proto.get = function(propertyNames, outputHint)
{
  switch(typeof propertyNames)
  {
    case "string":
      return this["get" + qx.lang.String.toFirstUp(propertyNames)]();

    case "object":
      if (typeof propertyNames.length === "number")
      {
        if (outputHint == "hash")
        {
          var h = {};

          propertyLength = propertyNames.length;
          for (var i=0; i<propertyLength; i++)
          {
            try{
              h[propertyNames[i]] = this["get" + qx.lang.String.toFirstUp(propertyNames[i])]();
            }
            catch(ex)
            {
              throw new Error("Could not get a valid value from property: " + propertyNames[i] + "! Is the property existing? (" + ex + ")");
            }
          }

          return h;
        }
        else
        {
          propertyLength = propertyNames.length;
          for (var i=0; i<propertyLength; i++)
          {
            try{
              propertyNames[i] = this["get" + qx.lang.String.toFirstUp(propertyNames[i])]();
            }
            catch(ex)
            {
              throw new Error("Could not get a valid value from property: " + propertyNames[i] + "! Is the property existing? (" + ex + ")");
            }
          }

          return propertyNames;
        }
      }
      else
      {
        for (var i in propertyNames) {
          propertyNames[i] = this["get" + qx.lang.String.toFirstUp(i)]();
        }

        return propertyNames;
      }

    default:
      throw new Error("Please use a valid array, hash or string as parameter!");
  }
}





/*
---------------------------------------------------------------------------
  USER DATA
---------------------------------------------------------------------------
*/

/**
 * Store user defined data inside the object.
 *
 * @param vKey {string}
 * @param vValue {Object}
 */
qx.Proto.setUserData = function(vKey, vValue)
{
  if (!this._userData) {
    this._userData = {};
  }

  this._userData[vKey] = vValue;
}


/**
 * Load user defined data from the object
 *
 * @param vKey {string}
 * @return {Object} the user data
 */
qx.Proto.getUserData = function(vKey)
{
  if (!this._userData) {
    return null;
  }

  return this._userData[vKey];
}






/*
---------------------------------------------------------------------------
  DISPOSER
---------------------------------------------------------------------------
*/

qx.Proto._disposed = false;

/**
 * Dispose this object
 */
qx.Proto.dispose = function()
{
  if (this.getDisposed()) {
    return;
  }

  // Dispose user data
  if (this._userData)
  {
    for(var vKey in this._userData) {
      this._userData[vKey] = null;
    }

    this._userData = null;
  }

  // Finally cleanup properties
  if (this._objectproperties)
  {
    var a = this._objectproperties.split(",");
    var d = qx.OO.values;

    for (var i=0, l=a.length; i<l; i++) {
      this[d[a[i]]] = null;
    }

    this._objectproperties = null;
  }

  if (this.getSetting("enableDisposerDebug"))
  {
    for (var vKey in this)
    {
      if (this[vKey] !== null && typeof this[vKey] === "object")
      {
        this.debug("Missing class implementation to dispose: " + vKey);
        delete this[vKey];
      }
    }
  }

  /*
  if (typeof CollectGarbage === "function") {
    CollectGarbage();
  }
  */

  // Delete Entry from Object DB
  qx.core.Object._db[this._hashCode] = null;

  // Mark as disposed
  this._disposed = true;
}