|
Lines 18-26
Link Here
|
| 18 |
import org.eclipse.swt.events.MouseAdapter; |
18 |
import org.eclipse.swt.events.MouseAdapter; |
| 19 |
import org.eclipse.swt.events.MouseEvent; |
19 |
import org.eclipse.swt.events.MouseEvent; |
| 20 |
import org.eclipse.swt.events.MouseListener; |
20 |
import org.eclipse.swt.events.MouseListener; |
| 21 |
import org.eclipse.swt.graphics.Rectangle; |
|
|
| 22 |
import org.eclipse.swt.widgets.Control; |
21 |
import org.eclipse.swt.widgets.Control; |
| 23 |
import org.eclipse.swt.widgets.Display; |
|
|
| 24 |
import org.eclipse.swt.widgets.Item; |
22 |
import org.eclipse.swt.widgets.Item; |
| 25 |
|
23 |
|
| 26 |
/** |
24 |
/** |
|
Lines 42-49
Link Here
|
| 42 |
|
40 |
|
| 43 |
private String[] columnProperties; |
41 |
private String[] columnProperties; |
| 44 |
|
42 |
|
| 45 |
private int columnNumber; |
|
|
| 46 |
|
| 47 |
private ICellEditorListener cellEditorListener; |
43 |
private ICellEditorListener cellEditorListener; |
| 48 |
|
44 |
|
| 49 |
private FocusListener focusListener; |
45 |
private FocusListener focusListener; |
|
Lines 54-60
Link Here
|
| 54 |
|
50 |
|
| 55 |
private ColumnViewer viewer; |
51 |
private ColumnViewer viewer; |
| 56 |
|
52 |
|
| 57 |
private Item item; |
53 |
private ViewerCell cell; |
| 58 |
|
54 |
|
| 59 |
/** |
55 |
/** |
| 60 |
* Create a new editor implementation for the viewer |
56 |
* Create a new editor implementation for the viewer |
|
Lines 86-93
Link Here
|
| 86 |
|
82 |
|
| 87 |
void activateCellEditor() { |
83 |
void activateCellEditor() { |
| 88 |
|
84 |
|
| 89 |
ViewerColumn part = viewer.getViewerColumn(columnNumber); |
85 |
ViewerColumn part = viewer.getViewerColumn(cell.getColumnIndex()); |
| 90 |
Object element = item.getData(); |
86 |
Object element = cell.getElement(); |
| 91 |
|
87 |
|
| 92 |
if (part != null && part.getEditingSupport() != null |
88 |
if (part != null && part.getEditingSupport() != null |
| 93 |
&& part.getEditingSupport().canEdit(element)) { |
89 |
&& part.getEditingSupport().canEdit(element)) { |
|
Lines 107-113
Link Here
|
| 107 |
return; |
103 |
return; |
| 108 |
} |
104 |
} |
| 109 |
setLayoutData(cellEditor.getLayoutData()); |
105 |
setLayoutData(cellEditor.getLayoutData()); |
| 110 |
setEditor(control, item, columnNumber); |
106 |
setEditor(control, cell); |
| 111 |
cellEditor.setFocus(); |
107 |
cellEditor.setFocus(); |
| 112 |
if (focusListener == null) { |
108 |
if (focusListener == null) { |
| 113 |
focusListener = new FocusAdapter() { |
109 |
focusListener = new FocusAdapter() { |
|
Lines 136-174
Link Here
|
| 136 |
} |
132 |
} |
| 137 |
|
133 |
|
| 138 |
/** |
134 |
/** |
| 139 |
* Activate a cell editor for the given mouse position. |
|
|
| 140 |
*/ |
| 141 |
private void activateCellEditor(MouseEvent event) { |
| 142 |
if (item == null || item.isDisposed()) { |
| 143 |
// item no longer exists |
| 144 |
return; |
| 145 |
} |
| 146 |
int columnToEdit; |
| 147 |
ViewerRow row = viewer.getViewerRowFromItem(item); |
| 148 |
int columns = row.getColumnCount(); |
| 149 |
if (columns == 0) { |
| 150 |
// If no TableColumn, Table acts as if it has a single column |
| 151 |
// which takes the whole width. |
| 152 |
columnToEdit = 0; |
| 153 |
} else { |
| 154 |
columnToEdit = -1; |
| 155 |
for (int i = 0; i < columns; i++) { |
| 156 |
Rectangle bounds = row.getBounds(i); |
| 157 |
if (bounds.contains(event.x, event.y)) { |
| 158 |
columnToEdit = i; |
| 159 |
break; |
| 160 |
} |
| 161 |
} |
| 162 |
if (columnToEdit == -1) { |
| 163 |
return; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
columnNumber = columnToEdit; |
| 168 |
activateCellEditor(); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Applies the current value and deactivates the currently active cell |
135 |
* Applies the current value and deactivates the currently active cell |
| 173 |
* editor. |
136 |
* editor. |
| 174 |
*/ |
137 |
*/ |
|
Lines 180-191
Link Here
|
| 180 |
// see 1GAHI8Z: ITPUI:ALL - How to code event notification when |
143 |
// see 1GAHI8Z: ITPUI:ALL - How to code event notification when |
| 181 |
// using cell editor ? |
144 |
// using cell editor ? |
| 182 |
this.cellEditor = null; |
145 |
this.cellEditor = null; |
| 183 |
Item t = this.item; |
146 |
Item t = cell.getItem(); |
|
|
147 |
|
| 184 |
// don't null out table item -- same item is still selected |
148 |
// don't null out table item -- same item is still selected |
| 185 |
if (t != null && !t.isDisposed()) { |
149 |
if (t != null && !t.isDisposed()) { |
| 186 |
saveEditorValue(c, t); |
150 |
saveEditorValue(c, cell); |
| 187 |
} |
151 |
} |
| 188 |
setEditor(null, null, 0); |
152 |
|
|
|
153 |
setEditor(null, null); |
| 189 |
c.removeListener(cellEditorListener); |
154 |
c.removeListener(cellEditorListener); |
| 190 |
Control control = c.getControl(); |
155 |
Control control = c.getControl(); |
| 191 |
if (control != null) { |
156 |
if (control != null) { |
|
Lines 205-211
Link Here
|
| 205 |
*/ |
170 |
*/ |
| 206 |
void cancelEditing() { |
171 |
void cancelEditing() { |
| 207 |
if (cellEditor != null) { |
172 |
if (cellEditor != null) { |
| 208 |
setEditor(null, null, 0); |
173 |
setEditor(null, null); |
| 209 |
cellEditor.removeListener(cellEditorListener); |
174 |
cellEditor.removeListener(cellEditorListener); |
| 210 |
CellEditor oldEditor = cellEditor; |
175 |
CellEditor oldEditor = cellEditor; |
| 211 |
cellEditor = null; |
176 |
cellEditor = null; |
|
Lines 214-281
Link Here
|
| 214 |
} |
179 |
} |
| 215 |
|
180 |
|
| 216 |
/** |
181 |
/** |
| 217 |
* Enable the editor by mouse down |
182 |
* Start editing the given cell. |
| 218 |
* |
|
|
| 219 |
* @param event |
| 220 |
*/ |
| 221 |
void handleMouseDown(MouseEvent event) { |
| 222 |
if (event.button != 1) { |
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
if (cellEditor != null) { |
| 227 |
applyEditorValue(); |
| 228 |
} |
| 229 |
|
| 230 |
// activate the cell editor immediately. If a second mouseDown |
| 231 |
// is received prior to the expiration of the doubleClick time then |
| 232 |
// the cell editor will be deactivated and a doubleClick event will |
| 233 |
// be processed. |
| 234 |
// |
| 235 |
doubleClickExpirationTime = event.time |
| 236 |
+ Display.getCurrent().getDoubleClickTime(); |
| 237 |
|
| 238 |
Item[] items = getSelection(); |
| 239 |
// Do not edit if more than one row is selected. |
| 240 |
if (items.length != 1) { |
| 241 |
item = null; |
| 242 |
return; |
| 243 |
} |
| 244 |
item = items[0]; |
| 245 |
activateCellEditor(event); |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Start editing the given element. |
| 250 |
* |
183 |
* |
| 251 |
* @param element |
184 |
* @param cell |
| 252 |
* @param column |
|
|
| 253 |
*/ |
185 |
*/ |
| 254 |
void editElement(Object element, int column) { |
186 |
void editElement(ViewerCell cell) { |
|
|
187 |
this.cell = cell; |
| 255 |
if (cellEditor != null) { |
188 |
if (cellEditor != null) { |
| 256 |
applyEditorValue(); |
189 |
applyEditorValue(); |
| 257 |
} |
190 |
} |
| 258 |
|
191 |
|
| 259 |
setSelection(createSelection(element), true); |
192 |
setSelection(createSelection(cell.getElement()), true); |
| 260 |
Item[] selection = getSelection(); |
|
|
| 261 |
if (selection.length != 1) { |
| 262 |
return; |
| 263 |
} |
| 264 |
|
| 265 |
item = selection[0]; |
| 266 |
|
193 |
|
| 267 |
// Make sure selection is visible |
|
|
| 268 |
showSelection(); |
| 269 |
columnNumber = column; |
| 270 |
activateCellEditor(); |
194 |
activateCellEditor(); |
| 271 |
|
|
|
| 272 |
} |
195 |
} |
| 273 |
|
196 |
|
| 274 |
private void saveEditorValue(CellEditor cellEditor, Item tableItem) { |
197 |
private void saveEditorValue(CellEditor cellEditor, ViewerCell cell) { |
| 275 |
ViewerColumn part = viewer.getViewerColumn(columnNumber); |
198 |
ViewerColumn part = viewer.getViewerColumn(cell.getColumnIndex()); |
| 276 |
|
199 |
|
| 277 |
if (part != null && part.getEditingSupport() != null) { |
200 |
if (part != null && part.getEditingSupport() != null) { |
| 278 |
part.getEditingSupport().setValue(tableItem.getData(), |
201 |
part.getEditingSupport().setValue(cell.getElement(), |
| 279 |
cellEditor.getValue()); |
202 |
cellEditor.getValue()); |
| 280 |
} |
203 |
} |
| 281 |
} |
204 |
} |
|
Lines 367-379
Link Here
|
| 367 |
* Position the editor inside the control |
290 |
* Position the editor inside the control |
| 368 |
* |
291 |
* |
| 369 |
* @param w |
292 |
* @param w |
| 370 |
* the editor control |
293 |
* the editor control or <code>null</code> if the editor has to |
| 371 |
* @param item |
294 |
* be reset |
| 372 |
* the item (row) in which the editor is drawn in |
295 |
* @param cell |
| 373 |
* @param fColumnNumber |
296 |
* the cell in which the editor should be displayed or |
| 374 |
* the column number in which the editor is shown |
297 |
* <code>null</code> if the editor has to be reset |
| 375 |
*/ |
298 |
*/ |
| 376 |
protected abstract void setEditor(Control w, Item item, int fColumnNumber); |
299 |
protected abstract void setEditor(Control w, ViewerCell cell); |
| 377 |
|
300 |
|
| 378 |
/** |
301 |
/** |
| 379 |
* set the layout data for the editor |
302 |
* set the layout data for the editor |