|
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 EditorActivationEvent activationEvent; |
| 65 |
|
| 66 |
private ListenerList editorActivationListener; |
| 67 |
|
| 68 |
private ColumnViewerKeyboardSupport 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 ColumnViewerKeyboardSupport(viewer) { |
| 79 |
protected boolean isEditorActivationEvent(EditorActivationEvent event) { |
| 80 |
return event.eventType == EditorActivationEvent.MOUSE_CLICK_SELECTION |
| 81 |
|| event.eventType == EditorActivationEvent.PROGRAMATIC |
| 82 |
|| event.eventType == EditorActivationEvent.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.cancle ) { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
((ColumnViewerEditorActivationListener)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 |
((ColumnViewerEditorActivationListener)ls[i]).afterEditorActivated(activationEvent); |
| 162 |
/** |
192 |
} |
| 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 |
} |
193 |
} |
| 185 |
} |
194 |
} |
| 186 |
if (columnToEdit == -1) { |
|
|
| 187 |
return; |
| 188 |
} |
| 189 |
} |
195 |
} |
| 190 |
|
|
|
| 191 |
columnNumber = columnToEdit; |
| 192 |
activateCellEditor(); |
| 193 |
} |
196 |
} |
| 194 |
|
197 |
|
| 195 |
/** |
198 |
/** |
|
Lines 203-210
Link Here
|
| 203 |
// in case save results in applyEditorValue being re-entered |
206 |
// in case save results in applyEditorValue being re-entered |
| 204 |
// see 1GAHI8Z: ITPUI:ALL - How to code event notification when |
207 |
// see 1GAHI8Z: ITPUI:ALL - How to code event notification when |
| 205 |
// using cell editor ? |
208 |
// using cell editor ? |
|
|
209 |
EditorDeactivationEvent tmp = new EditorDeactivationEvent(cell); |
| 210 |
if( editorActivationListener != null && ! editorActivationListener.isEmpty() ) { |
| 211 |
Object[] ls = editorActivationListener.getListeners(); |
| 212 |
for( int i = 0; i < ls.length; i++ ) { |
| 213 |
|
| 214 |
if( tmp.cancle ) { |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
((ColumnViewerEditorActivationListener)ls[i]).beforeEditorDeactivated(tmp); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 206 |
this.cellEditor = null; |
222 |
this.cellEditor = null; |
| 207 |
Item t = this.item; |
223 |
this.activationEvent = null; |
|
|
224 |
Item t = this.cell.getItem(); |
| 208 |
// don't null out table item -- same item is still selected |
225 |
// don't null out table item -- same item is still selected |
| 209 |
if (t != null && !t.isDisposed()) { |
226 |
if (t != null && !t.isDisposed()) { |
| 210 |
saveEditorValue(c, t); |
227 |
saveEditorValue(c, t); |
|
Lines 227-232
Link Here
|
| 227 |
} |
244 |
} |
| 228 |
} |
245 |
} |
| 229 |
c.deactivate(); |
246 |
c.deactivate(); |
|
|
247 |
|
| 248 |
if( editorActivationListener != null && ! editorActivationListener.isEmpty() ) { |
| 249 |
Object[] ls = editorActivationListener.getListeners(); |
| 250 |
for( int i = 0; i < ls.length; i++ ) { |
| 251 |
((ColumnViewerEditorActivationListener)ls[i]).afterEditorDeactivated(tmp); |
| 252 |
} |
| 253 |
} |
| 230 |
} |
254 |
} |
| 231 |
} |
255 |
} |
| 232 |
|
256 |
|
|
Lines 235-240
Link Here
|
| 235 |
*/ |
259 |
*/ |
| 236 |
void cancelEditing() { |
260 |
void cancelEditing() { |
| 237 |
if (cellEditor != null) { |
261 |
if (cellEditor != null) { |
|
|
262 |
EditorDeactivationEvent tmp = new EditorDeactivationEvent(cell); |
| 263 |
if( editorActivationListener != null && ! editorActivationListener.isEmpty() ) { |
| 264 |
Object[] ls = editorActivationListener.getListeners(); |
| 265 |
for( int i = 0; i < ls.length; i++ ) { |
| 266 |
if( tmp.cancle ) { |
| 267 |
return; |
| 268 |
} |
| 269 |
|
| 270 |
((ColumnViewerEditorActivationListener)ls[i]).beforeEditorDeactivated(tmp); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 238 |
setEditor(null, null, 0); |
274 |
setEditor(null, null, 0); |
| 239 |
cellEditor.removeListener(cellEditorListener); |
275 |
cellEditor.removeListener(cellEditorListener); |
| 240 |
|
276 |
|
|
Lines 256-325
Link Here
|
| 256 |
|
292 |
|
| 257 |
CellEditor oldEditor = cellEditor; |
293 |
CellEditor oldEditor = cellEditor; |
| 258 |
cellEditor = null; |
294 |
cellEditor = null; |
|
|
295 |
activationEvent=null; |
| 259 |
oldEditor.deactivate(); |
296 |
oldEditor.deactivate(); |
|
|
297 |
|
| 298 |
if( editorActivationListener != null && ! editorActivationListener.isEmpty() ) { |
| 299 |
Object[] ls = editorActivationListener.getListeners(); |
| 300 |
for( int i = 0; i < ls.length; i++ ) { |
| 301 |
((ColumnViewerEditorActivationListener)ls[i]).afterEditorDeactivated(tmp); |
| 302 |
} |
| 303 |
} |
| 260 |
} |
304 |
} |
| 261 |
} |
305 |
} |
| 262 |
|
306 |
|
| 263 |
/** |
307 |
/** |
| 264 |
* Enable the editor by mouse down |
308 |
* Enable the editor by mouse down |
| 265 |
* |
309 |
* |
| 266 |
* @param event |
310 |
* @param event |
| 267 |
*/ |
311 |
*/ |
| 268 |
void handleMouseDown(MouseEvent event) { |
312 |
void handleEditorActivationEvent(EditorActivationEvent event) { |
| 269 |
if (event.button != 1) { |
313 |
if( editorActivationStrategy.isEditorActivationEvent(event) ) { |
| 270 |
return; |
314 |
if (cellEditor != null) { |
| 271 |
} |
315 |
applyEditorValue(); |
| 272 |
|
316 |
} |
| 273 |
if (cellEditor != null) { |
|
|
| 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 |
|
317 |
|
| 306 |
setSelection(createSelection(element), true); |
318 |
this.cell = (ViewerCell)event.getSource(); |
| 307 |
Item[] selection = getSelection(); |
319 |
activationEvent = event; |
| 308 |
if (selection.length != 1) { |
320 |
activationTime = event.time + Display.getCurrent().getDoubleClickTime(); |
| 309 |
return; |
321 |
|
|
|
322 |
activateCellEditor(); |
| 310 |
} |
323 |
} |
| 311 |
|
|
|
| 312 |
item = selection[0]; |
| 313 |
|
| 314 |
// Make sure selection is visible |
| 315 |
showSelection(); |
| 316 |
columnNumber = column; |
| 317 |
activateCellEditor(); |
| 318 |
|
| 319 |
} |
324 |
} |
| 320 |
|
325 |
|
| 321 |
private void saveEditorValue(CellEditor cellEditor, Item tableItem) { |
326 |
private void saveEditorValue(CellEditor cellEditor, Item tableItem) { |
| 322 |
ViewerColumn part = viewer.getViewerColumn(columnNumber); |
327 |
ViewerColumn part = viewer.getViewerColumn(cell.getColumnIndex()); |
| 323 |
|
328 |
|
| 324 |
if (part != null && part.getEditingSupport() != null) { |
329 |
if (part != null && part.getEditingSupport() != null) { |
| 325 |
part.getEditingSupport().setValue(tableItem.getData(), |
330 |
part.getEditingSupport().setValue(tableItem.getData(), |
|
Lines 391-406
Link Here
|
| 391 |
return cellEditors; |
396 |
return cellEditors; |
| 392 |
} |
397 |
} |
| 393 |
|
398 |
|
| 394 |
void setSelection(StructuredSelection selection, boolean b) { |
|
|
| 395 |
viewer.setSelection(selection, b); |
| 396 |
} |
| 397 |
|
| 398 |
void handleDoubleClickEvent() { |
399 |
void handleDoubleClickEvent() { |
| 399 |
viewer.fireDoubleClick(new DoubleClickEvent(viewer, viewer |
400 |
viewer.fireDoubleClick(new DoubleClickEvent(viewer, viewer |
| 400 |
.getSelection())); |
401 |
.getSelection())); |
| 401 |
viewer.fireOpen(new OpenEvent(viewer, viewer.getSelection())); |
402 |
viewer.fireOpen(new OpenEvent(viewer, viewer.getSelection())); |
| 402 |
} |
403 |
} |
| 403 |
|
404 |
|
|
|
405 |
void addEditorActivationListener(ColumnViewerEditorActivationListener listener) { |
| 406 |
if( editorActivationListener == null ) { |
| 407 |
editorActivationListener = new ListenerList(); |
| 408 |
} |
| 409 |
editorActivationListener.add(listener); |
| 410 |
} |
| 411 |
|
| 412 |
void removeEditorActivationListener(ColumnViewerEditorActivationListener listener) { |
| 413 |
if( editorActivationListener != null ) { |
| 414 |
editorActivationListener.remove(listener); |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
void setEditorActivationStrategy( |
| 419 |
ColumnViewerKeyboardSupport editorActivationStrategy) { |
| 420 |
this.editorActivationStrategy = editorActivationStrategy; |
| 421 |
} |
| 422 |
|
| 404 |
/** |
423 |
/** |
| 405 |
* Create a selection for this model element |
424 |
* Create a selection for this model element |
| 406 |
* |
425 |
* |
|
Lines 429-442
Link Here
|
| 429 |
* the layout data used when editor is displayed |
448 |
* the layout data used when editor is displayed |
| 430 |
*/ |
449 |
*/ |
| 431 |
protected abstract void setLayoutData(CellEditor.LayoutData layoutData); |
450 |
protected abstract void setLayoutData(CellEditor.LayoutData layoutData); |
| 432 |
|
451 |
} |
| 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 |
} |