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 151377 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/jface/viewers/TableViewer.java (-2 / +7 lines)
Lines 631-638 Link Here
631
				return getTable().getSelection();
631
				return getTable().getSelection();
632
			}
632
			}
633
633
634
			protected void setEditor(Control w, Item item, int fColumnNumber) {
634
			protected void setEditor(Control w, ViewerCell cell) {
635
				tableEditor.setEditor(w, (TableItem) item, fColumnNumber);
635
				if( cell == null ) {
636
					tableEditor.setEditor(w, null, 0);
637
				} else {
638
					tableEditor.setEditor(w, (TableItem)cell.getItem(), cell.getColumnIndex());
639
				}
640
				
636
			}
641
			}
637
642
638
			protected void setLayoutData(LayoutData layoutData) {
643
			protected void setLayoutData(LayoutData layoutData) {
(-)src/org/eclipse/jface/viewers/TreeViewer.java (-2 / +7 lines)
Lines 345-352 Link Here
345
				return tree.getSelection();
345
				return tree.getSelection();
346
			}
346
			}
347
347
348
			protected void setEditor(Control w, Item item, int fColumnNumber) {
348
			protected void setEditor(Control w, ViewerCell cell) {
349
				treeEditor.setEditor(w, (TreeItem) item, fColumnNumber);
349
				if( cell == null ) {
350
					treeEditor.setEditor(w, null, 0);
351
				} else {
352
					treeEditor.setEditor(w, (TreeItem)cell.getItem(), cell.getColumnIndex());
353
				}
354
				
350
			}
355
			}
351
356
352
			protected void setLayoutData(LayoutData layoutData) {
357
			protected void setLayoutData(LayoutData layoutData) {
(-)src/org/eclipse/jface/viewers/ColumnViewer.java (-8 / +88 lines)
Lines 15-27 Link Here
15
package org.eclipse.jface.viewers;
15
package org.eclipse.jface.viewers;
16
16
17
import org.eclipse.core.runtime.Assert;
17
import org.eclipse.core.runtime.Assert;
18
import org.eclipse.swt.events.MouseAdapter;
18
import org.eclipse.core.runtime.ListenerList;
19
import org.eclipse.swt.events.MouseEvent;
19
import org.eclipse.swt.SWT;
20
import org.eclipse.swt.graphics.Point;
20
import org.eclipse.swt.graphics.Point;
21
import org.eclipse.swt.widgets.Control;
21
import org.eclipse.swt.widgets.Control;
22
import org.eclipse.swt.widgets.Event;
22
import org.eclipse.swt.widgets.Item;
23
import org.eclipse.swt.widgets.Item;
24
import org.eclipse.swt.widgets.Listener;
23
import org.eclipse.swt.widgets.Widget;
25
import org.eclipse.swt.widgets.Widget;
24
26
27
28
29
30
25
/**
31
/**
26
 * The ColumnViewer is the abstract superclass of viewers that have columns
32
 * The ColumnViewer is the abstract superclass of viewers that have columns
27
 * (e.g., AbstractTreeViewer and AbstractTableViewer). Concrete subclasses of
33
 * (e.g., AbstractTreeViewer and AbstractTableViewer). Concrete subclasses of
Lines 50-55 Link Here
50
56
51
	private int tabEditingStyle = EditingSupport.TABING_NONE;
57
	private int tabEditingStyle = EditingSupport.TABING_NONE;
52
	
58
	
59
	private ListenerList cellSelectionListeners;
60
	
53
	/**
61
	/**
54
	 * Create a new instance of the receiver.
62
	 * Create a new instance of the receiver.
55
	 */
63
	 */
Lines 74-84 Link Here
74
		// their
82
		// their
75
		// own impl
83
		// own impl
76
		if (viewerEditor != null) {
84
		if (viewerEditor != null) {
77
			control.addMouseListener(new MouseAdapter() {
85
			Listener l = new Listener() {
78
				public void mouseDown(MouseEvent e) {
86
79
					viewerEditor.handleMouseDown(e);
87
				public void handleEvent(Event event) {
88
					handleMouseDown(event);
80
				}
89
				}
81
			});
90
				
91
			};
92
			
93
			control.addListener(SWT.MouseDown, l);
94
			control.addListener(SWT.MouseDoubleClick, l);
95
			
96
			addCellSelectionListener(new EditingListener());
82
		}
97
		}
83
	}
98
	}
