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

(-)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.CellEditorActivationEvent;
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
					CellEditorActivationEvent event) {
156
				return event.eventType == CellEditorActivationEvent.TRAVERSAL || event.eventType == CellEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION || ( event.eventType == CellEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR ) || event.eventType == CellEditorActivationEvent.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
}
(-)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 (-3 / +295 lines)
Lines 13-21 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;
19
import org.eclipse.core.runtime.ListenerList;
17
import org.eclipse.swt.SWT;
20
import org.eclipse.swt.SWT;
21
import org.eclipse.swt.events.FocusAdapter;
22
import org.eclipse.swt.events.FocusEvent;
23
import org.eclipse.swt.events.FocusListener;
24
import org.eclipse.swt.events.MouseAdapter;
25
import org.eclipse.swt.events.MouseEvent;
26
import org.eclipse.swt.events.MouseListener;
18
import org.eclipse.swt.events.TraverseEvent;
27
import org.eclipse.swt.events.TraverseEvent;
28
import org.eclipse.swt.events.TraverseListener;
29
import org.eclipse.swt.widgets.Control;
30
import org.eclipse.swt.widgets.Display;
31
import org.eclipse.swt.widgets.Item;
19
32
20
/**
33
/**
21
 * EditingSupport is the abstract superclass of the support for cell editing.
34
 * EditingSupport is the abstract superclass of the support for cell editing.
Lines 56-61 Link Here
56
69
57
	private ColumnViewer viewer;
70
	private ColumnViewer viewer;
58
71
72
	private ICellEditorListener cellEditorListener;
73
74
	private FocusListener focusListener;
75
76
	private MouseListener mouseListener;
77
78
	private TraverseListener tabeditingListener;
79
		
80
	private ViewerCell cell;
81
	
82
	private int activationTime;
83
	
84
	private CellEditor cellEditor;
85
	
86
	private ColumnViewerEditor editor;
87
	
59
	/**
88
	/**
60
	 * @param viewer
89
	 * @param viewer
61
	 *            a new viewer
90
	 *            a new viewer
Lines 63-68 Link Here
63
	public EditingSupport(ColumnViewer viewer) {
92
	public EditingSupport(ColumnViewer viewer) {
64
		Assert.isNotNull(viewer,"Viewer is not allowed to be null"); //$NON-NLS-1$
93
		Assert.isNotNull(viewer,"Viewer is not allowed to be null"); //$NON-NLS-1$
65
		this.viewer = viewer;
94
		this.viewer = viewer;
95
		initCellEditorListener();
66
	}
96
	}
67
97
68
	/**
98
	/**
Lines 170-177 Link Here
170
		}
200
		}
171
201
172
		if (cell2edit != null) {
202
		if (cell2edit != null) {
173
			viewer.editElement(cell2edit.getElement(), cell2edit
203
			List l = viewer.getSelectionFromWidget();
174
					.getColumnIndex());
204
						
205
			viewer.getControl().setRedraw(false);
206
			
207
			//TODO Is this correct in terms of other controls e.g. grid
208
			if( ! l.contains(cell2edit.getElement()) ) {
209
				viewer.setSelection(new StructuredSelection(cell2edit.getElement()));
210
			}
211
						
212
			CellEditorActivationEvent acEvent = new CellEditorActivationEvent(cell2edit,event);
213
			viewer.triggerCellEditorActivation(acEvent);
214
			viewer.getControl().setRedraw(true);
175
		}
215
		}
176
	}
216
	}
177
217
Lines 208-213 Link Here
208
		if (columnIndex - 1 >= 0) {
248
		if (columnIndex - 1 >= 0) {
209
			ViewerColumn column = viewer.getViewerColumn(columnIndex - 1);
249
			ViewerColumn column = viewer.getViewerColumn(columnIndex - 1);
210
			if (column != null
250
			if (column != null
251
					&& column.getEditingSupport() != null
211
					&& column.getEditingSupport().canEdit(
252
					&& column.getEditingSupport().canEdit(
212
							row.getItem().getData())) {
253
							row.getItem().getData())) {
213
				rv = row.getCell(columnIndex - 1);
254
				rv = row.getCell(columnIndex - 1);
Lines 244-249 Link Here
244
		if (columnIndex + 1 < row.getColumnCount()) {
285
		if (columnIndex + 1 < row.getColumnCount()) {
245
			ViewerColumn column = viewer.getViewerColumn(columnIndex + 1);
286
			ViewerColumn column = viewer.getViewerColumn(columnIndex + 1);
246
			if (column != null
287
			if (column != null
288
					&& column.getEditingSupport() != null
247
					&& column.getEditingSupport().canEdit(
289
					&& column.getEditingSupport().canEdit(
248
							row.getItem().getData())) {
290
							row.getItem().getData())) {
249
				rv = row.getCell(columnIndex + 1);
291
				rv = row.getCell(columnIndex + 1);
Lines 275-278 Link Here
275
	public ColumnViewer getViewer() {
317
	public ColumnViewer getViewer() {
276
		return viewer;
318
		return viewer;
277
	}
319
	}
278
}
320
	
321
	// API MOVED FROM ColumnViewerEditor!
322
	
323
	void activateCellEditor(ColumnViewerEditor editor, CellEditorActivationEvent activationEvent) {
324
		this.editor = editor;
325
		activationTime = activationEvent.time + Display.getCurrent().getDoubleClickTime();
326
		this.cell = (ViewerCell) activationEvent.getSource();
327
			
328
		ViewerColumn part = viewer.getViewerColumn(cell.getColumnIndex());
329
		Object element = cell.getElement();
330
331
		if (part != null && part.getEditingSupport() != null
332
				&& part.getEditingSupport().canEdit(element)) {
333
			cellEditor = part.getEditingSupport().getCellEditor(element);
334
			if (cellEditor != null) {
335
				
336
				ListenerList list = editor.getEditorActivationListeners();
337
				
338
				if( list != null && ! list.isEmpty() ) {
339
					Object[] ls = list.getListeners();
340
					for( int i = 0; i < ls.length; i++ ) {
341
						
342
						if( ! activationEvent.doIt ) {
343
							return;
344
						}
345
						
346
						((CellEditorActivationListener)ls[i]).beforeEditorActivated(activationEvent);
347
					}
348
				}
349
				
350
				cellEditor.addListener(cellEditorListener);
351
				Object value = part.getEditingSupport().getValue(element);
352
				cellEditor.setValue(value);
353
				// Tricky flow of control here:
354
				// activate() can trigger callback to cellEditorListener which
355
				// will clear cellEditor
356
				// so must get control first, but must still call activate()
357
				// even if there is no control.
358
				final Control control = cellEditor.getControl();
359
				cellEditor.activate(activationEvent);
360
				if (control == null) {
361
					return;
362
				}
363
				editor.setLayoutData(cellEditor.getLayoutData());
364
				editor.setEditor(control, cell.getItem(), cell.getColumnIndex());
365
				cellEditor.setFocus();
366
				if (focusListener == null) {
367
					focusListener = new FocusAdapter() {
368
						public void focusLost(FocusEvent e) {
369
							applyEditorValue();
370
						}
371
					};
372
				}
373
				control.addFocusListener(focusListener);
374
375
				mouseListener = new MouseAdapter() {
376
					public void mouseDown(MouseEvent e) {
377
						// time wrap?
378
						// check for expiration of doubleClickTime
379
						if (e.time <= activationTime) {
380
							control.removeMouseListener(mouseListener);
381
							cancelEditing();
382
							EditingSupport.this.editor.handleDoubleClickEvent();
383
						} else if (mouseListener != null) {
384
							control.removeMouseListener(mouseListener);
385
						}
386
					}
387
				};
388
				control.addMouseListener(mouseListener);
389
390
				if( tabeditingListener == null ) {
391
					tabeditingListener = new TraverseListener() {
392
393
						public void keyTraversed(TraverseEvent e) {
394
							ViewerColumn col = viewer.getViewerColumn(cell.getColumnIndex());
395
							if ( col != null && col.getEditingSupport().isTabingSupported() ) {
396
								col.getEditingSupport().processTraversEvent(
397
										cell.getColumnIndex(),
398
										viewer.getViewerRowFromItem(cell.getItem()), e);
399
							}
400
						}
401
					};
402
				}
403
				
404
				control.addTraverseListener(tabeditingListener);
405
				
406
				if( list != null && ! list.isEmpty() ) {
407
					Object[] ls = list.getListeners();
408
					for( int i = 0; i < ls.length; i++ ) {
409
						if( ! activationEvent.doIt ) {
410
							return;
411
						}
412
						((CellEditorActivationListener)ls[i]).afterEditorActivated(activationEvent);
413
					}
414
				}
415
			}
416
		}
417
	}
418
	
419
	/**
420
	 * Applies the current value and deactivates the currently active cell
421
	 * editor.
422
	 */
