summaryrefslogtreecommitdiff
path: root/webapps/qooxdoo-0.6.3-sdk/frontend/framework/source/class/qx/ui/table/TablePaneScroller.js
blob: d6f7773148127df6cd613c5fc017721ed47d8cb0 (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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
/* ************************************************************************

   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 a whole meta column. This includes a {@link TablePaneHeader},
 * a {@link TablePane} and the needed scroll bars. This class handles the
 * virtual scrolling and does all the mouse event handling.
 *
 * @param table {Table} the table the scroller belongs to.
 */
qx.OO.defineClass("qx.ui.table.TablePaneScroller", qx.ui.layout.VerticalBoxLayout,
function(table) {
  qx.ui.layout.VerticalBoxLayout.call(this);

  this._table = table;

  // init scrollbars
  this._verScrollBar = new qx.ui.core.ScrollBar(false);
  this._horScrollBar = new qx.ui.core.ScrollBar(true);

  var scrollBarWidth = this._verScrollBar.getPreferredBoxWidth();

  this._verScrollBar.setWidth("auto");
  this._horScrollBar.setHeight("auto");
  this._horScrollBar.setPaddingRight(scrollBarWidth);
  //this._verScrollBar.setMergeEvents(true);

  this._horScrollBar.addEventListener("changeValue", this._onScrollX, this);
  this._verScrollBar.addEventListener("changeValue", this._onScrollY, this);

  // init header
  this._header = new qx.ui.table.TablePaneHeader(this);
  this._header.set({ width:"auto", height:"auto" });

  this._headerClipper = new qx.ui.layout.CanvasLayout;
  with (this._headerClipper) {
    setDimension("1*", "auto");
    setOverflow("hidden");
    add(this._header);
  }

  this._spacer = new qx.ui.basic.Terminator;
  this._spacer.setWidth(scrollBarWidth);

  this._top = new qx.ui.layout.HorizontalBoxLayout;
  with (this._top) {
    setHeight("auto");
    add(this._headerClipper, this._spacer);
  }

  // init pane
  this._tablePane = new qx.ui.table.TablePane(this);
  this._tablePane.set({ width:"auto", height:"auto" });

  this._focusIndicator = new qx.ui.layout.HorizontalBoxLayout;
  this._focusIndicator.setAppearance("table-focus-indicator");
  this._focusIndicator.hide();

  // Workaround: If the _focusIndicator has no content if always gets a too
  //       high hight in IE.
  var dummyContent = new qx.ui.basic.Terminator;
  dummyContent.setWidth(0);
  this._focusIndicator.add(dummyContent);

  this._paneClipper = new qx.ui.layout.CanvasLayout;
  with (this._paneClipper) {
    setWidth("1*");
    setOverflow("hidden");
    add(this._tablePane, this._focusIndicator);
    addEventListener("mousewheel", this._onmousewheel, this);
  }

  // add all child widgets
  var scrollerBody = new qx.ui.layout.HorizontalBoxLayout;
  scrollerBody.setHeight("1*");
  scrollerBody.add(this._paneClipper, this._verScrollBar);

  this.add(this._top, scrollerBody, this._horScrollBar);

  // init event handlers
  this.addEventListener("mousemove", this._onmousemove, this);
  this.addEventListener("mousedown", this._onmousedown, this);
  this.addEventListener("mouseup",   this._onmouseup,   this);
  this.addEventListener("click",     this._onclick,     this);
  this.addEventListener("dblclick",  this._ondblclick,  this);
  this.addEventListener("mouseout",  this._onmouseout,  this);
});

/** Whether to show the horizontal scroll bar */
qx.OO.addProperty({ name:"horizontalScrollBarVisible", type:"boolean", defaultValue:true });

/** Whether to show the vertical scroll bar */
qx.OO.addProperty({ name:"verticalScrollBarVisible", type:"boolean", defaultValue:true });

/** The table pane model. */
qx.OO.addProperty({ name:"tablePaneModel", type:"object", instance:"qx.ui.table.TablePaneModel" });

/** The current position of the the horizontal scroll bar. */
qx.OO.addProperty({ name:"scrollX", type:"number", allowNull:false, defaultValue:0 });

/** The current position of the the vertical scroll bar. */
qx.OO.addProperty({ name:"scrollY", type:"number", allowNull:false, defaultValue:0 });

/**
 * Whether column resize should be live. If false, during resize only a line is
 * shown and the real resize happens when the user releases the mouse button.
 */
qx.OO.addProperty({ name:"liveResize", type:"boolean", defaultValue:false });

/**
 * Whether the focus should moved when the mouse is moved over a cell. If false
 * the focus is only moved on mouse clicks.
 */
qx.OO.addProperty({ name:"focusCellOnMouseMove", type:"boolean", defaultValue:false });


// property modifier
qx.Proto._modifyHorizontalScrollBarVisible = function(propValue, propOldValue, propData) {
  // Workaround: We can't use setDisplay, because the scroll bar needs its
  //       correct height in order to check its value. When using
  //       setDisplay(false) the height isn't relayouted any more
  if (propValue) {
    this._horScrollBar.setHeight("auto");
  } else {
    this._horScrollBar.setHeight(0);
  }
  this._horScrollBar.setVisibility(propValue);

  // NOTE: We have to flush the queues before updating the content so the new
  //     layout has been applied and _updateContent is able to work with
  //     correct values.
  qx.ui.core.Widget.flushGlobalQueues();
  this._updateContent();

  return true;
}


// property modifier
qx.Proto._modifyVerticalScrollBarVisible = function(propValue, propOldValue, propData) {
  // Workaround: See _modifyHorizontalScrollBarVisible
  if (propValue) {
    this._verScrollBar.setWidth("auto");
  } else {
    this._verScrollBar.setWidth(0);
  }
  this._verScrollBar.setVisibility(propValue);

  var scrollBarWidth = propValue ? this._verScrollBar.getPreferredBoxWidth() : 0;
  this._horScrollBar.setPaddingRight(scrollBarWidth);
  this._spacer.setWidth(scrollBarWidth);

  return true;
}


// property modifier
qx.Proto._modifyTablePaneModel = function(propValue, propOldValue, propData) {
  if (propOldValue != null) {
    propOldValue.removeEventListener("modelChanged", this._onPaneModelChanged, this);
  }
  propValue.addEventListener("modelChanged", this._onPaneModelChanged, this);

  return true;
}


// property modifier
qx.Proto._modifyScrollX = function(propValue, propOldValue, propData) {
  this._horScrollBar.setValue(propValue);
  return true;
}


// property modifier
qx.Proto._modifyScrollY = function(propValue, propOldValue, propData) {
  this._verScrollBar.setValue(propValue);
  return true;
}


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


/**
 * Event handler. Called when the visibility of a column has changed.
 *
 * @param evt {Map} the event.
 */
qx.Proto._onColVisibilityChanged = function(evt) {
  this._updateHorScrollBarMaximum();
  this._updateFocusIndicator();
}


/**
 * Event handler. Called when the width of a column has changed.
 *
 * @param evt {Map} the event.
 */
qx.Proto._onColWidthChanged = function(evt) {
  this._header._onColWidthChanged(evt);
  this._tablePane._onColWidthChanged(evt);

  var data = evt.getData();
  var paneModel = this.getTablePaneModel();
  var x = paneModel.getX(data.col);
  if (x != -1) {
    // The change was in this scroller
    this._updateHorScrollBarMaximum();
    this._updateFocusIndicator();
  }
}


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

  this._updateHorScrollBarMaximum();
}


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

  var rowCount = this.getTable().getTableModel().getRowCount();
  if (rowCount != this._lastRowCount) {
    this._lastRowCount = rowCount;

    this._updateVerScrollBarMaximum();
    if (this.getFocusedRow() >= rowCount) {
      if (rowCount == 0) {
        this.setFocusedCell(null, null);
      } else {
        this.setFocusedCell(this.getFocusedColumn(), rowCount - 1);
      }
    }
  }
}


