Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 172646 | Differences between
and this patch

Collapse All | Expand All

(-)Eclipse (+247 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 Tom Schindl and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Tom Schindl - initial API and implementation
10
 *******************************************************************************/
11
12
package org.eclipse.jface.snippets.viewers;
13
14
import java.util.ArrayList;
15
16
import org.eclipse.jface.viewers.CellEditor;
17
import org.eclipse.jface.viewers.ColumnLabelProvider;
18
import org.eclipse.jface.viewers.ColumnViewerEditorActivationSupport;
19
import org.eclipse.jface.viewers.EditingSupport;
20
import org.eclipse.jface.viewers.EditorActivationEvent;
21
import org.eclipse.jface.viewers.FocusCellMarkerDelegateOwnerDraw;
22
import org.eclipse.jface.viewers.ICellModifier;
23
import org.eclipse.jface.viewers.ITreeContentProvider;
24
import org.eclipse.jface.viewers.SWTCellSupport;
25
import org.eclipse.jface.viewers.TextCellEditor;
26
import org.eclipse.jface.viewers.TreeViewer;
27
import org.eclipse.jface.viewers.TreeViewerColumn;
28
import org.eclipse.jface.viewers.Viewer;
29
import org.eclipse.swt.SWT;
30
import org.eclipse.swt.graphics.Color;
31
import org.eclipse.swt.layout.FillLayout;
32
import org.eclipse.swt.widgets.Display;
33
import org.eclipse.swt.widgets.Shell;
34
import org.eclipse.swt.widgets.TreeItem;
35
36
/**
37
 * A simple TreeViewer to demonstrate usage
38
 * 
39
 * @author Tom Schindl <tom.schindl@bestsolution.at>
40
 * 
41
 */
42
public class Snippet026TreeViewerTabEditing {
43
	private class MyContentProvider implements ITreeContentProvider {
44
45
		/*
46
		 * (non-Javadoc)
47
		 * 
48
		 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
49
		 */
50
		public Object[] getElements(Object inputElement) {
51
			return ((MyModel) inputElement).child.toArray();
52
		}
53
54
		/*
55
		 * (non-Javadoc)
56
		 * 
57
		 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
58
		 */
59
		public void dispose() {
60
61
		}
62
63
		/*
64
		 * (non-Javadoc)
65
		 * 
66
		 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
67
		 *      java.lang.Object, java.lang.Object)
68
		 */
69
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
70
71
		}
72
73
		/*
74
		 * (non-Javadoc)
75
		 * 
76
		 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
77
		 */
78
		public Object[] getChildren(Object parentElement) {
79
			return getElements(parentElement);
80
		}
81
82
		/*
83
		 * (non-Javadoc)
84
		 * 
85
		 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
86
		 */
87
		public Object getParent(Object element) {
88
			if (element == null) {
89
				return null;
90
			}
91
92
			return ((MyModel) element).parent;
93
		}
94
95
		/*
96
		 * (non-Javadoc)
97
		 * 
98
		 * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
99
		 */
100
		public boolean hasChildren(Object element) {
101
			return ((MyModel) element).child.size() > 0;
102
		}
103
104
	}
105
106
	public class MyModel {
107
		public MyModel parent;
108
109
		public ArrayList child = new ArrayList();
110
111
		public int counter;
112
113
		public MyModel(int counter, MyModel parent) {
114
			this.parent = parent;
115
			this.counter = counter;
116
		}
117
118
		public String toString() {
119
			String rv = "Item ";
120
			if (parent != null) {
121
				rv = parent.toString() + ".";
122
			}
123
124
			rv += counter;
125
126
			return rv;
127
		}
128
	}
129
130
	public Snippet026TreeViewerTabEditing(final Shell shell) {
131
		final TreeViewer v = new TreeViewer(shell,SWT.BORDER|SWT.FULL_SELECTION);
132
		v.getTree().setLinesVisible(true);
133
		v.getTree().setHeaderVisible(true);
134
		
135
		SWTCellSupport cellSupport = new SWTCellSupport(v,new FocusCellMarkerDelegateOwnerDraw(v));
136
		ColumnViewerEditorActivationSupport actSupport = new ColumnViewerEditorActivationSupport(v, cellSupport) {
137
138
			protected boolean isEditorActivationEvent(
139
					EditorActivationEvent event) {
140
				return event.eventType == EditorActivationEvent.TRAVERSAL || event.eventType == EditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION || ( event.eventType == EditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR ) || event.eventType == EditorActivationEvent.PROGRAMATIC;
141
			}
142
			
143
		};
144
		actSupport.setEnableEditorActivationWithKeyboard(true);
145
		
146
		v.setEditorActivationSupport(actSupport);
147
		
148
		TreeViewerColumn column = new TreeViewerColumn(v,SWT.NONE);
149
		column.getColumn().setWidth(200);
150
		column.getColumn().setText("Column 1");
151
		column.setLabelProvider(new ColumnLabelProvider() {
152
153
			public String getText(Object element) {
154
				return "Column 1 => " + element.toString();
155
			}
156
			
157
			public Color getSelectedCellBackgroundColor() {
158
				return shell.getDisplay().getSystemColor(SWT.COLOR_DARK_GREEN);
159
			}
160
		});
161
		
162
		column = new TreeViewerColumn(v,SWT.NONE);
163
		column.getColumn().setWidth(200);
164
		column.getColumn().setText("Column 2");
165
		column.setLabelProvider(new ColumnLabelProvider() {
166
167
			public String getText(Object element) {
168
				return "Column 2 => " + element.toString();
169
			}
170
171
			public Color getSelectedCellBackgroundColor() {
172
				return shell.getDisplay().getSystemColor(SWT.COLOR_DARK_GREEN);
173
			}
174
175
			
176
		});
177
178
		v.setContentProvider(new MyContentProvider());
179
		v.setCellModifier(new ICellModifier() {
180
181
			/* (non-Javadoc)
182
			 * @see org.eclipse.jface.viewers.ICellModifier#canModify(java.lang.Object, java.lang.String)
183
			 */
184
			public boolean canModify(Object element, String property) {
185
				return true;
186
			}
187
188
			/* (non-Javadoc)
189
			 * @see org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.Object, java.lang.String)
190
			 */
191
			public Object getValue(Object element, String property) {
192
				return ((MyModel)element).counter + "";
193
			}
194
195
			/* (non-Javadoc)
196
			 * @see org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Object, java.lang.String, java.lang.Object)
197
			 */
198
			public void modify(Object element, String property, Object value) {
199
				TreeItem item = (TreeItem)element;
200
				((MyModel)item.getData()).counter = Integer.parseInt(value.toString());
201
				v.update(item.getData(), null);
202
			}
203
			
204
		});
205
		
206
		v.setColumnProperties(new String[] { "column1", "column2" });
207
		v.setCellEditors(new CellEditor[] { new TextCellEditor(v.getTree()), new TextCellEditor(v.getTree()) });
208
		v.setTabEditingStyle(EditingSupport.TABBING_HORIZONTAL|EditingSupport.TABBING_MOVE_TO_ROW_NEIGHBOR|EditingSupport.TABBING_VERTICAL);
209
		
210
		v.setInput(createModel());
211
	}
212
213
	private MyModel createModel() {
214
215
		MyModel root = new MyModel(0, null);
216
		root.counter = 0;
217
218
		MyModel tmp;
219
		MyModel subItem;
220
		for (int i = 1; i < 10; i++) {
221
			tmp = new MyModel(i, root);
222
			root.child.add(tmp);
223
			for (int j = 1; j < i; j++) {
224
				subItem = new MyModel(j, tmp);
225
				subItem.child.add(new MyModel(j*100,subItem));
226
				tmp.child.add(subItem);
227
			}
228
		}
229
230
		return root;
231
	}
232
233
	public static void main(String[] args) {
234
		Display display = new Display();
235
		Shell shell = new Shell(display);
236
		shell.setLayout(new FillLayout());
237
		new Snippet026TreeViewerTabEditing(shell);
238
		shell.open();
239
240
		while (!shell.isDisposed()) {
241
			if (!display.readAndDispatch())
242
				display.sleep();
243
		}
244
245
		display.dispose();
246
	}
247
}
(-)src/org/eclipse/jface/viewers/TableViewer.java (-8 lines)
Lines 134-143 Link Here
134
				return new StructuredSelection(element);
134
				return new StructuredSelection(element);
135
			}
135
			}
136
136
137
			protected Item[] getSelection() {
138
				return table.getSelection();
139
			}
140
141
			protected void setEditor(Control w, Item item, int fColumnNumber) {
137
			protected void setEditor(Control w, Item item, int fColumnNumber) {
142
				tableEditor.setEditor(w, (TableItem) item, fColumnNumber);
138
				tableEditor.setEditor(w, (TableItem) item, fColumnNumber);
143
			}
139
			}
Lines 148-157 Link Here
148
				tableEditor.minimumWidth = layoutData.minimumWidth;
144
				tableEditor.minimumWidth = layoutData.minimumWidth;
149
			}
145
			}
150
146
151
			protected void showSelection() {
152
				table.showSelection();
153
			}
154
155
		};
147
		};
156
	}
148
	}
157
	
149
	
(-)src/org/eclipse/jface/viewers/TreeViewer.java (-8 lines)
Lines 284-293 Link Here
284
				return new StructuredSelection(element);
284
				return new StructuredSelection(element);
285
			}
285
			}
286
286
287
			protected Item[] getSelection() {
288
				return tree.getSelection();
289
			}
290
291
			protected void setEditor(Control w, Item item, int fColumnNumber) {
287
			protected void setEditor(Control w, Item item, int fColumnNumber) {
292
				treeEditor.setEditor(w, (TreeItem) item, fColumnNumber);
288
				treeEditor.setEditor(w, (TreeItem) item, fColumnNumber);
293
			}
289
			}
Lines 298-307 Link Here
298
				treeEditor.minimumWidth = layoutData.minimumWidth;
294
				treeEditor.minimumWidth = layoutData.minimumWidth;
299
			}
295
			}
300
296
301
			protected void showSelection() {
302
				getTree().showSelection();
303
			}
304
305
		};
297
		};
306
	}
298
	}
307
299
(-)src/org/eclipse/jface/viewers/EditingSupport.java (-2 / +14 lines)
Lines 13-18 Link Here
13
13
14
package org.eclipse.jface.viewers;
14
package org.eclipse.jface.viewers;
15
15
16
import java.util.List;
17
16
import org.eclipse.core.runtime.Assert;
18
import org.eclipse.core.runtime.Assert;
17
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.events.TraverseEvent;
20
import org.eclipse.swt.events.TraverseEvent;
Lines 170-177 Link Here
170
		}
172
		}
171
173
172
		if (cell2edit != null) {
174
		if (cell2edit != null) {
173
			viewer.editElement(cell2edit.getElement(), cell2edit
175
			List l = viewer.getSelectionFromWidget();
174
					.getColumnIndex());
176
						
177
			viewer.getControl().setRedraw(false);
178
			
179
			//TODO Is this correct in terms of other controls e.g. grid
180
			if( ! l.contains(cell2edit.getElement()) ) {
181
				viewer.setSelection(new StructuredSelection(cell2edit.getElement()));
182
			}
183
						
184
			EditorActivationEvent acEvent = new EditorActivationEvent(cell2edit,event);
185
			viewer.triggerEditorActivationEvent(acEvent);
186
			viewer.getControl().setRedraw(true);
175
		}
187
		}
176
	}
188
	}
177
189
(-)src/org/eclipse/jface/viewers/ColumnViewer.java (-39 / +110 lines)
Lines 14-19 Link Here
14
14
15
package org.eclipse.jface.viewers;
15
package org.eclipse.jface.viewers;
16
16
17
import java.util.List;
18
17
import org.eclipse.core.runtime.Assert;
19
import org.eclipse.core.runtime.Assert;
18
import org.eclipse.swt.events.MouseAdapter;
20
import org.eclipse.swt.events.MouseAdapter;
19
import org.eclipse.swt.events.MouseEvent;
21
import org.eclipse.swt.events.MouseEvent;
Lines 76-91 Link Here
76
		if (viewerEditor != null) {
78
		if (viewerEditor != null) {
77
			control.addMouseListener(new MouseAdapter() {
79
			control.addMouseListener(new MouseAdapter() {
78
				public void mouseDown(MouseEvent e) {
80
				public void mouseDown(MouseEvent e) {
79
					viewerEditor.handleMouseDown(e);
81
					handleMouseDown(e);
80
				}
82
				}
81
			});
83
			});
82
		}
84
		}
83
	}
85
	}
84
86
85
	/**
87
	/**
86
	 * Creates the viewer editor used for editing cell contents. To be implemented by subclasses.
88
	 * Creates the viewer editor used for editing cell contents. To be
89
	 * implemented by subclasses.
87
	 * 
90
	 * 
88
	 * @return the editor, or <code>null</code> if this viewer does not support editing cell contents.
91
	 * @return the editor, or <code>null</code> if this viewer does not
92
	 *         support editing cell contents.
89
	 */
93
	 */
90
	protected abstract ColumnViewerEditor createViewerEditor();
94
	protected abstract ColumnViewerEditor createViewerEditor();
91
95
Lines 95-101 Link Here
95
	 * 
99
	 * 
96
	 * @param point
100
	 * @param point
97
	 *            the widget-relative coordinates
101
	 *            the widget-relative coordinates
98
	 * @return the cell or <code>null</code> if no cell is found at the given point
102
	 * @return the cell or <code>null</code> if no cell is found at the given
103
	 *         point
99
	 */
104
	 */