423
	void applyEditorValue() {
424
		CellEditor c = this.cellEditor;
425
		if (c != null && editor != null) {
426
			// null out cell editor before calling save
427
			// in case save results in applyEditorValue being re-entered
428
			// see 1GAHI8Z: ITPUI:ALL - How to code event notification when
429
			// using cell editor ?
430
			CellEditorDeactivationEvent tmp = new CellEditorDeactivationEvent(cell);
431
			ListenerList listeners = editor.getEditorActivationListeners();
432
			if( listeners != null && ! editor.getEditorActivationListeners().isEmpty() ) {
433
				Object[] ls = editor.getEditorActivationListeners().getListeners();
434
				for( int i = 0; i < ls.length; i++ ) {
435
					
436
					if( ! tmp.doIt ) {
437
						return;
438
					}
439
					
440
					((CellEditorActivationListener)ls[i]).beforeEditorDeactivated(tmp);
441
				}
442
			}
443
			
444
			this.cellEditor = null;
445
			
446
			Item t = this.cell.getItem();
447
			// don't null out table item -- same item is still selected
448
			if (t != null && !t.isDisposed()) {
449
				saveEditorValue(c, t);
450
			}
451
			editor.setEditor(null, null, 0);
452
			this.editor = null;
453
			c.removeListener(cellEditorListener);
454
			Control control = c.getControl();
455
			if (control != null) {
456
				if (mouseListener != null) {
457
					control.removeMouseListener(mouseListener);
458
					// Clear the instance not needed any more
459
					mouseListener = null;
460
				}
461
				if (focusListener != null) {
462
					control.removeFocusListener(focusListener);
463
				}
464
465
				if (tabeditingListener != null) {
466
					control.removeTraverseListener(tabeditingListener);
467
				}
468
			}
469
			
470
			c.deactivate();
471
			
472
			if( listeners != null && ! listeners.isEmpty() ) {
473
				Object[] ls = listeners.getListeners();
474
				for( int i = 0; i < ls.length; i++ ) {
475
					if( ! tmp.doIt ) {
476
						return;
477
					}
478
					((CellEditorActivationListener)ls[i]).afterEditorDeactivated(tmp);
479
				}
480
			}
481
		}
482
	}
483
	
484
	/**
485
	 * Cancle editing
486
	 */
487
	void cancelEditing() {
488
		if (cellEditor != null && editor != null) {
489
			CellEditorDeactivationEvent tmp = new CellEditorDeactivationEvent(cell);
490
			ListenerList listeners = editor.getEditorActivationListeners();
491
			
492
			if( listeners != null && ! listeners.isEmpty() ) {
493
				Object[] ls = listeners.getListeners();
494
				for( int i = 0; i < ls.length; i++ ) {
495
					if( ! tmp.doIt ) {
496
						return;
497
					}
498
					
499
					((CellEditorActivationListener)ls[i]).beforeEditorDeactivated(tmp);
500
				}
501
			}
502
503
			editor.setEditor(null, null, 0);
504
			cellEditor.removeListener(cellEditorListener);
505
506
			Control control = cellEditor.getControl();
507
			if (control != null) {
508
				if (mouseListener != null) {
509
					control.removeMouseListener(mouseListener);
510
					// Clear the instance not needed any more
511
					mouseListener = null;
512
				}
513
				if (focusListener != null) {
514
					control.removeFocusListener(focusListener);
515
				}
516
517
				if (tabeditingListener != null) {
518
					control.removeTraverseListener(tabeditingListener);
519
				}
520
			}
521
522
			CellEditor oldEditor = cellEditor;
523
			cellEditor = null;
524
			editor=null;
525
			oldEditor.deactivate();
526
			
527
			if( listeners != null && ! listeners.isEmpty() ) {
528
				Object[] ls = listeners.getListeners();
529
				for( int i = 0; i < ls.length; i++ ) {
530
					
531
					if( ! tmp.doIt ) {
532
						return;
533
					}
534
					
535
					((CellEditorActivationListener)ls[i]).afterEditorDeactivated(tmp);
536
				}
537
			}
538
		}
539
	}