/**
 * Event handler. Called when the selection has changed.
 *
 * @param evt {Map} the event.
 */
qx.Proto._onSelectionChanged = function(evt) {
  this._tablePane._onSelectionChanged(evt);
};


/**
 * Event handler. Called when the table gets or looses the focus.
 */
qx.Proto._onFocusChanged = function(evt) {
  this._focusIndicator.setState("tableHasFocus", this.getTable().getFocused());

  this._tablePane._onFocusChanged(evt);
};


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


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


/**
 * Updates the maximum of the horizontal scroll bar, so it corresponds to the
 * total width of the columns in the table pane.
 */
qx.Proto._updateHorScrollBarMaximum = function() {
  this._horScrollBar.setMaximum(this.getTablePaneModel().getTotalWidth());
}


/**
 * Updates the maximum of the vertical scroll bar, so it corresponds to the
 * number of rows in the table.
 */
qx.Proto._updateVerScrollBarMaximum = function() {
  var rowCount = this.getTable().getTableModel().getRowCount();
  var rowHeight = this.getTable().getRowHeight();

  if (this.getTable().getKeepFirstVisibleRowComplete()) {
    this._verScrollBar.setMaximum((rowCount + 1) * rowHeight);
  } else {
    this._verScrollBar.setMaximum(rowCount * rowHeight);
  }
}


/**
 * Event handler. Called when the table property "keepFirstVisibleRowComplete"
 * changed.
 */
qx.Proto._onKeepFirstVisibleRowCompleteChanged = function() {
  this._updateVerScrollBarMaximum();
  this._updateContent();
};


// overridden
qx.Proto._changeInnerHeight = function(newValue, oldValue) {
  // The height has changed -> Update content
  this._postponedUpdateContent();

  return qx.ui.layout.VerticalBoxLayout.prototype._changeInnerHeight.call(this, newValue, oldValue);
}


// overridden
qx.Proto._afterAppear = function() {
  qx.ui.layout.VerticalBoxLayout.prototype._afterAppear.call(this);

  var self = this;
  this.getElement().onselectstart = qx.util.Return.returnFalse;

  this._updateContent();
  this._header._updateContent();
  this._updateHorScrollBarMaximum();
  this._updateVerScrollBarMaximum();
}