100
	ViewerCell getCell(Point point) {
105
	ViewerCell getCell(Point point) {
101
		ViewerRow row = getViewerRow(point);
106
		ViewerRow row = getViewerRow(point);
Lines 127-133 Link Here
127
	/**
132
	/**
128
	 * Returns the viewer row associated with the given row widget.
133
	 * Returns the viewer row associated with the given row widget.
129
	 * 
134
	 * 
130
	 * @param item the row widget
135
	 * @param item
136
	 *            the row widget
131
	 * @return ViewerRow the associated viewer row
137
	 * @return ViewerRow the associated viewer row
132
	 */
138
	 */
133
	protected ViewerRow getViewerRowFromItem(Widget item) {
139
	protected ViewerRow getViewerRowFromItem(Widget item) {
Lines 137-143 Link Here
137
	/**
143
	/**
138
	 * Returns the column widget at the given column index.
144
	 * Returns the column widget at the given column index.
139
	 * 
145
	 * 
140
	 * @param columnIndex the column index
146
	 * @param columnIndex
147
	 *            the column index
141
	 * @return Widget the column widget
148
	 * @return Widget the column widget
142
	 */
149
	 */
143
	protected abstract Widget getColumnViewerOwner(int columnIndex);
150
	protected abstract Widget getColumnViewerOwner(int columnIndex);
Lines 150-156 Link Here
150
	 * @return the viewer column at the given index, or <code>null</code> if
157
	 * @return the viewer column at the given index, or <code>null</code> if
151
	 *         there is none for the given index
158
	 *         there is none for the given index
152
	 */
159
	 */
153
	/* package */ ViewerColumn getViewerColumn(final int columnIndex) {
160
	/* package */ViewerColumn getViewerColumn(final int columnIndex) {
154
161
155
		ViewerColumn viewer;
162
		ViewerColumn viewer;
156
		Widget columnOwner = getColumnViewerOwner(columnIndex);
163
		Widget columnOwner = getColumnViewerOwner(columnIndex);
Lines 176-182 Link Here
176
	}
183
	}
177
184
178
	/**
185
	/**
179
	 * Sets up editing support for the given column based on the "old" cell editor API.
186
	 * Sets up editing support for the given column based on the "old" cell
187
	 * editor API.
180
	 * 
188
	 * 
181
	 * @param columnIndex
189
	 * @param columnIndex
182
	 * @param viewer
190
	 * @param viewer
Lines 229-243 Link Here
229
	}
237
	}
230
238
231
	/**
239
	/**
232
	 * Creates a generic viewer column for the given column widget, based on the given label provider.
240
	 * Creates a generic viewer column for the given column widget, based on the
241
	 * given label provider.
233
	 * 
242
	 * 
234
	 * @param columnOwner the column widget
243
	 * @param columnOwner
235
	 * @param labelProvider the label provider to use for the column
244
	 *            the column widget
245
	 * @param labelProvider
246
	 *            the label provider to use for the column
236
	 * @return ViewerColumn the viewer column
247
	 * @return ViewerColumn the viewer column
237
	 */
248
	 */
238
	private ViewerColumn createViewerColumn(Widget columnOwner,
249
	private ViewerColumn createViewerColumn(Widget columnOwner,
239
			CellLabelProvider labelProvider) {
250
			CellLabelProvider labelProvider) {
240
		ViewerColumn column = new ViewerColumn(this,columnOwner) {};
251
		ViewerColumn column = new ViewerColumn(this, columnOwner) {
252
		};
241
		column.setLabelProvider(labelProvider, false);
253
		column.setLabelProvider(labelProvider, false);
242
		return column;
254
		return column;
243
	}
255
	}
Lines 269-287 Link Here
269
	 * @param column
281
	 * @param column
270
	 * @return ViewerCell
282
	 * @return ViewerCell
271
	 */
283
	 */
272
	/* package */ ViewerCell updateCell(ViewerRow rowItem, int column) {
284
	/* package */ViewerCell updateCell(ViewerRow rowItem, int column) {
273
		cell.update(rowItem, column);
285
		cell.update(rowItem, column);
274
		return cell;
286
		return cell;
275
	}
287
	}
276
288
277
	/**
289
	/**
278
	 * Returns the {@link Item} at the given widget-relative coordinates, or
290
	 * Returns the {@link Item} at the given widget-relative coordinates, or
279
     * <code>null</code> if there is no item at the given coordinates.
291
	 * <code>null</code> if there is no item at the given coordinates.
280
	 * 
292
	 * 
281
	 * @param point
293
	 * @param point
282
	 *            the widget-relative coordinates
294
	 *            the widget-relative coordinates
283
	 * @return the {@link Item} at the coordinates or <code>null</code> if there
295
	 * @return the {@link Item} at the coordinates or <code>null</code> if
284
	 *         is no item at the given coordinates
296
	 *         there is no item at the given coordinates
285
	 */
297
	 */
286
	protected abstract Item getItemAt(Point point);
298
	protected abstract Item getItemAt(Point point);
287
299
Lines 339-345 Link Here
339
351
340
	/**
352
	/**
341
	 * Cancels a currently active cell editor if one is active. All changes
353
	 * Cancels a currently active cell editor if one is active. All changes
342
     * already done in the cell editor are lost.
354
	 * already done in the cell editor are lost.
343
	 * 
355
	 * 
344
	 * @since 3.1 (in subclasses, added in 3.3 to abstract class)
356
	 * @since 3.1 (in subclasses, added in 3.3 to abstract class)
345
	 */
357
	 */
Lines 371-387 Link Here
371
	 */
383
	 */
372
	public void editElement(Object element, int column) {
384
	public void editElement(Object element, int column) {
373
		if (viewerEditor != null) {
385
		if (viewerEditor != null) {
374
			viewerEditor.editElement(element, column);
386
			Widget item = findItem(element);
387
			if (item != null) {
388
				ViewerRow row = getViewerRowFromItem(item);
389
				if (row != null) {
390
					ViewerCell cell = row.getCell(column);
391
					if (cell != null) {
392
						getControl().setRedraw(false);
393
394
						List l = getSelectionFromWidget();
395
396
						if (l.size() > 1 || !l.contains(cell.getElement())) {
397
							setSelection(new StructuredSelection(cell
398
									.getElement()));
399
						}
400
401
						triggerEditorActivationEvent(new EditorActivationEvent(
402
								cell));
403
						getControl().setRedraw(true);
404
					}
405
				}
406
			}
375
		}
407
		}
376
	}
408
	}
377
409
378
	/**
410
	/**
379
	 * Return the CellEditors for the receiver, or <code>null</code> if no
411
	 * Return the CellEditors for the receiver, or <code>null</code> if no
380
     * cell editors are set.
412
	 * cell editors are set.
381
	 * <p>
413
	 * <p>
382
	 * Since 3.3, an alternative API is available, see
414
	 * Since 3.3, an alternative API is available, see
383
	 * {@link ViewerColumn#setEditingSupport(EditingSupport)} for a more flexible
415
	 * {@link ViewerColumn#setEditingSupport(EditingSupport)} for a more
384
	 * way of editing values in a column viewer.
416
	 * flexible way of editing values in a column viewer.
385
	 * </p>
417
	 * </p>
386
	 * 
418
	 * 
387
	 * @return CellEditor[]
419
	 * @return CellEditor[]
Lines 398-409 Link Here
398
430
399
	/**
431
	/**
400
	 * Returns the cell modifier of this viewer, or <code>null</code> if none
432
	 * Returns the cell modifier of this viewer, or <code>null</code> if none
401
     * has been set.
433
	 * has been set.
402
	 * 
434
	 * 
403
	 * <p>
435
	 * <p>
404
	 * Since 3.3, an alternative API is available, see
436
	 * Since 3.3, an alternative API is available, see
405
	 * {@link ViewerColumn#setEditingSupport(EditingSupport)} for a more flexible
437
	 * {@link ViewerColumn#setEditingSupport(EditingSupport)} for a more
406
	 * way of editing values in a column viewer.
438
	 * flexible way of editing values in a column viewer.
407
	 * </p>
439
	 * </p>
408
	 * 
440
	 * 
409
	 * @return the cell modifier, or <code>null</code>
441
	 * @return the cell modifier, or <code>null</code>
Lines 425-432 Link Here
425
	 * 
457
	 * 
426
	 * <p>
458
	 * <p>
427
	 * Since 3.3, an alternative API is available, see
459
	 * Since 3.3, an alternative API is available, see
428
	 * {@link ViewerColumn#setEditingSupport(EditingSupport)} for a more flexible
460
	 * {@link ViewerColumn#setEditingSupport(EditingSupport)} for a more
429
	 * way of editing values in a column viewer.
461
	 * flexible way of editing values in a column viewer.
430
	 * </p>
462
	 * </p>
431
	 * 
463
	 * 
432
	 * @return the list of column properties
464
	 * @return the list of column properties
Lines 446-453 Link Here
446
	 * 
478
	 * 
447
	 * <p>
479
	 * <p>
448
	 * Since 3.3, an alternative API is available, see
480
	 * Since 3.3, an alternative API is available, see
449
	 * {@link ViewerColumn#setEditingSupport(EditingSupport)} for a more flexible
481
	 * {@link ViewerColumn#setEditingSupport(EditingSupport)} for a more
450
	 * way of editing values in a column viewer.
482
	 * flexible way of editing values in a column viewer.
451
	 * </p>
483
	 * </p>
452
	 * 
484
	 * 
453
	 * @return <code>true</code> if there is an active cell editor, and
485
	 * @return <code>true</code> if there is an active cell editor, and
Lines 469-476 Link Here
469
	 * 
501
	 * 
470
	 * <p>
502
	 * <p>
471
	 * Since 3.3, an alternative API is available, see
503
	 * Since 3.3, an alternative API is available, see
472
	 * {@link ViewerColumn#setEditingSupport(EditingSupport)} for a more flexible
504
	 * {@link ViewerColumn#setEditingSupport(EditingSupport)} for a more
473
	 * way of editing values in a column viewer.
505
	 * flexible way of editing values in a column viewer.
474
	 * </p>
506
	 * </p>
475
	 * 
507
	 * 
476
	 * @param editors
508
	 * @param editors
Lines 486-498 Link Here
486
	}
518
	}
487
519
488
	/**
520
	/**
489
	 * Sets the cell modifier for this column viewer. This method does nothing if editing
521
	 * Sets the cell modifier for this column viewer. This method does nothing
490
	 * is not supported by this viewer.
522
	 * if editing is not supported by this viewer.
491
	 * 
523
	 * 
492
	 * <p>
524
	 * <p>
493
	 * Since 3.3, an alternative API is available, see
525
	 * Since 3.3, an alternative API is available, see
494
	 * {@link ViewerColumn#setEditingSupport(EditingSupport)} for a more flexible
526
	 * {@link ViewerColumn#setEditingSupport(EditingSupport)} for a more
495
	 * way of editing values in a column viewer.
527
	 * flexible way of editing values in a column viewer.
496
	 * </p>
528
	 * </p>
497
	 * 
529
	 * 
498
	 * @param modifier
530
	 * @param modifier
Lines 515-522 Link Here
515
	 * 
547
	 * 
516
	 * <p>
548
	 * <p>
517
	 * Since 3.3, an alternative API is available, see
549
	 * Since 3.3, an alternative API is available, see
518
	 * {@link ViewerColumn#setEditingSupport(EditingSupport)} for a more flexible
550
	 * {@link ViewerColumn#setEditingSupport(EditingSupport)} for a more
519
	 * way of editing values in a column viewer.
551
	 * flexible way of editing values in a column viewer.
520
	 * </p>
552
	 * </p>
521
	 * 
553
	 * 
522
	 * @param columnProperties
554
	 * @param columnProperties
Lines 530-536 Link Here
530
			viewerEditor.setColumnProperties(columnProperties);
562
			viewerEditor.setColumnProperties(columnProperties);
531
		}
563
		}
532
	}
564
	}
533
	
565
534
	/**
566
	/**
535
	 * The tab-editing style used if the default implementation is used
567
	 * The tab-editing style used if the default implementation is used
536
	 * 
568
	 * 
Lines 544-550 Link Here
544
	int getTabEditingStyle() {
576
	int getTabEditingStyle() {
545
		return this.tabEditingStyle;
577
		return this.tabEditingStyle;
546
	}
578
	}
547
	
579
548
	/**
580
	/**
549
	 * Returns the number of columns contained in the receiver. If no columns
581
	 * Returns the number of columns contained in the receiver. If no columns
550
	 * were created by the programmer, this value is zero, despite the fact that
582
	 * were created by the programmer, this value is zero, despite the fact that
Lines 576-579 Link Here
576
		}
608
		}
577
		return null;
609
		return null;
578
	}
610
	}
579
}
611
612
	private void handleMouseDown(MouseEvent e) {
613
		ViewerCell cell = getCell(new Point(e.x, e.y));
614
615
		if (cell != null) {
616
			triggerEditorActivationEvent(new EditorActivationEvent(cell, e));
617
		}
618
	}
619
620
	/**
621
	 * Invoking this method fires an editor activation event which tries to
622
	 * enable the editor but before this event is passed to
623
	 * {@link ColumnViewerEditorActivationSupport} to see if this event should
624
	 * really trigger editor activation
625
	 * 
626
	 * @param event
627
	 *            the activation event
628
	 */
629
	protected void triggerEditorActivationEvent(EditorActivationEvent event) {
630
		viewerEditor.handleEditorActivationEvent(event);
631
	}
632
633
	/**
634
	 * To fully control the strategy used to control on which cell-selection
635
	 * events the editor has to be activated you can pass your customized
636
	 * strategy using this method.
637
	 * 
638
	 * @param editorActivationStrategy
639
	 *            the strategy used to activate an editor
640
	 */
641
	public void setEditorActivationSupport(
642
			ColumnViewerEditorActivationSupport editorActivationStrategy) {
643
		if (viewerEditor != null) {
644
			viewerEditor.setEditorActivationStrategy(editorActivationStrategy);
645
		} else {
646
			throw new IllegalStateException(
647
					"This feature is not supported by the widget"); //$NON-NLS-1$
648
		}
649
	}
650
}
(-)src/org/eclipse/jface/viewers/CellEditor.java (+10 lines)
Lines 853-856 Link Here
853
        dirty = true;
853
        dirty = true;
854
        fireEditorValueChanged(oldValidState, newValidState);
854
        fireEditorValueChanged(oldValidState, newValidState);
855
    }
855
    }
856
    
857
    /**
858
     * Activate the editor but also inform the editor which event triggered its activation.
859
     * <b>The default implementation simply calls {@link #activate()}</b>
860
     * 
861
     * @param activationEvent the editor activation event
862
     */
863
    public void activate(EditorActivationEvent activationEvent) {
864
    	activate();
865
    }
856
}
866
}
(-)src/org/eclipse/jface/viewers/ColumnViewerEditor.java (-115 / +124 lines)
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 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(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
			ColumnViewerEditorActivationSupport 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
}
(-)src/org/eclipse/jface/viewers/FocusCellMarkerDelegateOwnerDraw.java (+128 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.jface.viewers;
13
14
import org.eclipse.core.runtime.Assert;
15
import org.eclipse.swt.SWT;
16
import org.eclipse.swt.graphics.Color;
17
import org.eclipse.swt.graphics.GC;
18
import org.eclipse.swt.widgets.Event;
19
import org.eclipse.swt.widgets.Listener;
20
21
/**
22
 * @since 3.3
23
 * 
24
 */
25
public class FocusCellMarkerDelegateOwnerDraw extends FocusCellMarkerDelegate {
26
	
27
	/**
28
	 * @param viewer the viewer
29
	 */
30
	public FocusCellMarkerDelegateOwnerDraw(ColumnViewer viewer) {
31
		super(viewer);
32
		hookListener(viewer);
33
	}
34
	
35
	private void markFocusedCell(Event event,
36
			ViewerCell cell) {
37
		Color background = getSelectedCellBackgroundColor(cell);
38
		Color foreground = getSelectedCellForegroundColor(cell);
39
40
		if (foreground != null || background != null) {
41
			GC gc = event.gc;
42
43
			if (background == null) {
44
				background = cell.getItem().getDisplay().getSystemColor(
45
						SWT.COLOR_LIST_SELECTION);
46
			}
47
48
			if (foreground == null) {
49
				foreground = cell.getItem().getDisplay().getSystemColor(
50
						SWT.COLOR_LIST_SELECTION_TEXT);
51
			}
52
53
			gc.setBackground(background);
54
			gc.setForeground(foreground);
55
			gc.fillRectangle(event.getBounds());
56
			
57
			// This is a workaround for an SWT-Bug on WinXP bug 169517
58
			gc.drawText(" ", cell.getBounds().x, cell.getBounds().y, false); //$NON-NLS-1$
59
			event.detail &= ~SWT.SELECTED;
60
		}
61
	}
62
63
	private void removeSelectionInformation(Event event,
64
			ViewerCell cell) {
65
		GC gc = event.gc;
66
		gc.setBackground(cell.getViewerRow().getBackground(
67
				cell.getColumnIndex()));
68
		gc.setForeground(cell.getViewerRow().getForeground(
69
				cell.getColumnIndex()));
70
		gc.fillRectangle(cell.getBounds());
71
		// This is a workaround for an SWT-Bug on WinXP bug 169517
72
		gc.drawText(" ", cell.getBounds().x, cell.getBounds().y, false); //$NON-NLS-1$
73
		event.detail &= ~SWT.SELECTED;
74
	}
75
76
	private void hookListener(final ColumnViewer viewer) {
77
		System.err.println("HOOKING LISTENER"); //$NON-NLS-1$
78
		
79
		Listener listener = new Listener() {
80
81
			public void handleEvent(Event event) {
82
				if((event.detail & SWT.SELECTED) > 0 ) {
83
					System.err.println("ERASE EVENT"); //$NON-NLS-1$
84
					ViewerCell focusCell = getFocusCell();
85
					ViewerRow row = viewer.getViewerRowFromItem(event.item);
86
					
87
					Assert.isNotNull(row, "Internal structure invalid. Item without associated row is not possible."); //$NON-NLS-1$
88
					
89
					ViewerCell cell = row.getCell(event.index);
90
					
91
//					if( focusCell != null ) {
92
//						System.err.println("FOCUS: " + focusCell.getColumnIndex() + " =>" + focusCell.getItem() ); //$NON-NLS-1$ //$NON-NLS-2$
93
//					}
94
//					
95
//					if( cell != null ) {
96
//						System.err.println("TO COLOR: " + cell.getColumnIndex() + " =>" + cell.getItem() ); //$NON-NLS-1$ //$NON-NLS-2$
97
//					}
98
					
99
					if (focusCell == null || ! cell.equals(focusCell)) {
100
						removeSelectionInformation(event, cell);
101
					} else {
102
						markFocusedCell(event,cell);
103
					}
104
				}
105
			}
106
			
107
		};
108
		viewer.getControl().addListener(SWT.EraseItem, listener);
109
	}
110
111
	/**
112
	 * @param cell
113
	 *            the cell which is colored
114
	 * @return the color
115
	 */
116
	protected Color getSelectedCellBackgroundColor(ViewerCell cell) {
117
		return null;
118
	}
119
120
	/**
121
	 * @param cell
122
	 *            the cell which is colored
123
	 * @return the color
124
	 */
125
	protected Color getSelectedCellForegroundColor(ViewerCell cell) {
126
		return null;
127
	}
128
}
(-)src/org/eclipse/jface/viewers/CellNavigationDelegate.java (+151 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.jface.viewers;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.widgets.Event;
16
17
/**
18
 * @since 3.3
19
 * 
20
 */
21
public class CellNavigationDelegate {
22
	/**
23
	 * is the given event an event which moves the selection to another cell
24
	 * 
25
	 * @param viewer
26
	 *            the viewer we are working for
27
	 * 
28
	 * @param event
29
	 *            the key event
30
	 * @return <code>true</code> if a new cell is searched
31
	 */
32
	public boolean isNavigationEvent(ColumnViewer viewer, Event event) {
33
		switch (event.keyCode) {
34
		case SWT.ARROW_UP:
35
		case SWT.ARROW_DOWN:
36
		case SWT.ARROW_LEFT:
37
		case SWT.ARROW_RIGHT:
38
		case SWT.HOME:
39
		case SWT.PAGE_DOWN:
40
		case SWT.PAGE_UP:
41
		case SWT.END:
42
			return true;
43
		default:
44
			return false;
45
		}
46
	}
47
48
	/**
49
	 * @param viewer
50
	 *            the viewer we are working for
51
	 * @param cellToCollapse the cell to collapse
52
	 * @param event
53
	 *            the key event
54
	 * @return <code>true</code> if this event triggers collapsing of a node
55
	 */
56
	public boolean isCollapseEvent(ColumnViewer viewer, ViewerCell cellToCollapse, Event event) {
57
		return false;
58
	}
59
60
	/**
61
	 * @param viewer
62
	 *            the viewer we are working for
63
	 * @param cellToExpand the cell to expand
64
	 * @param event
65
	 *            the key event
66
	 * @return <code>true</code> if this event triggers expanding of a node
67
	 */
68
	public boolean isExpandEvent(ColumnViewer viewer, ViewerCell cellToExpand, Event event) {
69
		return false;
70
	}
71
72
	/**
73
	 * @param viewer
74
	 *            the viewer working for
75
	 * @param cellToExpand
76
	 *            the cell the user wants to expand
77
	 * @param event
78
	 *            the event triggering the exansion
79
	 */
80
	public void expand(ColumnViewer viewer, ViewerCell cellToExpand, Event event) {
81
82
	}
83
84
	/**
85
	 * @param viewer
86
	 *            the viewer working for
87
	 * @param cellToCollapse
88
	 *            the cell the user wants to collapse
89
	 * @param event
90
	 *            the event triggering the exansion
91
	 */
92
	public void collapse(ColumnViewer viewer, ViewerCell cellToCollapse,
93
			Event event) {
94
95
	}
96
97
	/**
98
	 * @param viewer
99
	 *            the viewer we are working for
100
	 * @param currentSelectedCell
101
	 *            the cell currently selected
102
	 * @param event
103
	 *            the key event
104
	 * @return the cell which is highlighted next or <code>null</code> if the
105
	 *         default implementation is taken. E.g. it's fairly impossible to
106
	 *         react on PAGE_DOWN requests
107
	 */
108
	public ViewerCell findSelectedCell(ColumnViewer viewer,
109
			ViewerCell currentSelectedCell, Event event) {
110
111
		switch (event.keyCode) {
112
		case SWT.ARROW_UP:
113
			if (currentSelectedCell != null) {
114
				return currentSelectedCell.getNeighbor(ViewerCell.ABOVE, false);
115
			}
116
			break;
117
		case SWT.ARROW_DOWN:
118
			if (currentSelectedCell != null) {
119
				return currentSelectedCell.getNeighbor(ViewerCell.BELOW, false);
120
			}
121
			break;
122
		case SWT.ARROW_LEFT:
123
			if (currentSelectedCell != null) {
124
				return currentSelectedCell.getNeighbor(ViewerCell.LEFT, true);
125
			}
126
			break;
127
		case SWT.ARROW_RIGHT:
128
			if (currentSelectedCell != null) {
129
				return currentSelectedCell.getNeighbor(ViewerCell.RIGHT, true);
130
			}
131
			break;
132
		}
133
134
		return null;
135
	}
136
137
	/**
138
	 * This method is consulted to decide whether an event has to be cancled or
139
	 * not. By default events who collapse/expand tree-nodes are cancled
140
	 * 
141
	 * @param viewer
142
	 *            the viewer working for
143
	 * @param event
144
	 *            the event
145
	 * @return <code>true</code> if the event has to be cancled
146
	 */
147
	public boolean cancleEvent(ColumnViewer viewer, Event event) {
148
		return event.keyCode == SWT.ARROW_LEFT
149
				|| event.keyCode == SWT.ARROW_RIGHT;
150
	}
151
}
(-)src/org/eclipse/jface/viewers/FocusCellMarkerDelegate.java (+53 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.jface.viewers;
13
14
/**
15
 * Implementors of this class are responsible to highlight the selected cell and
16
 * to remove the highlighting of cells who are not selected
17
 * 
18
 * @since 3.3
19
 * 
20
 */
21
public abstract class FocusCellMarkerDelegate {
22
	private CellSupport cellSupport;
23
	
24
	private ColumnViewer viewer;
25
26
	/**
27
	 * @param viewer
28
	 */
29
	public FocusCellMarkerDelegate(ColumnViewer viewer) {
30
		this.viewer = viewer;
31
	}
32
	
33
	/**
34
	 * @param cellSupport associate the cell support with this class
35
	 */
36
	protected void setCellSupport(CellSupport cellSupport) {
37
		this.cellSupport = cellSupport;
38
	}
39
40
	/**
41
	 * @return the viewer
42
	 */
43
	public ColumnViewer getViewer() {
44
		return viewer;
45
	}
46
	
47
	/**
48
	 * @return the cell currently holding the focus
49
	 */
50
	protected ViewerCell getFocusCell() {
51
		return cellSupport.getFocusCell();
52
	}
53
}
(-)src/org/eclipse/jface/viewers/ColumnViewerEditorActivationSupport.java (+96 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.jface.viewers;
13
14
import org.eclipse.swt.events.KeyEvent;
15
import org.eclipse.swt.events.KeyListener;
16
17
/**
18
 * This class is responsible to determin if a cell selection event is triggers
19
 * an editor activation
20
 * 
21
 * @since 3.3
22
 */
23
public class ColumnViewerEditorActivationSupport {
24
	private ColumnViewer viewer;
25
	
26
	private KeyListener keyboardActivationListener;
27
	
28
	private CellSupport cellSupport;
29
	
30
	/**
31
	 * @param viewer the viewer the editor support is attached to
32
	 * @param cellSupport the cell support or <code>null</code> if there's none
33
	 */
34
	public ColumnViewerEditorActivationSupport(ColumnViewer viewer, CellSupport cellSupport) {
35
		this.viewer = viewer;
36
		this.cellSupport = cellSupport;
37
	}
38
	
39
	/**
40
	 * @return the cell holding the current focus
41
	 */
42
	protected ViewerCell getFocusCell() {
43
		if( cellSupport != null ) {
44
			return cellSupport.getFocusCell();
45
		}
46
		
47
		return null;
48
	}
49
	
50
	/**
51
	 * @param event
52
	 * @return bla bl
53
	 */
54
	protected boolean isEditorActivationEvent(EditorActivationEvent event) {
55
		return event.eventType == EditorActivationEvent.TRAVERSAL || event.eventType == EditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION || event.eventType == EditorActivationEvent.PROGRAMATIC;
56
	}
57
	
58
	/**
59
	 * @return the viewer
60
	 */
61
	public ColumnViewer getViewer() {
62
		return viewer;
63
	}
64
	
65
	/**
66
	 * Enable activation of cell editors by keyboard
67
	 * @param enable <code>true</code> to enable
68
	 */
69
	public void setEnableEditorActivationWithKeyboard(boolean enable) {
70
		if( enable ) {
71
			if( keyboardActivationListener == null ) {
72
				keyboardActivationListener = new KeyListener() {
73
74
					public void keyPressed(KeyEvent e) {
75
						ViewerCell cell = getFocusCell();
76
						
77
						if( cell != null ) {
78
							viewer.triggerEditorActivationEvent(new EditorActivationEvent(cell,e));
79
						}
80
					}
81
82
					public void keyReleased(KeyEvent e) {
83
						
84
					}
85
					
86
				};
87
				viewer.getControl().addKeyListener(keyboardActivationListener);
88
			}
89
		} else {
90
			if( keyboardActivationListener != null ) {
91
				viewer.getControl().removeKeyListener(keyboardActivationListener);
92
				keyboardActivationListener = null;
93
			}
94
		}
95
	}
96
}
(-)src/org/eclipse/jface/viewers/CellFocusChangedEvent.java (+51 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.jface.viewers;
13
14
import java.util.EventObject;
15
16
/**
17
 * @since 3.3
18
 * 
19
 */
20
public class CellFocusChangedEvent extends EventObject {
21
22
	/**
23
	 * 
24
	 */
25
	private static final long serialVersionUID = 1L;
26
27
	/**
28
	 * the cell who recevied the focus
29
	 */
30
	public ViewerCell focusCell;
31
	
32
	/**
33
	 * the cell who lost the focus
34
	 */
35
	public ViewerCell focusLostCell;
36
	
37
	/**
38
	 * @param viewer
39
	 *            the viewer
40
	 * @param focusCell
41
	 *            the cell who recevied the focus
42
	 * @param focusLostCell
43
	 *            the cell who lost the focus
44
	 */
45
	public CellFocusChangedEvent(ColumnViewer viewer, ViewerCell focusCell,
46
			ViewerCell focusLostCell) {
47
		super(viewer);
48
		this.focusCell=focusCell;
49
		this.focusLostCell=focusLostCell;
50
	}
51
}
(-)src/org/eclipse/jface/viewers/EditorDeactivationEvent.java (+41 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.jface.viewers;
13
14
import java.util.EventObject;
15
16
/**
17
 * This event is fired when an editor deactivated
18
 * 
19
 * @since 3.3
20
 * 
21
 */
22
public class EditorDeactivationEvent extends EventObject {
23
24
	/**
25
	 * 
26
	 */
27
	private static final long serialVersionUID = 1L;
28
29
	/**
30
	 * Cancle the event
31
	 */
32
	public boolean cancle = false;
33
	
34
	/**
35
	 * @param source
36
	 */
37
	public EditorDeactivationEvent(Object source) {
38
		super(source);
39
	}
40
41
}
(-)src/org/eclipse/jface/viewers/SWTCellSupport.java (+164 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.jface.viewers;
13
14
import org.eclipse.core.runtime.Assert;
15
import org.eclipse.swt.SWT;
16
import org.eclipse.swt.graphics.Point;
17
import org.eclipse.swt.widgets.Event;
18
import org.eclipse.swt.widgets.Listener;
19
import org.eclipse.swt.widgets.TreeItem;
20
21
/**
22
 * @since 3.3
23
 * 
24
 */
25
public final class SWTCellSupport extends CellSupport {
26
	private static final CellNavigationDelegate TABLE_NAVIGATE = new CellNavigationDelegate();
27
28
	private static final CellNavigationDelegate TREE_NAVIGATE = new CellNavigationDelegate() {
29
		public void collapse(ColumnViewer viewer, ViewerCell cellToCollapse,
30
				Event event) {
31
			if (cellToCollapse != null) {
32
				((TreeItem) cellToCollapse.getItem()).setExpanded(false);
33
			}
34
		}
35
36
		public void expand(ColumnViewer viewer, ViewerCell cellToExpand,
37
				Event event) {
38
			if (cellToExpand != null) {
39
				TreeViewer v = (TreeViewer) viewer;
40
				v.setExpandedState(v
41
						.getTreePathFromItem(cellToExpand.getItem()), true);
42
			}
43
		}
44
45
		public boolean isCollapseEvent(ColumnViewer viewer,
46
				ViewerCell cellToCollapse, Event event) {
47
			return cellToCollapse != null
48
					&& ((TreeItem) cellToCollapse.getItem()).getExpanded()
49
					&& cellToCollapse.getColumnIndex() == 0
50
					&& event.keyCode == SWT.ARROW_LEFT;
51
		}
52
53
		public boolean isExpandEvent(ColumnViewer viewer,
54
				ViewerCell cellToExpand, Event event) {
55
			return cellToExpand != null
56
					&& ((TreeItem) cellToExpand.getItem()).getItemCount() > 0
57
					&& !((TreeItem) cellToExpand.getItem()).getExpanded()
58
					&& cellToExpand.getColumnIndex() == 0
59
					&& event.keyCode == SWT.ARROW_RIGHT;
60
		}
61
	};
62
63
	private CellNavigationDelegate navigationDelegate;
64
	
65
	/**
66
	 * @param viewer
67
	 * @param focusDrawingDelegate
68
	 */
69
	public SWTCellSupport(TableViewer viewer,
70
			FocusCellMarkerDelegate focusDrawingDelegate) {
71
		this(viewer, focusDrawingDelegate, TABLE_NAVIGATE);
72
	}
73
74
	/**
75
	 * @param viewer
76
	 * @param focusDrawingDelegate
77
	 */
78
	public SWTCellSupport(TreeViewer viewer,
79
			FocusCellMarkerDelegate focusDrawingDelegate) {
80
		this(viewer, focusDrawingDelegate, TREE_NAVIGATE);
81
	}
82
83
	private SWTCellSupport(ColumnViewer viewer,
84
			FocusCellMarkerDelegate focusDrawingDelegate,
85
			CellNavigationDelegate navigationDelegate) {
86
		super(viewer);
87
		if (viewer != focusDrawingDelegate.getViewer()) {
88
			throw new IllegalArgumentException(
89
					"The FocusCellMarkerDelegate passed is not working for the same ColumnViewer than SWTCellSupport"); //$NON-NLS-1$
90
		}
91
92
		this.navigationDelegate = navigationDelegate;
93
		focusDrawingDelegate.setCellSupport(this);
94
	}
95
96
	private void handleMouseDown(Event event) {
97
		ViewerCell cell = getViewer().getCell(new Point(event.x, event.y));
98
		if (cell != null) {
99
			if( fireCellFocusEvent(new CellFocusChangedEvent(getViewer(), cell,
100
					getFocusCell()))) {
101
				getViewer().getControl().redraw();
102
			}
103
		}
104
	}
105
106
	private void handleKeyDown(Event event) {
107
		ViewerCell focusCell = getFocusCell();
108
		ViewerCell tmp = null;
109
110
		if (navigationDelegate.isCollapseEvent(getViewer(), focusCell, event)) {
111
			navigationDelegate.collapse(getViewer(), focusCell, event);
112
		} else if (navigationDelegate.isExpandEvent(getViewer(), focusCell,
113
				event)) {
114
			System.err.println("EXPAND!!!"); //$NON-NLS-1$
115
			navigationDelegate.expand(getViewer(), focusCell, event);
116
		} else if (navigationDelegate.isNavigationEvent(getViewer(), event)) {
117
			tmp = navigationDelegate.findSelectedCell(getViewer(), focusCell,event);
118
119
			System.err.println("NEW CELL:" + tmp); //$NON-NLS-1$
120
			
121
			
122
			if (tmp != null) {
123
				if( fireCellFocusEvent(new CellFocusChangedEvent(getViewer(),tmp,focusCell)) ) {
124
					System.err.println("CHANGED!"); //$NON-NLS-1$
125
					getViewer().getControl().redraw();
126
				}
127
			}
128
		}
129
130
		if (navigationDelegate.cancleEvent(getViewer(), event)) {
131
			event.doit = false;
132
		}
133
	}
134
135
	private void handleSelection(Event event) {
136
		ViewerCell focusCell = getFocusCell();
137
		if( focusCell != null && focusCell.getItem() != event.item && event.item != null ) {
138
			ViewerRow row = getViewer().getViewerRowFromItem(event.item);
139
			Assert.isNotNull(row,"Internal Structure invalid. Row item has no row ViewerRow assigned"); //$NON-NLS-1$
140
			if( fireCellFocusEvent(new CellFocusChangedEvent(getViewer(),row.getCell(focusCell.getColumnIndex()),focusCell)) ) {
141
				getViewer().getControl().redraw();
142
			}
143
		}
144
	}
145
146
	protected void hookListener(ColumnViewer viewer) {
147
		Listener listener = new Listener() {
148
149
			public void handleEvent(Event event) {
150
				if (event.type == SWT.MouseDown) {
151
					handleMouseDown(event);
152
				} else if (event.type == SWT.KeyDown) {
153
					handleKeyDown(event);
154
				} else {
155
					handleSelection(event);
156
				}
157
			}
158
		};
159
160
		viewer.getControl().addListener(SWT.MouseDown, listener);
161
		viewer.getControl().addListener(SWT.KeyDown, listener);
162
		viewer.getControl().addListener(SWT.Selection, listener);
163
	}
164
}
(-)src/org/eclipse/jface/viewers/EditorActivationEvent.java (+164 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.jface.viewers;
13
14
import java.util.EventObject;
15
16
import org.eclipse.swt.events.KeyEvent;
17
import org.eclipse.swt.events.MouseEvent;
18
import org.eclipse.swt.events.TraverseEvent;
19
20
/**
21
 * This event is passed on when a cell-editor is going to be activated
22
 * 
23
 * @since 3.3
24
 * 
25
 */
26
public class EditorActivationEvent extends EventObject {
27
	/**
28
	 * 
29
	 */
30
	private static final long serialVersionUID = 1L;
31
32
	/**
33
	 * if a key is pressed on a selected cell
34
	 */
35
	public static final int KEY_PRESSED = 1;
36
37
	/**
38
	 * if a cell is selected using a single click of the mouse
39
	 */
40
	public static final int MOUSE_CLICK_SELECTION = 2;
41
42
	/**
43
	 * if a cell is selected using double clicking of the mouse
44
	 */
45
	public static final int MOUSE_DOUBLE_CLICK_SELECTION = 3;
46
47
	/**
48
	 * if a cell is activated using code like e.g
49
	 * {@link ColumnViewer#editElement(Object, int)}
50
	 */
51
	public static final int PROGRAMATIC = 4;
52
53
	/**
54
	 * is a cell is activated by traversing
55
	 */
56
	public static final int TRAVERSAL = 5;
57
58
	/**
59
	 * the original event triggered
60
	 */
61
	public EventObject sourceEvent;
62
63
	/**
64
	 * The time the event is triggered
65
	 */
66
	public int time;
67
68
	/**
69
	 * The event type triggered:
70
	 * <ul>
71
	 * <li>{@link #KEY_PRESSED} if a key is pressed on a selected cell</li>
72
	 * <li>{@link #MOUSE_CLICK_SELECTION} if a cell is selected using a single
73
	 * click of the mouse</li>
74
	 * <li>{@link #MOUSE_DOUBLE_CLICK_SELECTION} if a cell is selected using
75
	 * double clicking of the mouse</li>
76
	 * </ul>
77
	 */
78
	public int eventType;
79
80
	/**
81
	 * <b>Only set for {@link #KEY_PRESSED}</b>
82
	 */
83
	public int keyCode;
84
85
	/**
86
	 * <b>Only set for {@link #KEY_PRESSED}</b>
87
	 */
88
	public char character;
89
90
	/**
91
	 * The statemask
92
	 */
93
	public int stateMask;
94
95
	/**
96
	 * Cancle the event (=> editor is not activated)
97
	 */
98
	public boolean cancle = false;
99
	
100
	/**
101
	 * This constructor can be used when no event exists. The type set is
102
	 * {@link #PROGRAMATIC}
103
	 * 
104
	 * @param cell
105
	 *            the cell
106
	 */
107
	public EditorActivationEvent(ViewerCell cell) {
108
		super(cell);
109
		eventType = PROGRAMATIC;
110
	}
111
112
	/**
113
	 * This constructor is used for all types of mouse events. Currently the
114
	 * type is can be {@link #MOUSE_CLICK_SELECTION} and
115
	 * {@link #MOUSE_DOUBLE_CLICK_SELECTION}
116
	 * 
117
	 * @param cell
118
	 *            the cell source of the event
119
	 * @param event
120
	 *            the event
121
	 */
122
	public EditorActivationEvent(ViewerCell cell, MouseEvent event) {
123
		super(cell);
124
125
		if (event.count >= 2) {
126
			eventType = MOUSE_DOUBLE_CLICK_SELECTION;
127
		} else {
128
			eventType = MOUSE_CLICK_SELECTION;
129
		}
130
131
		this.sourceEvent = event;
132
		this.time = event.time;
133
	}
134
135
	/**
136
	 * @param cell
137
	 *            the cell source of the event
138
	 * @param event
139
	 *            the event
140
	 */
141
	public EditorActivationEvent(ViewerCell cell, KeyEvent event) {
142
		super(cell);
143
		this.eventType = KEY_PRESSED;
144
		this.sourceEvent = event;
145
		this.time = 0;
146
		this.keyCode = event.keyCode;
147
		this.character = event.character;
148
		this.stateMask = event.stateMask;
149
	}
150
151
	/**
152
	 * This constructur is used to mark the activation triggered by a traversal
153
	 * 
154
	 * @param cell
155
	 *            the cell source of the event
156
	 * @param event
157
	 *            the event
158
	 */
159
	public EditorActivationEvent(ViewerCell cell, TraverseEvent event) {
160
		super(cell);
161
		this.eventType = TRAVERSAL;
162
		this.sourceEvent = event;
163
	}
164
}
(-)src/org/eclipse/jface/viewers/ColumnViewerEditorActivationListener.java (+52 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.jface.viewers;
13
14
/**
15
 * Parties interested in activation and deactivation of editors extend this
16
 * class and implement any or all of the methods
17
 * 
18
 * @since 3.3
19
 * 
20
 */
21
public abstract class ColumnViewerEditorActivationListener {
22
	/**
23
	 * Called before an editor is activated
24
	 * 
25
	 * @param event
26
	 *            the event
27
	 */
28
	public abstract void beforeEditorActivated(EditorActivationEvent event);
29
30
	/**
31
	 * Called after an editor has been activated
32
	 * 
33
	 * @param event the event
34
	 */
35
	public abstract void afterEditorActivated(EditorActivationEvent event);
36
37
	/**
38
	 * Called before an editor is deactivated
39
	 * 
40
	 * @param event
41
	 *            the event
42
	 */
43
	public abstract void beforeEditorDeactivated(EditorDeactivationEvent event);
44
45
	
46
	/**
47
	 * Called after an editor is deactivated
48
	 * 
49
	 * @param event the event
50
	 */
51
	public abstract void afterEditorDeactivated(EditorDeactivationEvent event);
52
}
(-)src/org/eclipse/jface/viewers/CellFocusChangedListener.java (+24 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.jface.viewers;
13
14
/**
15
 * @since 3.3
16
 * 
17
 */
18
public abstract class CellFocusChangedListener {
19
	/**
20
	 * @param event
21
	 *            the event informing about the selection of a cell
22
	 */
23
	public abstract void cellSelected(CellFocusChangedEvent event);
24
}
(-)src/org/eclipse/jface/viewers/CellSupport.java (+95 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.jface.viewers;
13
14
import org.eclipse.core.runtime.ListenerList;
15
16
/**
17
 * @since 3.3
18
 * 
19
 */
20
public abstract class CellSupport {
21
	private ListenerList cellFocusListener;
22
23
	private ViewerCell focusCell;
24
25
	private ColumnViewer viewer;
26
27
	/**
28
	 * @param viewer
29
	 *            the viewer who requests cell support
30
	 */
31
	public CellSupport(ColumnViewer viewer) {
32
		this.viewer = viewer;
33
		hookListener(viewer);
34
	}
35
36
	/**
37
	 * @param viewer
38
	 *            hook listeners into the viewer
39
	 */
40
	protected abstract void hookListener(ColumnViewer viewer);
41
42
	/**
43
	 * @return the viewer
44
	 */
45
	protected ColumnViewer getViewer() {
46
		return viewer;
47
	}
48
49
	/**
50
	 * @param listener
51
	 *            the listener to be added
52
	 */
53
	public void addCellSelectionListener(CellFocusChangedListener listener) {
54
		cellFocusListener.add(listener);
55
	}
56
57
	/**
58
	 * @param listener
59
	 *            the listener to be removed
60
	 */
61
	public void removeCellSelectionListener(CellFocusChangedListener listener) {
62
		cellFocusListener.remove(listener);
63
	}
64
65
	/**
66
	 * @return the cell who holds the keyboard focus
67
	 */
68
	public ViewerCell getFocusCell() {
69
		return focusCell;
70
	}
71
72
	/**
73
	 * @param event
74
	 *            inform listeners about the selection
75
	 * @return has this really been an change event
76
	 */
77
	protected boolean fireCellFocusEvent(CellFocusChangedEvent event) {
78
		if (event.focusCell.equals(event.focusLostCell)) {
79
			return false;
80
		}
81
82
		this.focusCell = event.focusCell;
83
84
		if( cellFocusListener == null ) {
85
			cellFocusListener = new ListenerList();
86
		}
87
		
88
		Object[] ls = cellFocusListener.getListeners();
89
90
		for (int i = 0; i < ls.length; i++) {
91
			((CellFocusChangedListener) ls[i]).cellSelected(event);
92
		}
93
		return true;
94
	}
95
}
(-)META-INF/MANIFEST.MF (-3 / +3 lines)
Lines 5-10 Link Here
5
Bundle-Version: 1.0.0
5
Bundle-Version: 1.0.0
6
Bundle-Localization: plugin
6
Bundle-Localization: plugin
7
Require-Bundle: org.eclipse.swt,
7
Require-Bundle: org.eclipse.swt,
8
 org.eclipse.swt.nebula
8
 org.eclipse.swt.nebula,
9
Export-Package: org.eclipse.swt.nebula.snippets.ctree,
9
 org.eclipse.swt.nebula.nebface
10
 org.eclipse.swt.nebula.snippets.grid
10
Export-Package: org.eclipse.swt.nebula.snippets.grid
(-)src/org/eclipse/swt/nebface/snippets/viewers/GridViewerSnippet1.java (+208 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 Tom Schindl and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Tom Schindl - initial API and implementation
10
 *******************************************************************************/
11
12
package org.eclipse.swt.nebface.snippets.viewers;
13
14
import org.eclipse.jface.resource.FontRegistry;
15
import org.eclipse.jface.viewers.CellEditor;
16
import org.eclipse.jface.viewers.EditingSupport;
17
import org.eclipse.jface.viewers.EditorActivationEvent;
18
import org.eclipse.jface.viewers.ICellModifier;
19
import org.eclipse.jface.viewers.IStructuredContentProvider;
20
import org.eclipse.jface.viewers.ITableColorProvider;
21
import org.eclipse.jface.viewers.ITableFontProvider;
22
import org.eclipse.jface.viewers.ITableLabelProvider;
23
import org.eclipse.jface.viewers.LabelProvider;
24
import org.eclipse.jface.viewers.TextCellEditor;
25
import org.eclipse.jface.viewers.Viewer;
26
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.graphics.Color;
28
import org.eclipse.swt.graphics.Font;
29
import org.eclipse.swt.graphics.Image;
30
import org.eclipse.swt.layout.FillLayout;
31
import org.eclipse.swt.nebula.nebface.viewers.GridViewerEditorActivationSupport;
32
import org.eclipse.swt.nebula.nebface.viewers.GridViewer;
33
import org.eclipse.swt.nebula.widgets.grid.GridColumn;
34
import org.eclipse.swt.widgets.Display;
35
import org.eclipse.swt.widgets.Shell;
36
37
/**
38
 * Example usage of none mandatory interfaces of ITableFontProvider and
39
 * ITableColorProvider
40
 * 
41
 * @author Tom Schindl <tom.schindl@bestsolution.at>
42
 * 
43
 */
44
public class GridViewerSnippet1 {
45
46
	private class MyContentProvider implements IStructuredContentProvider {
47
48
		/*
49
		 * (non-Javadoc)
50
		 * 
51
		 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
52
		 */
53
		public Object[] getElements(Object inputElement) {
54
			return (MyModel[]) inputElement;
55
		}
56
57
		/*
58
		 * (non-Javadoc)
59
		 * 
60
		 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
61
		 */
62
		public void dispose() {
63
64
		}
65
66
		/*
67
		 * (non-Javadoc)
68
		 * 
69
		 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
70
		 *      java.lang.Object, java.lang.Object)
71
		 */
72
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
73
74
		}
75
76
	}
77
78
	public static boolean flag = true;
79
	
80
	public class MyModel {
81
		public int counter;
82
83
		public MyModel(int counter) {
84
			this.counter = counter;
85
		}
86
87
		public String toString() {
88
			return "Item " + this.counter;
89
		}
90
	}
91
92
	public class MyLabelProvider extends LabelProvider implements
93
			ITableLabelProvider, ITableFontProvider, ITableColorProvider {
94
		FontRegistry registry = new FontRegistry();
95
96
		public Image getColumnImage(Object element, int columnIndex) {
97
			return null;
98
		}
99
100
		public String getColumnText(Object element, int columnIndex) {
101
			return "Column " + columnIndex + " => " + element.toString();
102
		}
103
104
		public Font getFont(Object element, int columnIndex) {
105
			if (((MyModel) element).counter % 2 == 0) {
106
				return registry.getBold(Display.getCurrent().getSystemFont()
107
						.getFontData()[0].getName());
108
			}
109
			return null;
110
		}
111
112
		public Color getBackground(Object element, int columnIndex) {
113
			if (((MyModel) element).counter % 2 == 0) {
114
				return Display.getCurrent().getSystemColor(SWT.COLOR_RED);
115
			}
116
			return null;
117
		}
118
119
		public Color getForeground(Object element, int columnIndex) {
120
			if (((MyModel) element).counter % 2 == 1) {
121
				return Display.getCurrent().getSystemColor(SWT.COLOR_RED);
122
			}
123
			return null;
124
		}
125
126
	}
127
128
	public GridViewerSnippet1(Shell shell) {
129
		final GridViewer v = new GridViewer(shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
130
		v.setLabelProvider(new MyLabelProvider());
131
		v.setContentProvider(new MyContentProvider());
132
		v.getGrid().setCellSelectionEnabled(true);
133
				
134
		v.setCellEditors(new CellEditor[] { new TextCellEditor(v.getGrid()), new TextCellEditor(v.getGrid()) });
135
		v.setCellModifier(new ICellModifier() {
136
137
			public boolean canModify(Object element, String property) {
138
				return true;
139
			}
140
141
			public Object getValue(Object element, String property) {
142
				return "Column " + property + " => " + element.toString();
143
			}
144
145
			public void modify(Object element, String property, Object value) {
146
				
147
			}
148
			
149
		});
150
		
151
		v.setColumnProperties(new String[] {"1","2"});
152
		
153
		GridViewerEditorActivationSupport support = new GridViewerEditorActivationSupport(v) {
154
			protected boolean isEditorActivationEvent(
155
					EditorActivationEvent event) {
156
				return event.eventType == EditorActivationEvent.TRAVERSAL || event.eventType == EditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION || ( event.eventType == EditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR ) || event.eventType == EditorActivationEvent.PROGRAMATIC;
157
			}
158
		};
159
		support.setEnableEditorActivationWithKeyboard(true);
160
		
161
		v.setEditorActivationSupport(support);
162
		v.setTabEditingStyle(EditingSupport.TABBING_HORIZONTAL|EditingSupport.TABBING_MOVE_TO_ROW_NEIGHBOR|EditingSupport.TABBING_VERTICAL);
163
		
164
		GridColumn column = new GridColumn(v.getGrid(), SWT.NONE);
165
		column.setWidth(200);
166
		column.setText("Column 1");
167
168
		column = new GridColumn(v.getGrid(), SWT.NONE);
169
		column.setWidth(200);
170
		column.setText("Column 2");
171
172
		MyModel[] model = createModel();
173
		v.setInput(model);
174
		v.getGrid().setLinesVisible(true);
175
		v.getGrid().setHeaderVisible(true);
176
	}
177
178
	private MyModel[] createModel() {
179
		MyModel[] elements = new MyModel[10];
180
181
		for (int i = 0; i < 10; i++) {
182
			elements[i] = new MyModel(i);
183
		}
184
185
		return elements;
186
	}
187
188
	/**
189
	 * @param args
190
	 */
191
	public static void main(String[] args) {
192
		Display display = new Display();
193
194
		Shell shell = new Shell(display);
195
		shell.setLayout(new FillLayout());
196
		new GridViewerSnippet1(shell);
197
		shell.open();
198
199
		while (!shell.isDisposed()) {
200
			if (!display.readAndDispatch())
201
				display.sleep();
202
		}
203
204
		display.dispose();
205
206
	}
207
208
}
(-)META-INF/MANIFEST.MF (-1 / +1 lines)
Lines 4-10 Link Here
4
Bundle-SymbolicName: org.eclipse.swt.nebula.nebface
4
Bundle-SymbolicName: org.eclipse.swt.nebula.nebface
5
Bundle-Version: 1.0.0
5
Bundle-Version: 1.0.0
6
Bundle-Localization: plugin
6
Bundle-Localization: plugin
7
Require-Bundle: org.eclipse.jface,
7
Require-Bundle: org.eclipse.jface;visibility:=reexport,
8
 org.eclipse.swt.nebula;visibility:=reexport,
8
 org.eclipse.swt.nebula;visibility:=reexport,
9
 org.eclipse.equinox.common;visibility:=reexport
9
 org.eclipse.equinox.common;visibility:=reexport
10
Bundle-RequiredExecutionEnvironment: J2SE-1.4
10
Bundle-RequiredExecutionEnvironment: J2SE-1.4
(-)src/org/eclipse/swt/nebula/nebface/viewers/CheckEditingSupport.java (-5 / +32 lines)
Lines 1-29 Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *    chris.gross@us.ibm.com - initial API and implementation
10
 *******************************************************************************/ 
11
1
package org.eclipse.swt.nebula.nebface.viewers;
12
package org.eclipse.swt.nebula.nebface.viewers;
2
13
3
import org.eclipse.jface.viewers.CellEditor;
14
import org.eclipse.jface.viewers.CellEditor;
15
import org.eclipse.jface.viewers.ColumnViewer;
4
import org.eclipse.jface.viewers.EditingSupport;
16
import org.eclipse.jface.viewers.EditingSupport;
5
17
18
/**
19
 * .
20
 *
21
 * @author chris.gross@us.ibm.com
22
 * @since 3.3
23
 */
6
public abstract class CheckEditingSupport extends EditingSupport
24
public abstract class CheckEditingSupport extends EditingSupport
7
{
25
{
26
    /**
27
     * Checkbox editing support.
28
     * 
29
     * @param viewer column to add check box support for.
30
     */
31
    public CheckEditingSupport(ColumnViewer viewer)
32
    {
33
        super(viewer);
34
    }
8
35
36
    /** {@inheritDoc} */
9
    protected boolean canEdit(Object element)
37
    protected boolean canEdit(Object element)
10
    {
38
    {
11
        // TODO Auto-generated method stub
12
        return false;
39
        return false;
13
    }
40
    }
14
41
42
    /** {@inheritDoc} */
15
    protected CellEditor getCellEditor(Object element)
43
    protected CellEditor getCellEditor(Object element)
16
    {
44
    {
17
        // TODO Auto-generated method stub
18
        return null;
45
        return null;
19
    }
46
    }
20
47
48
    /** {@inheritDoc} */
21
    protected Object getValue(Object element)
49
    protected Object getValue(Object element)
22
    {
50
    {
23
        // TODO Auto-generated method stub
24
        return null;
51
        return null;
25
    }
52
    }
26
53
27
    protected abstract void setValue(Object element, Object value);
54
    /** {@inheritDoc} */
28
55
    public abstract void setValue(Object element, Object value);
29
}
56
}
(-)src/org/eclipse/swt/nebula/nebface/viewers/GridViewer.java (-141 / +250 lines)
Lines 1-22 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2006 Tom Schindl and others.
2
 * Copyright (c) 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
9
 *    rmcamara@us.ibm.com - initial API and implementation
10
 *     IBM Corporation
10
 *******************************************************************************/ 
11
 *******************************************************************************/
11
12
package org.eclipse.swt.nebula.nebface.viewers;
12
package org.eclipse.swt.nebula.nebface.viewers;
13
13
14
import org.eclipse.jface.viewers.AbstractViewerEditor;
14
import org.eclipse.jface.viewers.AbstractTableViewer;
15
import org.eclipse.jface.viewers.BaseTableViewer;
15
import org.eclipse.jface.viewers.ColumnViewerEditor;
16
import org.eclipse.jface.viewers.StructuredSelection;
16
import org.eclipse.jface.viewers.StructuredSelection;
17
import org.eclipse.jface.viewers.ViewerCell;
17
import org.eclipse.jface.viewers.ViewerRow;
18
import org.eclipse.jface.viewers.ViewerRow;
18
import org.eclipse.jface.viewers.CellEditor.LayoutData;
19
import org.eclipse.jface.viewers.CellEditor.LayoutData;
19
import org.eclipse.swt.SWT;
20
import org.eclipse.swt.SWT;
21
import org.eclipse.swt.graphics.Image;
20
import org.eclipse.swt.graphics.Point;
22
import org.eclipse.swt.graphics.Point;
21
import org.eclipse.swt.nebula.widgets.grid.Grid;
23
import org.eclipse.swt.nebula.widgets.grid.Grid;
22
import org.eclipse.swt.nebula.widgets.grid.GridEditor;
24
import org.eclipse.swt.nebula.widgets.grid.GridEditor;
Lines 24-210 Link Here
24
import org.eclipse.swt.widgets.Composite;
26
import org.eclipse.swt.widgets.Composite;
25
import org.eclipse.swt.widgets.Control;
27
import org.eclipse.swt.widgets.Control;
26
import org.eclipse.swt.widgets.Item;
28
import org.eclipse.swt.widgets.Item;
29
import org.eclipse.swt.widgets.TableItem;
27
import org.eclipse.swt.widgets.Widget;
30
import org.eclipse.swt.widgets.Widget;
28
31
29
public class GridViewer extends BaseTableViewer {
32
/**
30
	private static final int DEFAULT_STYLE = SWT.MULTI | SWT.H_SCROLL
33
 * A concrete viewer based on an Grid control.
31
			| SWT.V_SCROLL | SWT.BORDER;
34
 * <p>
32
35
 * This class is not intended to be subclassed outside the viewer framework. It
33
	private Grid grid;
36
 * is designed to be instantiated with a pre-existing Grid control and
34
37
 * configured with a domain-specific content provider, label provider, element
35
	private GridEditor gridEditor;
38
 * filter (optional), and element sorter (optional).
36
39
 * <p>
37
	public GridViewer(Composite parent) {
40
 * Content providers for grid viewers must not implement the
38
		this(parent, DEFAULT_STYLE);
41
 * {@code ITreeContentProvider} interface.
39
	}
42
 * <p>
43
 * 
44
 * @author rmcamara@us.ibm.com
45
 * @since
46
 * @3.3
47
 */
48
public class GridViewer extends AbstractTableViewer
49
{
50
    /** This viewer's grid control. */
51
    private Grid grid;
52
    
53
    /** Editor support for tables. */
54
    private GridEditor gridEditor;
55
56
    /**
57
     * Creates a grid viewer on a newly-created grid control under the given
58
     * parent. The grid control is created using the SWT style bits
59
     * <code>MULTI, H_SCROLL, V_SCROLL,</code> and <code>BORDER</code>. The
60
     * viewer has no input, no content provider, a default label provider, no
61
     * sorter, and no filters.
62
     * 
63
     * @param parent the parent control
64
     */
65
    public GridViewer(Composite parent)
66
    {
67
        this(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
68
    }
40
69
41
	public GridViewer(Composite parent, int style) {
70
    /**
42
		this(new Grid(parent, style));
71
     * Creates a grid viewer on a newly-created grid control under the given
43
	}
72
     * parent. The grid control is created using the given SWT style bits. The
73
     * viewer has no input, no content provider, a default label provider, no
74
     * sorter, and no filters.
75
     * 
76
     * @param parent the parent control
77
     * @param style the SWT style bits used to create the grid.
78
     */
79
    public GridViewer(Composite parent, int style)
80
    {
81
        this(new Grid(parent, style));
82
    }
44
83
45
	public GridViewer(Grid gridControl) {
84
    /**
46
		super();
85
     * Creates a grid viewer on the given grid control. The viewer has no
47
        grid = gridControl;
86
     * input, no content provider, a default label provider, no sorter, and no
48
		gridEditor = new GridEditor(gridControl);
87
     * filters.
88
     * 
89
     * @param grid the grid control
90
     */
91
    public GridViewer(Grid grid)
92
    {
93
        this.grid = grid;
94
        gridEditor = new GridEditor(grid);
49
        hookControl(grid);
95
        hookControl(grid);
50
	}
96
    }
51
97
    
52
	public Grid getGrid() {
98
    /**
53
		return grid;
99
     * Returns the underlying Grid Control. 
54
	}
100
     * 
55
101
     * @return grid control.
56
	protected ViewerRow createNewRowPart(int style, int rowIndex) {
102
     */
57
		GridItem item;
103
    public Grid getGrid()
58
104
    {
59
		if (rowIndex >= 0) {
105
        return grid;
60
			item = new GridItem(grid, style, rowIndex);
106
    }
61
		} else {
62
			item = new GridItem(grid, style);
63
		}
64
65
		return getViewerRowFromItem(item);
66
	}
67
107
68
    protected ViewerRow getViewerRowFromItem(Widget item) {
108
    /** {@inheritDoc} */
69
        ViewerRow part = (ViewerRow) item.getData(ViewerRow.ROWPART_KEY);
109
    protected ViewerRow internalCreateNewRowPart(int style, int rowIndex)
110
    {
111
        GridItem item;
70
112
71
        if (part == null) {
113
        if (rowIndex >= 0) 
72
            part = new GridViewerRow(((GridItem) item));
114
        {
115
            item = new GridItem(grid, style, rowIndex);
116
        } 
117
        else 
118
        {
119
            item = new GridItem(grid, style);
73
        }
120
        }
74
121
75
        return part;
122
        return getViewerRowFromItem(item);
76
    }
123
    }
77
124
78
	protected AbstractViewerEditor createViewerEditor() {
125
    /** {@inheritDoc} */
79
        return new AbstractViewerEditor(this) {
126
    protected ColumnViewerEditor createViewerEditor() 
80
127
    {
81
            protected StructuredSelection createSelection(Object element) {
128
        return new ColumnViewerEditor(this) 
129
        {
130
            protected StructuredSelection createSelection(Object element) 
131
            {
82
                return new StructuredSelection(element);
132
                return new StructuredSelection(element);
83
            }
133
            }
84
134
85
            protected Item[] getSelection() {
135
            protected void setEditor(Control w, Item item, int fColumnNumber) 
86
                return getGrid().getSelection();
136
            {
87
            }
88
89
            protected void setEditor(Control w, Item item, int fColumnNumber) {
90
                gridEditor.setEditor(w, (GridItem) item, fColumnNumber);
137
                gridEditor.setEditor(w, (GridItem) item, fColumnNumber);
91
            }
138
            }
92
139
93
            protected void setLayoutData(LayoutData layoutData) {
140
            protected void setLayoutData(LayoutData layoutData) 
141
            {
94
                gridEditor.grabHorizontal = layoutData.grabHorizontal;
142
                gridEditor.grabHorizontal = layoutData.grabHorizontal;
95
                gridEditor.horizontalAlignment = layoutData.horizontalAlignment;
143
                gridEditor.horizontalAlignment = layoutData.horizontalAlignment;
96
                gridEditor.minimumWidth = layoutData.minimumWidth;
144
                gridEditor.minimumWidth = layoutData.minimumWidth;
97
            }
145
            }
98
99
            protected void showSelection() {
100
                getGrid().showSelection();
101
            }
102
103
        };
146
        };
104
	}
147
    }
105
106
	protected void internalClear(int index) {
107
		// TODO NEEDS IMP
108
	}
109
148
110
	protected void internalClearAll() {
149
    /** {@inheritDoc} */
111
		// TODO NEEDS IMP
150
    protected void doClear(int index)
112
	}
151
    {
152
        // TODO Fix when grid supports virtual
153
    }
113
154
114
	protected void internalDeselectAll() {
155
    /** {@inheritDoc} */
115
		grid.deselectAll();
156
    protected void doClearAll()
116
	}
157
    {
158
        //TODO Fix when grid supports virtual
159
    }
160
    
161
    /** {@inheritDoc} */
162
    protected void doSetItemCount(int count)
163
    {
164
        //TODO Once grid supports virtual
165
    }
117
166
118
	protected Widget internalGetColumn(int index) {
167
    /** {@inheritDoc} */
119
		return grid.getColumn(index);
168
    protected void doDeselectAll()
120
	}
169
    {
170
        grid.deselectAll();
171
    }
121
172
122
	protected int internalGetColumnCount() {
173
    /** {@inheritDoc} */
123
		return grid.getColumnCount();
174
    protected Widget doGetColumn(int index)
124
	}
175
    {
176
        return grid.getColumn(index);
177
    }
125
178
126
	protected Widget[] internalGetColumns() {
179
    /** {@inheritDoc} */
127
		return grid.getColumns();
180
    protected int doGetColumnCount()
128
	}
181
    {
182
        return grid.getColumnCount();
183
    }
129
184
130
	protected Item internalGetItem(int index) {
185
    /** {@inheritDoc} */
131
		return grid.getItem(index);
186
    protected Item doGetItem(int index)
132
	}
187
    {
188
        return grid.getItem(index);
189
    }
133
190
134
	protected Item internalGetItem(Point point) {
191
    /** {@inheritDoc} */
135
		return grid.getItem(point);
192
    protected int doGetItemCount()
136
	}
193
    {
194
        return grid.getItemCount();
195
    }
137
196
138
	protected int internalGetItemCount() {
197
    /** {@inheritDoc} */
139
		return grid.getItemCount();
198
    protected Item[] doGetItems()
140
	}
199
    {
200
        return grid.getItems();
201
    }
141
202
142
	protected Item[] internalGetItems() {
203
    /** {@inheritDoc} */
143
		return grid.getItems();
204
    protected Item[] doGetSelection()
144
	}
205
    {
206
        return grid.getSelection();
207
    }
145
208
146
	protected Widget[] internalGetSelection() {
209
    /** {@inheritDoc} */
147
		return grid.getSelection();
210
    protected int[] doGetSelectionIndices()
148
	}
211
    {
212
        return grid.getSelectionIndices();
213
    }
149
214
150
	protected int[] internalGetSelectionIndices() {
215
    /** {@inheritDoc} */
151
		return grid.getSelectionIndices();
216
    protected int doIndexOf(Item item)
152
	}
217
    {
218
        return grid.indexOf((GridItem) item);
219
    }
153
220
154
	protected int internalIndexOf(Item item) {
221
    /** {@inheritDoc} */
155
		return grid.indexOf((GridItem) item);
222
    protected void doRemove(int[] indices)
156
	}
223
    {
224
        grid.remove(indices);
225
    }
157
226
158
	protected void internalRemove(int start, int end) {
227
    /** {@inheritDoc} */
159
		grid.remove(start, end);
228
    protected void doRemove(int start, int end)
160
	}
229
    {
230
        grid.remove(start, end);
231
    }
161
232
162
	protected void internalRemove(int[] indices) {
233
    /** {@inheritDoc} */
163
		grid.remove(indices);
234
    protected void doRemoveAll()
164
	}
235
    {
236
        grid.removeAll();
237
    }
165
238
166
	protected void internalRemoveAll() {
239
    /** {@inheritDoc} */
167
		grid.removeAll();
240
    protected void doSetSelection(Item[] items)
168
	}
241
    {
242
        GridItem[] items2 = new GridItem[items.length];
243
        for (int i = 0; i < items.length; i++)
244
        {
245
            items2[i] = (GridItem) items[i];
246
        }
247
        grid.setSelection(items2);
248
    }
169
249
170
	protected void internalSetItemCount(int count) {
250
    /** {@inheritDoc} */
171
		// TODO NEEDS IMP
251
    protected void doSetSelection(int[] indices)
172
	}
252
    {
253
        grid.setSelection(indices);
254
    }
173
255
174
	protected void internalSetSelection(Item[] items) {
256
    /** {@inheritDoc} */
175
		if (items != null) {
257
    protected void doShowItem(Item item)
176
			grid.setSelection(new GridItem[0]);
258
    {
177
		} else {
259
        grid.showItem((GridItem)item);
178
			GridItem[] tmp = new GridItem[items.length];
260
    }
179
			System.arraycopy(items, 0, tmp, 0, items.length);
180
			grid.setSelection(tmp);
181
		}
182
261
183
	}
262
    /** {@inheritDoc} */
263
    protected void doShowSelection()
264
    {
265
        grid.showSelection();
266
    }
184
267
185
	protected void internalSetSelection(int[] indices) {
268
    /** {@inheritDoc} */
186
		grid.setSelection(indices);
269
    protected Item getItemAt(Point point)
187
	}
270
    {
271
        return grid.getItem(point);
272
    }
188
273
189
	protected void internalShowItem(Item item) {
274
    /** {@inheritDoc} */
190
		grid.showItem((GridItem) item);
275
    public Control getControl()
191
	}
276
    {
277
        return grid;
278
    }
279
    
280
    /** {@inheritDoc} */
281
    protected ViewerRow getViewerRowFromItem(Widget item) 
282
    {
283
        ViewerRow part = (ViewerRow) item.getData(ViewerRow.ROWPART_KEY);
192
284
193
	protected void internalShowSelection() {
285
        if (part == null) 
194
		grid.showSelection();
286
        {
195
	}
287
            part = new GridViewerRow(((GridItem) item));
288
        }
196
289
197
	protected void internalSetControl(Control table) {
290
        return part;
198
		grid = (Grid) table;
291
    }
199
	}
200
292
201
	public Control getControl() {
293
    
202
		return grid;
294
    
295
    protected void doResetItem(Item item) {
296
		GridItem gridItem = (GridItem) item;
297
		int columnCount = Math.max(1, grid.getColumnCount());
298
		for (int i = 0; i < columnCount; i++) {
299
			gridItem.setText(i, ""); //$NON-NLS-1$
300
			gridItem.setImage(null);
301
		}
203
	}
302
	}
204
303
205
    protected Item getItemAt(Point point)
304
	ViewerCell getFocusCell() {
206
    {
305
		if( getGrid().getCellSelectionEnabled() ) {
207
        return grid.getItem(point);
306
			Point p = getGrid().getFocusCell();
208
    }
307
			
308
			if( p.x >= 0 && p.y >= 0 ) {
309
				GridItem item = getGrid().getItem(p.y);
310
				if( item != null ) {
311
					ViewerRow row = (ViewerRow) item.getData(ViewerRow.ROWPART_KEY);
312
					return row.getCell(p.x);
313
				}
314
			}
315
		}
209
316
317
		return null;
318
	}
210
}
319
}
(-)src/org/eclipse/swt/nebula/nebface/viewers/GridViewerRow.java (-76 / +145 lines)
Lines 1-14 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2006 Tom Schindl and others.
2
 * Copyright (c) 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
9
 *    rmcamara@us.ibm.com - initial API and implementation
10
 *     IBM Corporation
10
 *******************************************************************************/ 
11
 *******************************************************************************/
11
12
package org.eclipse.swt.nebula.nebface.viewers;
12
package org.eclipse.swt.nebula.nebface.viewers;
13
13
14
import org.eclipse.jface.viewers.ViewerRow;
14
import org.eclipse.jface.viewers.ViewerRow;
Lines 20-101 Link Here
20
import org.eclipse.swt.widgets.Control;
20
import org.eclipse.swt.widgets.Control;
21
import org.eclipse.swt.widgets.Item;
21
import org.eclipse.swt.widgets.Item;
22
22
23
public class GridViewerRow extends ViewerRow {
23
/**
24
	private GridItem item;
24
 * GridViewerRow is the concrete implementation of the part that represents items in a
25
25
 * Grid.
26
	public GridViewerRow(GridItem item) {
26
 *
27
		super(item);
27
 * @author rmcamara@us.ibm.com
28
		this.item = item;
28
 * @since 3.3
29
	}
29
 */
30
30
public class GridViewerRow extends ViewerRow
31
	public void clear() {
31
{
32
		item.setText("");
32
    private GridItem item;
33
		if (getColumnCount() == 0) {
33
34
			item.setImage(null);
34
    /**
35
     * Create a new instance of the receiver.
36
     * 
37
     * @param item GridItem source.
38
     */
39
    public GridViewerRow(Item item)
40
    {
41
        super(item);
42
        this.item = (GridItem)item;
43
    }
44
45
    /** {@inheritDoc} */
46
    public Rectangle getBounds(int columnIndex)
47
    {
48
        return item.getBounds(columnIndex);
49
    }
50
51
    /** {@inheritDoc} */
52
    public Rectangle getBounds()
53
    {
54
        // TODO This is not correct. Update once item returns the correct information.
55
        return item.getBounds(0);
56
    }
57
58
    /** {@inheritDoc} */
59
    public Item getItem()
60
    {
61
        return item;
62
    }
63
64
    /** {@inheritDoc} */
65
    public int getColumnCount()
66
    {
67
        return item.getParent().getColumnCount();
68
    }
69
70
    /** {@inheritDoc} */
71
    public Color getBackground(int columnIndex)
72
    {
73
        return item.getBackground(columnIndex);
74
    }
75
76
    /** {@inheritDoc} */
77
    public Font getFont(int columnIndex)
78
    {
79
        return item.getFont(columnIndex);
80
    }
81
82
    /** {@inheritDoc} */
83
    public Color getForeground(int columnIndex)
84
    {
85
        return item.getForeground(columnIndex);
86
    }
87
88
    /** {@inheritDoc} */
89
    public Image getImage(int columnIndex)
90
    {
91
        return item.getImage(columnIndex);
92
    }
93
94
    /** {@inheritDoc} */
95
    public String getText(int columnIndex)
96
    {
97
        return item.getText(columnIndex);
98
    }
99
100
    /** {@inheritDoc} */
101
    public void setBackground(int columnIndex, Color color)
102
    {
103
        item.setBackground(columnIndex, color);
104
    }
105
106
    /** {@inheritDoc} */
107
    public void setFont(int columnIndex, Font font)
108
    {
109
        item.setFont(columnIndex, font);
110
    }
111
112
    /** {@inheritDoc} */
113
    public void setForeground(int columnIndex, Color color)
114
    {
115
        item.setForeground(columnIndex, color);
116
    }
117
118
    /** {@inheritDoc} */
119
    public void setImage(int columnIndex, Image image)
120
    {
121
        item.setImage(columnIndex, image);
122
    }
123
124
    /** {@inheritDoc} */
125
    public void setText(int columnIndex, String text)
126
    {
127
        item.setText(columnIndex, text == null ? "" : text); //$NON-NLS-1$
128
    }
129
130
    /** {@inheritDoc} */
131
    public Control getControl()
132
    {
133
        return item.getParent();
134
    }
135
136
    public ViewerRow getNeighbor(int direction, boolean sameLevel) {
137
		if( direction == ViewerRow.ABOVE ) {
138
			return getRowAbove();
139
		} else if( direction == ViewerRow.BELOW ) {
140
			return getRowBelow();
35
		} else {
141
		} else {
36
			for (int i = 0; i < getColumnCount(); i++) {
142
			throw new IllegalArgumentException("Illegal value of direction argument."); //$NON-NLS-1$
37
				item.setImage(i, null);
38
			}
39
		}
143
		}
40
	}
144
	}
41
145
42
	public Color getBackground(int columnIndex) {
146
	
43
		return item.getBackground(columnIndex);
147
	private ViewerRow getRowAbove() {
44
	}
148
		int index = item.getParent().indexOf(item) - 1;
45
149
		
46
	public Rectangle getBounds(int columnIndex) {
150
		if( index >= 0 ) {
47
		return item.getBounds(columnIndex);
151
			return (ViewerRow)item.getParent().getItem(index).getData(ViewerRow.ROWPART_KEY); 
48
	}
152
		}
49
153
		
50
	public Rectangle getBounds() {
154
		return null;
51
		return item.getBounds(0); // TODO IS THIS CORRECT
52
	}
53
54
	public int getColumnCount() {
55
		return item.getParent().getColumnCount();
56
	}
57
58
	public Font getFont(int columnIndex) {
59
		return item.getFont(columnIndex);
60
	}
61
62
	public Color getForeground(int columnIndex) {
63
		return item.getForeground(columnIndex);
64
	}
65
66
	public Image getImage(int columnIndex) {
67
		return item.getImage(columnIndex);
68
	}
69
70
	public Item getItem() {
71
		return item;
72
	}
73
74
	public String getText(int columnIndex) {
75
		return item.getText(columnIndex);
76
	}
77
78
	public void setBackground(int columnIndex, Color color) {
79
		item.setBackground(columnIndex, color);
80
	}
81
82
	public void setFont(int columnIndex, Font font) {
83
		item.setFont(columnIndex, font);
84
	}
85
86
	public void setForeground(int columnIndex, Color color) {
87
		item.setForeground(columnIndex, color);
88
	}
89
90
	public void setImage(int columnIndex, Image image) {
91
		item.setImage(columnIndex, image);
92
	}
93
94
	public void setText(int columnIndex, String text) {
95
		item.setText(columnIndex, text);
96
	}
155
	}
97
156
98
	public Control getControl() {
157
	private ViewerRow getRowBelow() {
99
		return item.getParent();
158
		int index = item.getParent().indexOf(item) + 1;
159
		
160
		if( index < item.getParent().getItemCount() ) {
161
			GridItem tmp = item.getParent().getItem(index);
162
			//TODO NULL can happen in case of VIRTUAL => How do we deal with that
163
			if( tmp != null ) {
164
				return (ViewerRow)tmp.getData(ViewerRow.ROWPART_KEY);
165
			}
166
		}
167
		
168
		return null;
100
	}
169
	}
101
}
170
}
(-)src/org/eclipse/swt/nebula/nebface/viewers/GridViewerColumn.java (-27 / +38 lines)
Lines 1-18 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2006 IBM Corporation and others.
2
 * Copyright (c) 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *    rmcamara@us.ibm.com - initial API and implementation
10
 ******************************************************************************/
10
 *******************************************************************************/ 
11
11
12
package org.eclipse.swt.nebula.nebface.viewers;
12
package org.eclipse.swt.nebula.nebface.viewers;
13
13
14
import org.eclipse.jface.viewers.EditingSupport;
14
import org.eclipse.jface.viewers.EditingSupport;
15
import org.eclipse.jface.viewers.TableViewer;
16
import org.eclipse.jface.viewers.ViewerColumn;
15
import org.eclipse.jface.viewers.ViewerColumn;
17
import org.eclipse.swt.SWT;
16
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.nebula.widgets.grid.Grid;
17
import org.eclipse.swt.nebula.widgets.grid.Grid;
Lines 20-68 Link Here
20
import org.eclipse.swt.nebula.widgets.grid.GridItem;
19
import org.eclipse.swt.nebula.widgets.grid.GridItem;
21
import org.eclipse.swt.widgets.Event;
20
import org.eclipse.swt.widgets.Event;
22
import org.eclipse.swt.widgets.Listener;
21
import org.eclipse.swt.widgets.Listener;
23
import org.eclipse.swt.widgets.Table;
24
import org.eclipse.swt.widgets.TableColumn;
25
22
26
/**
23
/**
27
 * This is the JFace' implementation of SWT's {@link TableColumn} like
24
 * The concrete implementation of the ColumnViewer for the grid.
28
 * {@link TableViewer} is JFace' implementation of {@link Table}
25
 *
29
 * 
26
 * @author rmcamara@us.ibm.com
30
 * @since 3.3
27
 * @since 3.3
31
 */
28
 */
32
public final class GridViewerColumn extends ViewerColumn {
29
public final class GridViewerColumn extends ViewerColumn 
33
30
{
31
    /** The concrete grid column that is being represented by the {@code ViewerColumn}.*/
34
    private GridColumn column;
32
    private GridColumn column;
33
    
34
    /** Editor support for handling check events. */
35
    private CheckEditingSupport checkEditingSupport;
35
    private CheckEditingSupport checkEditingSupport;
36
    
37
    /** The parent grid viewer. */
36
    private GridViewer viewer;
38
    private GridViewer viewer;
37
39
38
    /**
40
    /**
39
     * Create a new column in the {@link TableViewer}
41
     * Create a new column in the {@link GridViewer}
40
     * 
42
     * 
41
     * @param viewer
43
     * @param viewer
42
     *            the viewer the column belongs to
44
     *            the viewer the column belongs to
43
     * @param style
45
     * @param style
44
     *            the style used to create the column for style bits see
46
     *            the style used to create the column for style bits see
45
     *            {@link TableColumn}
47
     *            {@link GridColumn}
46
     * @see TableColumn#TableColumn(Table, int)
48
     * @see GridColumn#GridColumn(Grid, int)
47
     */
49
     */
48
    public GridViewerColumn(GridViewer viewer, int style) {
50
    public GridViewerColumn(GridViewer viewer, int style) 
51
    {
49
        this(viewer, style, -1);
52
        this(viewer, style, -1);
50
        this.viewer = viewer;
53
        this.viewer = viewer;
51
    }
54
    }
52
55
53
    /**
56
    /**
54
     * Create a new column in the {@link TableViewer}
57
     * Create a new column in the {@link GridViewer}
55
     * 
58
     * 
56
     * @param viewer
59
     * @param viewer
57
     *            the viewer the column belongs to
60
     *            the viewer the column belongs to
58
     * @param style
61
     * @param style
59
     *            the style used to create the column for style bits see
62
     *            the style used to create the column for style bits see
60
     *            {@link TableColumn}
63
     *            {@link GridColumn}
61
     * @param index
64
     * @param index
62
     *            the index of the newly created column
65
     *            the index of the newly created column
63
     * @see TableColumn#TableColumn(Table, int, int)
66
     * @see GridColumn#GridColumn(Grid, int, int)
64
     */
67
     */
65
    public GridViewerColumn(GridViewer viewer, int style, int index) {
68
    public GridViewerColumn(GridViewer viewer, int style, int index) 
69
    {
66
        this(viewer, createColumn((Grid) viewer.getControl(), style, index));
70
        this(viewer, createColumn((Grid) viewer.getControl(), style, index));
67
        this.viewer = viewer;
71
        this.viewer = viewer;
68
    }
72
    }
Lines 74-100 Link Here
74
     * @param column
78
     * @param column
75
     *            the column the viewer is attached to
79
     *            the column the viewer is attached to
76
     */
80
     */
77
    public GridViewerColumn(GridViewer viewer, GridColumn column) {
81
    public GridViewerColumn(GridViewer viewer, GridColumn column) 
82
    {
78
        super(viewer, column);
83
        super(viewer, column);
79
        this.column = column;
84
        this.column = column;
80
        this.viewer = viewer;
85
        this.viewer = viewer;
81
    }
86
    }
82
    
87
    
83
    private static GridColumn createColumn(Grid grid, int style, int index) {
88
    private static GridColumn createColumn(Grid table, int style, int index) 
84
        if (index >= 0) {
89
    {
85
            return new GridColumn(grid, style, index);
90
        if (index >= 0) 
91
        {
92
            return new GridColumn(table, style, index);
86
        }
93
        }
87
94
88
        return new GridColumn(grid, style);
95
        return new GridColumn(table, style);
89
    }
96
    }
90
97
91
    /**
98
    /**
92
     * @return the underlying SWT column
99
     * Returns the underlying column.
100
     * 
101
     * @return the underlying Nebula column
93
     */
102
     */
94
    public GridColumn getColumn() {
103
    public GridColumn getColumn() 
104
    {
95
        return column;
105
        return column;
96
    }
106
    }
97
    
107
    
108
    /** {@inheritDoc} */
98
    public void setEditingSupport(EditingSupport editingSupport)
109
    public void setEditingSupport(EditingSupport editingSupport)
99
    {
110
    {
100
        if (editingSupport instanceof CheckEditingSupport)
111
        if (editingSupport instanceof CheckEditingSupport)
Lines 123-126 Link Here
123
            super.setEditingSupport(editingSupport);
134
            super.setEditingSupport(editingSupport);
124
        }        
135
        }        
125
    }
136
    }
126
}
137
}
(-)src/org/eclipse/swt/nebula/nebface/viewers/GridViewerEditorActivationSupport.java (+14 lines)
Added Link Here
1
package org.eclipse.swt.nebula.nebface.viewers;
2
3
import org.eclipse.jface.viewers.ColumnViewerEditorActivationSupport;
4
import org.eclipse.jface.viewers.ViewerCell;
5
6
public class GridViewerEditorActivationSupport extends ColumnViewerEditorActivationSupport {
7
	public GridViewerEditorActivationSupport(GridViewer viewer) {
8
		super(viewer,null);
9
	}
10
	
11
	protected ViewerCell getFocusCell() {
12
		return ((GridViewer)getViewer()).getFocusCell();
13
	}
14
}
(-)src/org/eclipse/swt/nebula/nebface/viewers/GridTreeViewer.java (+392 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *    rmcamara@us.ibm.com - initial API and implementation
10
 *******************************************************************************/ 
11
12
package org.eclipse.swt.nebula.nebface.viewers;
13
14
import org.eclipse.jface.viewers.AbstractTreeViewer;
15
import org.eclipse.jface.viewers.ColumnViewerEditor;
16
import org.eclipse.jface.viewers.StructuredSelection;
17
import org.eclipse.jface.viewers.TreePath;
18
import org.eclipse.jface.viewers.TreeSelection;
19
import org.eclipse.jface.viewers.ViewerRow;
20
import org.eclipse.jface.viewers.CellEditor.LayoutData;
21
import org.eclipse.swt.SWT;
22
import org.eclipse.swt.events.TreeListener;
23
import org.eclipse.swt.graphics.Point;
24
import org.eclipse.swt.nebula.widgets.grid.Grid;
25
import org.eclipse.swt.nebula.widgets.grid.GridEditor;
26
import org.eclipse.swt.nebula.widgets.grid.GridItem;
27
import org.eclipse.swt.widgets.Composite;
28
import org.eclipse.swt.widgets.Control;
29
import org.eclipse.swt.widgets.Item;
30
import org.eclipse.swt.widgets.Widget;
31
32
import java.util.List;
33
34
35
/**
36
 * A concrete viewer based on an {@link Grid} control. This viewer is meant
37
 * to provide grid like support for a grid.
38
 * <p>
39
 * This class is not intended to be subclassed outside the viewer framework. It
40
 * is designed to be instantiated with a pre-existing grid control and
41
 * configured with a domain-specific content provider, label provider, element
42
 * filter (optional), and element sorter (optional).
43
 * <p>
44
 * Content providers for grid viewers must implement the
45
 * {@code ITreeContentProvider} interface.
46
 * 
47
 * @author rmcamara@us.ibm.com
48
 * @since 3.3
49
 */
50
public class GridTreeViewer extends AbstractTreeViewer
51
{
52
    /** This viewer's grid control. */
53
    private Grid grid;
54
55
    /** Table editor. */
56
    private GridEditor gridEditor;
57
58
    /**
59
     * Creates a grid viewer on a newly-created grid control under the given
60
     * parent. The grid control is created using the SWT style bits
61
     * <code>MULTI, H_SCROLL, V_SCROLL,</code> and <code>BORDER</code>. The
62
     * viewer has no input, no content provider, a default label provider, no
63
     * sorter, and no filters.
64
     * 
65
     * @param parent the parent control.
66
     */
67
    public GridTreeViewer(Composite parent)
68
    {
69
        this(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
70
    }
71
72
    /**
73
     * Creates a grid viewer on a newly-created grid control under the given
74
     * parent. The grid control is created using the given SWT style bits. The
75
     * viewer has no input, no content provider, a default label provider, no
76
     * sorter, and no filters.
77
     * 
78
     * @param parent the parent control.
79
     * @param style the SWT style bits used to create the grid.
80
     */
81
    public GridTreeViewer(Composite parent, int style)
82
    {
83
        this(new Grid(parent, style));
84
    }
85
86
    /**
87
     * Creates a grid viewer on the given grid control. The viewer has no
88
     * input, no content provider, a default label provider, no sorter, and no
89
     * filters.
90
     * 
91
     * @param grid the grid control.
92
     */
93
    public GridTreeViewer(Grid table)
94
    {
95
        this.grid = table;
96
        gridEditor = new GridEditor(table);
97
        hookControl(table);
98
    }
99
100
    /**
101
     * {@inheritDoc}
102
     */
103
    public Control getControl()
104
    {
105
        return grid;
106
    }
107
108
    /**
109
     * Returns this grid viewer's grid control.
110
     * 
111
     * @return the grid control
112
     */
113
    public Grid getGrid()
114
    {
115
        return grid;
116
    }
117
118
    /** {@inheritDoc} */
119
    protected void addTreeListener(Control c, TreeListener listener)
120
    {
121
        ((Grid)c).addTreeListener(listener);
122
    }
123
124
    /** {@inheritDoc} */
125
    protected Widget doGetColumn(int index)
126
    {
127
        return grid.getColumn(index);
128
    }
129
130
    /** {@inheritDoc} */
131
    protected int doGetColumnCount()
132
    {
133
        return grid.getColumnCount();
134
    }
135
136
    /** {@inheritDoc} */
137
    protected Item[] getChildren(Widget o)
138
    {
139
        if (o instanceof GridItem)
140
        {
141
            return ((GridItem)o).getItems();
142
        }
143
        if (o instanceof Grid)
144
        {
145
            List rootlist = ((Grid)o).getRootItems();
146
            GridItem[] roots = new GridItem[rootlist.size()];
147
            
148
            for (int i = 0; i < roots.length; i++)
149
            {
150
                roots[i] = (GridItem) rootlist.get(i);
151
            }
152
            
153
            return roots;
154
        }
155
        return null;
156
    }
157
158
    /** {@inheritDoc} */
159
    protected boolean getExpanded(Item item)
160
    {
161
        return ((GridItem)item).isExpanded();
162
    }
163
164
    /** {@inheritDoc} */
165
    protected Item getItemAt(Point p)
166
    {
167
        return grid.getItem(p);
168
    }
169
170
    /** {@inheritDoc} */
171
    protected Item[] getItems(Item item)
172
    {
173
        return ((GridItem)item).getItems();
174
    }
175
176
    /** {@inheritDoc} */
177
    protected Item getParentItem(Item item)
178
    {
179
        return ((GridItem)item).getParentItem();
180
    }
181
182
    /** {@inheritDoc} */
183
    protected Item[] getSelection(Control widget)
184
    {
185
        return ((Grid)widget).getSelection();
186
    }
187
188
    /** {@inheritDoc} */
189
    protected Item doGetParentItem(Item item)
190
    {
191
        return ((GridItem)item).getParentItem();
192
    }
193
194
    /** {@inheritDoc} */
195
    protected int doIndexOf(Item parentItem, Item item)
196
    {
197
        return ((GridItem)parentItem).indexOf((GridItem)item);
198
    }
199
200
    /** {@inheritDoc} */
201
    protected int doIndexOf(Item item)
202
    {
203
        return grid.indexOf((GridItem)item);
204
    }
205
206
    /** {@inheritDoc} */
207
    protected ColumnViewerEditor createViewerEditor()
208
    {
209
        return new ColumnViewerEditor(this)
210
        {
211
            protected StructuredSelection createSelection(Object element)
212
            {
213
                if (element instanceof TreePath)
214
                {
215
                    return new TreeSelection((TreePath)element, getComparer());
216
                }
217
218
                return new StructuredSelection(element);
219
            }
220
221
            protected void setEditor(Control w, Item item, int fColumnNumber)
222
            {
223
                gridEditor.setEditor(w, (GridItem)item, fColumnNumber);
224
            }
225
226
            protected void setLayoutData(LayoutData layoutData)
227
            {
228
                gridEditor.grabHorizontal = layoutData.grabHorizontal;
229
                gridEditor.horizontalAlignment = layoutData.horizontalAlignment;
230
                gridEditor.minimumWidth = layoutData.minimumWidth;
231
            }
232
        };
233
    }
234
235
    /** {@inheritDoc} */
236
    protected void removeAll(Control widget)
237
    {
238
        ((Grid)widget).removeAll();
239
    }
240
241
    /** {@inheritDoc} */
242
    protected void doSetExpanded(Item item, boolean expanded)
243
    {
244
        ((GridItem)item).setExpanded(expanded);
245
    }
246
247
    /** {@inheritDoc} */
248
    protected void doSetSelection(List items)
249
    {
250
        GridItem[] newItems = new GridItem[items.size()];
251
        items.toArray(newItems);
252
        getGrid().setSelection(newItems);
253
    }
254
255
    /** {@inheritDoc} */
256
    protected void showItem(Item item)
257
    {
258
        getGrid().showItem((GridItem)item);
259
    }
260
261
    /** {@inheritDoc} */
262
    protected Item getChild(Widget widget, int index)
263
    {
264
        if (widget instanceof GridItem)
265
        {
266
            return ((GridItem)widget).getItem(index);
267
        }
268
        if (widget instanceof Grid)
269
        {
270
            return (GridItem) ((Grid)widget).getRootItems().get(index);
271
        }
272
        return null;
273
    }
274
275
    /** {@inheritDoc} */
276
    protected int doGetItemCount()
277
    {
278
        return grid.getItemCount();
279
    }
280
281
    /** {@inheritDoc} */
282
    protected Item doGetItem(int index)
283
    {
284
        return grid.getItem(index);
285
    }
286
287
    /** {@inheritDoc} */
288
    protected Item doGetItem(Item item, int index)
289
    {
290
        return ((GridItem)item).getItem(index);
291
    }
292
    
293
    /** {@inheritDoc} */
294
    protected int doGetItemCount(Item item)
295
    {
296
        return ((GridItem)item).getItemCount();
297
    }
298
299
    /** {@inheritDoc} */
300
    protected void doSetItemCount(int count)
301
    {
302
        // TODO Implement once grid supports virtual
303
    }
304
305
    /** {@inheritDoc} */
306
    protected void doSetItemCount(Item item, int count)
307
    {
308
        // TODO Implement once grid supports virtual
309
    }
310
    
311
    /** {@inheritDoc} */
312
    protected void doClear(Item item, boolean all)
313
    {
314
        // TODO Once grid implements virtual.
315
    }
316
317
    /** {@inheritDoc} */
318
    protected void doClearAll(Item item, boolean all)
319
    {
320
        //TODO implement once grid supports virtual.
321
    }
322
323
    /** {@inheritDoc} */
324
    protected void doClearAll(boolean all)
325
    {
326
        //TODO implement once grid supports virtual.
327
    }
328
329
    /** {@inheritDoc} */
330
    protected ViewerRow getViewerRowFromItem(Widget item)
331
    {
332
        ViewerRow part = (ViewerRow)item.getData(ViewerRow.ROWPART_KEY);
333
334
        if (part == null)
335
        {
336
            part = new GridViewerRow(((GridItem)item));
337
        }
338
339
        return part;
340
    }
341
342
    /** {@inheritDoc} */
343
    protected ViewerRow doCreateNewRowPart(ViewerRow parent, int style, int rowIndex)
344
    {
345
        if (parent == null)
346
        {
347
            if (rowIndex >= 0)
348
            {
349
                return getViewerRowFromItem(new GridItem(grid, style, rowIndex));
350
            }
351
            return getViewerRowFromItem(new GridItem(grid, style));
352
        }
353
354
        if (rowIndex >= 0)
355
        {
356
            return getViewerRowFromItem(new GridItem((GridItem)parent.getItem(), 
357
                                                       SWT.NONE, rowIndex));
358
        }
359
360
        return getViewerRowFromItem(new GridItem((GridItem)parent.getItem(), SWT.NONE));
361
    }
362
363
    protected int getItemCount(Control control)
364
    {
365
        // TODO Auto-generated method stub
366
        return 0;
367
    }
368
369
    protected int getItemCount(Item item)
370
    {
371
        // TODO Auto-generated method stub
372
        return 0;
373
    }
374
375
    protected Item newItem(Widget parent, int style, int index)
376
    {
377
        // TODO Auto-generated method stub
378
        return null;
379
    }
380
381
    protected void setExpanded(Item item, boolean expand)
382
    {
383
        // TODO Auto-generated method stub
384
        
385
    }
386
387
    protected void setSelection(List items)
388
    {
389
        // TODO Auto-generated method stub
390
        
391
    }
392
}

Return to bug 172646