540
	
541
	private void saveEditorValue(CellEditor cellEditor, Item tableItem) {
542
		ViewerColumn part = viewer.getViewerColumn(cell.getColumnIndex());
543
544
		if (part != null && part.getEditingSupport() != null) {
545
			part.getEditingSupport().setValue(tableItem.getData(),
546
					cellEditor.getValue());
547
		}
548
	}
549
	
550
	private void initCellEditorListener() {
551
		cellEditorListener = new ICellEditorListener() {
552
			public void editorValueChanged(boolean oldValidState,
553
					boolean newValidState) {
554
				// Ignore.
555
			}
556
557
			public void cancelEditor() {
558
				EditingSupport.this.cancelEditing();
559
			}
560
561
			public void applyEditorValue() {
562
				EditingSupport.this.applyEditorValue();
563
			}
564
		};
565
	}
566
	
567
	boolean isCellEditorActive() {
568
		return cellEditor != null;
569
	}
570
}
(-)src/org/eclipse/jface/viewers/ColumnViewer.java (-2 / +63 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-82 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
		}
Lines 371-377 Link Here
371
	 */
373
	 */
372
	public void editElement(Object element, int column) {
374
	public void editElement(Object element, int column) {
373
		if (viewerEditor != null) {
375
		if (viewerEditor != null) {
374
			viewerEditor.editElement(element, column);
376
			Widget item = findItem(element);
377
			if (item != null) {
378
				ViewerRow row = getViewerRowFromItem(item);
379
				if (row != null) {
380
					ViewerCell cell = row.getCell(column);
381
					if (cell != null) {
382
						getControl().setRedraw(false);
383
384
						List l = getSelectionFromWidget();
385
386
						if (l.size() > 1 || !l.contains(cell.getElement())) {
387
							setSelection(new StructuredSelection(cell
388
									.getElement()));
389
						}
390
391
						triggerCellEditorActivation(new CellEditorActivationEvent(
392
								cell));
393
						getControl().setRedraw(true);
394
					}
395
				}
396
			}
375
		}
397
		}
376
	}
398
	}
377
399
Lines 576-579 Link Here
576
		}
598
		}
577
		return null;
599
		return null;
578
	}
600
	}
601
602
	private void handleMouseDown(MouseEvent e) {
603
		ViewerCell cell = getCell(new Point(e.x, e.y));
604
605
		if (cell != null) {
606
			triggerCellEditorActivation(new CellEditorActivationEvent(cell, e));
607
		}
608
	}
609
610
	/**
611
	 * Invoking this method fires an editor activation event which tries to
612
	 * enable the editor but before this event is passed to
613
	 * {@link ColumnViewerEditorActivationSupport} to see if this event should
614
	 * really trigger editor activation
615
	 * 
616
	 * @param event
617
	 *            the activation event
618
	 */
619
	protected void triggerCellEditorActivation(CellEditorActivationEvent event) {
620
		viewerEditor.triggerCellEditorActivation(event);
621
	}
622
623
	/**
624
	 * To fully control the strategy used to control on which cell-selection
625
	 * events the editor has to be activated you can pass your customized
626
	 * strategy using this method.
627
	 * 
628
	 * @param editorActivationStrategy
629
	 *            the strategy used to activate an editor
630
	 */
631
	public void setEditorActivationSupport(
632
			ColumnViewerEditorActivationSupport editorActivationStrategy) {
633
		if (viewerEditor != null) {
634
			viewerEditor.setEditorActivationStrategy(editorActivationStrategy);
635
		} else {
636
			throw new IllegalStateException(
637
					"This feature is not supported by the widget"); //$NON-NLS-1$
638
		}
639
	}
579
}
640
}
(-)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(CellEditorActivationEvent activationEvent) {
864
    	activate();
865
    }
856
}
866
}
(-)src/org/eclipse/jface/viewers/ColumnViewerEditor.java (-280 / +61 lines)
Lines 14-30 Link Here
14
14
15
package org.eclipse.jface.viewers;
15
package org.eclipse.jface.viewers;
16
16
17
import org.eclipse.swt.events.FocusAdapter;
17
18
import org.eclipse.swt.events.FocusEvent;
18
import org.eclipse.core.runtime.ListenerList;
19
import org.eclipse.swt.events.FocusListener;
20
import org.eclipse.swt.events.MouseAdapter;
21
import org.eclipse.swt.events.MouseEvent;
22
import org.eclipse.swt.events.MouseListener;
23
import org.eclipse.swt.events.TraverseEvent;
24
import org.eclipse.swt.events.TraverseListener;
25
import org.eclipse.swt.graphics.Rectangle;
26
import org.eclipse.swt.widgets.Control;
19
import org.eclipse.swt.widgets.Control;
27
import org.eclipse.swt.widgets.Display;
28
import org.eclipse.swt.widgets.Item;
20
import org.eclipse.swt.widgets.Item;
29
21
30
/**
22
/**
Lines 38-66 Link Here
38
 * 
30
 * 
39
 */
31
 */