/**
 * Event handler. Called when the horizontal scroll bar moved.
 *
 * @param evt {Map} the event.
 */
qx.Proto._onScrollX = function(evt) {
  // Workaround: See _updateContent
  this._header.setLeft(-evt.getData());

  this._paneClipper.setScrollLeft(evt.getData());
  this.setScrollX(evt.getData());
}


/**
 * Event handler. Called when the vertical scroll bar moved.
 *
 * @param evt {Map} the event.
 */
qx.Proto._onScrollY = function(evt) {
  this._postponedUpdateContent();
  this.setScrollY(evt.getData());
}


/**
 * Event handler. Called when the user moved the mouse wheel.
 *
 * @param evt {Map} the event.
 */
qx.Proto._onmousewheel = function(evt) {
  this._verScrollBar.setValue(this._verScrollBar.getValue()
    - evt.getWheelDelta() * this.getTable().getRowHeight());

  // Update the focus
  if (this._lastMousePageX && this.getFocusCellOnMouseMove()) {
    this._focusCellAtPagePos(this._lastMousePageX, this._lastMousePageY);
  }
}


/**
 * Event handler. Called when the user moved the mouse.
 *
 * @param evt {Map} the event.
 */
qx.Proto._onmousemove = function(evt) {
  var tableModel = this.getTable().getTableModel();
  var columnModel = this.getTable().getTableColumnModel();

  var useResizeCursor = false;
  var mouseOverColumn = null;

  var pageX = evt.getPageX();
  var pageY = evt.getPageY();

  // Workaround: In onmousewheel the event has wrong coordinates for pageX
  //       and pageY. So we remember the last move event.
  this._lastMousePageX = pageX;
  this._lastMousePageY = pageY;

  if (this._resizeColumn != null) {
    // We are currently resizing -> Update the position
    var minColumnWidth = qx.ui.table.TablePaneScroller.MIN_COLUMN_WIDTH;
    var newWidth = Math.max(minColumnWidth, this._lastResizeWidth + pageX - this._lastResizeMousePageX);

    if (this.getLiveResize()) {
      columnModel.setColumnWidth(this._resizeColumn, newWidth);
    } else {
      this._header.setColumnWidth(this._resizeColumn, newWidth);

      var paneModel = this.getTablePaneModel();
      this._showResizeLine(paneModel.getColumnLeft(this._resizeColumn) + newWidth);
    }

    useResizeCursor = true;
    this._lastResizeMousePageX += newWidth - this._lastResizeWidth;
    this._lastResizeWidth = newWidth;
  } else if (this._moveColumn != null) {
    // We are moving a column

    // Check whether we moved outside the click tolerance so we can start
    // showing the column move feedback
    // (showing the column move feedback prevents the onclick event)
    var clickTolerance = qx.ui.table.TablePaneScroller.CLICK_TOLERANCE;
    if (this._header.isShowingColumnMoveFeedback()
      || pageX > this._lastMoveMousePageX + clickTolerance
      || pageX < this._lastMoveMousePageX - clickTolerance)
    {
      this._lastMoveColPos += pageX - this._lastMoveMousePageX;

      this._header.showColumnMoveFeedback(this._moveColumn, this._lastMoveColPos);

      // Get the responsible scroller
      var targetScroller = this._table.getTablePaneScrollerAtPageX(pageX);
      if (this._lastMoveTargetScroller && this._lastMoveTargetScroller != targetScroller) {
        this._lastMoveTargetScroller.hideColumnMoveFeedback();
      }
      if (targetScroller != null) {
        this._lastMoveTargetX = targetScroller.showColumnMoveFeedback(pageX);
      } else {
        this._lastMoveTargetX = null;
      }

      this._lastMoveTargetScroller = targetScroller;
      this._lastMoveMousePageX = pageX;
    }
  } else {
    // This is a normal mouse move
    var row = this._getRowForPagePos(pageX, pageY);
    if (row == -1) {
      // The mouse is over the header
      var resizeCol = this._getResizeColumnForPageX(pageX);
      if (resizeCol != -1) {
        // The mouse is over a resize region -> Show the right cursor
        useResizeCursor = true;
      } else {
        var col = this._getColumnForPageX(pageX);
        if (col != null && tableModel.isColumnSortable(col)) {
          mouseOverColumn = col;
        }
      }
    } else if (row != null) {
      // The mouse is over the data -> update the focus
      if (this.getFocusCellOnMouseMove()) {
        this._focusCellAtPagePos(pageX, pageY);
      }
    }
  }

  // Workaround: Setting the cursor to the right widget doesn't work
  //this._header.setCursor(useResizeCursor ? "e-resize" : null);
  this.getTopLevelWidget().setGlobalCursor(useResizeCursor ? qx.ui.table.TablePaneScroller.CURSOR_RESIZE_HORIZONTAL : null);

  this._header.setMouseOverColumn(mouseOverColumn);
}


/**
 * Event handler. Called when the user pressed a mouse button.
 *
 * @param evt {Map} the event.
 */
