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 (-7 / +94 lines)
Lines 14-26 Link Here
14
package org.eclipse.jface.viewers;
14
package org.eclipse.jface.viewers;
15
15
16
import org.eclipse.core.runtime.Assert;
16
import org.eclipse.core.runtime.Assert;
17
import org.eclipse.swt.events.MouseAdapter;
17
import org.eclipse.core.runtime.ListenerList;
18
import org.eclipse.swt.events.MouseEvent;
18
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.graphics.Point;
19
import org.eclipse.swt.graphics.Point;
20
import org.eclipse.swt.widgets.Control;
20
import org.eclipse.swt.widgets.Control;
21
import org.eclipse.swt.widgets.Event;
21
import org.eclipse.swt.widgets.Item;
22
import org.eclipse.swt.widgets.Item;
23
import org.eclipse.swt.widgets.Listener;
22
import org.eclipse.swt.widgets.Widget;
24
import org.eclipse.swt.widgets.Widget;
23
25
26
27
28
29
24
/**
30
/**
25
 * The ColumnViewer is the abstract superclass of viewers that have columns
31
 * The ColumnViewer is the abstract superclass of viewers that have columns
26
 * (e.g., AbstractTreeViewer and AbstractTableViewer). Concrete subclasses of
32
 * (e.g., AbstractTreeViewer and AbstractTableViewer). Concrete subclasses of
Lines 46-51 Link Here
46
	private ViewerCell cell = new ViewerCell(null, 0);
52
	private ViewerCell cell = new ViewerCell(null, 0);
47
53
48
	private AbstractViewerEditor viewerEditor;
54
	private AbstractViewerEditor viewerEditor;
55
	
56
	private ListenerList cellSelectionListeners;
49
57
50
	/**
58
	/**
51
	 * Create a new instance of the receiver.
59
	 * Create a new instance of the receiver.
Lines 71-81 Link Here
71
		// their
79
		// their
72
		// own impl
80
		// own impl
73
		if (viewerEditor != null) {
81
		if (viewerEditor != null) {
74
			control.addMouseListener(new MouseAdapter() {
82
			Listener l = new Listener() {
75
				public void mouseDown(MouseEvent e) {
83
76
					viewerEditor.handleMouseDown(e);
84
				public void handleEvent(Event event) {
85
					handleMouseDown(event);
77
				}
86
				}
78
			});
87
				
88
			};
89
			
90
			control.addListener(SWT.MouseDown, l);
91
			control.addListener(SWT.MouseDoubleClick, l);
92
			
93
			addCellSelectionListener(new EditingListener());
79
		}
94
		}
80
	}
95
	}
81
96
Lines 365-374 Link Here
365
	 * @param column
380
	 * @param column
366
	 *            the column index
381
	 *            the column index
367
	 * @since 3.1 (in subclasses, added in 3.3 to abstract class)
382
	 * @since 3.1 (in subclasses, added in 3.3 to abstract class)
383
	 * @see #editCell(ViewerCell)
368
	 */
384
	 */
369
	public void editElement(Object element, int column) {
385
	public void editElement(Object element, int column) {
370
		if (viewerEditor != null) {
386
		if (viewerEditor != null) {
371
			viewerEditor.editElement(element, column);
387
			Widget item = findItem(element);
388
			if( item != null ) {
389
				ViewerRow row = (ViewerRow)item.getData(ViewerRow.ROWPART_KEY);
390
				ViewerCell cell = row.getCell(column);
391
				if( cell != null ) {
392
					viewerEditor.editElement(cell);
393
				}
394
			}
395
			//viewerEditor.editElement(element, column);
372
		}
396
		}
373
	}
397
	}
374
398
Lines 527-530 Link Here
527
			viewerEditor.setColumnProperties(columnProperties);
551
			viewerEditor.setColumnProperties(columnProperties);
528
		}
552
		}
529
	}
553
	}
554
	
555
	/**
556
	 * Adding a cell selection listener
557
	 * 
558
	 * @param listener
559
	 *            listener for cell selections
560
	 */
561
	public void addCellSelectionListener(ICellSelectionListener listener) {
562
		if( cellSelectionListeners == null ) {
563
			cellSelectionListeners = new ListenerList();
564
		}
565
		this.cellSelectionListeners.add(listener);
566
	}
567
568
	/**
569
	 * Remove the cell selection listener
570
	 * 
571
	 * @param listener
572
	 *            listener for cell selections
573
	 */
574
	public void removeCellSelectionListener(ICellSelectionListener listener) {
575
		this.cellSelectionListeners.remove(listener);
576
	}
577
	
578
	/**
579
	 * Start editing the given cell if possible
580
	 * @param cell the cell to edit
581
	 */
582
	public void editCell(ViewerCell cell) {
583
		viewerEditor.editElement(cell);
584
	}
585
	