40
public abstract class ColumnViewerEditor {
32
public abstract class ColumnViewerEditor {
41
	private CellEditor cellEditor;
42
43
	private CellEditor[] cellEditors;
33
	private CellEditor[] cellEditors;
44
34
45
	private ICellModifier cellModifier;
35
	private ICellModifier cellModifier;
46
36
47
	private String[] columnProperties;
37
	private String[] columnProperties;
48
38
49
	private int columnNumber;
50
51
	private ICellEditorListener cellEditorListener;
52
53
	private FocusListener focusListener;
54
55
	private MouseListener mouseListener;
56
57
	private int doubleClickExpirationTime;
58
59
	private ColumnViewer viewer;
39
	private ColumnViewer viewer;
60
40
	
61
	private Item item;
41
	private ListenerList editorActivationListener;
62
42
	
63
	private TraverseListener tabeditingListener;
43
	private ColumnViewerEditorActivationSupport editorActivationStrategy;
44
	
45
	private ViewerColumn activePart;
64
46
65
	/**
47
	/**
66
	 * Create a new editor implementation for the viewer
48
	 * Create a new editor implementation for the viewer
Lines 70-329 Link Here
70
	 */
52
	 */
71
	public ColumnViewerEditor(ColumnViewer viewer) {
53
	public ColumnViewerEditor(ColumnViewer viewer) {
72
		this.viewer = viewer;
54
		this.viewer = viewer;
73
		initCellEditorListener();
55
		editorActivationStrategy = new ColumnViewerEditorActivationSupport(viewer,null) {
74
	}
56
			protected boolean isEditorActivationEvent(CellEditorActivationEvent event) {
75
57
				return event.eventType == CellEditorActivationEvent.MOUSE_CLICK_SELECTION
76
	private void initCellEditorListener() {
58
					|| event.eventType == CellEditorActivationEvent.PROGRAMATIC
77
		cellEditorListener = new ICellEditorListener() {
59
					|| event.eventType == CellEditorActivationEvent.TRAVERSAL;
78
			public void editorValueChanged(boolean oldValidState,
79
					boolean newValidState) {
80
				// Ignore.
81
			}
82
83
			public void cancelEditor() {
84
				ColumnViewerEditor.this.cancelEditing();
85
			}
86
87
			public void applyEditorValue() {
88
				ColumnViewerEditor.this.applyEditorValue();
89
			}
90
		};
91
	}
92
93
	void activateCellEditor() {
94
95
		ViewerColumn part = viewer.getViewerColumn(columnNumber);
96
		Object element = item.getData();
97
98
		if (part != null && part.getEditingSupport() != null
99
				&& part.getEditingSupport().canEdit(element)) {
100
			cellEditor = part.getEditingSupport().getCellEditor(element);
101
			if (cellEditor != null) {
102
				cellEditor.addListener(cellEditorListener);
103
				Object value = part.getEditingSupport().getValue(element);
104
				cellEditor.setValue(value);
105
				// Tricky flow of control here:
106
				// activate() can trigger callback to cellEditorListener which
107
				// will clear cellEditor
108
				// so must get control first, but must still call activate()
109
				// even if there is no control.
110
				final Control control = cellEditor.getControl();
111
				cellEditor.activate();
112
				if (control == null) {
113
					return;
114
				}
115
				setLayoutData(cellEditor.getLayoutData());
116
				setEditor(control, item, columnNumber);
117
				cellEditor.setFocus();
118
				if (focusListener == null) {
119
					focusListener = new FocusAdapter() {
120
						public void focusLost(FocusEvent e) {
121
							applyEditorValue();
122
						}
123
					};
124
				}
125
				control.addFocusListener(focusListener);
126
127
				mouseListener = new MouseAdapter() {
128
					public void mouseDown(MouseEvent e) {
129
						// time wrap?
130
						// check for expiration of doubleClickTime
131
						if (e.time <= doubleClickExpirationTime) {
132
							control.removeMouseListener(mouseListener);
133
							cancelEditing();
134
							handleDoubleClickEvent();
135
						} else if (mouseListener != null) {
136
							control.removeMouseListener(mouseListener);
137
						}
138
					}
139
				};
140
				control.addMouseListener(mouseListener);
141
142
				if( tabeditingListener == null ) {
143
					tabeditingListener = new TraverseListener() {
144
145
						public void keyTraversed(TraverseEvent e) {
146
							ViewerColumn col = viewer.getViewerColumn(columnNumber);
147
							if ( col != null && col.getEditingSupport().isTabingSupported() ) {
148
								col.getEditingSupport().processTraversEvent(
149
										columnNumber,
150
										viewer.getViewerRowFromItem(item), e);
151
							}
152
						}
153
					};
154
				}
60
				}
155
				
156
				control.addTraverseListener(tabeditingListener);
157
61
62
			protected ViewerCell getFocusCell() {
63
				return null;
158
			}
64
			}
159
		}
65
			
66
		};
67
		
160
	}
68
	}
161
69
	
162
	/**
70
	void cancelEditing() {
163
	 * Activate a cell editor for the given mouse position.
71
		if( activePart != null && activePart.getEditingSupport().isCellEditorActive() ) {
164
	 */
72
			activePart.getEditingSupport().cancelEditing();
165
	private void activateCellEditor(MouseEvent event) {
166
		if (item == null || item.isDisposed()) {
167
			// item no longer exists
168
			return;
169
		}
170
		int columnToEdit;
171
		ViewerRow row = viewer.getViewerRowFromItem(item);
172
		int columns = row.getColumnCount();
173
		if (columns == 0) {
174
			// If no TableColumn, Table acts as if it has a single column
175
			// which takes the whole width.
176
			columnToEdit = 0;
177
		} else {
178
			columnToEdit = -1;
179
			for (int i = 0; i < columns; i++) {
180
				Rectangle bounds = row.getBounds(i);
181
				if (bounds.contains(event.x, event.y)) {
182
					columnToEdit = i;
183
					break;
184
				}
185
			}
186
			if (columnToEdit == -1) {
187
				return;
188
			}
189
		}
73
		}
190
191
		columnNumber = columnToEdit;
192
		activateCellEditor();
193
	}
74
	}
194
75
	
195
	/**
196
	 * Applies the current value and deactivates the currently active cell
197
	 * editor.
198
	 */