qx.Proto._onmousedown = function(evt) {
  var tableModel = this.getTable().getTableModel();
  var columnModel = this.getTable().getTableColumnModel();

  var pageX = evt.getPageX();
  var pageY = evt.getPageY();
  var row = this._getRowForPagePos(pageX, pageY);
  if (row == -1) {
    // mouse is in header
    var resizeCol = this._getResizeColumnForPageX(pageX);
    if (resizeCol != -1) {
      // The mouse is over a resize region -> Start resizing
      this._resizeColumn = resizeCol;
      this._lastResizeMousePageX = pageX;
      this._lastResizeWidth = columnModel.getColumnWidth(this._resizeColumn);
      this.setCapture(true);
    } else {
      // The mouse is not in a resize region
      var col = this._getColumnForPageX(pageX);
      if (col != null) {
        // Prepare column moving
        this._moveColumn = col;
        this._lastMoveMousePageX = pageX;
        this._lastMoveColPos = this.getTablePaneModel().getColumnLeft(col);
        this.setCapture(true);
      }
    }
  } else if (row != null) {
    // The mouse is over the data -> update the focus
    if (! this.getFocusCellOnMouseMove()) {
      this._focusCellAtPagePos(pageX, pageY);
    }

    this.getTable()._getSelectionManager().handleMouseDown(row, evt);
  }
}


/**
 * Event handler. Called when the user released a mouse button.
 *
 * @param evt {Map} the event.
 */
qx.Proto._onmouseup = function(evt) {
  var columnModel = this.getTable().getTableColumnModel();
  var paneModel = this.getTablePaneModel();

  if (this._resizeColumn != null) {
    // We are currently resizing -> Finish resizing
    if (! this.getLiveResize()) {
      this._hideResizeLine();
      columnModel.setColumnWidth(this._resizeColumn, this._lastResizeWidth);
    }

    this._resizeColumn = null;
    this.setCapture(false);

    this.getTopLevelWidget().setGlobalCursor(null);
  } else if (this._moveColumn != null) {
    // We are moving a column -> Drop the column
    this._header.hideColumnMoveFeedback();
    if (this._lastMoveTargetScroller) {
      this._lastMoveTargetScroller.hideColumnMoveFeedback();
    }

    if (this._lastMoveTargetX != null) {
      var fromVisXPos = paneModel.getFirstColumnX() + paneModel.getX(this._moveColumn);
      var toVisXPos = this._lastMoveTargetX;
      if (toVisXPos != fromVisXPos && toVisXPos != fromVisXPos + 1) {
        // The column was really moved to another position
        // (and not moved before or after itself, which is a noop)

        // Translate visible positions to overall positions
        var fromCol = columnModel.getVisibleColumnAtX(fromVisXPos);
        var toCol   = columnModel.getVisibleColumnAtX(toVisXPos);
        var fromOverXPos = columnModel.getOverallX(fromCol);
        var toOverXPos = (toCol != null) ? columnModel.getOverallX(toCol) : columnModel.getOverallColumnCount();

        if (toOverXPos > fromOverXPos) {
          // Don't count the column itself
          toOverXPos--;
        }

        // Move the column
        columnModel.moveColumn(fromOverXPos, toOverXPos);
      }
    }

    this._moveColumn = null;
    this._lastMoveTargetX = null;
    this.setCapture(false);
  } else {
    // This is a normal mouse up
    var row = this._getRowForPagePos(evt.getPageX(), evt.getPageY());
    if (row != -1 && row != null) {
      this.getTable()._getSelectionManager().handleMouseUp(row, evt);
    }
  }
}


/**
 * Event handler. Called when the user clicked a mouse button.
 *
 * @param evt {Map} the event.
 */
qx.Proto._onclick = function(evt) {
  var tableModel = this.getTable().getTableModel();

  var pageX = evt.getPageX();
  var pageY = evt.getPageY();
  var row = this._getRowForPagePos(pageX, pageY);
  if (row == -1) {
    // mouse is in header
    var resizeCol = this._getResizeColumnForPageX(pageX);
    if (resizeCol == -1) {
      // mouse is not in a resize region
      var col = this._getColumnForPageX(pageX);
      if (col != null && tableModel.isColumnSortable(col)) {
        // Sort that column
        var sortCol = tableModel.getSortColumnIndex();
        var ascending = (col != sortCol) ? true : !tableModel.isSortAscending();

        tableModel.sortByColumn(col, ascending);
        this.getTable().getSelectionModel().clearSelection();
      }
    }
  } else if (row != null) {
    this.getTable()._getSelectionManager().handleClick(row, evt);
  }
}


/**
 * Event handler. Called when the user double clicked a mouse button.
 *
 * @param evt {Map} the event.
 */
qx.Proto._ondblclick = function(evt) {
  if (! this.isEditing()) {
    this._focusCellAtPagePos(evt.getPageX(), evt.getPageY());
    this.startEditing();
  }
}


/**
 * Event handler. Called when the mouse moved out.
 *
 * @param evt {Map} the event.
 */
qx.Proto._onmouseout = function(evt) {
  /*
  // Workaround: See _onmousemove
  this._lastMousePageX = null;
  this._lastMousePageY = null;
  */

  // Reset the resize cursor when the mouse leaves the header
  // If currently a column is resized then do nothing
  // (the cursor will be reset on mouseup)
  if (this._resizeColumn == null) {
    this.getTopLevelWidget().setGlobalCursor(null);
  }

  this._header.setMouseOverColumn(null);
}