586
	private void handleMouseDown(Event event) {
587
		Point p = new Point(event.x,event.y);
588
		
589
		ViewerRow row = getViewerRow(p);
590
		
591
		if( row != null ) {
592
			ViewerCell cell = row.getCell(p);
593
			
594
			if( cell != null ) {
595
				fireCellSelectionEvent(new CellSelectionEvent(this,cell,event));
596
			}
597
		}
598
	}
599
	
600
	private void fireCellSelectionEvent(CellSelectionEvent event) {
601
		if( ! cellSelectionListeners.isEmpty() ) {
602
			Object[] listeners = cellSelectionListeners.getListeners();
603
			for( int i = 0; i < listeners.length; i++ ) {
604
				((ICellSelectionListener)listeners[i]).cellSelected(event);
605
			}
606
		}
607
	}
608
	
609
	private class EditingListener implements ICellSelectionListener {
610
611
		public void cellSelected(CellSelectionEvent event) {
612
			if( event.swtEvent.type == SWT.MouseDown || event.swtEvent.type == SWT.MouseDoubleClick ) {
613
				editCell(event.cell);
614
			}
615
		}
616
	}
530
}
617
}
(-)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 (-101 / +24 lines)
Lines 18-26 Link Here
18
import org.eclipse.swt.events.MouseAdapter;
18
import org.eclipse.swt.events.MouseAdapter;
19
import org.eclipse.swt.events.MouseEvent;
19
import org.eclipse.swt.events.MouseEvent;
20
import org.eclipse.swt.events.MouseListener;
20
import org.eclipse.swt.events.MouseListener;
21
import org.eclipse.swt.graphics.Rectangle;
22
import org.eclipse.swt.widgets.Control;
21
import org.eclipse.swt.widgets.Control;
23
import org.eclipse.swt.widgets.Display;
24
import org.eclipse.swt.widgets.Item;
22
import org.eclipse.swt.widgets.Item;
25
23
26
/**
24
/**
Lines 42-49 Link Here
42
40
43
	private String[] columnProperties;
41
	private String[] columnProperties;
44
42
45
	private int columnNumber;
46
47
	private ICellEditorListener cellEditorListener;
43
	private ICellEditorListener cellEditorListener;
48
44
49
	private FocusListener focusListener;
45
	private FocusListener focusListener;
Lines 54-60 Link Here
54
50
55
	private ColumnViewer viewer;
51
	private ColumnViewer viewer;
56
52
57
	private Item item;
53
	private ViewerCell cell;
58
54
59
	/**
55
	/**
60
	 * Create a new editor implementation for the viewer
56
	 * Create a new editor implementation for the viewer
Lines 86-93 Link Here
86
82
87
	void activateCellEditor() {
83
	void activateCellEditor() {
88
84
89
		ViewerColumn part = viewer.getViewerColumn(columnNumber);
85
		ViewerColumn part = viewer.getViewerColumn(cell.getColumnIndex());
90
		Object element = item.getData();
86
		Object element = cell.getElement();
91
87
92
		if (part != null && part.getEditingSupport() != null
88
		if (part != null && part.getEditingSupport() != null
93
				&& part.getEditingSupport().canEdit(element)) {
89
				&& part.getEditingSupport().canEdit(element)) {
Lines 107-113 Link Here
107
					return;
103
					return;
108
				}
104
				}
109
				setLayoutData(cellEditor.getLayoutData());
105
				setLayoutData(cellEditor.getLayoutData());
110
				setEditor(control, item, columnNumber);
106
				setEditor(control, cell);
111
				cellEditor.setFocus();
107
				cellEditor.setFocus();
112
				if (focusListener == null) {
108
				if (focusListener == null) {
113
					focusListener = new FocusAdapter() {
109
					focusListener = new FocusAdapter() {
Lines 136-174 Link Here
136
	}
132
	}
137
133
138
	/**
134
	/**
139
	 * Activate a cell editor for the given mouse position.
140
	 */
141
	private void activateCellEditor(MouseEvent event) {
142
		if (item == null || item.isDisposed()) {
143
			// item no longer exists
144
			return;
145
		}
146
		int columnToEdit;
147
		ViewerRow row = viewer.getViewerRowFromItem(item);
148
		int columns = row.getColumnCount();
149
		if (columns == 0) {
150
			// If no TableColumn, Table acts as if it has a single column
151
			// which takes the whole width.
152
			columnToEdit = 0;
153
		} else {
154
			columnToEdit = -1;
155
			for (int i = 0; i < columns; i++) {
156
				Rectangle bounds = row.getBounds(i);
157
				if (bounds.contains(event.x, event.y)) {
158
					columnToEdit = i;
159
					break;
160
				}
161
			}
162
			if (columnToEdit == -1) {
163
				return;
164
			}
165
		}
166
167
		columnNumber = columnToEdit;
168
		activateCellEditor();
169
	}
170
171
	/**
172
	 * Applies the current value and deactivates the currently active cell
135
	 * Applies the current value and deactivates the currently active cell
173
	 * editor.
136
	 * editor.
174
	 */
137
	 */
Lines 180-191 Link Here
180
			// see 1GAHI8Z: ITPUI:ALL - How to code event notification when