199
	void applyEditorValue() {
76
	void applyEditorValue() {
200
		CellEditor c = this.cellEditor;
77
		if( activePart != null && activePart.getEditingSupport().isCellEditorActive() ) {
201
		if (c != null) {
78
			activePart.getEditingSupport().applyEditorValue();
202
			// null out cell editor before calling save
203
			// in case save results in applyEditorValue being re-entered
204
			// see 1GAHI8Z: ITPUI:ALL - How to code event notification when
205
			// using cell editor ?
206
			this.cellEditor = null;
207
			Item t = this.item;
208
			// don't null out table item -- same item is still selected
209
			if (t != null && !t.isDisposed()) {
210
				saveEditorValue(c, t);
211
			}
212
			setEditor(null, null, 0);
213
			c.removeListener(cellEditorListener);
214
			Control control = c.getControl();
215
			if (control != null) {
216
				if (mouseListener != null) {
217
					control.removeMouseListener(mouseListener);
218
					// Clear the instance not needed any more
219
					mouseListener = null;
220
				}
221
				if (focusListener != null) {
222
					control.removeFocusListener(focusListener);
223
				}
224
225
				if (tabeditingListener != null) {
226
					control.removeTraverseListener(tabeditingListener);
227
				}
228
			}
229
			c.deactivate();
230
		}
79
		}
231
	}
80
	}
232
81
	
233
	/**
234
	 * Cancle editing
235
	 */
236
	void cancelEditing() {
237
		if (cellEditor != null) {
238
			setEditor(null, null, 0);
239
			cellEditor.removeListener(cellEditorListener);
240
241
			Control control = cellEditor.getControl();
242
			if (control != null) {
243
				if (mouseListener != null) {
244
					control.removeMouseListener(mouseListener);
245
					// Clear the instance not needed any more
246
					mouseListener = null;
247
				}
248
				if (focusListener != null) {
249
					control.removeFocusListener(focusListener);
250
				}
251
252
				if (tabeditingListener != null) {
253
					control.removeTraverseListener(tabeditingListener);
254
				}
255
			}
256
257
			CellEditor oldEditor = cellEditor;
258
			cellEditor = null;
259
			oldEditor.deactivate();
260
		}
261
	}
262
263
	/**
82
	/**
264
	 * Enable the editor by mouse down
83
	 * Enable the editor by mouse down
265
	 * 
84
	 * 
266
	 * @param event
85
	 * @param event
267
	 */
86
	 */