/**
 * Shows the resize line.
 *
 * @param x {int} the position where to show the line (in pixels, relative to
 *    the left side of the pane).
 */
qx.Proto._showResizeLine = function(x) {
  var resizeLine = this._resizeLine;
  if (resizeLine == null) {
    resizeLine = new qx.ui.basic.Terminator;
    resizeLine.setBackgroundColor("#D6D5D9");
    resizeLine.setWidth(3);
    this._paneClipper.add(resizeLine);
    qx.ui.core.Widget.flushGlobalQueues();

    this._resizeLine = resizeLine;
  }

  resizeLine._applyRuntimeLeft(x - 2); // -1 for the width
  resizeLine._applyRuntimeHeight(this._paneClipper.getBoxHeight() + this._paneClipper.getScrollTop());

  this._resizeLine.removeStyleProperty("visibility");
}


/**
 * Hides the resize line.
 */
qx.Proto._hideResizeLine = function() {
  this._resizeLine.setStyleProperty("visibility", "hidden");
}


/**
 * Shows the feedback shown while a column is moved by the user.
 *
 * @param pageX {int} the x position of the mouse in the page (in pixels).
 * @return {int} the visible x position of the column in the whole table.
 */
qx.Proto.showColumnMoveFeedback = function(pageX) {
  var paneModel = this.getTablePaneModel();
  var columnModel = this.getTable().getTableColumnModel();
  var paneLeftX = qx.dom.Location.getClientBoxLeft(this._tablePane.getElement());
  var colCount = paneModel.getColumnCount();

  var targetXPos = 0;
  var targetX = 0;
  var currX = paneLeftX;
  for (var xPos = 0; xPos < colCount; xPos++) {
    var col = paneModel.getColumnAtX(xPos);
    var colWidth = columnModel.getColumnWidth(col);

    if (pageX < currX + colWidth / 2) {
      break;
    }

    currX += colWidth;
    targetXPos = xPos + 1;
    targetX = currX - paneLeftX;
  }

  // Ensure targetX is visible
  var clipperLeftX = qx.dom.Location.getClientBoxLeft(this._paneClipper.getElement());
  var clipperWidth = this._paneClipper.getBoxWidth();
  var scrollX = clipperLeftX - paneLeftX;
  // NOTE: +2/-1 because of feedback width
  targetX = qx.lang.Number.limit(targetX, scrollX + 2, scrollX + clipperWidth - 1);

  this._showResizeLine(targetX);

  // Return the overall target x position
  return paneModel.getFirstColumnX() + targetXPos;
}


/**
 * Hides the feedback shown while a column is moved by the user.
 */
qx.Proto.hideColumnMoveFeedback = function() {
  this._hideResizeLine();
}


/**
 * Sets the focus to the cell that's located at the page position
 * <code>pageX</code>/<code>pageY</code>. If there is no cell at that position,
 * nothing happens.
 *
 * @param pageX {int} the x position in the page (in pixels).
 * @param pageY {int} the y position in the page (in pixels).
 */
qx.Proto._focusCellAtPagePos = function(pageX, pageY) {
  var row = this._getRowForPagePos(pageX, pageY);
  if (row != -1 && row != null) {
    // The mouse is over the data -> update the focus
    var col = this._getColumnForPageX(pageX);
    if (col != null) {
      this._table.setFocusedCell(col, row);
    }
  }
}


/**
 * Sets the currently focused cell.
 *
 * @param col {int} the model index of the focused cell's column.
 * @param row {int} the model index of the focused cell's row.
 */
qx.Proto.setFocusedCell = function(col, row) {
  if (!this.isEditing()) {
    this._tablePane.setFocusedCell(col, row, this._updateContentPlanned);

    this._focusedCol = col;
    this._focusedRow = row;

    // Move the focus indicator
    if (! this._updateContentPlanned) {
      this._updateFocusIndicator();
    }
  }
}


/**
 * Returns the column of currently focused cell.
 *
 * @return {int} the model index of the focused cell's column.
 */
qx.Proto.getFocusedColumn = function() {
  return this._focusedCol;
};


/**
 * Returns the row of currently focused cell.
 *
 * @return {int} the model index of the focused cell's column.
 */
qx.Proto.getFocusedRow = function() {
  return this._focusedRow;
};


/**
 * Scrolls a cell visible.
 *
 * @param col {int} the model index of the column the cell belongs to.
 * @param row {int} the model index of the row the cell belongs to.
 */