84
99
Lines 371-377 Link Here
371
	 */
386
	 */
372
	public void editElement(Object element, int column) {
387
	public void editElement(Object element, int column) {
373
		if (viewerEditor != null) {
388
		if (viewerEditor != null) {
374
			viewerEditor.editElement(element, column);
389
			Widget item = findItem(element);
390
			if( item != null ) {
391
				ViewerRow row = (ViewerRow)item.getData(ViewerRow.ROWPART_KEY);
392
				ViewerCell cell = row.getCell(column);
393
				if( cell != null ) {
394
					viewerEditor.editCell(cell);
395
				}
396
			}
375
		}
397
		}
376
	}
398
	}
377
399
Lines 544-547 Link Here
544
	int getTabEditingStyle() {
566
	int getTabEditingStyle() {
545
		return this.tabEditingStyle;
567
		return this.tabEditingStyle;
546
	}
568
	}
547
}
569
	
570
		
571
		/**
572
		 * Adding a cell selection listener
573
		 * 
574
		 * @param listener
575
		 *            listener for cell selections
576
		 */
577
		public void addCellSelectionListener(ICellSelectionListener listener) {
578
			if( cellSelectionListeners == null ) {
579
				cellSelectionListeners = new ListenerList();
580
			}
581
			this.cellSelectionListeners.add(listener);
582
		}
583
	
584
		/**
585
		 * Remove the cell selection listener
586
		 * 
587
		 * @param listener
588
		 *            listener for cell selections
589
		 */
590
		public void removeCellSelectionListener(ICellSelectionListener listener) {
591
			this.cellSelectionListeners.remove(listener);
592
		}
593
		
594
		private void handleMouseDown(Event event) {
595
			Point p = new Point(event.x,event.y);
596
			
597
			ViewerRow row = getViewerRow(p);
598
			
599
			if( row != null ) {
600
				ViewerCell cell = row.getCell(p);
601
				
602
				if( cell != null ) {
603
					fireCellSelectionEvent(new CellSelectionEvent(this,cell,event));
604
				}
605
			}
606
		}
607
		
608
		private void fireCellSelectionEvent(CellSelectionEvent event) {
609
			if( ! cellSelectionListeners.isEmpty() ) {
610
				Object[] listeners = cellSelectionListeners.getListeners();
611
				for( int i = 0; i < listeners.length; i++ ) {
612
					((ICellSelectionListener)listeners[i]).cellSelected(event);
613
				}
614
			}
615
		}
616
		
617
		private class EditingListener implements ICellSelectionListener {
618
	
619
			public void cellSelected(CellSelectionEvent event) {
620
				if( event.swtEvent.type == SWT.MouseDown || event.swtEvent.type == SWT.MouseDoubleClick ) {
621
					if( viewerEditor != null ) {
622
						viewerEditor.handleCellSelectionEvent(event);
623
					}
624
				}
625
			}
626
		}
627
}
(-)src/org/eclipse/jface/viewers/ViewerCell.java (+4 lines)
Lines 177-180 Link Here
177
	public Control getControl() {
177
	public Control getControl() {
178
		return row.getControl();
178
		return row.getControl();
179
	}
179
	}
180
	
181
	ViewerRow getViewerRow() {
182
		return row;
183
	}
180
}
184
}
(-)src/org/eclipse/jface/viewers/ViewerRow.java (-1 / +7 lines)
Lines 8-13 Link Here
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Tom Shindl <tom.schindl@bestsolution.at> - initial API and implementation
10
 *     Tom Shindl <tom.schindl@bestsolution.at> - initial API and implementation
11
 *                                                fix for bug 166346
11
 ******************************************************************************/
12
 ******************************************************************************/
12
13
13
package org.eclipse.jface.viewers;
14
package org.eclipse.jface.viewers;
Lines 189-195 Link Here
189
	 */
190
	 */
