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

   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)

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

/**
 * A selection manager. This is a helper class that handles all selection
 * related events and updates a SelectionModel.
 * <p>
 * Widgets that support selection should use this manager. This way the only
 * thing the widget has to do is mapping mouse or key events to indexes and
 * call the corresponding handler method.
 *
 * @see SelectionModel
 */
qx.OO.defineClass("qx.ui.table.SelectionManager", qx.core.Object,
function() {
  qx.core.Object.call(this);
});


/**
 * The selection model where to set the selection changes.
 */
qx.OO.addProperty({ name:"selectionModel", type:"object", instance:"qx.ui.table.SelectionModel" });


/**
 * Handles the mouse down event.
 *
 * @param index {int} the index the mouse is pointing at.
 * @param evt {Map} the mouse event.
 */
qx.Proto.handleMouseDown = function(index, evt) {
  if (evt.isLeftButtonPressed()) {
    var selectionModel = this.getSelectionModel();
    if (!selectionModel.isSelectedIndex(index)) {
      // This index is not selected -> We react when the mouse is pressed (because of drag and drop)
      this._handleSelectEvent(index, evt);
      this._lastMouseDownHandled = true;
    } else {
      // This index is already selected -> We react when the mouse is released (because of drag and drop)
      this._lastMouseDownHandled = false;
    }
  } else if (evt.isRightButtonPressed() && evt.getModifiers() == 0) {
    var selectionModel = this.getSelectionModel();
    if (!selectionModel.isSelectedIndex(index)) {
      // This index is not selected -> Set the selection to this index
      selectionModel.setSelectionInterval(index, index);
    }
  }
}


/**
 * Handles the mouse up event.
 *
 * @param index {int} the index the mouse is pointing at.
 * @param evt {Map} the mouse event.
 */
qx.Proto.handleMouseUp = function(index, evt) {
  if (evt.isLeftButtonPressed() && !this._lastMouseDownHandled) {
    this._handleSelectEvent(index, evt);
  }
}


/**
 * Handles the mouse click event.
 *
 * @param index {int} the index the mouse is pointing at.
 * @param evt {Map} the mouse event.
 */
qx.Proto.handleClick = function(index, evt) {
}


/**
 * Handles the key down event that is used as replacement for mouse clicks
 * (Normally space).
 *
 * @param index {int} the index that is currently focused.
 * @param evt {Map} the key event.
 */
qx.Proto.handleSelectKeyDown = function(index, evt) {
  this._handleSelectEvent(index, evt);
};


/**
 * Handles a key down event that moved the focus (E.g. up, down, home, end, ...).
 *
 * @param index {int} the index that is currently focused.
 * @param evt {Map} the key event.
 */
qx.Proto.handleMoveKeyDown = function(index, evt) {
  var selectionModel = this.getSelectionModel();
  switch (evt.getModifiers()) {
    case 0:
      selectionModel.setSelectionInterval(index, index);
      break;
    case qx.event.type.DomEvent.SHIFT_MASK:
      var anchor = selectionModel.getAnchorSelectionIndex();
      if (anchor == -1) {
        selectionModel.setSelectionInterval(index, index);
      } else {
        selectionModel.setSelectionInterval(anchor, index);
      }
      break;
  }
}


/**
 * Handles a select event.
 *
 * @param index {int} the index the event is pointing at.
 * @param evt {Map} the mouse event.
 */
qx.Proto._handleSelectEvent = function(index, evt) {
  var selectionModel = this.getSelectionModel();
  if (evt.getShiftKey()) {
    var leadIndex = selectionModel.getLeadSelectionIndex();
    if (index != leadIndex || selectionModel.isSelectionEmpty()) {
      // The lead selection index was changed
      var anchorIndex = selectionModel.getAnchorSelectionIndex();
      if (anchorIndex == -1) {
          anchorIndex = index;
      }
      if (evt.isCtrlOrCommandPressed()) {
        selectionModel.addSelectionInterval(anchorIndex, index);
      } else {
        selectionModel.setSelectionInterval(anchorIndex, index);
      }
    }
  } else if (evt.isCtrlOrCommandPressed()) {
    if (selectionModel.isSelectedIndex(index)) {
      selectionModel.removeSelectionInterval(index, index);
    } else {
      selectionModel.addSelectionInterval(index, index);
    }
  } else {
    selectionModel.setSelectionInterval(index, index);
  }
}