qx.Proto.scrollCellVisible = function(col, row) {
  var paneModel = this.getTablePaneModel();
  var xPos = paneModel.getX(col);

  if (xPos != -1) {
    var columnModel = this.getTable().getTableColumnModel();

    var colLeft = paneModel.getColumnLeft(col);
    var colWidth = columnModel.getColumnWidth(col);
    var rowHeight = this.getTable().getRowHeight();
    var rowTop = row * rowHeight;

    var scrollX = this.getScrollX();
    var scrollY = this.getScrollY();
    var viewWidth = this._paneClipper.getBoxWidth();
    var viewHeight = this._paneClipper.getBoxHeight();

    // NOTE: We don't use qx.lang.Number.limit, because min should win if max < min
    var minScrollX = Math.min(colLeft, colLeft + colWidth - viewWidth);
    var maxScrollX = colLeft;
    this.setScrollX(Math.max(minScrollX, Math.min(maxScrollX, scrollX)));

    var minScrollY = rowTop + rowHeight - viewHeight;
    if (this.getTable().getKeepFirstVisibleRowComplete()) {
      minScrollY += rowHeight - 1;
    }
    var maxScrollY = rowTop;
    this.setScrollY(Math.max(minScrollY, Math.min(maxScrollY, scrollY)));
  }
}


/**
 * Returns whether currently a cell is editing.
 *
 * @return whether currently a cell is editing.
 */
qx.Proto.isEditing = function() {
  return this._cellEditor != null;
}


/**
 * Starts editing the currently focused cell. Does nothing if already editing
 * or if the column is not editable.
 *
 * @return {boolean} whether editing was started
 */
qx.Proto.startEditing = function() {
  var tableModel = this.getTable().getTableModel();
  var col   = this._focusedCol;

  if (!this.isEditing() && (col != null) && tableModel.isColumnEditable(col)) {
    var row   = this._focusedRow;
    var xPos  = this.getTablePaneModel().getX(col);
    var value = tableModel.getValue(col, row);

    this._cellEditorFactory = this.getTable().getTableColumnModel().getCellEditorFactory(col);
    var cellInfo = { col:col, row:row, xPos:xPos, value:value }
    this._cellEditor = this._cellEditorFactory.createCellEditor(cellInfo);
    this._cellEditor.set({ width:"100%", height:"100%" });

    this._focusIndicator.add(this._cellEditor);
    this._focusIndicator.addState("editing");

    this._cellEditor.addEventListener("changeFocused", this._onCellEditorFocusChanged, this);

    // Workaround: Calling focus() directly has no effect
    var editor = this._cellEditor;
    window.setTimeout(function() {
      editor.focus();
    }, 0);

    return true;
  }

  return false;
}


/**
 * Stops editing and writes the editor's value to the model.
 */
qx.Proto.stopEditing = function() {
  this.flushEditor();
  this.cancelEditing();
}


/**
 * Writes the editor's value to the model.
 */
qx.Proto.flushEditor = function() {
  if (this.isEditing()) {
    var value = this._cellEditorFactory.getCellEditorValue(this._cellEditor);
    this.getTable().getTableModel().setValue(this._focusedCol, this._focusedRow, value);

    this._table.focus();
  }
}


/**
 * Stops editing without writing the editor's value to the model.
 */
qx.Proto.cancelEditing = function() {
  if (this.isEditing()) {
    this._focusIndicator.remove(this._cellEditor);
    this._focusIndicator.removeState("editing");
    this._cellEditor.dispose();

    this._cellEditor.removeEventListener("changeFocused", this._onCellEditorFocusChanged, this);
    this._cellEditor = null;
    this._cellEditorFactory = null;
  }
}


/**
 * Event handler. Called when the focused state of the cell editor changed.
 *
 * @param evt {Map} the event.
 */
qx.Proto._onCellEditorFocusChanged = function(evt) {
  if (!this._cellEditor.getFocused()) {
    this.stopEditing();
  }
}


/**
 * Returns the model index of the column the mouse is over or null if the mouse
 * is not over a column.
 *
 * @param pageX {int} the x position of the mouse in the page (in pixels).
 * @return {int} the model index of the column the mouse is over.
 */
qx.Proto._getColumnForPageX = function(pageX) {
  var headerLeftX = qx.dom.Location.getClientBoxLeft(this._header.getElement());

  var columnModel = this.getTable().getTableColumnModel();
  var paneModel = this.getTablePaneModel();
  var colCount = paneModel.getColumnCount();
  var currX = headerLeftX;
  for (var x = 0; x < colCount; x++) {
    var col = paneModel.getColumnAtX(x);
    var colWidth = columnModel.getColumnWidth(col);
    currX += colWidth;

    if (pageX < currX) {
      return col;
    }
  }

  return null;
}


/**
 * Returns the model index of the column that should be resized when dragging
 * starts here. Returns -1 if the mouse is in no resize region of any column.
 *
 * @param pageX {int} the x position of the mouse in the page (in pixels).
 * @return {int} the column index.
 */
qx.Proto._getResizeColumnForPageX = function(pageX) {
  var headerLeftX = qx.dom.Location.getClientBoxLeft(this._header.getElement());

  var columnModel = this.getTable().getTableColumnModel();
  var paneModel = this.getTablePaneModel();
  var colCount = paneModel.getColumnCount();
  var currX = headerLeftX;
  var regionRadius = qx.ui.table.TablePaneScroller.RESIZE_REGION_RADIUS;
  for (var x = 0; x < colCount; x++) {
    var col = paneModel.getColumnAtX(x);
    var colWidth = columnModel.getColumnWidth(col);
    currX += colWidth;

    if (pageX >= (currX - regionRadius) && pageX <= (currX + regionRadius)) {
      return col;
    }
  }

  return -1;
}