268
	void handleMouseDown(MouseEvent event) {
87
	void triggerCellEditorActivation(CellEditorActivationEvent event) {
269
		if (event.button != 1) {
88
		if( editorActivationStrategy.isEditorActivationEvent(event) ) {
270
			return;
89
			
271
		}
90
			if (activePart != null && activePart.getEditingSupport() != null ) {
272
91
				activePart.getEditingSupport().applyEditorValue();
273
		if (cellEditor != null) {
92
			}
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
306
		setSelection(createSelection(element), true);
307
		Item[] selection = getSelection();
308
		if (selection.length != 1) {
309
			return;
310
		}
311
312
		item = selection[0];
313
314
		// Make sure selection is visible
315
		showSelection();
316
		columnNumber = column;
317
		activateCellEditor();
318
319
	}
320
93
321
	private void saveEditorValue(CellEditor cellEditor, Item tableItem) {
94
			ViewerCell cell = (ViewerCell)event.getSource();			
322
		ViewerColumn part = viewer.getViewerColumn(columnNumber);
95
			ViewerColumn part = viewer.getViewerColumn(cell.getColumnIndex());
323
96
324
		if (part != null && part.getEditingSupport() != null) {
97
			if (part != null && part.getEditingSupport() != null ) {
325
			part.getEditingSupport().setValue(tableItem.getData(),
98
				part.getEditingSupport().activateCellEditor(this,event);
326
					cellEditor.getValue());
99
			}
327
		}
100
		}
328
	}
101
	}
329
102
Lines 334-340 Link Here
334
	 *         <code>false</code> is returned.
107
	 *         <code>false</code> is returned.
335
	 */
108
	 */
336
	boolean isCellEditorActive() {
109
	boolean isCellEditorActive() {
337
		return cellEditor != null;
110
		return activePart != null && activePart.getEditingSupport() != null && activePart.getEditingSupport().isCellEditorActive();
338
	}
111
	}
339
112
340
	/**
113
	/**
Lines 391-406 Link Here
391
		return cellEditors;
164
		return cellEditors;
392
	}
165
	}
393
166
394
	void setSelection(StructuredSelection selection, boolean b) {
395
		viewer.setSelection(selection, b);
396
	}
397
398
	void handleDoubleClickEvent() {
167
	void handleDoubleClickEvent() {
399
		viewer.fireDoubleClick(new DoubleClickEvent(viewer, viewer
168
		viewer.fireDoubleClick(new DoubleClickEvent(viewer, viewer
400
				.getSelection()));
169
				.getSelection()));
401
		viewer.fireOpen(new OpenEvent(viewer, viewer.getSelection()));
170
		viewer.fireOpen(new OpenEvent(viewer, viewer.getSelection()));
402
	}
171
	}
403
172
173
	void addEditorActivationListener(CellEditorActivationListener listener) {
174
		if( editorActivationListener == null ) {
175
			editorActivationListener = new ListenerList();
176
		}
177
		editorActivationListener.add(listener);
178
	}
179
	
180
	void removeEditorActivationListener(CellEditorActivationListener listener) {
181
		if( editorActivationListener != null ) {
182
			editorActivationListener.remove(listener);
183
		}
184
	}
185
	
186
	ListenerList getEditorActivationListeners() {
187
		return editorActivationListener;
188
	}
189
	
190
	void setEditorActivationStrategy(
191
			ColumnViewerEditorActivationSupport editorActivationStrategy) {
192
		this.editorActivationStrategy = editorActivationStrategy;
193
	}
194
	
404
	/**
195
	/**
405
	 * Create a selection for this model element
196
	 * Create a selection for this model element
406
	 * 
197
	 * 
Lines 429-442 Link Here
429
	 *            the layout data used when editor is displayed
220
	 *            the layout data used when editor is displayed
430
	 */
221
	 */
431
	protected abstract void setLayoutData(CellEditor.LayoutData layoutData);
222
	protected abstract void setLayoutData(CellEditor.LayoutData layoutData);
432
223
}
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/CellEditorDeactivationEvent.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 CellEditorDeactivationEvent extends EventObject {
23
24
	/**
25
	 * 
26
	 */
27
	private static final long serialVersionUID = 1L;
28
29
	/**
30
	 * Cancle the event
31
	 */
32
	public boolean doIt = true;
33
	
34
	/**
35
	 * @param source
36
	 */
37
	public CellEditorDeactivationEvent(Object source) {
38
		super(source);
39
	}
40
41
}
(-)src/org/eclipse/jface/viewers/ColumnViewerEditorActivationSupport.java (+121 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.SWT;
15
import org.eclipse.swt.events.KeyEvent;
16
import org.eclipse.swt.events.KeyListener;
17
18
/**
19
 * This class is responsible to determin if a cell selection event is triggers
20
 * an editor activation
21
 * 
22
 * @since 3.3
23
 */
24
public class ColumnViewerEditorActivationSupport {
25
	/**
26
	 * Enable editor activation using the keyboard 
27
	 */
28
	public static final int WITH_KEYBOARD_ACTIVATION = 1;
29
	
30
	private ColumnViewer viewer;
31
	
32
	private KeyListener keyboardActivationListener;
33
	
34
	private CellSupport cellSupport;
35
	
36
	/**
37
	 * @param viewer the viewer the editor support is attached to
38
	 */
39
	public ColumnViewerEditorActivationSupport(ColumnViewer viewer) {
40
		this(viewer,null);
41
	}
42
	
43
	/**
44
	 * @param viewer the viewer the editor support is attached to
45
	 * @param cellSupport the cell support or <code>null</code> if there's none
46
	 */
47
	public ColumnViewerEditorActivationSupport(ColumnViewer viewer, CellSupport cellSupport) {
48
		this(viewer,cellSupport,SWT.NONE);
49
	}
50
	
51
	/**
52
	 * @param viewer the viewer the editor support is attached to
53
	 * @param cellSupport the cell support or <code>null</code> if there's none
54
	 * @param feature the feature bits used to turn on features easily ({@link #WITH_KEYBOARD_ACTIVATION})
55
	 */
56
	public ColumnViewerEditorActivationSupport(ColumnViewer viewer, CellSupport cellSupport, int feature) {
57
		this.viewer = viewer;
58
		this.cellSupport = cellSupport;
59
		if( (feature & WITH_KEYBOARD_ACTIVATION) == WITH_KEYBOARD_ACTIVATION ) {
60
			setEnableEditorActivationWithKeyboard(true);
61
		}
62
	}
63
	
64
	/**
65
	 * @return the cell holding the current focus
66
	 */
67
	protected ViewerCell getFocusCell() {
68
		if( cellSupport != null ) {
69
			return cellSupport.getFocusCell();
70
		}
71
		
72
		return null;
73
	}
74
	
75
	/**
76
	 * @param event
77
	 * @return <code>true</code> if the this event triggers editor activation
78
	 */
79
	protected boolean isEditorActivationEvent(CellEditorActivationEvent event) {
80
		return event.eventType == CellEditorActivationEvent.TRAVERSAL || event.eventType == CellEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION || event.eventType == CellEditorActivationEvent.PROGRAMATIC;
81
	}
82
	
83
	/**
84
	 * @return the viewer
85
	 */
86
	public ColumnViewer getViewer() {
87
		return viewer;
88
	}
89
	
90
	/**
91
	 * Enable activation of cell editors by keyboard
92
	 * @param enable <code>true</code> to enable
93
	 */
94
	public void setEnableEditorActivationWithKeyboard(boolean enable) {
95
		if( enable ) {
96
			if( keyboardActivationListener == null ) {
97
				keyboardActivationListener = new KeyListener() {
98
99
					public void keyPressed(KeyEvent e) {
100
						ViewerCell cell = getFocusCell();
101
						
102
						if( cell != null ) {
103
							viewer.triggerCellEditorActivation(new CellEditorActivationEvent(cell,e));
104
						}
105
					}
106
107
					public void keyReleased(KeyEvent e) {
108
						
109
					}
110
					
111
				};
112
				viewer.getControl().addKeyListener(keyboardActivationListener);
113
			}
114
		} else {
115
			if( keyboardActivationListener != null ) {
116
				viewer.getControl().removeKeyListener(keyboardActivationListener);
117
				keyboardActivationListener = null;
118
			}
119
		}
120
	}
121
}
(-)src/org/eclipse/jface/viewers/FocusCellHighlighter.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 FocusCellHighlighter {
22
	private CellSupport cellSupport;
23
	
24
	private ColumnViewer viewer;
25
26
	/**
27
	 * @param viewer
28
	 */
29
	public FocusCellHighlighter(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/FocusCellChangeListener.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 FocusCellChangeListener {
19
	/**
20
	 * @param event
21
	 *            the event informing about the selection of a cell
22
	 */
23
	public abstract void handleFocusCellChange(FocusCellChangeEvent event);
24
}
(-)src/org/eclipse/jface/viewers/FocusCellChangeEvent.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 FocusCellChangeEvent 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 FocusCellChangeEvent(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/FocusCellOwnerDrawHighlighter.java (+118 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 FocusCellOwnerDrawHighlighter extends FocusCellHighlighter {
26
	
27
	/**
28
	 * @param viewer the viewer
29
	 */
30
	public FocusCellOwnerDrawHighlighter(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
		
78
		Listener listener = new Listener() {
79
80
			public void handleEvent(Event event) {
81
				if((event.detail & SWT.SELECTED) > 0 ) {
82
					ViewerCell focusCell = getFocusCell();
83
					ViewerRow row = viewer.getViewerRowFromItem(event.item);
84
					
85
					Assert.isNotNull(row, "Internal structure invalid. Item without associated row is not possible."); //$NON-NLS-1$
86
					
87
					ViewerCell cell = row.getCell(event.index);
88
										
89
					if (focusCell == null || ! cell.equals(focusCell)) {
90
						removeSelectionInformation(event, cell);
91
					} else {
92
						markFocusedCell(event,cell);
93
					}
94
				}
95
			}
96
			
97
		};
98
		viewer.getControl().addListener(SWT.EraseItem, listener);
99
	}
100
101
	/**
102
	 * @param cell
103
	 *            the cell which is colored
104
	 * @return the color
105
	 */
106
	protected Color getSelectedCellBackgroundColor(ViewerCell cell) {
107
		return null;
108
	}
109
110
	/**
111
	 * @param cell
112
	 *            the cell which is colored
113
	 * @return the color
114
	 */
115
	protected Color getSelectedCellForegroundColor(ViewerCell cell) {
116
		return null;
117
	}
118
}
(-)src/org/eclipse/jface/viewers/SWTCellSupport.java (+159 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 CellNavigationStrategy TABLE_NAVIGATE = new CellNavigationStrategy();
27
28
	private static final CellNavigationStrategy TREE_NAVIGATE = new CellNavigationStrategy() {
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 CellNavigationStrategy navigationDelegate;
64
	
65
	/**
66
	 * @param viewer
67
	 * @param focusDrawingDelegate
68
	 */
69
	public SWTCellSupport(TableViewer viewer,
70
			FocusCellHighlighter focusDrawingDelegate) {
71
		this(viewer, focusDrawingDelegate, TABLE_NAVIGATE);
72
	}
73
74
	/**
75
	 * @param viewer
76
	 * @param focusDrawingDelegate
77
	 */
78
	public SWTCellSupport(TreeViewer viewer,
79
			FocusCellHighlighter focusDrawingDelegate) {
80
		this(viewer, focusDrawingDelegate, TREE_NAVIGATE);
81
	}
82
83
	private SWTCellSupport(ColumnViewer viewer,
84
			FocusCellHighlighter focusDrawingDelegate,
85
			CellNavigationStrategy navigationDelegate) {
86
		super(viewer);
87
		if (viewer != focusDrawingDelegate.getViewer()) {
88
			throw new IllegalArgumentException(
89
					"The FocusCellHighlighter 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( fireFocusCellChanged(new FocusCellChangeEvent(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
			navigationDelegate.expand(getViewer(), focusCell, event);
115
		} else if (navigationDelegate.isNavigationEvent(getViewer(), event)) {
116
			tmp = navigationDelegate.findSelectedCell(getViewer(), focusCell,event);
117
			
118
			if (tmp != null) {
119
				if( fireFocusCellChanged(new FocusCellChangeEvent(getViewer(),tmp,focusCell)) ) {
120
					getViewer().getControl().redraw();
121
				}
122
			}
123
		}
124
125
		if (navigationDelegate.cancleEvent(getViewer(), event)) {
126
			event.doit = false;
127
		}
128
	}
129
130
	private void handleSelection(Event event) {
131
		ViewerCell focusCell = getFocusCell();
132
		if( focusCell != null && focusCell.getItem() != event.item && event.item != null ) {
133
			ViewerRow row = getViewer().getViewerRowFromItem(event.item);
134
			Assert.isNotNull(row,"Internal Structure invalid. Row item has no row ViewerRow assigned"); //$NON-NLS-1$
135
			if( fireFocusCellChanged(new FocusCellChangeEvent(getViewer(),row.getCell(focusCell.getColumnIndex()),focusCell)) ) {
136
				getViewer().getControl().redraw();
137
			}
138
		}
139
	}
140
141
	protected void hookListener(ColumnViewer viewer) {
142
		Listener listener = new Listener() {
143
144
			public void handleEvent(Event event) {
145
				if (event.type == SWT.MouseDown) {
146
					handleMouseDown(event);
147
				} else if (event.type == SWT.KeyDown) {
148
					handleKeyDown(event);
149
				} else {
150
					handleSelection(event);
151
				}
152
			}
153
		};
154
155
		viewer.getControl().addListener(SWT.MouseDown, listener);
156
		viewer.getControl().addListener(SWT.KeyDown, listener);
157
		viewer.getControl().addListener(SWT.Selection, listener);
158
	}
159
}
(-)src/org/eclipse/jface/viewers/CellNavigationStrategy.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 CellNavigationStrategy {
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/CellEditorActivationListener.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 CellEditorActivationListener {
22
	/**
23
	 * Called before an editor is activated
24
	 * 
25
	 * @param event
26
	 *            the event
27
	 */
28
	public abstract void beforeEditorActivated(CellEditorActivationEvent event);
29
30
	/**
31
	 * Called after an editor has been activated
32
	 * 
33
	 * @param event the event
34
	 */
35
	public abstract void afterEditorActivated(CellEditorActivationEvent event);
36
37
	/**
38
	 * Called before an editor is deactivated
39
	 * 
40
	 * @param event
41
	 *            the event
42
	 */
43
	public abstract void beforeEditorDeactivated(CellEditorDeactivationEvent event);
44
45
	
46
	/**
47
	 * Called after an editor is deactivated
48
	 * 
49
	 * @param event the event
50
	 */
51
	public abstract void afterEditorDeactivated(CellEditorDeactivationEvent event);
52
}
(-)src/org/eclipse/jface/viewers/CellSupport.java (+91 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 focusCellChangedListeners = new ListenerList();
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 addFocusCellChangedListener(FocusCellChangeListener listener) {
54
		focusCellChangedListeners.add(listener);
55
	}
56
57
	/**
58
	 * @param listener
59
	 *            the listener to be removed
60
	 */
61
	public void removeFocusCellChangedListener(FocusCellChangeListener listener) {
62
		focusCellChangedListeners.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 fireFocusCellChanged(FocusCellChangeEvent event) {
78
		if (event.focusCell.equals(event.focusLostCell)) {
79
			return false;
80
		}
81
82
		this.focusCell = event.focusCell;
83
84
		Object[] ls = focusCellChangedListeners.getListeners();
85
86
		for (int i = 0; i < ls.length; i++) {
87
			((FocusCellChangeListener) ls[i]).handleFocusCellChange(event);
88
		}
89
		return true;
90
	}
91
}
(-)src/org/eclipse/jface/viewers/CellEditorActivationEvent.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 CellEditorActivationEvent 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 doIt = true;
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 CellEditorActivationEvent(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 CellEditorActivationEvent(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 CellEditorActivationEvent(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 CellEditorActivationEvent(ViewerCell cell, TraverseEvent event) {
160
		super(cell);
161
		this.eventType = TRAVERSAL;
162
		this.sourceEvent = event;
163
	}
164
}
(-)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
}
(-)Eclipse (+203 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.CellEditorActivationEvent;
18
import org.eclipse.jface.viewers.ColumnLabelProvider;
19
import org.eclipse.jface.viewers.ColumnViewerEditorActivationSupport;
20
import org.eclipse.jface.viewers.EditingSupport;
21
import org.eclipse.jface.viewers.FocusCellOwnerDrawHighlighter;
22
import org.eclipse.jface.viewers.ITreeContentProvider;
23
import org.eclipse.jface.viewers.SWTCellSupport;
24
import org.eclipse.jface.viewers.TextCellEditor;
25
import org.eclipse.jface.viewers.TreeViewer;
26
import org.eclipse.jface.viewers.TreeViewerColumn;
27
import org.eclipse.jface.viewers.Viewer;
28
import org.eclipse.swt.SWT;
29
import org.eclipse.swt.layout.FillLayout;
30
import org.eclipse.swt.widgets.Display;
31
import org.eclipse.swt.widgets.Shell;
32
33
/**
34
 * A simple TreeViewer to demonstrate usage
35
 * 
36
 * @author Tom Schindl <tom.schindl@bestsolution.at>
37
 * 
38
 */
39
public class Snippet026TreeViewerTabEditing {
40
	public Snippet026TreeViewerTabEditing(final Shell shell) {
41
		final TreeViewer v = new TreeViewer(shell, SWT.BORDER
42
				| SWT.FULL_SELECTION);
43
		v.getTree().setLinesVisible(true);
44
		v.getTree().setHeaderVisible(true);
45
46
		SWTCellSupport cellSupport = new SWTCellSupport(v,
47
				new FocusCellOwnerDrawHighlighter(v));
48
		ColumnViewerEditorActivationSupport actSupport = new ColumnViewerEditorActivationSupport(
49
				v, cellSupport) {
50
51
			protected boolean isEditorActivationEvent(
52
					CellEditorActivationEvent event) {
53
				return event.eventType == CellEditorActivationEvent.TRAVERSAL
54
						|| event.eventType == CellEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
55
						|| (event.eventType == CellEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR)
56
						|| event.eventType == CellEditorActivationEvent.PROGRAMATIC;
57
			}
58
59
		};
60
		actSupport.setEnableEditorActivationWithKeyboard(true);
61
62
		v.setEditorActivationSupport(actSupport);
63
64
		final TextCellEditor textCellEditor = new TextCellEditor(v.getTree());
65
66
		TreeViewerColumn column = new TreeViewerColumn(v, SWT.NONE);
67
		column.getColumn().setWidth(200);
68
		column.getColumn().setText("Column 1");
69
		column.setLabelProvider(new ColumnLabelProvider() {
70
71
			public String getText(Object element) {
72
				return "Column 1 => " + element.toString();
73
			}
74
75
		});
76
		column.setEditingSupport(new EditingSupport(v) {
77
			protected boolean canEdit(Object element) {
78
				return true;
79
			}
80
81
			protected CellEditor getCellEditor(Object element) {
82
				return textCellEditor;
83
			}
84
85
			protected Object getValue(Object element) {
86
				return ((MyModel) element).counter + "";
87
			}
88
89
			protected void setValue(Object element, Object value) {
90
				((MyModel) element).counter = Integer
91
						.parseInt(value.toString());
92
				v.update(element, null);
93
			}
94
		});
95
		v.setTabEditingStyle(EditingSupport.TABBING_HORIZONTAL
96
				| EditingSupport.TABBING_MOVE_TO_ROW_NEIGHBOR
97
				| EditingSupport.TABBING_VERTICAL);
98
99
		column = new TreeViewerColumn(v, SWT.NONE);
100
		column.getColumn().setWidth(200);
101
		column.getColumn().setText("Column 2");
102
		column.setLabelProvider(new ColumnLabelProvider() {
103
104
			public String getText(Object element) {
105
				return "Column 2 => " + element.toString();
106
			}
107
108
		});
109
		
110
		v.setContentProvider(new MyContentProvider());
111
112
		v.setInput(createModel());
113
	}
114
115
	private MyModel createModel() {
116
117
		MyModel root = new MyModel(0, null);
118
		root.counter = 0;
119
120
		MyModel tmp;
121
		MyModel subItem;
122
		for (int i = 1; i < 10; i++) {
123
			tmp = new MyModel(i, root);
124
			root.child.add(tmp);
125
			for (int j = 1; j < i; j++) {
126
				subItem = new MyModel(j, tmp);
127
				subItem.child.add(new MyModel(j * 100, subItem));
128
				tmp.child.add(subItem);
129
			}
130
		}
131
132
		return root;
133
	}
134
135
	public static void main(String[] args) {
136
		Display display = new Display();
137
		Shell shell = new Shell(display);
138
		shell.setLayout(new FillLayout());
139
		new Snippet026TreeViewerTabEditing(shell);
140
		shell.open();
141
142
		while (!shell.isDisposed()) {
143
			if (!display.readAndDispatch())
144
				display.sleep();
145
		}
146
147
		display.dispose();
148
	}
149
150
	private class MyContentProvider implements ITreeContentProvider {
151
152
		public Object[] getElements(Object inputElement) {
153
			return ((MyModel) inputElement).child.toArray();
154
		}
155
156
		public void dispose() {
157
		}
158
159
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
160
		}
161
162
		public Object[] getChildren(Object parentElement) {
163
			return getElements(parentElement);
164
		}
165
166
		public Object getParent(Object element) {
167
			if (element == null) {
168
				return null;
169
			}
170
			return ((MyModel) element).parent;
171
		}
172
173
		public boolean hasChildren(Object element) {
174
			return ((MyModel) element).child.size() > 0;
175
		}
176
177
	}
178
179
	public class MyModel {
180
		public MyModel parent;
181
182
		public ArrayList child = new ArrayList();
183
184
		public int counter;
185
186
		public MyModel(int counter, MyModel parent) {
187
			this.parent = parent;
188
			this.counter = counter;
189
		}
190
191
		public String toString() {
192
			String rv = "Item ";
193
			if (parent != null) {
194
				rv = parent.toString() + ".";
195
			}
196
197
			rv += counter;
198
199
			return rv;
200
		}
201
	}
202
203
}

Return to bug 172646