143
			// see 1GAHI8Z: ITPUI:ALL - How to code event notification when
181
			// using cell editor ?
144
			// using cell editor ?
182
			this.cellEditor = null;
145
			this.cellEditor = null;
183
			Item t = this.item;
146
			Item t = cell.getItem();
147
184
			// don't null out table item -- same item is still selected
148
			// don't null out table item -- same item is still selected
185
			if (t != null && !t.isDisposed()) {
149
			if (t != null && !t.isDisposed()) {
186
				saveEditorValue(c, t);
150
				saveEditorValue(c, cell);
187
			}
151
			}
188
			setEditor(null, null, 0);
152
153
			setEditor(null, null);
189
			c.removeListener(cellEditorListener);
154
			c.removeListener(cellEditorListener);
190
			Control control = c.getControl();
155
			Control control = c.getControl();
191
			if (control != null) {
156
			if (control != null) {
Lines 205-211 Link Here
205
	 */
170
	 */
206
	void cancelEditing() {
171
	void cancelEditing() {
207
		if (cellEditor != null) {
172
		if (cellEditor != null) {
208
			setEditor(null, null, 0);
173
			setEditor(null, null);
209
			cellEditor.removeListener(cellEditorListener);
174
			cellEditor.removeListener(cellEditorListener);
210
			CellEditor oldEditor = cellEditor;
175
			CellEditor oldEditor = cellEditor;
211
			cellEditor = null;
176
			cellEditor = null;
Lines 214-281 Link Here
214
	}
179
	}
215
180
216
	/**
181
	/**
217
	 * Enable the editor by mouse down
182
	 * Start editing the given cell.
218
	 * 
219
	 * @param event
220
	 */
221
	void handleMouseDown(MouseEvent event) {
222
		if (event.button != 1) {
223
			return;
224
		}
225
226
		if (cellEditor != null) {
227
			applyEditorValue();
228
		}
229
230
		// activate the cell editor immediately. If a second mouseDown
231
		// is received prior to the expiration of the doubleClick time then
232
		// the cell editor will be deactivated and a doubleClick event will
233
		// be processed.
234
		//
235
		doubleClickExpirationTime = event.time
236
				+ Display.getCurrent().getDoubleClickTime();
237
238
		Item[] items = getSelection();
239
		// Do not edit if more than one row is selected.
240
		if (items.length != 1) {
241
			item = null;
242
			return;
243
		}
244
		item = items[0];
245
		activateCellEditor(event);
246
	}
247
248
	/**
249
	 * Start editing the given element.
250
	 * 
183
	 * 
251
	 * @param element
184
	 * @param cell
252
	 * @param column
253
	 */
185
	 */
254
	void editElement(Object element, int column) {
186
	void editElement(ViewerCell cell) {
187
		this.cell = cell;
255
		if (cellEditor != null) {
188
		if (cellEditor != null) {
256
			applyEditorValue();
189
			applyEditorValue();
257
		}
190
		}
258
191
259
		setSelection(createSelection(element), true);
192
		setSelection(createSelection(cell.getElement()), true);
260
		Item[] selection = getSelection();
261
		if (selection.length != 1) {
262
			return;
263
		}
264
265
		item = selection[0];
266
193
267
		// Make sure selection is visible
268
		showSelection();
269
		columnNumber = column;
270
		activateCellEditor();
194
		activateCellEditor();
271
272
	}
195
	}
273
196
274
	private void saveEditorValue(CellEditor cellEditor, Item tableItem) {
197
	private void saveEditorValue(CellEditor cellEditor, ViewerCell cell) {
275
		ViewerColumn part = viewer.getViewerColumn(columnNumber);
198
		ViewerColumn part = viewer.getViewerColumn(cell.getColumnIndex());
276
199
277
		if (part != null && part.getEditingSupport() != null) {
200
		if (part != null && part.getEditingSupport() != null) {
278
			part.getEditingSupport().setValue(tableItem.getData(),
201
			part.getEditingSupport().setValue(cell.getElement(),
279
					cellEditor.getValue());
202
					cellEditor.getValue());
280
		}
203
		}
281
	}
204
	}
Lines 367-379 Link Here
367
	 * Position the editor inside the control
290
	 * Position the editor inside the control
368
	 * 
291
	 * 
369
	 * @param w
292
	 * @param w
370
	 *            the editor control
293
	 *            the editor control or <code>null</code> if the editor has to
371
	 * @param item
294
	 *            be reset
372
	 *            the item (row) in which the editor is drawn in
295
	 * @param cell
373
	 * @param fColumnNumber
296
	 *            the cell in which the editor should be displayed or
374
	 *            the column number in which the editor is shown
297
	 *            <code>null</code> if the editor has to be reset
375
	 */
298
	 */
376
	protected abstract void setEditor(Control w, Item item, int fColumnNumber);
299
	protected abstract void setEditor(Control w, ViewerCell cell);
377
300
378
	/**
301
	/**
379
	 * set the layout data for the editor
302
	 * set the layout data for the editor
(-)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