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

   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)

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

/**
 * Shows the header of a table.
 *
 * @param paneScroller {TablePaneScroller} the TablePaneScroller the header belongs to.
 */
qx.OO.defineClass("qx.ui.table.TablePaneHeader", qx.ui.layout.HorizontalBoxLayout,
function(paneScroller) {
  qx.ui.layout.HorizontalBoxLayout.call(this);

  this._paneScroller = paneScroller;
});


/**
 * Returns the TablePaneScroller this header belongs to.
 *
 * @return {TablePaneScroller} the TablePaneScroller.
 */
qx.Proto.getPaneScroller = function() {
  return this._paneScroller;
};


/**
 * Returns the table this header belongs to.
 *
 * @return {Table} the table.
 */
qx.Proto.getTable = function() {
  return this._paneScroller.getTable();
};


/**
 * Event handler. Called when the width of a column has changed.
 *
 * @param evt {Map} the event.
 */
qx.Proto._onColWidthChanged = function(evt) {
  var data = evt.getData();
  this.setColumnWidth(data.col, data.newWidth);
}


/**
 * Event handler. Called the column order has changed.
 *
 * @param evt {Map} the event.
 */
qx.Proto._onColOrderChanged = function(evt) {
  this._updateContent(true);
}


/**
 * Event handler. Called when the pane model has changed.
 *
 * @param evt {Map} the event.
 */
qx.Proto._onPaneModelChanged = function(evt) {
  this._updateContent(true);
}


/**
 * Event handler. Called when the table model meta data has changed.
 *
 * @param evt {Map} the event.
 */
qx.Proto._onTableModelMetaDataChanged = function(evt) {
  this._updateContent();
}


/**
 * Sets the column width. This overrides the width from the column model.
 *
 * @param col {int} the column to change the width for.
 * @param width {int} the new width.
 */
qx.Proto.setColumnWidth = function(col, width) {
  var x = this.getPaneScroller().getTablePaneModel().getX(col);
  var children = this.getChildren();
  if (children[x] != null) {
    children[x].setWidth(width);
  }
}


/**
 * Sets the column the mouse is currently over.
 *
 * @param col {int} the model index of the column the mouse is currently over or
 *    null if the mouse is over no column.
 */
qx.Proto.setMouseOverColumn = function(col) {
  if (col != this._lastMouseOverColumn) {
    var paneModel = this.getPaneScroller().getTablePaneModel();
    var children = this.getChildren();

    if (this._lastMouseOverColumn != null) {
      var widget = children[paneModel.getX(this._lastMouseOverColumn)];
      if (widget != null) {
        widget.removeState("mouseover");
      }
    }
    if (col != null) {
      children[paneModel.getX(col)].addState("mouseover");
    }

    this._lastMouseOverColumn = col;
  }
}


/**
 * Shows the feedback shown while a column is moved by the user.
 *
 * @param col {int} the model index of the column to show the move feedback for.
 * @param x {int} the x position the left side of the feeback should have
 *    (in pixels, relative to the left side of the header).
 */
qx.Proto.showColumnMoveFeedback = function(col, x) {
  var elem = this.getElement();
  if (this._moveFeedback == null) {
    var xPos = this.getPaneScroller().getTablePaneModel().getX(col);
    var cellWidget = this.getChildren()[xPos];

    // Create the feedback
    // Workaround: Since a cloned widget throws an exception when it is
    //       added to another component we have to create a new one
    //       using the renderer
    //this._moveFeedback = cellWidget.clone();
    var tableModel = this.getTable().getTableModel();
    var columnModel = this.getTable().getTableColumnModel();
    var cellInfo = { xPos:xPos, col:col, name:tableModel.getColumnName(col) }
    var cellRenderer = columnModel.getHeaderCellRenderer(col);
    this._moveFeedback = cellRenderer.createHeaderCell(cellInfo);

    // Configure the feedback
    with (this._moveFeedback) {
      setWidth(cellWidget.getBoxWidth());
      setHeight(cellWidget.getBoxHeight());
      setZIndex(1000000);
      setOpacity(0.8);
      setTop(qx.dom.Location.getClientBoxTop(elem));
    }
    this.getTopLevelWidget().add(this._moveFeedback);
  }

  this._moveFeedback.setLeft(qx.dom.Location.getClientBoxLeft(elem) + x);
}


/**
 * Hides the feedback shown while a column is moved by the user.
 */
qx.Proto.hideColumnMoveFeedback = function() {
  if (this._moveFeedback != null) {
    this.getTopLevelWidget().remove(this._moveFeedback);
    this._moveFeedback.dispose();
    this._moveFeedback = null;
  }
}


/**
 * Returns whether the column move feedback is currently shown.
 */
qx.Proto.isShowingColumnMoveFeedback = function() {
  return this._moveFeedback != null;
}


/**
 * Updates the content of the header.
 *
 * @param completeUpdate {boolean} if true a complete update is performed. On a
 *    complete update all header widgets are recreated.
 */
qx.Proto._updateContent = function(completeUpdate) {
  var tableModel = this.getTable().getTableModel();
  var columnModel = this.getTable().getTableColumnModel();
  var paneModel = this.getPaneScroller().getTablePaneModel();

  var children = this.getChildren();
  var oldColCount = children.length;
  var colCount = paneModel.getColumnCount();

  var sortedColum = tableModel.getSortColumnIndex();

  // Remove all widgets on the complete update
  if (completeUpdate) {
    this._cleanUpCells();
  }

  // Update the header
  var cellInfo = {};
  cellInfo.sortedAscending = tableModel.isSortAscending();
  for (var x = 0; x < colCount; x++) {
    var col = paneModel.getColumnAtX(x);

    var colWidth = columnModel.getColumnWidth(col);

    // TODO: Get real cell renderer
    var cellRenderer = columnModel.getHeaderCellRenderer(col);

    cellInfo.xPos = x;
    cellInfo.col = col;
    cellInfo.name = tableModel.getColumnName(col);
    cellInfo.editable = tableModel.isColumnEditable(col);
    cellInfo.sorted = (col == sortedColum);

    // Get the cached widget
    var cachedWidget = children[x];

    // Create or update the widget
    if (cachedWidget == null) {
      // We have no cached widget -> create it
      cachedWidget = cellRenderer.createHeaderCell(cellInfo);
      cachedWidget.set({ width:colWidth, height:"100%" });

      this.add(cachedWidget);
    } else {
      // This widget already created before -> recycle it
      cellRenderer.updateHeaderCell(cellInfo, cachedWidget);
    }
  }
}


/**
 * Cleans up all header cells.
 */
qx.Proto._cleanUpCells = function() {
  var children = this.getChildren();
  for (var x = children.length - 1; x >= 0; x--) {
    var cellWidget = children[x];
    //this.debug("disposed:" + cellWidget.getDisposed() + ",has parent: " + (cellWidget.getParent() != null) + ",x:"+x);
    this.remove(cellWidget);
    cellWidget.dispose();
  }
}


// overridden
qx.Proto.dispose = function() {
  if (this.getDisposed()) {
    return true;
  }

  return qx.ui.layout.HorizontalBoxLayout.prototype.dispose.call(this);
}