|
Lines 14-19
Link Here
|
| 14 |
|
14 |
|
| 15 |
package org.eclipse.jface.viewers; |
15 |
package org.eclipse.jface.viewers; |
| 16 |
|
16 |
|
|
|
17 |
|
| 18 |
import org.eclipse.core.runtime.ListenerList; |
| 17 |
import org.eclipse.swt.events.FocusAdapter; |
19 |
import org.eclipse.swt.events.FocusAdapter; |
| 18 |
import org.eclipse.swt.events.FocusEvent; |
20 |
import org.eclipse.swt.events.FocusEvent; |
| 19 |
import org.eclipse.swt.events.FocusListener; |
21 |
import org.eclipse.swt.events.FocusListener; |
|
Lines 22-28
Link Here
|
| 22 |
import org.eclipse.swt.events.MouseListener; |
24 |
import org.eclipse.swt.events.MouseListener; |
| 23 |
import org.eclipse.swt.events.TraverseEvent; |
25 |
import org.eclipse.swt.events.TraverseEvent; |
| 24 |
import org.eclipse.swt.events.TraverseListener; |
26 |
import org.eclipse.swt.events.TraverseListener; |
| 25 |
import org.eclipse.swt.graphics.Rectangle; |
|
|
| 26 |
import org.eclipse.swt.widgets.Control; |
27 |
import org.eclipse.swt.widgets.Control; |
| 27 |
import org.eclipse.swt.widgets.Display; |
28 |
import org.eclipse.swt.widgets.Display; |
| 28 |
import org.eclipse.swt.widgets.Item; |
29 |
import org.eclipse.swt.widgets.Item; |
|
Lines 46-67
Link Here
|
| 46 |
|
47 |
|
| 47 |
private String[] columnProperties; |
48 |
private String[] columnProperties; |
| 48 |
|
49 |
|
| 49 |
private int columnNumber; |
|
|
| 50 |
|
| 51 |
private ICellEditorListener cellEditorListener; |
50 |
private ICellEditorListener cellEditorListener; |
| 52 |
|
51 |
|
| 53 |
private FocusListener focusListener; |
52 |
private FocusListener focusListener; |
| 54 |
|
53 |
|
| 55 |
private MouseListener mouseListener; |
54 |
private MouseListener mouseListener; |
| 56 |
|
55 |
|
| 57 |
private int doubleClickExpirationTime; |
|
|
| 58 |
|
| 59 |
private ColumnViewer viewer; |
56 |
private ColumnViewer viewer; |
| 60 |
|
57 |
|
| 61 |
private Item item; |
|
|
| 62 |
|
| 63 |
private TraverseListener tabeditingListener; |
58 |
private TraverseListener tabeditingListener; |
| 64 |
|
59 |
|
|
|
60 |
private int activationTime; |
| 61 |
|
| 62 |
private ViewerCell cell; |
| 63 |
|
| 64 |
private CellEditorActivationEvent activationEvent; |
| 65 |
|
| 66 |
private ListenerList editorActivationListener; |
| 67 |
|
| 68 |
private ColumnViewerEditorActivationSupport editorActivationStrategy; |
| 69 |
|
| 65 |
/** |
70 |
/** |
| 66 |
* Create a new editor implementation for the viewer |
71 |
* Create a new editor implementation for the viewer |
| 67 |
* |
72 |
* |
|
Lines 70-75
Link Here
|
| 70 |
*/ |
75 |
*/ |
| 71 |
public ColumnViewerEditor(ColumnViewer viewer) { |
76 |
public ColumnViewerEditor(ColumnViewer viewer) { |
| 72 |
this.viewer = viewer; |
77 |
this.viewer = viewer; |
|
|
78 |
editorActivationStrategy = new ColumnViewerEditorActivationSupport(viewer,null) { |
| 79 |
protected boolean isEditorActivationEvent(CellEditorActivationEvent event) { |
| 80 |
return event.eventType == CellEditorActivationEvent.MOUSE_CLICK_SELECTION |
| 81 |
|| event.eventType == CellEditorActivationEvent.PROGRAMATIC |
| 82 |
|| event.eventType == CellEditorActivationEvent.TRAVERSAL; |
| 83 |
} |
| 84 |
|
| 85 |
protected ViewerCell getFocusCell() { |
| 86 |
return null; |
| 87 |
} |
| 88 |
|
| 89 |
}; |
| 90 |
|
| 73 |
initCellEditorListener(); |
91 |
initCellEditorListener(); |
| 74 |
} |
92 |
} |
| 75 |
|
93 |
|
|
Lines 92-104
Link Here
|
| 92 |
|
110 |
|
| 93 |
void activateCellEditor() { |
111 |
void activateCellEditor() { |
| 94 |
|
112 |
|
| 95 |
ViewerColumn part = viewer.getViewerColumn(columnNumber); |
113 |
ViewerColumn part = viewer.getViewerColumn(cell.getColumnIndex()); |
| 96 |
Object element = item.getData(); |
114 |
Object element = cell.getElement(); |
| 97 |
|
115 |
|
| 98 |
if (part != null && part.getEditingSupport() != null |
116 |
if (part != null && part.getEditingSupport() != null |
| 99 |
&& part.getEditingSupport().canEdit(element)) { |
117 |
&& part.getEditingSupport().canEdit(element)) { |
| 100 |
cellEditor = part.getEditingSupport().getCellEditor(element); |
118 |
cellEditor = part.getEditingSupport().getCellEditor(element); |
| 101 |
if (cellEditor != null) { |
119 |
if (cellEditor != null) { |
|
|
120 |
if( editorActivationListener != null && ! editorActivationListener.isEmpty() ) { |
| 121 |
Object[] ls = editorActivationListener.getListeners(); |
| 122 |
for( int i = 0; i < ls.length; i++ ) { |
| 123 |
|
| 124 |
if( ! activationEvent.doIt ) { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
((CellEditorActivationListener)ls[i]).beforeEditorActivated(activationEvent); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 102 |
cellEditor.addListener(cellEditorListener); |
132 |
cellEditor.addListener(cellEditorListener); |
| 103 |
Object value = part.getEditingSupport().getValue(element); |
133 |
Object value = part.getEditingSupport().getValue(element); |
| 104 |
cellEditor.setValue(value); |
134 |
cellEditor.setValue(value); |
|
Lines 108-119
Link Here
|
| 108 |
// so must get control first, but must still call activate() |
138 |
// so must get control first, but must still call activate() |
| 109 |
// even if there is no control. |
139 |
// even if there is no control. |
| 110 |
final Control control = cellEditor.getControl(); |
140 |
final Control control = cellEditor.getControl(); |
| 111 |
cellEditor.activate(); |
141 |
cellEditor.activate(activationEvent); |
| 112 |
if (control == null) { |
142 |
if (control == null) { |
| 113 |
return; |
143 |
return; |
| 114 |
} |
144 |
} |
| 115 |
setLayoutData(cellEditor.getLayoutData()); |
145 |
setLayoutData(cellEditor.getLayoutData()); |
| 116 |
setEditor(control, item, columnNumber); |
146 |
setEditor(control, cell.getItem(), cell.getColumnIndex()); |
| 117 |
cellEditor.setFocus(); |
147 |
cellEditor.setFocus(); |
| 118 |
if (focusListener == null) { |
148 |
if (focusListener == null) { |
| 119 |
focusListener = new FocusAdapter() { |
149 |
focusListener = new FocusAdapter() { |
|
Lines 128-134
Link Here
|
| 128 |
public void mouseDown(MouseEvent e) { |
158 |
public void mouseDown(MouseEvent e) { |
| 129 |
// time wrap? |
159 |
// time wrap? |
| 130 |
// check for expiration of doubleClickTime |
160 |
// check for expiration of doubleClickTime |
| 131 |
if (e.time <= doubleClickExpirationTime) { |
161 |
if (e.time <= activationTime) { |
| 132 |
control.removeMouseListener(mouseListener); |
162 |
control.removeMouseListener(mouseListener); |
| 133 |
cancelEditing(); |
163 |
cancelEditing(); |
| 134 |
handleDoubleClickEvent(); |
164 |
handleDoubleClickEvent(); |
|
Lines 143-195
Link Here
|
| 143 |
tabeditingListener = new TraverseListener() { |
173 |
tabeditingListener = new TraverseListener() { |
| 144 |
|
174 |
|
| 145 |
public void keyTraversed(TraverseEvent e) { |
175 |
public void keyTraversed(TraverseEvent e) { |
| 146 |
ViewerColumn col = viewer.getViewerColumn(columnNumber); |
176 |
ViewerColumn col = viewer.getViewerColumn(cell.getColumnIndex()); |
| 147 |
if ( col != null && col.getEditingSupport().isTabingSupported() ) { |
177 |
if ( col != null && col.getEditingSupport().isTabingSupported() ) { |
| 148 |
col.getEditingSupport().processTraversEvent( |
178 |
col.getEditingSupport().processTraversEvent( |
| 149 |
columnNumber, |
179 |
cell.getColumnIndex(), |
| 150 |
viewer.getViewerRowFromItem(item), e); |
180 |
viewer.getViewerRowFromItem(cell.getItem()), e); |
| 151 |
} |
181 |
} |
| 152 |
} |
182 |
} |
| 153 |
}; |
183 |
}; |
| 154 |
} |
184 |
} |
| 155 |
|
185 |
|
| 156 |
control.addTraverseListener(tabeditingListener); |
186 |
control.addTraverseListener(tabeditingListener); |
| 157 |
|
187 |
|
| 158 |
} |
188 |
if( editorActivationListener != null && ! editorActivationListener.isEmpty() ) { |
| 159 |
} |
189 |
Object[] ls = editorActivationListener.getListeners(); |
| 160 |
} |
190 |
for( int i = 0; i < ls.length; i++ ) { |
| 161 |
|
191 |
if( ! activationEvent.doIt ) { |
| 162 |
/** |
192 |
return; |
| 163 |
* Activate a cell editor for the given mouse position. |
193 |
} |
| 164 |
*/ |
194 |
((CellEditorActivationListener)ls[i]).afterEditorActivated(activationEvent); |
| 165 |
private void activateCellEditor(MouseEvent event) { |
195 |
} |
| 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 |
} |
196 |
} |
| 185 |
} |
197 |
} |
| 186 |
if (columnToEdit == -1) { |
|
|
| 187 |
return; |
| 188 |
} |
| 189 |
} |
198 |
} |
| 190 |
|
|
|
| 191 |
columnNumber = columnToEdit; |
| 192 |
activateCellEditor(); |
| 193 |
} |
199 |
} |
| 194 |
|
200 |
|
| 195 |
/** |
201 |
/** |
|
Lines 203-210
Link Here
|
| 203 |
// in case save results in applyEditorValue being re-entered |
209 |
// in case save results in applyEditorValue being re-entered |
| 204 |
// see 1GAHI8Z: ITPUI:ALL - How to code event notification when |
210 |
// see 1GAHI8Z: ITPUI:ALL - How to code event notification when |
| 205 |
// using cell editor ? |
211 |
// using cell editor ? |
|
|
212 |
CellEditorDeactivationEvent tmp = new CellEditorDeactivationEvent(cell); |
| 213 |
if( editorActivationListener != null && ! editorActivationListener.isEmpty() ) { |
| 214 |
Object[] ls = editorActivationListener.getListeners(); |
| 215 |
for( int i = 0; i < ls.length; i++ ) { |
| 216 |
|
| 217 |
if( ! tmp.doIt ) { |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
((CellEditorActivationListener)ls[i]).beforeEditorDeactivated(tmp); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 206 |
this.cellEditor = null; |
225 |
this.cellEditor = null; |
| 207 |
Item t = this.item; |
226 |
this.activationEvent = null; |
|
|
227 |
Item t = this.cell.getItem(); |
| 208 |
// don't null out table item -- same item is still selected |
228 |
// don't null out table item -- same item is still selected |
| 209 |
if (t != null && !t.isDisposed()) { |
229 |
if (t != null && !t.isDisposed()) { |
| 210 |
saveEditorValue(c, t); |
230 |
saveEditorValue(c, t); |
|
Lines 227-232
Link Here
|
| 227 |
} |
247 |
} |
| 228 |
} |
248 |
} |
| 229 |
c.deactivate(); |
249 |
c.deactivate(); |
|
|
250 |
|
| 251 |
if( editorActivationListener != null && ! editorActivationListener.isEmpty() ) { |
| 252 |
Object[] ls = editorActivationListener.getListeners(); |
| 253 |
for( int i = 0; i < ls.length; i++ ) { |
| 254 |
if( ! tmp.doIt ) { |
| 255 |
return; |
| 256 |
} |
| 257 |
((CellEditorActivationListener)ls[i]).afterEditorDeactivated(tmp); |
| 258 |
} |
| 259 |
} |
| 230 |
} |
260 |
} |
| 231 |
} |
261 |
} |
| 232 |
|
262 |
|
|
Lines 235-240
Link Here
|
| 235 |
*/ |
265 |
*/ |
| 236 |
void cancelEditing() { |
266 |
void cancelEditing() { |
| 237 |
if (cellEditor != null) { |
267 |
if (cellEditor != null) { |
|
|
268 |
CellEditorDeactivationEvent tmp = new CellEditorDeactivationEvent(cell); |
| 269 |
if( editorActivationListener != null && ! editorActivationListener.isEmpty() ) { |
| 270 |
Object[] ls = editorActivationListener.getListeners(); |
| 271 |
for( int i = 0; i < ls.length; i++ ) { |
| 272 |
if( ! tmp.doIt ) { |
| 273 |
return; |
| 274 |
} |
| 275 |
|
| 276 |
((CellEditorActivationListener)ls[i]).beforeEditorDeactivated(tmp); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 238 |
setEditor(null, null, 0); |
280 |
setEditor(null, null, 0); |
| 239 |
cellEditor.removeListener(cellEditorListener); |
281 |
cellEditor.removeListener(cellEditorListener); |
| 240 |
|
282 |
|
|
Lines 256-325
Link Here
|
| 256 |
|
298 |
|
| 257 |
CellEditor oldEditor = cellEditor; |
299 |
CellEditor oldEditor = cellEditor; |
| 258 |
cellEditor = null; |
300 |
cellEditor = null; |
|
|
301 |
activationEvent=null; |
| 259 |
oldEditor.deactivate(); |
302 |
oldEditor.deactivate(); |
|
|
303 |
|
| 304 |
if( editorActivationListener != null && ! editorActivationListener.isEmpty() ) { |
| 305 |
Object[] ls = editorActivationListener.getListeners(); |
| 306 |
for( int i = 0; i < ls.length; i++ ) { |
| 307 |
|
| 308 |
if( ! tmp.doIt ) { |
| 309 |
return; |
| 310 |
} |
| 311 |
|
| 312 |
((CellEditorActivationListener)ls[i]).afterEditorDeactivated(tmp); |
| 313 |
} |
| 314 |
} |
| 260 |
} |
315 |
} |
| 261 |
} |
316 |
} |
| 262 |
|
317 |
|
| 263 |
/** |
318 |
/** |
| 264 |
* Enable the editor by mouse down |
319 |
* Enable the editor by mouse down |
| 265 |
* |
320 |
* |
| 266 |
* @param event |
321 |
* @param event |
| 267 |
*/ |
322 |
*/ |
| 268 |
void handleMouseDown(MouseEvent event) { |
323 |
void triggerCellEditorActivation(CellEditorActivationEvent event) { |
| 269 |
if (event.button != 1) { |
324 |
if( editorActivationStrategy.isEditorActivationEvent(event) ) { |
| 270 |
return; |
325 |
|
| 271 |
} |
326 |
if (cellEditor != null) { |
| 272 |
|
327 |
applyEditorValue(); |
| 273 |
if (cellEditor != null) { |
328 |
} |
| 274 |
applyEditorValue(); |
|
|
| 275 |
} |
| 276 |
|
| 277 |
// activate the cell editor immediately. If a second mouseDown |
| 278 |
// is received prior to the expiration of the doubleClick time then |
| 279 |
// the cell editor will be deactivated and a doubleClick event will |
| 280 |
// be processed. |
| 281 |
// |
| 282 |
doubleClickExpirationTime = event.time |
| 283 |
+ 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 |
} |
| 291 |
item = items[0]; |
| 292 |
activateCellEditor(event); |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Start editing the given element. |
| 297 |
* |
| 298 |
* @param element |
| 299 |
* @param column |
| 300 |
*/ |
| 301 |
void editElement(Object element, int column) { |
| 302 |
if (cellEditor != null) { |
| 303 |
applyEditorValue(); |
| 304 |
} |
| 305 |
|
329 |
|
| 306 |
setSelection(createSelection(element), true); |
330 |
this.cell = (ViewerCell)event.getSource(); |
| 307 |
Item[] selection = getSelection(); |
331 |
activationEvent = event; |
| 308 |
if (selection.length != 1) { |
332 |
activationTime = event.time + Display.getCurrent().getDoubleClickTime(); |
| 309 |
return; |
333 |
|
|
|
334 |
activateCellEditor(); |
| 310 |
} |
335 |
} |
| 311 |
|
|
|
| 312 |
item = selection[0]; |
| 313 |
|
| 314 |
// Make sure selection is visible |
| 315 |
showSelection(); |
| 316 |
columnNumber = column; |
| 317 |
activateCellEditor(); |
| 318 |
|
| 319 |
} |
336 |
} |
| 320 |
|
337 |
|
| 321 |
private void saveEditorValue(CellEditor cellEditor, Item tableItem) { |
338 |
private void saveEditorValue(CellEditor cellEditor, Item tableItem) { |
| 322 |
ViewerColumn part = viewer.getViewerColumn(columnNumber); |
339 |
ViewerColumn part = viewer.getViewerColumn(cell.getColumnIndex()); |
| 323 |
|
340 |
|
| 324 |
if (part != null && part.getEditingSupport() != null) { |
341 |
if (part != null && part.getEditingSupport() != null) { |
| 325 |
part.getEditingSupport().setValue(tableItem.getData(), |
342 |
part.getEditingSupport().setValue(tableItem.getData(), |
|
Lines 391-406
Link Here
|
| 391 |
return cellEditors; |
408 |
return cellEditors; |
| 392 |
} |
409 |
} |
| 393 |
|
410 |
|
| 394 |
void setSelection(StructuredSelection selection, boolean b) { |
|
|
| 395 |
viewer.setSelection(selection, b); |
| 396 |
} |
| 397 |
|
| 398 |
void handleDoubleClickEvent() { |
411 |
void handleDoubleClickEvent() { |
| 399 |
viewer.fireDoubleClick(new DoubleClickEvent(viewer, viewer |
412 |
viewer.fireDoubleClick(new DoubleClickEvent(viewer, viewer |
| 400 |
.getSelection())); |
413 |
.getSelection())); |
| 401 |
viewer.fireOpen(new OpenEvent(viewer, viewer.getSelection())); |
414 |
viewer.fireOpen(new OpenEvent(viewer, viewer.getSelection())); |
| 402 |
} |
415 |
} |
| 403 |
|
416 |
|
|
|
417 |
void addEditorActivationListener(CellEditorActivationListener listener) { |
| 418 |
if( editorActivationListener == null ) { |
| 419 |
editorActivationListener = new ListenerList(); |
| 420 |
} |
| 421 |
editorActivationListener.add(listener); |
| 422 |
} |
| 423 |
|
| 424 |
void removeEditorActivationListener(CellEditorActivationListener listener) { |
| 425 |
if( editorActivationListener != null ) { |
| 426 |
editorActivationListener.remove(listener); |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
void setEditorActivationStrategy( |
| 431 |
ColumnViewerEditorActivationSupport editorActivationStrategy) { |
| 432 |
this.editorActivationStrategy = editorActivationStrategy; |
| 433 |
} |
| 434 |
|
| 404 |
/** |
435 |
/** |
| 405 |
* Create a selection for this model element |
436 |
* Create a selection for this model element |
| 406 |
* |
437 |
* |
|
Lines 429-442
Link Here
|
| 429 |
* the layout data used when editor is displayed |
460 |
* the layout data used when editor is displayed |
| 430 |
*/ |
461 |
*/ |
| 431 |
protected abstract void setLayoutData(CellEditor.LayoutData layoutData); |
462 |
protected abstract void setLayoutData(CellEditor.LayoutData layoutData); |
| 432 |
|
463 |
} |
| 433 |
/** |
|
|
| 434 |
* Show up the current selection (scroll the selection into view) |
| 435 |
*/ |
| 436 |
protected abstract void showSelection(); |
| 437 |
|
| 438 |
/** |
| 439 |
* @return the current selection |
| 440 |
*/ |
| 441 |
protected abstract Item[] getSelection(); |
| 442 |
} |