Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f906a12d91d213806b99a86e9d330a1b82c2ebd9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*******************************************************************************
 * Copyright (c) 2000, 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Tom Schindl <tom.schindl@bestsolution.at> - concept of ViewerRow,
 *                                                 fix for 159597, refactoring (bug 153993),
 *                                                 widget-independency (bug 154329), fix for 187826, 191468
 *     Peter Centgraf - bug 251575
 *******************************************************************************/

package org.eclipse.jface.viewers;

import org.eclipse.core.runtime.Assert;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Widget;

/**
 * A concrete viewer based on a SWT <code>Table</code> control.
 * <p>
 * This class is not intended to be subclassed outside the viewer framework. It
 * is designed to be instantiated with a pre-existing SWT table control and
 * configured with a domain-specific content provider, table label provider,
 * element filter (optional), and element sorter (optional).
 * </p>
 * <p>
 * Label providers for table viewers must implement either the
 * <code>ITableLabelProvider</code> or the <code>ILabelProvider</code> interface
 * (see <code>TableViewer.setLabelProvider</code> for more details).
 * </p>
 * <p>
 * As of 3.1 the TableViewer now supports the SWT.VIRTUAL flag. If the
 * underlying table is SWT.VIRTUAL, the content provider may implement {@link
 * ILazyContentProvider} instead of {@link IStructuredContentProvider} . Note
 * that in this case, the viewer does not support sorting or filtering. Also
 * note that in this case, the Widget based APIs may return null if the element
 * is not specified or not created yet.
 * </p>
 * <p>
 * Users of SWT.VIRTUAL should also avoid using getItems() from the Table within
 * the TreeViewer as this does not necessarily generate a callback for the
 * TreeViewer to populate the items. It also has the side effect of creating all
 * of the items thereby eliminating the performance improvements of SWT.VIRTUAL.
 * </p>
 * <p>
 * Users setting up an editable table with more than 1 column <b>have</b> to pass the
 * SWT.FULL_SELECTION style bit
 * </p>
 * 
 * @see SWT#VIRTUAL
 * @see #doFindItem(Object)
 * @see #internalRefresh(Object, boolean)
 * @noextend This class is not intended to be subclassed by clients.
 */
public class TableViewer extends AbstractTableViewer {
	/**
	 * This viewer's table control.
	 */
	private Table table;

	/**
	 * The cached row which is reused all over
	 */
	private TableViewerRow cachedRow;