/**
 * Returns the model index of the row the mouse is currently over. Returns -1 if
 * the mouse is over the header. Returns null if the mouse is not over any
 * column.
 *
 * @param pageX {int} the mouse x position in the page.
 * @param pageY {int} the mouse y position in the page.
 * @return {int} the model index of the row the mouse is currently over.
 */
qx.Proto._getRowForPagePos = function(pageX, pageY) {
  var paneClipperElem = this._paneClipper.getElement();
  var paneClipperLeftX = qx.dom.Location.getClientBoxLeft(paneClipperElem);
  var paneClipperRightX = qx.dom.Location.getClientBoxRight(paneClipperElem);
  if (pageX < paneClipperLeftX || pageX > paneClipperRightX) {
    // There was no cell or header cell hit
    return null;
  }

  var paneClipperTopY = qx.dom.Location.getClientBoxTop(paneClipperElem);
  var paneClipperBottomY = qx.dom.Location.getClientBoxBottom(paneClipperElem);
  if (pageY >= paneClipperTopY && pageY <= paneClipperBottomY) {
    // This event is in the pane -> Get the row
    var rowHeight = this.getTable().getRowHeight();

    var scrollY = this._verScrollBar.getValue();
    if (this.getTable().getKeepFirstVisibleRowComplete()) {
      scrollY = Math.floor(scrollY / rowHeight) * rowHeight;
    }

    var tableY = scrollY + pageY - paneClipperTopY;
    var row = Math.floor(tableY / rowHeight);

    var rowCount = this.getTable().getTableModel().getRowCount();
    return (row < rowCount) ? row : null;
  }

  var headerElem = this._headerClipper.getElement();
  if (pageY >= qx.dom.Location.getClientBoxTop(headerElem)
    && pageY <= qx.dom.Location.getClientBoxBottom(headerElem)
    && pageX <= qx.dom.Location.getClientBoxRight(headerElem))
  {
    // This event is in the pane -> Return -1 for the header
    return -1;
  }

  return null;
}


/**
 * Sets the widget that should be shown in the top right corner.
 * <p>
 * The widget will not be disposed, when this table scroller is disposed. So the
 * caller has to dispose it.
 *
 * @param widget {qx.ui.core.Widget} The widget to set. May be null.
 */
qx.Proto.setTopRightWidget = function(widget) {
  var oldWidget = this._topRightWidget;
  if (oldWidget != null) {
    this._top.remove(oldWidget);
  }

  if (widget != null) {
    this._top.remove(this._spacer);
    this._top.add(widget);
  } else if (oldWidget != null) {
    this._top.add(this._spacer);
  }

  this._topRightWidget = widget;
}


/**
 * Returns the header.
 *
 * @return {TablePaneHeader} the header.
 */
qx.Proto.getHeader = function() {
  return this._header;
}


/**
 * Returns the table pane.
 *
 * @return {TablePane} the table pane.
 */
qx.Proto.getTablePane = function() {
  return this._tablePane;
}


/**
 * Returns which scrollbars are needed.
 *
 * @param forceHorizontal {boolean ? false} Whether to show the horizontal
 *    scrollbar always.
 * @param preventVertical {boolean ? false} Whether tp show the vertical scrollbar
 *    never.
 * @return {int} which scrollbars are needed. This may be any combination of
 *    {@link #HORIZONTAL_SCROLLBAR} or {@link #VERTICAL_SCROLLBAR}
 *    (combined by OR).
 */
qx.Proto.getNeededScrollBars = function(forceHorizontal, preventVertical) {
  var barWidth = this._verScrollBar.getPreferredBoxWidth();

  // Get the width and height of the view (without scroll bars)
  var viewWidth = this._paneClipper.getInnerWidth();
  if (this.getVerticalScrollBarVisible()) {
    viewWidth += barWidth;
  }
  var viewHeight = this._paneClipper.getInnerHeight();
  if (this.getHorizontalScrollBarVisible()) {
    viewHeight += barWidth;
  }

  // Get the (virtual) width and height of the pane
  var paneWidth = this.getTablePaneModel().getTotalWidth();
  var paneHeight = this.getTable().getRowHeight() * this.getTable().getTableModel().getRowCount();

  // Check which scrollbars are needed
  var horNeeded = false;
  var verNeeded = false;
  if (paneWidth > viewWidth) {
    horNeeded = true;
    if (paneHeight > viewHeight - barWidth) {
      verNeeded = true;
    }
  } else if (paneHeight > viewHeight) {
    verNeeded = true;
    if (!preventVertical && (paneWidth > viewWidth - barWidth)) {
      horNeeded = true;
    }
  }

  // Create the mask
  var horBar = qx.ui.table.TablePaneScroller.HORIZONTAL_SCROLLBAR;
  var verBar = qx.ui.table.TablePaneScroller.VERTICAL_SCROLLBAR;
  return ((forceHorizontal || horNeeded) ? horBar : 0)
     | ((preventVertical || !verNeeded) ? 0 : verBar);
}


/**
 * Does a postponed update of the content.
 *
 * @see #_updateContent
 */
