summaryrefslogtreecommitdiff
path: root/webapps/qooxdoo-0.6.3-sdk/frontend/framework/source/class/qx/ui/table/AbstractTableModel.js
blob: 99470e9361a79bdb19adc26d5f393ac2fa083699 (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
/* ************************************************************************

   qooxdoo - the new era of web development

   http://qooxdoo.org

   Copyright:
     2006 by STZ-IDA, Germany, http://www.stz-ida.de

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

   Authors:
     * Til Schneider (til132)

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

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

#module(ui_table)

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

/**
 * An abstract table model that performs the column handling, so subclasses only
 * need to care for row handling.
 */
qx.OO.defineClass("qx.ui.table.AbstractTableModel", qx.ui.table.TableModel,
function() {
  qx.ui.table.TableModel.call(this);

  this._columnIdArr = [];
  this._columnNameArr = [];
  this._columnIndexMap = {};
});


// overridden
qx.Proto.getColumnCount = function() {
  return this._columnIdArr.length;
}


// overridden
qx.Proto.getColumnIndexById = function(columnId) {
  return this._columnIndexMap[columnId];
}


// overridden
qx.Proto.getColumnId = function(columnIndex) {
  return this._columnIdArr[columnIndex];
}


// overridden
qx.Proto.getColumnName = function(columnIndex) {
  return this._columnNameArr[columnIndex];
}


/**
 * Sets the column IDs. These IDs may be used internally to identify a column.
 * <p>
 * Note: This will clear previously set column names.
 * </p>
 *
 * @param columnIdArr {string[]} the IDs of the columns.
 * @see #setColumns
 */
qx.Proto.setColumnIds = function(columnIdArr) {
  this._columnIdArr = columnIdArr;

  // Create the reverse map
  this._columnIndexMap = {};
  for (var i = 0; i < columnIdArr.length; i++) {
    this._columnIndexMap[columnIdArr[i]] = i;
  }
  this._columnNameArr = new Array(columnIdArr.length);

  // Inform the listeners
  if (!this._internalChange) {
    this.createDispatchEvent(qx.ui.table.TableModel.EVENT_TYPE_META_DATA_CHANGED);
  }
}


/**
 * Sets the column names. These names will be shown to the user.
 * <p>
 * Note: The column IDs have to be defined before.
 * </p>
 *
 * @param columnNameArr {string[]} the names of the columns.
 * @see #setColumnIds
 */
qx.Proto.setColumnNamesByIndex = function(columnNameArr) {
  if (this._columnIdArr.length != columnNameArr.length) {
    throw new Error("this._columnIdArr and columnNameArr have different length: "
      + this._columnIdArr.length + " != " + columnNameArr.length);
  }
  this._columnNameArr = columnNameArr;

  // Inform the listeners
  this.createDispatchEvent(qx.ui.table.TableModel.EVENT_TYPE_META_DATA_CHANGED);
}


/**
 * Sets the column names. These names will be shown to the user.
 * <p>
 * Note: The column IDs have to be defined before.
 * </p>
 *
 * @param columnNameMap {Map} a map containing the column IDs as keys and the
 *        column name as values.
 * @see #setColumnIds
 */
qx.Proto.setColumnNamesById = function(columnNameMap) {
  this._columnNameArr = new Array(this._columnIdArr.length);
  for (var i = 0; i < this._columnIdArr.length; ++i) {
    this._columnNameArr[i] = columnNameMap[this._columnIdArr[i]];
  }
}


/**
 * Sets the columns.
 *
 * @param columnNameArr {string[]} The column names. These names will be shown to
 *        the user.
 * @param columnIdArr {string[] ? null} The column IDs. These IDs may be used
 *        internally to identify a column. If null, the column names are used as
 *        IDs.
 */
qx.Proto.setColumns = function(columnNameArr, columnIdArr) {
  if (columnIdArr == null) {
    columnIdArr = columnNameArr;
  }

  if (columnIdArr.length != columnNameArr.length) {
    throw new Error("columnIdArr and columnNameArr have different length: "
      + columnIdArr.length + " != " + columnNameArr.length);
  }

  this._internalChange = true;
  this.setColumnIds(columnIdArr);
  this._internalChange = false;
  this.setColumnNamesByIndex(columnNameArr);
}