	/**
	 * Creates a table viewer on a newly-created table control under the given
	 * parent. The table control is created using the SWT style bits
	 * <code>MULTI, H_SCROLL, V_SCROLL,</code> and <code>BORDER</code>. The
	 * viewer has no input, no content provider, a default label provider, no
	 * sorter, and no filters. The table has no columns.
	 * 
	 * @param parent
	 * 		the parent control
	 */
	public TableViewer(Composite parent) {
		this(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
	}

	/**
	 * Creates a table viewer on a newly-created table control under the given
	 * parent. The table control is created using the given style bits. The
	 * viewer has no input, no content provider, a default label provider, no
	 * sorter, and no filters. The table has no columns.
	 * 
	 * @param parent
	 * 		the parent control
	 * @param style
	 * 		SWT style bits
	 */
	public TableViewer(Composite parent, int style) {
		this(new Table(parent, style));
	}

	/**
	 * Creates a table viewer on the given table control. The viewer has no
	 * input, no content provider, a default label provider, no sorter, and no
	 * filters.
	 * 
	 * @param table
	 * 		the table control
	 */
	public TableViewer(Table table) {
		this.table = table;
		hookControl(table);
	}

	@Override
	public Control getControl() {
		return table;
	}

	/**
	 * Returns this table viewer's table control.
	 * 
	 * @return the table control
	 */
	public Table getTable() {
		return table;
	}

	@Override
	protected ColumnViewerEditor createViewerEditor() {
		return new TableViewerEditor(this, null,
				new ColumnViewerEditorActivationStrategy(this),
				ColumnViewerEditor.DEFAULT);
	}

	/**
	 * <p>
	 * Sets a new selection for this viewer and optionally makes it visible. The
	 * TableViewer implementation of this method is inefficient for the
	 * ILazyContentProvider as lookup is done by indices rather than elements
	 * and may require population of the entire table in worse case.
	 * </p>
	 * <p>
	 * Use Table#setSelection(int[] indices) and Table#showSelection() if you
	 * wish to set selection more efficiently when using a ILazyContentProvider.
	 * </p>
	 * 
	 * @param selection
	 * 		the new selection
	 * @param reveal
	 * 		<code>true</code> if the selection is to be made visible, and
	 * 		<code>false</code> otherwise
	 * @see Table#setSelection(int[])
	 * @see Table#showSelection()
	 */
	@Override
	public void setSelection(ISelection selection, boolean reveal) {
		super.setSelection(selection, reveal);
	}

	@Override
	protected ViewerRow getViewerRowFromItem(Widget item) {
		if (cachedRow == null) {
			cachedRow = new TableViewerRow((TableItem) item);
		} else {
			cachedRow.setItem((TableItem) item);
		}

		return cachedRow;
	}

	/**
	 * Create a new row with style at index
	 * 
	 * @param style
	 * @param rowIndex
	 * @return ViewerRow
	 * @since 3.3
	 */
	@Override
	protected ViewerRow internalCreateNewRowPart(int style, int rowIndex) {
		TableItem item;

		if (rowIndex >= 0) {
			item = new TableItem(table, style, rowIndex);
		} else {
			item = new TableItem(table, style);
		}

		return getViewerRowFromItem(item);
	}

	@Override
	protected Item getItemAt(Point p) {
		TableItem[] selection = table.getSelection();

		if (selection.length == 1) {
			int columnCount = table.getColumnCount();

			for (int i = 0; i < columnCount; i++) {
				if (selection[0].getBounds(i).contains(p)) {
					return selection[0];
				}
			}
		}

		return table.getItem(p);
	}

	// Methods to provide widget independency

	@Override
	protected int doGetItemCount() {
		return table.getItemCount();
	}

	@Override
	protected int doIndexOf(Item item) {
		return table.indexOf((TableItem) item);
	}

	@Override
	protected void doSetItemCount(int count) {
		table.setItemCount(count);
	}

	@Override
	protected Item[] doGetItems() {
		return table.getItems();
	}

	@Override
	protected int doGetColumnCount() {
		return table.getColumnCount();
	}

	@Override
	protected Widget doGetColumn(int index) {
		return table.getColumn(index);
	}

	@Override
	protected Item doGetItem(int index) {
		return table.getItem(index);
	}

	@Override
	protected Item[] doGetSelection() {
		return table.getSelection();
	}

	@Override
	protected int[] doGetSelectionIndices() {
		return table.getSelectionIndices();
	}

	@Override
	protected void doClearAll() {
		table.clearAll();
	}

	@Override
	protected void doResetItem(Item item) {
		TableItem tableItem = (TableItem) item;
		int columnCount = Math.max(1, table.getColumnCount());
		for (int i = 0; i < columnCount; i++) {
			tableItem.setText(i, ""); //$NON-NLS-1$
			if (tableItem.getImage(i) != null) {
				tableItem.setImage(i, null);
			}
		}
	}

	@Override
	protected void doRemove(int start, int end) {
		table.remove(start, end);
	}

	@Override
	protected void doRemoveAll() {
		table.removeAll();
	}

	@Override
	protected void doRemove(int[] indices) {
		table.remove(indices);
	}

	@Override
	protected void doShowItem(Item item) {
		table.showItem((TableItem) item);
	}

	@Override
	protected void doDeselectAll() {
		table.deselectAll();
	}

	@Override
	protected void doSetSelection(Item[] items) {
		Assert.isNotNull(items, "Items-Array can not be null"); //$NON-NLS-1$

		TableItem[] t = new TableItem[items.length];
		System.arraycopy(items, 0, t, 0, t.length);

		table.setSelection(t);
	}

	@Override
	protected void doShowSelection() {
		table.showSelection();
	}

	@Override
	protected void doSetSelection(int[] indices) {
		table.setSelection(indices);
	}

	@Override
	protected void doClear(int index) {
		table.clear(index);
	}

	@Override
	protected void doSelect(int[] indices) {
		table.select(indices);
	}

	/**
	 * Refreshes this viewer starting with the given element. Labels are updated
	 * as described in <code>refresh(boolean updateLabels)</code>. The methods
	 * attempts to preserve the selection.
	 * <p>
	 * Unlike the <code>update</code> methods, this handles structural changes
	 * to the given element (e.g. addition or removal of children). If only the
	 * given element needs updating, it is more efficient to use the
	 * <code>update</code> methods.
	 * </p>
	 * 
	 * <p>
	 * Subclasses who can provide this feature can open this method for the
	 * public
	 * </p>
	 * 
	 * @param element
	 * 		the element
	 * @param updateLabels
	 * 		<code>true</code> to update labels for existing elements,
	 * 		<code>false</code> to only update labels as needed, assuming that labels
	 * 		for existing elements are unchanged.
	 * @param reveal
	 * 		<code>true</code> to make the preserved selection visible afterwards
	 * 
	 * @since 3.3
	 */
	public void refresh(final Object element, final boolean updateLabels,
			boolean reveal) {
		if (checkBusy())
			return;

		if (isCellEditorActive()) {
			cancelEditing();
		}

		preservingSelection(new Runnable() {
			public void run() {
				internalRefresh(element, updateLabels);
			}
		}, reveal);
	}

	/**
	 * Refreshes this viewer with information freshly obtained from this
	 * viewer's model. If <code>updateLabels</code> is <code>true</code> then
	 * labels for otherwise unaffected elements are updated as well. Otherwise,
	 * it assumes labels for existing elements are unchanged, and labels are
	 * only obtained as needed (for example, for new elements).
	 * <p>
	 * Calling <code>refresh(true)</code> has the same effect as
	 * <code>refresh()</code>.
	 * <p>
	 * Note that the implementation may still obtain labels for existing
	 * elements even if <code>updateLabels</code> is false. The intent is simply
	 * to allow optimization where possible.
	 * 
	 * @param updateLabels
	 * 		<code>true</code> to update labels for existing elements,
	 * 		<code>false</code> to only update labels as needed, assuming that labels
	 * 		for existing elements are unchanged.
	 * @param reveal
	 * 		<code>true</code> to make the preserved selection visible afterwards
	 * 
	 * @since 3.3
	 */
	public void refresh(boolean updateLabels, boolean reveal) {
		refresh(getRoot(), updateLabels, reveal);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.AbstractTableViewer#remove(java.lang.Object[])
	 */
	@Override
	public void remove(Object[] elements) {
		assertElementsNotNull(elements);
		if (checkBusy())
			return;
		if (elements.length == 0) {
			return;
		}

		// deselect any items that are being removed, see bug 97786
		boolean deselectedItems = false;
		Object elementToBeRemoved = null;
		CustomHashtable elementsToBeRemoved = null;
		if (elements.length == 1) {
			elementToBeRemoved = elements[0];
		} else {
			elementsToBeRemoved = new CustomHashtable(getComparer());
			for (int i = 0; i < elements.length; i++) {
				Object element = elements[i];
				elementsToBeRemoved.put(element, element);
			}
		}
		int[] selectionIndices = doGetSelectionIndices();
		for (int i = 0; i < selectionIndices.length; i++) {
			int index = selectionIndices[i];
			Item item = doGetItem(index);
			Object data = item.getData();
			if (data != null) {
				if ((elementsToBeRemoved != null && elementsToBeRemoved
						.containsKey(data))
						|| equals(elementToBeRemoved, data)) {
					table.deselect(index);
					deselectedItems = true;
				}
			}
		}
		super.remove(elements);

		if (deselectedItems) {
			ISelection sel = getSelection();
			updateSelection(sel);
			firePostSelectionChanged(new SelectionChangedEvent(this, sel));
		}
	}
	
	@Override
	protected Widget doFindItem(Object element) {
		IContentProvider contentProvider = getContentProvider();
		if (contentProvider instanceof IIndexableLazyContentProvider) {
			IIndexableLazyContentProvider indexable = (IIndexableLazyContentProvider) contentProvider;
			int idx = indexable.findElement(element);
			if (idx != -1) {
				return doGetItem(idx);
			}
			return null;
		}
		return super.doFindItem(element);
	}

}

Back to the top