qx.Proto._postponedUpdateContent = function() {
  if (! this._updateContentPlanned) {
    var self = this;
    window.setTimeout(function() {
      self._updateContent();
      self._updateContentPlanned = false;
      qx.ui.core.Widget.flushGlobalQueues();
    }, 0);
    this._updateContentPlanned = true;
  }
}


/**
 * Updates the content. Sets the right section the table pane should show and
 * does the scrolling.
 */
qx.Proto._updateContent = function() {
  var paneHeight = this._paneClipper.getInnerHeight();
  var scrollX = this._horScrollBar.getValue();
  var scrollY = this._verScrollBar.getValue();
  var rowHeight = this.getTable().getRowHeight();

  var firstRow = Math.floor(scrollY / rowHeight);
  var oldFirstRow = this._tablePane.getFirstVisibleRow();
  this._tablePane.setFirstVisibleRow(firstRow);

  var rowCount = Math.ceil(paneHeight / rowHeight);
  var paneOffset = 0;
  if (! this.getTable().getKeepFirstVisibleRowComplete()) {
    // NOTE: We don't consider paneOffset, because this may cause alternating
    //       adding and deleting of one row when scolling. Instead we add one row
    //       in every case.
    rowCount++;
    paneOffset = scrollY % rowHeight;
  }
  this._tablePane.setVisibleRowCount(rowCount);

  if (firstRow != oldFirstRow) {
    this._updateFocusIndicator();
  }

  // Workaround: We can't use scrollLeft for the header because IE
  //       automatically scrolls the header back, when a column is
  //       resized.
  this._header.setLeft(-scrollX);
  this._paneClipper.setScrollLeft(scrollX);
  this._paneClipper.setScrollTop(paneOffset);

  //this.debug("paneHeight:"+paneHeight+",rowHeight:"+rowHeight+",firstRow:"+firstRow+",rowCount:"+rowCount+",paneOffset:"+paneOffset);
}


/**
 * Updates the location and the visibility of the focus indicator.
 */
qx.Proto._updateFocusIndicator = function() {
  if (this._focusedCol == null) {
    this._focusIndicator.hide();
  } else {
    var xPos = this.getTablePaneModel().getX(this._focusedCol);
    if (xPos == -1) {
      this._focusIndicator.hide();
    } else {
      var columnModel = this.getTable().getTableColumnModel();
      var paneModel = this.getTablePaneModel();

      var firstRow = this._tablePane.getFirstVisibleRow();
      var rowHeight = this.getTable().getRowHeight();

      this._focusIndicator.setHeight(rowHeight + 3);
      this._focusIndicator.setWidth(columnModel.getColumnWidth(this._focusedCol) + 3);
      this._focusIndicator.setTop((this._focusedRow - firstRow) * rowHeight - 2);
      this._focusIndicator.setLeft(paneModel.getColumnLeft(this._focusedCol) - 2);

      this._focusIndicator.show();
    }
  }
}


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

  if (this.getElement() != null) {
    this.getElement().onselectstart = null;
  }

  this._verScrollBar.dispose();
  this._horScrollBar.dispose();
  this._header.dispose();
  this._headerClipper.dispose();
  this._spacer.dispose();
  this._top.dispose();
  this._tablePane.dispose();
  this._paneClipper.dispose();

  if (this._resizeLine != null) {
    this._resizeLine.dispose();
  }

  this.removeEventListener("mousemove", this._onmousemove, this);
  this.removeEventListener("mousedown", this._onmousedown, this);
  this.removeEventListener("mouseup", this._onmouseup, this);
  this.removeEventListener("click", this._onclick, this);
  this.removeEventListener("dblclick", this._ondblclick, this);
  this.removeEventListener("mouseout", this._onmouseout, this);

  var tablePaneModel = this.getTablePaneModel();
  if (tablePaneModel != null) {
    tablePaneModel.removeEventListener("modelChanged", this._onPaneModelChanged, this);
  }

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


/** {int} The minimum width a colum could get in pixels. */
qx.Class.MIN_COLUMN_WIDTH = 10;

/** {int} The radius of the resize region in pixels. */
qx.Class.RESIZE_REGION_RADIUS = 5;

/**
 * (int) The number of pixels the mouse may move between mouse down and mouse up
 * in order to count as a click.
 */
qx.Class.CLICK_TOLERANCE = 5;

/**
 * (int) The mask for the horizontal scroll bar.
 * May be combined with {@link #VERTICAL_SCROLLBAR}.
 *
 * @see #getNeededScrollBars
 */
qx.Class.HORIZONTAL_SCROLLBAR = 1;

/**
 * (int) The mask for the vertical scroll bar.
 * May be combined with {@link #HORIZONTAL_SCROLLBAR}.
 *
 * @see #getNeededScrollBars
 */
qx.Class.VERTICAL_SCROLLBAR = 2;

/**
 * (string) The correct value for the CSS style attribute "cursor" for the
 * horizontal resize cursor.
 */
qx.Class.CURSOR_RESIZE_HORIZONTAL = (qx.sys.Client.getInstance().isGecko() && (qx.sys.Client.getInstance().getMajor() > 1 || qx.sys.Client.getInstance().getMinor() >= 8)) ? "ew-resize" : "e-resize";