|
Lines 22-28
Link Here
|
| 22 |
import org.eclipse.swt.events.MouseListener; |
22 |
import org.eclipse.swt.events.MouseListener; |
| 23 |
import org.eclipse.swt.events.TraverseEvent; |
23 |
import org.eclipse.swt.events.TraverseEvent; |
| 24 |
import org.eclipse.swt.events.TraverseListener; |
24 |
import org.eclipse.swt.events.TraverseListener; |
| 25 |
import org.eclipse.swt.graphics.Rectangle; |
|
|
| 26 |
import org.eclipse.swt.widgets.Control; |
25 |
import org.eclipse.swt.widgets.Control; |
| 27 |
import org.eclipse.swt.widgets.Display; |
26 |
import org.eclipse.swt.widgets.Display; |
| 28 |
import org.eclipse.swt.widgets.Item; |
27 |
import org.eclipse.swt.widgets.Item; |
|
Lines 46-66
Link Here
|
| 46 |
|
45 |
|
| 47 |
private String[] columnProperties; |
46 |
private String[] columnProperties; |
| 48 |
|
47 |
|
| 49 |
private int columnNumber; |
|
|
| 50 |
|
| 51 |
private ICellEditorListener cellEditorListener; |
48 |
private ICellEditorListener cellEditorListener; |
| 52 |
|
49 |
|
| 53 |
private FocusListener focusListener; |
50 |
private FocusListener focusListener; |
| 54 |
|
51 |
|
| 55 |
private MouseListener mouseListener; |
52 |
private MouseListener mouseListener; |
| 56 |
|
53 |
|
| 57 |
private int doubleClickExpirationTime; |
|
|
| 58 |
|
| 59 |
private ColumnViewer viewer; |
54 |
private ColumnViewer viewer; |
| 60 |
|
55 |
|
| 61 |
private Item item; |
|
|
| 62 |
|
| 63 |
private TraverseListener tabeditingListener; |
56 |
private TraverseListener tabeditingListener; |
|
|
57 |
|
| 58 |
private int activationTime; |
| 59 |
|
| 60 |
private ViewerCell cell; |
| 64 |
|
61 |
|
| 65 |
/** |
62 |
/** |
| 66 |
* Create a new editor implementation for the viewer |
63 |
* Create a new editor implementation for the viewer |
|
Lines 92-99
Link Here
|
| 92 |
|
89 |
|
| 93 |
void activateCellEditor() { |
90 |
void activateCellEditor() { |
| 94 |
|
91 |
|
| 95 |
ViewerColumn part = viewer.getViewerColumn(columnNumber); |
92 |
ViewerColumn part = viewer.getViewerColumn(cell.getColumnIndex()); |
| 96 |
Object element = item.getData(); |
93 |
Object element = cell.getElement(); |
| 97 |
|
94 |
|
| 98 |
if (part != null && part.getEditingSupport() != null |
95 |
if (part != null && part.getEditingSupport() != null |
| 99 |
&& part.getEditingSupport().canEdit(element)) { |
96 |
&& part.getEditingSupport().canEdit(element)) { |
|
Lines 113-119
Link Here
|
| 113 |
return; |
110 |
return; |
| 114 |
} |
111 |
} |
| 115 |
setLayoutData(cellEditor.getLayoutData()); |
112 |
setLayoutData(cellEditor.getLayoutData()); |
| 116 |
setEditor(control, item, columnNumber); |
113 |
setEditor(control, cell); |
| 117 |
cellEditor.setFocus(); |
114 |
cellEditor.setFocus(); |
| 118 |
if (focusListener == null) { |
115 |
if (focusListener == null) { |
| 119 |
focusListener = new FocusAdapter() { |
116 |
focusListener = new FocusAdapter() { |
|
Lines 128-134
Link Here
|
| 128 |
public void mouseDown(MouseEvent e) { |
125 |
public void mouseDown(MouseEvent e) { |
| 129 |
// time wrap? |
126 |
// time wrap? |
| 130 |
// check for expiration of doubleClickTime |
127 |
// check for expiration of doubleClickTime |
| 131 |
if (e.time <= doubleClickExpirationTime) { |
128 |
if (e.time <= activationTime) { |
| 132 |
control.removeMouseListener(mouseListener); |
129 |
control.removeMouseListener(mouseListener); |
| 133 |
cancelEditing(); |
130 |
cancelEditing(); |
| 134 |
handleDoubleClickEvent(); |
131 |
handleDoubleClickEvent(); |
|
Lines 143-153
Link Here
|
| 143 |
tabeditingListener = new TraverseListener() { |
140 |
tabeditingListener = new TraverseListener() { |
| 144 |
|
141 |
|
| 145 |
public void keyTraversed(TraverseEvent e) { |
142 |
public void keyTraversed(TraverseEvent e) { |
| 146 |
ViewerColumn col = viewer.getViewerColumn(columnNumber); |
143 |
ViewerColumn col = viewer.getViewerColumn(cell.getColumnIndex()); |
| 147 |
if ( col != null && col.getEditingSupport().isTabingSupported() ) { |
144 |
if ( col != null && col.getEditingSupport().isTabingSupported() ) { |
| 148 |
col.getEditingSupport().processTraversEvent( |
145 |
col.getEditingSupport().processTraversEvent( |
| 149 |
columnNumber, |
146 |
cell.getColumnIndex(), |
| 150 |
viewer.getViewerRowFromItem(item), e); |
147 |
cell.getViewerRow(), e); |
| 151 |
} |
148 |
} |
| 152 |
} |
149 |
} |
| 153 |
}; |
150 |
}; |
|
Lines 159-196
Link Here
|
| 159 |
} |
156 |
} |
| 160 |
} |
157 |
} |
| 161 |
|
158 |
|
| 162 |
/** |
|
|
| 163 |
* Activate a cell editor for the given mouse position. |
| 164 |
*/ |
| 165 |
private void activateCellEditor(MouseEvent event) { |
| 166 |
if (item == null || item.isDisposed()) { |
| 167 |
// item no longer exists |
| 168 |
return; |
| 169 |
} |
| 170 |
int columnToEdit; |
| 171 |
ViewerRow row = viewer.getViewerRowFromItem(item); |
| 172 |
int columns = row.getColumnCount(); |
| 173 |
if (columns == 0) { |
| 174 |
// If no TableColumn, Table acts as if it has a single column |
| 175 |
// which takes the whole width. |
| 176 |
columnToEdit = 0; |
| 177 |
} else { |
| 178 |
columnToEdit = -1; |
| 179 |
for (int i = 0; i < columns; i++) { |
| 180 |
Rectangle bounds = row.getBounds(i); |
| 181 |
if (bounds.contains(event.x, event.y)) { |
| 182 |
columnToEdit = i; |
| 183 |
break; |
| 184 |
} |
| 185 |
} |
| 186 |
if (columnToEdit == -1) { |
| 187 |
return; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
columnNumber = columnToEdit; |
| 192 |
activateCellEditor(); |
| 193 |
} |
| 194 |
|
159 |
|
| 195 |
/** |
160 |
/** |
| 196 |
* Applies the current value and deactivates the currently active cell |
161 |
* Applies the current value and deactivates the currently active cell |
|
Lines 204-215
Link Here
|
| 204 |
// see 1GAHI8Z: ITPUI:ALL - How to code event notification when |
169 |
// see 1GAHI8Z: ITPUI:ALL - How to code event notification when |
| 205 |
// using cell editor ? |
170 |
// using cell editor ? |
| 206 |
this.cellEditor = null; |
171 |
this.cellEditor = null; |
| 207 |
Item t = this.item; |
172 |
Item t = this.cell.getItem(); |
|
|
173 |
|
| 208 |
// don't null out table item -- same item is still selected |
174 |
// don't null out table item -- same item is still selected |
| 209 |
if (t != null && !t.isDisposed()) { |
175 |
if (t != null && !t.isDisposed()) { |
| 210 |
saveEditorValue(c, t); |
176 |
saveEditorValue(c, t); |
| 211 |
} |
177 |
} |
| 212 |
setEditor(null, null, 0); |
178 |
setEditor(null, null); |
| 213 |
c.removeListener(cellEditorListener); |
179 |
c.removeListener(cellEditorListener); |
| 214 |
Control control = c.getControl(); |
180 |
Control control = c.getControl(); |
| 215 |
if (control != null) { |
181 |
if (control != null) { |
|
Lines 235-241
Link Here
|
| 235 |
*/ |
201 |
*/ |
| 236 |
void cancelEditing() { |
202 |
void cancelEditing() { |
| 237 |
if (cellEditor != null) { |
203 |
if (cellEditor != null) { |
| 238 |
setEditor(null, null, 0); |
204 |
setEditor(null, null); |
| 239 |
cellEditor.removeListener(cellEditorListener); |
205 |
cellEditor.removeListener(cellEditorListener); |
| 240 |
|
206 |
|
| 241 |
Control control = cellEditor.getControl(); |
207 |
Control control = cellEditor.getControl(); |
|
Lines 265-275
Link Here
|
| 265 |
* |
231 |
* |
| 266 |
* @param event |
232 |
* @param event |
| 267 |
*/ |
233 |
*/ |
| 268 |
void handleMouseDown(MouseEvent event) { |
234 |
void handleCellSelectionEvent(CellSelectionEvent event) { |
| 269 |
if (event.button != 1) { |
235 |
|
| 270 |
return; |
|
|
| 271 |
} |
| 272 |
|
| 273 |
if (cellEditor != null) { |
236 |
if (cellEditor != null) { |
| 274 |
applyEditorValue(); |
237 |
applyEditorValue(); |
| 275 |
} |
238 |
} |
|
Lines 279-325
Link Here
|
| 279 |
// the cell editor will be deactivated and a doubleClick event will |
242 |
// the cell editor will be deactivated and a doubleClick event will |
| 280 |
// be processed. |
243 |
// be processed. |
| 281 |
// |
244 |
// |
| 282 |
doubleClickExpirationTime = event.time |
245 |
if( event.swtEvent != null ) { |
| 283 |
+ Display.getCurrent().getDoubleClickTime(); |
246 |
activationTime = event.swtEvent.time + Display.getCurrent().getDoubleClickTime(); |
| 284 |
|
|
|
| 285 |
Item[] items = getSelection(); |
| 286 |
// Do not edit if more than one row is selected. |
| 287 |
if (items.length != 1) { |
| 288 |
item = null; |
| 289 |
return; |
| 290 |
} |
247 |
} |
| 291 |
item = items[0]; |
248 |
|
| 292 |
activateCellEditor(event); |
249 |
|
|
|
250 |
this.cell = event.cell; |
| 251 |
activateCellEditor(); |
| 293 |
} |
252 |
} |
| 294 |
|
253 |
|
| 295 |
/** |
254 |
/** |
| 296 |
* Start editing the given element. |
255 |
* Start editing the given element. |
| 297 |
* |
256 |
* |
| 298 |
* @param element |
257 |
* @param cell the cell to edit |
| 299 |
* @param column |
|
|
| 300 |
*/ |
258 |
*/ |
| 301 |
void editElement(Object element, int column) { |
259 |
void editCell(ViewerCell cell) { |
| 302 |
if (cellEditor != null) { |
260 |
if (cellEditor != null) { |
| 303 |
applyEditorValue(); |
261 |
applyEditorValue(); |
| 304 |
} |
262 |
} |
| 305 |
|
263 |
|
| 306 |
setSelection(createSelection(element), true); |
264 |
setSelection(createSelection(cell.getElement()), true); |
| 307 |
Item[] selection = getSelection(); |
265 |
Item[] selection = getSelection(); |
| 308 |
if (selection.length != 1) { |
266 |
if (selection.length != 1) { |
| 309 |
return; |
267 |
return; |
| 310 |
} |
268 |
} |
| 311 |
|
269 |
|
| 312 |
item = selection[0]; |
|
|
| 313 |
|
| 314 |
// Make sure selection is visible |
270 |
// Make sure selection is visible |
| 315 |
showSelection(); |
271 |
showSelection(); |
| 316 |
columnNumber = column; |
272 |
|
| 317 |
activateCellEditor(); |
273 |
activateCellEditor(); |
| 318 |
|
274 |
|
| 319 |
} |
275 |
} |
| 320 |
|
276 |
|
| 321 |
private void saveEditorValue(CellEditor cellEditor, Item tableItem) { |
277 |
private void saveEditorValue(CellEditor cellEditor, Item tableItem) { |
| 322 |
ViewerColumn part = viewer.getViewerColumn(columnNumber); |
278 |
ViewerColumn part = viewer.getViewerColumn(cell.getColumnIndex()); |
| 323 |
|
279 |
|
| 324 |
if (part != null && part.getEditingSupport() != null) { |
280 |
if (part != null && part.getEditingSupport() != null) { |
| 325 |
part.getEditingSupport().setValue(tableItem.getData(), |
281 |
part.getEditingSupport().setValue(tableItem.getData(), |
|
Lines 414-426
Link Here
|
| 414 |
* Position the editor inside the control |
370 |
* Position the editor inside the control |
| 415 |
* |
371 |
* |
| 416 |
* @param w |
372 |
* @param w |
| 417 |
* the editor control |
373 |
* the editor control or <code>null</code> if the editor has to be reset |
| 418 |
* @param item |
374 |
* @param cell the cell in which the editor should be displayed or <code>null</code> if the editor has to be reset |
| 419 |
* the item (row) in which the editor is drawn in |
375 |
* |
| 420 |
* @param fColumnNumber |
|
|
| 421 |
* the column number in which the editor is shown |
| 422 |
*/ |
376 |
*/ |
| 423 |
protected abstract void setEditor(Control w, Item item, int fColumnNumber); |
377 |
protected abstract void setEditor(Control w, ViewerCell cell); |
| 424 |
|
378 |
|
| 425 |
/** |
379 |
/** |
| 426 |
* set the layout data for the editor |
380 |
* set the layout data for the editor |