Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 581b8bc765b128c4c1eaf976148c74577fa1f1f2 (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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*****************************************************************************
 * Copyright (c) 2012, 2017 CEA LIST.
 *
 *
 * 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:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *  Nicolas FAUVERGUE (ALL4TEC) nicolas.fauvergue@all4tec.net - Bug 476618
 *  Vincent Lorenzo (CEA LIST) - bug 525221
 *****************************************************************************/

package org.eclipse.papyrus.infra.nattable.provider;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate;
import org.eclipse.nebula.widgets.nattable.coordinate.Range;
import org.eclipse.nebula.widgets.nattable.layer.ILayerListener;
import org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell;
import org.eclipse.nebula.widgets.nattable.layer.event.ILayerEvent;
import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer;
import org.eclipse.nebula.widgets.nattable.selection.event.CellSelectionEvent;
import org.eclipse.nebula.widgets.nattable.selection.event.ColumnSelectionEvent;
import org.eclipse.nebula.widgets.nattable.selection.event.ISelectionEvent;
import org.eclipse.nebula.widgets.nattable.selection.event.RowSelectionEvent;
import org.eclipse.papyrus.infra.nattable.manager.table.INattableModelManager;
import org.eclipse.papyrus.infra.nattable.utils.AxisUtils;
import org.eclipse.papyrus.infra.nattable.utils.TableSelectionWrapper;
import org.eclipse.papyrus.infra.nattable.utils.TypeSelectionEnum;
import org.eclipse.papyrus.infra.tools.util.ListHelper;
import org.eclipse.ui.services.IDisposable;

/**
 * The selection provider used by the table. The returned selection is used to display property views or to do the synchronization in the
 * ModelExplorer
 */
public class TableSelectionProvider implements ISelectionProvider, IDisposable, ILayerListener {

	/**
	 * the selection layer used in the table
	 */
	private SelectionLayer selectionLayer;

	/**
	 * the selection listener
	 */
	private final ILayerListener selectionListener;

	/**
	 * the current selection in the table
	 */
	private ISelection currentSelection;

	/**
	 * the list of listener on the selections
	 */
	private final List<ISelectionChangedListener> listeners;

	/**
	 * the table model manager to use to find selected elements
	 */
	private INattableModelManager manager;

	/**
	 * boolean indicating than the class is disposed
	 */
	private boolean isDisposed = false;

	/**
	 * Constructor.
	 *
	 * @param manager
	 *            the table model manager to use to find selected elements
	 * @param selectionLayer
	 *            the selection layer
	 */
	public TableSelectionProvider(final INattableModelManager manager, final SelectionLayer selectionLayer) {
		this.selectionLayer = selectionLayer;
		this.selectionListener = this;
		this.selectionLayer.addLayerListener(this.selectionListener);
		this.currentSelection = new StructuredSelection();
		this.listeners = new ArrayList<ISelectionChangedListener>();
		this.manager = manager;
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.jface.viewers.ISelectionProvider#addSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
	 */
	@Override
	public void addSelectionChangedListener(final ISelectionChangedListener listener) {
		this.listeners.add(listener);
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.jface.viewers.ISelectionProvider#getSelection()
	 */
	@Override
	public synchronized ISelection getSelection() {
		return this.currentSelection;
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
	 */
	@Override
	public void removeSelectionChangedListener(final ISelectionChangedListener listener) {
		this.listeners.remove(listener);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection)
	 */
	@Override
	public synchronized void setSelection(final ISelection selection) {
		if (this.currentSelection.equals(selection)) {
			return;
		}
		this.currentSelection = selection;
		final SelectionChangedEvent event = new SelectionChangedEvent(this, this.currentSelection);
		for (final ISelectionChangedListener current : this.listeners) {
			current.selectionChanged(event);
		}
	}

	/**
	 * This allows to calculate the new selection from the event and the layer.
	 * 
	 * @param event
	 *            The event caught.
	 */
	protected void calculateAndStoreNewSelection(final ILayerEvent event) {
		// the list of the selected elements
		Collection<Object> selection = new HashSet<Object>();
		final ISelection newSelection;
		if (event instanceof ISelectionEvent) {
			TableSelectionWrapper wrapper = new TableSelectionWrapper(this.manager, ListHelper.asList(this.selectionLayer.getSelectedCellPositions()), new HashMap<Integer, Object>(0), new HashMap<Integer, Object>(0));

			// Get the type selection event if it is a specific selection event
			TypeSelectionEnum typeSelectionEvent = TypeSelectionEnum.NONE;
			boolean isWithShiftMask = false;
			boolean isWithControlMask = false;

			// Check if it is a simple cell selection event
			if (event instanceof CellSelectionEvent) {
				typeSelectionEvent = TypeSelectionEnum.CELL;
				isWithShiftMask = ((CellSelectionEvent) event).isWithShiftMask();
				isWithControlMask = ((CellSelectionEvent) event).isWithControlMask();
				// Check if it is a column selection event
			} else if (event instanceof ColumnSelectionEvent) {
				typeSelectionEvent = TypeSelectionEnum.COLUMN;
				isWithShiftMask = ((ColumnSelectionEvent) event).isWithShiftMask();
				isWithControlMask = ((ColumnSelectionEvent) event).isWithControlMask();
				// Check if it is a row selection event
			} else if (event instanceof RowSelectionEvent) {
				typeSelectionEvent = TypeSelectionEnum.ROW;
				isWithShiftMask = ((RowSelectionEvent) event).isWithShiftMask();
				isWithControlMask = ((RowSelectionEvent) event).isWithControlMask();
			}

			// If this is a specific selection event, manage the selection with event
			if (!TypeSelectionEnum.NONE.equals(typeSelectionEvent)) {
				// The shift mask will be managed by adding all the selection between the previous selection and the current
				if (isWithShiftMask) {
					// Calculate the selection with the shift mask
					selection = calculateSelectionWithShiftMask(wrapper, typeSelectionEvent);
					// The control mask will be managed by adding the current selection to the previous one
				} else if (isWithControlMask) {
					// Calculate the selection with the control mask
					selection = calculateSelectionWithControlMask(wrapper, typeSelectionEvent, event);
				} else {
					// Manage the rows and the columns selection event
					selection = calculateSelectionRowsAndColumnsWithTypeSelectionEvent(wrapper, typeSelectionEvent, event);
				}

			} else {
				selection = calculateSelectionRowsAndColumnsWithoutTypeSelectionEvent(wrapper, event);
			}
			// If no selection appended, the selection must be the context of the table 
			if(selection.isEmpty()){
				newSelection = new TableStructuredSelection(manager.getTable().getContext(), wrapper);
			}else{
				newSelection = new TableStructuredSelection(selection.toArray(), wrapper);
			}
		} else {
			newSelection = new StructuredSelection(manager.getTable().getContext());
		}
		setSelection(newSelection);
	}

	/**
	 * This allows to calculate the selection when the shift key is pressed with the selection.
	 * 
	 * @param wrapper
	 *            The table selection wrapper to fill.è
	 * @param typeSelectionEvent
	 *            The type of selection from the event (cell, row or column selection).
	 * @return The collection of object selected.
	 */
	protected Collection<Object> calculateSelectionWithShiftMask(final TableSelectionWrapper wrapper, final TypeSelectionEnum typeSelectionEvent) {
		// If a cell is selected with shift mask, only keep the line and column selected (the cells added with be added automatically
		if (TypeSelectionEnum.CELL.equals(typeSelectionEvent)) {
			if (this.currentSelection instanceof TableStructuredSelection) {
				// Add the previous selected rows and columns to the current wrapper
				final TableSelectionWrapper existingWrapper = (TableSelectionWrapper) ((TableStructuredSelection) currentSelection).getAdapter(TableSelectionWrapper.class);
				wrapper.getFullySelectedRows().putAll(existingWrapper.getFullySelectedRows());
				wrapper.getFullySelectedColumns().putAll(existingWrapper.getFullySelectedColumns());
			}
			// If a row is selected with shift mask, the selected rows by the layer must be the wrapper selected rows
		} else if (TypeSelectionEnum.ROW.equals(typeSelectionEvent)) {
			for (final int i : this.selectionLayer.getFullySelectedRowPositions()) {
				final int rowIndex = this.selectionLayer.getRowIndexByPosition(i);
				Object el = this.manager.getRowElement(rowIndex);
				if (el != null) {
					if (!wrapper.getFullySelectedRows().containsKey(rowIndex)) {
						// Check if the column to select is corresponding to at least one cell
						if (isSelectedCellsContainsRow(wrapper.getSelectedCells(), this.manager.getBodyLayerStack().getRowHideShowLayer().getRowPositionByIndex(rowIndex))) {
							wrapper.getFullySelectedRows().put(Integer.valueOf(rowIndex), el);
						}
					}
				}
			}
			// If a row is selected with shift mask, the selected rows by the layer must be the wrapper selected rows
		} else if (TypeSelectionEnum.COLUMN.equals(typeSelectionEvent)) {
			// TODO : Some bugs exists in nattable column selection
			// When the bugs are resolved, we needed to be like rows shift selection

			if (this.currentSelection instanceof TableStructuredSelection) {
				// Add the previous selected rows and columns to the current wrapper
				final TableSelectionWrapper existingWrapper = (TableSelectionWrapper) ((TableStructuredSelection) currentSelection).getAdapter(TableSelectionWrapper.class);
				wrapper.getFullySelectedRows().putAll(existingWrapper.getFullySelectedRows());
				wrapper.getFullySelectedColumns().putAll(existingWrapper.getFullySelectedColumns());
			}

			for (final int i : this.selectionLayer.getFullySelectedColumnPositions()) {
				final int columnIndex = this.selectionLayer.getColumnIndexByPosition(i);
				Object el = this.manager.getColumnElement(columnIndex);
				if (el != null) {
					if (!wrapper.getFullySelectedColumns().containsKey(columnIndex)) {
						// Check if the column to select is corresponding to at least one cell
						if (isSelectedCellsContainsColumn(wrapper.getSelectedCells(), this.manager.getBodyLayerStack().getColumnHideShowLayer().getColumnPositionByIndex(columnIndex))) {
							wrapper.getFullySelectedColumns().put(Integer.valueOf(columnIndex), el);
						}
					}
				}
			}
		}

		return calculateSelectionFromWrapper(wrapper);
	}

	/**
	 * This allows to calculate the selection when the control key is pressed with the selection.
	 * 
	 * @param wrapper
	 *            The table selection wrapper to fill.
	 * @param typeSelectionEvent
	 *            The type of selection from the event (cell, row or column selection).
	 * @param event
	 *            The event caught.
	 * @return The collection of object selected.
	 */
	protected Collection<Object> calculateSelectionWithControlMask(final TableSelectionWrapper wrapper, final TypeSelectionEnum typeSelectionEvent, final ILayerEvent event) {
		if (this.currentSelection instanceof TableStructuredSelection) {
			// Add the previous selected rows and columns to the current wrapper
			final TableSelectionWrapper existingWrapper = (TableSelectionWrapper) ((TableStructuredSelection) currentSelection).getAdapter(TableSelectionWrapper.class);
			wrapper.getFullySelectedRows().putAll(existingWrapper.getFullySelectedRows());
			wrapper.getFullySelectedColumns().putAll(existingWrapper.getFullySelectedColumns());
		}

		// If a cell is selected by the control and was already selected, remove it and remove the selected row and column
		if (TypeSelectionEnum.CELL.equals(typeSelectionEvent)) {
			final int columnPosition = ((CellSelectionEvent) event).getColumnPosition();
			final int rowPosition = ((CellSelectionEvent) event).getRowPosition();
			final PositionCoordinate tmpCoordinate = new PositionCoordinate(selectionLayer, columnPosition, rowPosition);

			// Check if the cell was unselected
			if (!wrapper.getSelectedCells().contains(tmpCoordinate)) {
				if (wrapper.getFullySelectedRows().containsKey(rowPosition)) {
					wrapper.getFullySelectedRows().remove(rowPosition);
				}
				if (wrapper.getFullySelectedColumns().containsKey(columnPosition)) {
					wrapper.getFullySelectedColumns().remove(columnPosition);
				}
			}
		} else {
			calculateSelectionRowsAndColumnsWithTypeSelectionEvent(wrapper, typeSelectionEvent, event);
		}

		return calculateSelectionFromWrapper(wrapper);
	}

	/**
	 * This allows to calculate the selection for rows and columns (cells already added to the wrapper).
	 * 
	 * @param wrapper
	 *            The table selection wrapper to fill.
	 * @param typeSelectionEvent
	 *            The type of selection from the event (cell, row or column selection).
	 * @param event
	 *            The event caught.
	 * @return The collection of object selected.
	 */
	protected Collection<Object> calculateSelectionRowsAndColumnsWithTypeSelectionEvent(final TableSelectionWrapper wrapper, final TypeSelectionEnum typeSelectionEvent, final ILayerEvent event) {
		// Manage the column selection event
		if (TypeSelectionEnum.COLUMN.equals(typeSelectionEvent)) {
			for (Range range : ((ColumnSelectionEvent) event).getColumnPositionRanges()) {
				for (int index = range.start; index < range.end; index++) {
					final int columnIndex = this.selectionLayer.getColumnIndexByPosition(index);
					Object el = this.manager.getColumnElement(columnIndex);
					if (wrapper.getFullySelectedColumns().containsKey(index)) {
						// The selected column was only selected, so it need to be removed
						wrapper.getFullySelectedColumns().remove(columnIndex);
					} else if (el != null) {
						// Check if the column to select is corresponding to at least one cell
						if (isSelectedCellsContainsColumn(wrapper.getSelectedCells(), this.manager.getBodyLayerStack().getColumnHideShowLayer().getColumnPositionByIndex(columnIndex))) {
							// The selected column was not already in selection, add it
							wrapper.getFullySelectedColumns().put(Integer.valueOf(columnIndex), el);
						}
					}
				}
			}
			// Manage the row selection event
		} else if (TypeSelectionEnum.ROW.equals(typeSelectionEvent)) {
			for (Range range : ((RowSelectionEvent) event).getRowPositionRanges()) {
				for (int index = range.start; index < range.end; index++) {
					final int rowIndex = this.selectionLayer.getRowIndexByPosition(index);
					Object el = this.manager.getRowElement(rowIndex);
					if (wrapper.getFullySelectedRows().containsKey(index)) {
						// The selected row was only selected, so it need to be removed
						wrapper.getFullySelectedRows().remove(rowIndex);
					} else if (el != null) {
						// Check if the row to select is corresponding to at least one cell
						if (isSelectedCellsContainsRow(wrapper.getSelectedCells(), this.manager.getBodyLayerStack().getRowHideShowLayer().getRowPositionByIndex(rowIndex))) {
							// The selected row was not already in selection, add it
							wrapper.getFullySelectedRows().put(Integer.valueOf(rowIndex), el);
						}
					}
				}
			}
		}

		return calculateSelectionFromWrapper(wrapper);
	}

	/**
	 * This allows to determinate if the row index to add to the rows selected have at least one of its cells in the selected cells.
	 * 
	 * @param selectedCells
	 *            The selected cells.
	 * @param rowIndex
	 *            The row index to search.
	 * @return <code>true</code> if the row corresponding to at least one cell, <code>false</code> otherwise.
	 */
	private boolean isSelectedCellsContainsRow(final Collection<PositionCoordinate> selectedCells, final int rowIndex) {
		boolean result = false;

		Iterator<PositionCoordinate> selectedCellsIterator = selectedCells.iterator();
		while (!result && selectedCellsIterator.hasNext()) {
			final PositionCoordinate selectedCell = selectedCellsIterator.next();
			if (selectedCell.getRowPosition() == rowIndex) {
				result = true;
			}
		}

		return result;
	}

	/**
	 * This allows to determinate if the column index to add to the columns selected have at least one of its cells in the selected cells.
	 * 
	 * @param selectedCells
	 *            The selected cells.
	 * @param columnIndex
	 *            The column index to search.
	 * @return <code>true</code> if the column corresponding to at least one cell, <code>false</code> otherwise.
	 */
	private boolean isSelectedCellsContainsColumn(final Collection<PositionCoordinate> selectedCells, final int columnIndex) {
		boolean result = false;

		Iterator<PositionCoordinate> selectedCellsIterator = selectedCells.iterator();
		while (!result && selectedCellsIterator.hasNext()) {
			final PositionCoordinate selectedCell = selectedCellsIterator.next();
			if (selectedCell.getColumnPosition() == columnIndex) {
				result = true;
			}
		}

		return result;
	}

	/**
	 * This allows to calculate the selected objects from the wrapper filled.
	 * 
	 * @param wrapper
	 *            The wrapper filled.
	 * @return The collection of object selected.
	 */
	protected Collection<Object> calculateSelectionFromWrapper(final TableSelectionWrapper wrapper) {
		final Collection<Object> selection = new ArrayList<Object>();

		// Fill the selection list with the selected columns
		final List<Integer> selectedColumnsIndexes = new ArrayList<Integer>();
		for (final Entry<Integer, Object> selectedColumn : wrapper.getFullySelectedColumns().entrySet()) {
			final Object selectedObject = AxisUtils.getRepresentedElement(selectedColumn.getValue());
			selection.add(selectedObject);
			selectedColumnsIndexes.add(selectedColumn.getKey());
		}
		// Fill the selection list with the selected rows
		final List<Integer> selectedRowsIndexes = new ArrayList<Integer>();
		for (final Entry<Integer, Object> selectedRow : wrapper.getFullySelectedRows().entrySet()) {
			final Object selectedObject = AxisUtils.getRepresentedElement(selectedRow.getValue());
			selection.add(selectedObject);
			selectedRowsIndexes.add(selectedRow.getKey());
		}
		// Fill the selection list with the selected cells
		for (final PositionCoordinate cellLocation : wrapper.getSelectedCells()) {
			final int colPos = cellLocation.getColumnPosition();
			final int rowPos = cellLocation.getRowPosition();
			if (!selectedColumnsIndexes.contains(new Integer(this.selectionLayer.getColumnIndexByPosition(colPos))) && !selectedRowsIndexes.contains(new Integer(this.selectionLayer.getRowIndexByPosition(rowPos)))) {
				final ILayerCell cell = this.selectionLayer.getCellByPosition(colPos, rowPos);
				if (cell != null) {
					final Object value = cell.getDataValue();
					if (value != null) {
						if (value instanceof Collection<?>) {
							final Iterator<?> iter = ((Collection<?>) value).iterator();
							while (iter.hasNext()) {
								final Object current = iter.next();
								selection.add(current);
							}
						} else {
							selection.add(value);
						}
					}else{
						// Bug 481817 : When the value is null, we need to have the cell selection, so add the cell as selection instead of value
						selection.add(cell);
					}
				}
			}
		}

		return selection;
	}

	/**
	 * This allows to manage the selection when any type of selection was done (cell, row or column selection from the selection event).
	 * 
	 * @param wrapper
	 *            The wrapper to fill.
	 * @param event
	 *            The event caught.
	 * @return The collection of object selected.
	 */
	protected Collection<Object> calculateSelectionRowsAndColumnsWithoutTypeSelectionEvent(final TableSelectionWrapper wrapper, final ILayerEvent event) {
		final Collection<Object> selection = new ArrayList<Object>();

		// we are not able to distinguish the 2 ways to select a full axis :
		// - first way : clicking on axis header
		// - second way : clicking on first cell of the axis, Pressing SHIFT, clicking on the last cell of the axis (or selecting each cell of the axis pressing CTRL)
		// so we are not able to know if the user want to select the element represented by the axis OR all values displayed on the axis, without the element represented by the axis
		// we decided to implements this behavior for all kind of selection event :
		// 1- we add in the selection elements represented by fully selected rows
		// 2- we add in the selection elements represented by fully selected columns
		// 3- we add in the selection the contents of selected cell which are not included in the fully selected axis

		final List<Integer> selectedRowsIndexes = new ArrayList<Integer>();
		for (int i : this.selectionLayer.getFullySelectedRowPositions()) {
			int rowIndex = this.selectionLayer.getRowIndexByPosition(i);
			selectedRowsIndexes.add(new Integer(rowIndex));
			Object el = this.manager.getRowElement(rowIndex);
			if (el != null) {
				wrapper.getFullySelectedRows().put(Integer.valueOf(rowIndex), el);
				el = AxisUtils.getRepresentedElement(el);
				selection.add(el);
			}
		}
		final List<Integer> selectedColumnsIndexes = new ArrayList<Integer>();
		for (int i : this.selectionLayer.getFullySelectedColumnPositions()) {
			int columnIndex = this.selectionLayer.getColumnIndexByPosition(i);
			selectedColumnsIndexes.add(new Integer(columnIndex));
			Object el = this.manager.getColumnElement(columnIndex);
			if (el != null) {
				wrapper.getFullySelectedColumns().put(Integer.valueOf(columnIndex), el);
				el = AxisUtils.getRepresentedElement(el);
				selection.add(el);
			}
		}

		for (final PositionCoordinate cellLocation : wrapper.getSelectedCells()) {
			final int colPos = cellLocation.getColumnPosition();
			final int rowPos = cellLocation.getRowPosition();
			if (!selectedColumnsIndexes.contains(new Integer(this.selectionLayer.getColumnIndexByPosition(colPos))) && !selectedRowsIndexes.contains(new Integer(this.selectionLayer.getRowIndexByPosition(rowPos)))) {
				final ILayerCell cell = this.selectionLayer.getCellByPosition(colPos, rowPos);
				if (cell != null) {
					final Object value = cell.getDataValue();
					if (value != null) {
						if (value instanceof Collection<?>) {
							final Iterator<?> iter = ((Collection<?>) value).iterator();
							while (iter.hasNext()) {
								final Object current = iter.next();
								selection.add(current);
							}
						} else {
							selection.add(value);
						}
					}else{
						// Bug 481817 : When the value is null, we need to have the cell selection, so add the cell as selection instead of value
						selection.add(cell);
					}
				}
			}
		}

		return selection;
	}


	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.ui.services.IDisposable#dispose()
	 */
	@Override
	public void dispose() {
		this.isDisposed = true;
		this.manager = null;
		// To be sure, to fix the bug 469376: [Table] Memory Leak : (Tree)NattableWidgetManager, EObjectTreeItemAxis and others objects are not disposed when the table is closed
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=469376
		setSelection(StructuredSelection.EMPTY);

		if (this.selectionLayer != null) {
			this.selectionLayer.removeLayerListener(this.selectionListener);
			this.selectionLayer = null;
		}
		this.listeners.clear();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.nebula.widgets.nattable.layer.ILayerListener#handleLayerEvent(org.eclipse.nebula.widgets.nattable.layer.event.ILayerEvent)
	 */
	@Override
	public void handleLayerEvent(final ILayerEvent event) {
		if (!isDisposed && event instanceof ISelectionEvent) {
			calculateAndStoreNewSelection(event);
		}
	}
}

Back to the top