190
	public int getColumnIndex(Point point) {
191
	public int getColumnIndex(Point point) {
191
		int count = getColumnCount();
192
		int count = getColumnCount();
192
193
		
194
		// If there are no columns the column-index is 0
195
		if( count == 0 ) {
196
			return 0;
197
		}
198
		
193
		for (int i = 0; i < count; i++) {
199
		for (int i = 0; i < count; i++) {
194
			if (getBounds(i).contains(point)) {
200
			if (getBounds(i).contains(point)) {
195
				return i;
201
				return i;
(-)src/org/eclipse/jface/viewers/AbstractViewerEditor.java (-78 / +32 lines)
Lines 22-28 Link Here
22
import org.eclipse.swt.events.MouseListener;
22
import org.eclipse.swt.events.MouseListener;
23
import org.eclipse.swt.events.TraverseEvent;
23
import org.eclipse.swt.events.TraverseEvent;
24
import org.eclipse.swt.events.TraverseListener;
24
import org.eclipse.swt.events.TraverseListener;
25
import org.eclipse.swt.graphics.Rectangle;
26
import org.eclipse.swt.widgets.Control;
25
import org.eclipse.swt.widgets.Control;
27
import org.eclipse.swt.widgets.Display;
26
import org.eclipse.swt.widgets.Display;
28
import org.eclipse.swt.widgets.Item;
27
import org.eclipse.swt.widgets.Item;
Lines 46-66 Link Here
46
45
47
	private String[] columnProperties;
46
	private String[] columnProperties;
48
47
49
	private int columnNumber;
50
51
	private ICellEditorListener cellEditorListener;
48
	private ICellEditorListener cellEditorListener;
52
49
53
	private FocusListener focusListener;
50
	private FocusListener focusListener;
54
51
55
	private MouseListener mouseListener;
52
	private MouseListener mouseListener;
56
53
57
	private int doubleClickExpirationTime;
58
59
	private ColumnViewer viewer;
54
	private ColumnViewer viewer;
60
55
61
	private Item item;
62
63
	private TraverseListener tabeditingListener;
56
	private TraverseListener tabeditingListener;
57
	
58
	private int activationTime;
59
	
60
	private ViewerCell cell;
64
61
65
	/**
62
	/**
66
	 * Create a new editor implementation for the viewer
63
	 * Create a new editor implementation for the viewer
Lines 92-99 Link Here
92
89
93
	void activateCellEditor() {
90
	void activateCellEditor() {
94
91
95
		ViewerColumn part = viewer.getViewerColumn(columnNumber);
92
		ViewerColumn part = viewer.getViewerColumn(cell.getColumnIndex());
96
		Object element = item.getData();
93
		Object element = cell.getElement();
97
94
98
		if (part != null && part.getEditingSupport() != null
95
		if (part != null && part.getEditingSupport() != null
99
				&& part.getEditingSupport().canEdit(element)) {
96
				&& part.getEditingSupport().canEdit(element)) {
Lines 113-119 Link Here
113
					return;
110
					return;
114
				}
111
				}
115
				setLayoutData(cellEditor.getLayoutData());
112
				setLayoutData(cellEditor.getLayoutData());
116
				setEditor(control, item, columnNumber);
113
				setEditor(control, cell);
117
				cellEditor.setFocus();
114
				cellEditor.setFocus();
118
				if (focusListener == null) {
115
				if (focusListener == null) {
119
					focusListener = new FocusAdapter() {
116
					focusListener = new FocusAdapter() {
Lines 128-134 Link Here
128
					public void mouseDown(MouseEvent e) {
125
					public void mouseDown(MouseEvent e) {
129
						// time wrap?
126
						// time wrap?
130
						// check for expiration of doubleClickTime
127
						// check for expiration of doubleClickTime
131
						if (e.time <= doubleClickExpirationTime) {
128
						if (e.time <= activationTime) {
132
							control.removeMouseListener(mouseListener);
129
							control.removeMouseListener(mouseListener);
133
							cancelEditing();
130
							cancelEditing();
134
							handleDoubleClickEvent();
131
							handleDoubleClickEvent();
Lines 143-153 Link Here
143
					tabeditingListener = new TraverseListener() {
140
					tabeditingListener = new TraverseListener() {
144
141
145
						public void keyTraversed(TraverseEvent e) {
142
						public void keyTraversed(TraverseEvent e) {
146
							ViewerColumn col = viewer.getViewerColumn(columnNumber);
143
							ViewerColumn col = viewer.getViewerColumn(cell.getColumnIndex());
147
							if ( col != null && col.getEditingSupport().isTabingSupported() ) {
144
							if ( col != null && col.getEditingSupport().isTabingSupported() ) {
148
								col.getEditingSupport().processTraversEvent(
145
								col.getEditingSupport().processTraversEvent(
149
										columnNumber,
146
										cell.getColumnIndex(),
150
										viewer.getViewerRowFromItem(item), e);
147
										cell.getViewerRow(), e);
151
							}
148
							}
152
						}
149
						}
153
					};
150
					};
Lines 159-196 Link Here
159
		}
156
		}
160
	}
157
	}
161
158
162
	/**
163
	 * Activate a cell editor for the given mouse position.
164
	 */
165
	private void activateCellEditor(MouseEvent event) {
166
		if (item == null || item.isDisposed()) {
167
			// item no longer exists
168
			return;
169
		}
170
		int columnToEdit;
171
		ViewerRow row = viewer.getViewerRowFromItem(item);
172
		int columns = row.getColumnCount();
173
		if (columns == 0) {
174
			// If no TableColumn, Table acts as if it has a single column
175
			// which takes the whole width.
176
			columnToEdit = 0;
177
		} else {
178
			columnToEdit = -1;
179
			for (int i = 0; i < columns; i++) {
180
				Rectangle bounds = row.getBounds(i);
181
				if (bounds.contains(event.x, event.y)) {
182
					columnToEdit = i;
183
					break;
184
				}
185
			}
186
			if (columnToEdit == -1) {
187
				return;
188
			}
189
		}
190
191
		columnNumber = columnToEdit;
192
		activateCellEditor();
193
	}
194
159
195
	/**
160
	/**
196
	 * Applies the current value and deactivates the currently active cell
161
	 * Applies the current value and deactivates the currently active cell
Lines 204-215 Link Here
204
			// see 1GAHI8Z: ITPUI:ALL - How to code event notification when
169
			// see 1GAHI8Z: ITPUI:ALL - How to code event notification when
205
			// using cell editor ?
170
			// using cell editor ?
206
			this.cellEditor = null;
171
			this.cellEditor = null;
207
			Item t = this.item;
172
			Item t = this.cell.getItem();
173
			
208
			// don't null out table item -- same item is still selected
174
			// don't null out table item -- same item is still selected
209
			if (t != null && !t.isDisposed()) {
175
			if (t != null && !t.isDisposed()) {
210
				saveEditorValue(c, t);
176
				saveEditorValue(c, t);
211
			}
177
			}
212
			setEditor(null, null, 0);
178
			setEditor(null, null);
213
			c.removeListener(cellEditorListener);
179
			c.removeListener(cellEditorListener);
214
			Control control = c.getControl();
180
			Control control = c.getControl();
215
			if (control != null) {
181
			if (control != null) {
Lines 235-241 Link Here
235
	 */
201
	 */
236
	void cancelEditing() {
202
	void cancelEditing() {
237
		if (cellEditor != null) {
203
		if (cellEditor != null) {
238
			setEditor(null, null, 0);
204
			setEditor(null, null);
239
			cellEditor.removeListener(cellEditorListener);
205
			cellEditor.removeListener(cellEditorListener);
240
206
241
			Control control = cellEditor.getControl();
207
			Control control = cellEditor.getControl();
Lines 265-275 Link Here
265
	 * 
231
	 * 
266
	 * @param event
232
	 * @param event
267
	 */
233
	 */
268
	void handleMouseDown(MouseEvent event) {
234
	void handleCellSelectionEvent(CellSelectionEvent event) {
269
		if (event.button != 1) {
235
		
270
			return;
271
		}
272
273
		if (cellEditor != null) {
236
		if (cellEditor != null) {
274
			applyEditorValue();
237
			applyEditorValue();
275
		}
238
		}
Lines 279-325 Link Here
279
		// the cell editor will be deactivated and a doubleClick event will
242
		// the cell editor will be deactivated and a doubleClick event will
280
		// be processed.
243
		// be processed.
281
		//
244
		//
282
		doubleClickExpirationTime = event.time
245
		if( event.swtEvent != null ) {
283
				+ Display.getCurrent().getDoubleClickTime();
246
			activationTime = event.swtEvent.time + 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
		}
247
		}
291
		item = items[0];
248
		
292
		activateCellEditor(event);
249
250
		this.cell = event.cell;
251
		activateCellEditor();
293
	}
252
	}
294
253
295
	/**
254
	/**
296
	 * Start editing the given element.
255
	 * Start editing the given element.
297
	 * 
256
	 * 
298
	 * @param element
257
	 * @param cell the cell to edit
299
	 * @param column
300
	 */
258
	 */
301
	void editElement(Object element, int column) {
259
	void editCell(ViewerCell cell) {
302
		if (cellEditor != null) {
260
		if (cellEditor != null) {
303
			applyEditorValue();
261
			applyEditorValue();
304
		}
262
		}
305
263
306
		setSelection(createSelection(element), true);
264
		setSelection(createSelection(cell.getElement()), true);
307
		Item[] selection = getSelection();
265
		Item[] selection = getSelection();
308
		if (selection.length != 1) {
266
		if (selection.length != 1) {
309
			return;
267
			return;
310
		}
268
		}
311
269
312
		item = selection[0];
313
314
		// Make sure selection is visible
270
		// Make sure selection is visible
315
		showSelection();
271
		showSelection();
316
		columnNumber = column;
272
		
317
		activateCellEditor();
273
		activateCellEditor();
318
274
319
	}
275
	}
320
276
321
	private void saveEditorValue(CellEditor cellEditor, Item tableItem) {
277
	private void saveEditorValue(CellEditor cellEditor, Item tableItem) {
322
		ViewerColumn part = viewer.getViewerColumn(columnNumber);
278
		ViewerColumn part = viewer.getViewerColumn(cell.getColumnIndex());
323
279
324
		if (part != null && part.getEditingSupport() != null) {
280
		if (part != null && part.getEditingSupport() != null) {
325
			part.getEditingSupport().setValue(tableItem.getData(),
281
			part.getEditingSupport().setValue(tableItem.getData(),
Lines 414-426 Link Here
414
	 * Position the editor inside the control
370
	 * Position the editor inside the control
415
	 * 
371
	 * 
416
	 * @param w
372
	 * @param w
417
	 *            the editor control
373
	 *            the editor control or <code>null</code> if the editor has to be reset
418
	 * @param item
374
	 * @param cell the cell in which the editor should be displayed or <code>null</code> if the editor has to be reset
419
	 *            the item (row) in which the editor is drawn in
375
	 *            
420
	 * @param fColumnNumber
421
	 *            the column number in which the editor is shown
422
	 */
376
	 */
423
	protected abstract void setEditor(Control w, Item item, int fColumnNumber);
377
	protected abstract void setEditor(Control w, ViewerCell cell);
424
378
425
	/**
379
	/**
426
	 * set the layout data for the editor
380
	 * set the layout data for the editor
(-)src/org/eclipse/jface/viewers/CellSelectionEvent.java (+57 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 Shindl <tom.schindl@bestsolution.at> - initial API and implementation (bug 151377)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.viewers;
13
14
import java.util.EventObject;
15
16
import org.eclipse.jface.viewers.ColumnViewer;
17
import org.eclipse.jface.viewers.ViewerCell;
18
import org.eclipse.swt.widgets.Event;
19
20
/**
21
 * This event is trigger when a cell is selected through key board or any other
22
 * action
23
 * 
24
 * @since 3.3
25
 * 
26
 */
27
public class CellSelectionEvent extends EventObject {
28
	/**
29
	 * The real SWT-Event which triggered the action
30
	 */
31
	public Event swtEvent;
32
33
	/**
34
	 * The cell selected
35
	 */
36
	public ViewerCell cell;
37
38
	private static final long serialVersionUID = 1L;
39
40
	/**
41
	 * A new event object holding all data to decide what was the event that has
42
	 * been triggered
43
	 * 
44
	 * @param viewer
45
	 *            the viewer which is the source of the event
46
	 * @param cell
47
	 *            the cell selected
48
	 * @param swtEvent
49
	 *            the real swt event
50
	 */
51
	CellSelectionEvent(ColumnViewer viewer, ViewerCell cell, Event swtEvent) {
52
		super(viewer);
53
		this.cell = cell;
54
		this.swtEvent = swtEvent;
55
	}
56
57
}
(-)src/org/eclipse/jface/viewers/ICellSelectionListener.java (+18 lines)
Added Link Here
1
package org.eclipse.jface.viewers;
2
3
/**
4
 * Classes interested when cell-selections occur in a table/tree control have to
5
 * implement this interface
6
 * 
7
 * @since 3.3
8
 * 
9
 */
10
public interface ICellSelectionListener {
11
	/**
12
	 * This method is called when a cell is selected
13
	 * 
14
	 * @param event
15
	 *            the selection event
16
	 */
17
	public void cellSelected(CellSelectionEvent event);
18
}

Return to bug 151377