summaryrefslogtreecommitdiff
path: root/webapps/qooxdoo-0.6.5-sdk/frontend/framework/source/class/qx/ui/treevirtual/SimpleTreeDataModel.js
blob: 215830c224763bb130741f47ce121ff219321f79 (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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/* ************************************************************************

   qooxdoo - the new era of web development

   http://qooxdoo.org

   Copyright:
     2007 Derrell Lipman

   License:
     LGPL: http://www.gnu.org/licenses/lgpl.html
     EPL: http://www.eclipse.org/org/documents/epl-v10.php
     See the LICENSE file in the project's top-level directory for details.

   Authors:
     * Derrell Lipman (derrell)

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

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

#module(treevirtual)

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



/*
 * A simple tree data model used as the table model
 *
 * The object structure of a single node of the tree is:
 *
 * {
 *   type          : qx.ui.treevirtual.Type.LEAF,
 *   parentNodeId  : 23,   // index in _nodeArr of the parent node
 *   label         : "My Documents",
 *   bSelected     : true, // true if node is selected; false otherwise
 *   opened        : null, // true (-), false (+), or null (no +/-)
 *   icon          : "images/folder.gif",
 *   iconSelected  : "images/folder_selected.gif",
 *   children      : [ ],  // each value is an index into _nodeArr
 *
 *   cellStyle     : "background-color:cyan"
 *   labelStyle    : "background-color:red;color:white"
 *
 *   // The following properties need not (and should not) be set by the
 *   // caller, but are automatically calculated.  Some are used internally,
 *   // while others may be of use to event listeners.
 *
 *   nodeId        : 42,   // The index in _nodeArr, useful to event listeners
 *
 *   level         : 2,    // The indentation level of this tree node
 *
 *   bFirstChild   : true,
 *   lastChild     : [ false ],  // Array where the index is the column of
 *                               // indentation, and the value is a boolean.
 *                               // These are used to locate the
 *                               // appropriate "tree line" icon.
 * }
 */
qx.OO.defineClass("qx.ui.treevirtual.SimpleTreeDataModel",
                  qx.ui.table.AbstractTableModel,
function()
{
  qx.ui.table.AbstractTableModel.call(this);

  this._rowArr = [ ];           // rows, resorted into tree order as necessary
  this._nodeArr = [ ];          // tree nodes, organized with hierarchy

  this._nodeRowMap = [ ];       // map nodeArr index to rowArr index.  The
                                // index of this array is the index of
                                // _nodeArr, and the values in this array are
                                // the indexes into _rowArr.


  this._treeColumn = 0;         // default column for tree nodes

  this._selections = { };       // list of indexes of selected nodes

  this._nodeArr.push(           // the root node, needed to store its children
    {
      label     : "<virtual root>",
      opened    : true,
      children  : [ ]
    });
});


// overridden
qx.Proto.setEditable = function(editable)
{
  throw new Error("Tree columns can not be made editable");
};


// overridden
qx.Proto.setColumnEditable = function(columnIndex, editable)
{
  throw new Error("Tree columns can not be made editable");
};


// overridden
qx.Proto.isColumnEditable = function(columnIndex)
{
  return false;
};


// overridden
qx.Proto.isColumnSortable = function(columnIndex)
{
  return false;
};


// overridden
qx.Proto.sortByColumn = function(columnIndex, ascending)
{
  throw new Error("Trees can not be sorted by column");
};


qx.Proto.getSortColumnIndex = function()
{
  return -1;
};


qx.Proto.isSortAscending = function()
{
  return true;
};


qx.Proto.getRowCount = function()
{
  return this._rowArr.length;
};


qx.Proto.setTreeColumn = function(columnIndex)
{
  this._treeColumn = columnIndex;
}


qx.Proto.getTreeColumn = function()
{
  return this._treeColumn;
}


qx.Proto.getRowData = function(rowIndex)
{
  return this._rowArr[rowIndex];
}


qx.Proto.getValue = function(columnIndex, rowIndex)
{
  if (rowIndex < 0 || rowIndex >= this._rowArr.length)
  {
    throw new Error ("this._rowArr row " +
                     "(" + rowIndex + ") out of bounds: " +
                     this._rowArr +
                     " (0.." +
                     (this._rowArr.length - 1) + ")");b
  }

  if (columnIndex < 0 || columnIndex >= this._rowArr[rowIndex].length)
  {
    throw new Error ("this._rowArr column " +
                     "(" + columnIndex + ") out of bounds: " +
                     this._rowArr[rowIndex] +
                     " (0.." +
                     (this._rowArr[rowIndex].length - 1) + ")");
  }

  return this._rowArr[rowIndex][columnIndex];
};


qx.Proto._addNode = function(parentNodeId,
                             label,
                             opened,
                             type,
                             icon,
                             iconSelected)
{
  var parentNode;

  // Ensure that if parent was specified, it exists
  if (parentNodeId)
  {
    parentNode = this._nodeArr[parentNodeId];
    if (! parentNode)
    {
        throw new Error("Request to add a child to a non-existent parent");
    }

    // Ensure parent isn't a leaf
    if (parentNode.type == qx.ui.treevirtual.SimpleTreeDataModel.Type.LEAF)
    {
      throw new Error("Sorry, a LEAF may not have children.");
    }
  }
  else
  {
    // This is a child of the root
    parentNode = this._nodeArr[0];
    parentNodeId = 0;
  }

  // If this is a file, we don't present open/close icon
  if (type == qx.ui.treevirtual.SimpleTreeDataModel.Type.LEAF && opened)
  {
    throw new Error("Attempt to display a LEAF opened [" + label + "]");
  }

  // Determine the node id of this new node
  var nodeId = this._nodeArr.length;

  // Set the data for this node.
  var node =
    {
      type         : type,
      parentNodeId : parentNodeId,
      label        : label,
      bSelected    : false,
      opened       : opened,
      icon         : icon,
      iconSelected : iconSelected,
      children     : [ ],
      columnData   : [ ]
    };

  // Add this node to the array
  this._nodeArr.push(node);

  // Add this node to its parent's child array.
  parentNode.children.push(nodeId);

  // Return the node id we just added
  return nodeId;
};



qx.Proto.addBranch = function(parentNodeId,
                              label,
                              opened,
                              icon,
                              iconSelected)
{
  return this._addNode(parentNodeId,
                       label,
                       opened,
                       qx.ui.treevirtual.SimpleTreeDataModel.Type.BRANCH,
                       icon,
                       iconSelected);
};


qx.Proto.addLeaf = function(parentNodeId,
                            label,
                            icon,
                            iconSelected)
{
  return this._addNode(parentNodeId,
                       label,
                       false,
                       qx.ui.treevirtual.SimpleTreeDataModel.Type.LEAF,
                       icon,
                       iconSelected);
};


qx.Proto.prune = function(nodeId)
{
  // First, recursively remove all children
  for (var i = 0; i < this._nodeArr[nodeId].children.length; i++)
  {
    this.prune(this._nodeArr[nodeId].children[i]);
  }

  // Delete ourself from our parent's children list
  var node = this._nodeArr[nodeId];
  qx.lang.Array.remove(this._nodeArr[node.parentNodeId].children, nodeId);

  // Delete ourself from the selections list, if we're in it.
  if (this._selections[nodeId])
  {
    delete this._selections[nodeId];
  }

  // We can't splice the node itself out, because that would muck up the
  // nodeId == index correspondence.  Instead, just replace the node with
  // null so its index just becomes unused.
  this._nodeArr[nodeId] = null;
};



/**
 * Sets the whole data en bulk, or notifies the data model that node
 * modifications are complete.
 *
 * @param nodeArr {Array | null}
 *   Pass either an Array of node objects, or null.
 *
 *   If non-null, nodeArr is an array of node objects containing the entire
 *   tree to be displayed.  If loading the whole data en bulk in this way, it
 *   is assumed that the data is correct!  No error checking or validation is
 *   done.  You'd better know what you're doing!  Caveat emptor.
 *
 *   If nodeArr is null, then this call is a notification that the user has
 *   completed building or modifying a tree by issuing a series of calls to
 *   addNode().
 */
qx.Proto.setData = function(nodeArr)
{
  if (nodeArr instanceof Array)
  {
    // Determine the set of selected nodes
    for (i = 0; i < nodeArr.length; i++)
    {
      if (nodeArr[i].selected)
      {
        this._selections[i] = true;
      }
    }

    // Save the user-supplied data.
    this._nodeArr = nodeArr;
  }
  else if (nodeArr !== null && nodeArr !== undefined)
  {
    throw new Error("Expected array of node objects or null/undefined; got " +
                    typeof(nodeArr));
  }

  // Re-render the row array
  this._render();
};


/**
 * Return the array of node data.
 *
 * @return {Array}
 *   Array of node objects.  See {@link qx.ui.treevirtual.SimpleTreeDataModel}
 *   for a description of each node.
 */
qx.Proto.getData = function()
{
  return this._nodeArr;
};



/**
 * Add data to an additional column of the tree.
 *
 * @param nodeId
 *   A node identifier, as previously returned by addBranch() or addLeaf().
 *
 * @param columnIndex
 *   The column number to which the provided data applies
 *
 * @param data
 *   The cell data for the specified column
 */
qx.Proto.setColumnData = function(nodeId, columnIndex, data)
{
  this._nodeArr[nodeId].columnData[columnIndex] = data;
}


qx.Proto.setState = function(nodeId, attributes)
{
  for (var attribute in attributes)
  {
    // If the selected state is changing...
    if (attribute == "bSelected")
    {
      // ... then keep track of what is selected
      if (attributes[attribute])
      {
        this._selections[nodeId] = true;
      }
      else
      {
        delete this._selections[nodeId];
      }
    }

    this._nodeArr[nodeId][attribute] = attributes[attribute];
  }
};


qx.Proto.getNodeRowMap = function()
{
  return this._nodeRowMap;
};


qx.Proto.clearSelections = function()
{
  // Clear selected state for any selected nodes.
  for (var selection in this._selections)
  {
    this._nodeArr[selection].bSelected = false;
  }

  // Reinitialize selections array.
  this._selections = { };
};


qx.Proto.getSelections = function()
{
  return this._selections;
};


/**
 * Render (or re-render) the tree.  Call this function after having added
 * and/or deleted tree nodes (Files or Folders), or after having made changes
 * to tree (or tree node) options that will cause the tree to be rendered
 * differently.  This function should typically be called after a set of
 * concurrent changes, not after each change.
 */
qx.Proto._render = function()
{
  var _this = this;

  var inorder = function(nodeId, level)
  {
    var child = null;
    var childNodeId;

    // For each child of the specified node...
    var numChildren = _this._nodeArr[nodeId].children.length;
    for (var i = 0; i < numChildren; i++)
    {
      // Determine the node id of this child
      childNodeId = _this._nodeArr[nodeId].children[i];

      // Get the child node
      child = _this._nodeArr[childNodeId];

      // Skip deleted nodes
      if (child == null)
      {
        continue;
      }

      // Listeners will need to know a node's id when they receive an event
      child.nodeId = childNodeId;

      // (Re-)assign this node's level
      child.level = level;

      // Determine if we're the first child of our parent
      child.bFirstChild = (i == 0);

      // Determine if we're the last child of our parent
      child.lastChild = [ i == numChildren - 1 ];

      // Get our parent.
      var parent = _this._nodeArr[child.parentNodeId];

      // For each parent node, determine if it is a last child
      while (parent.nodeId)
      {
        var bLast = parent.lastChild[parent.lastChild.length - 1];
        child.lastChild.unshift(bLast);
        parent = _this._nodeArr[parent.parentNodeId];
      }

      // Ensure there's an entry in the columnData array for each column
      if (! child.columnData)
      {
        child.columnData = [ ];
      }

      if (child.columnData.length < _this.getColumnCount())
      {
        child.columnData[_this.getColumnCount() - 1] = null;
      }

      // Add this node to the row array.  Initialize a row data array.
      var rowData = [ ];

      // If additional column data is provided...
      if (child.columnData)
      {
        // ... then add each column data.
        for (var j = 0; j < child.columnData.length; j++)
        {
          // Is this the tree column?
          if (j == _this._treeColumn)
          {
            // Yup.  Add the tree node data
            rowData.push(child);
          }
          else
          {
            // Otherwise, add the column data verbatim.
            rowData.push(child.columnData[j]);
          }
        }
      }
      else
      {
        // No column data.  Just add the tree node.
        rowData.push(child);
      }

      // If this node is selected, ...
      if (child.bSelected)
      {
        // ... indicate so for the row.
        rowData.selected = true;
      }

      // Track the _rowArr index for each node so we can handle selections
      _this._nodeRowMap[child.nodeId] = _this._rowArr.length;

      // Add the row data to the row array
      _this._rowArr.push(rowData)

      // If this child is opened, ...
      if (child.opened)
      {
        // ... then add its children too.
        inorder(childNodeId, level + 1);
      }
    }
  }

  // Reset the row array
  this._rowArr = [];

  // Reset the _nodeArr -> _rowArr map
  this._nodeRowMap = [ ];

  // Begin in-order traversal of the tree from the root to regenerate _rowArr
  inorder(0, 1);

  // Inform the listeners
  if (this.hasEventListeners(qx.ui.table.TableModel.EVENT_TYPE_DATA_CHANGED))
  {
    var data =
      {
        firstRow        : 0,
        lastRow         : this._rowArr.length - 1,
        firstColumn     : 0,
        lastColumn      : this.getColumnCount() - 1
      };

    this.dispatchEvent(new qx.event.type.DataEvent(
                         qx.ui.table.TableModel.EVENT_TYPE_DATA_CHANGED,
                         data),
                       true);
  }
};


// We currently support these types of tree nodes
qx.Class.Type = {};
qx.Class.Type.LEAF            = 1;
qx.Class.Type.BRANCH          = 2;