Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1b7aef2f42d52da057ed933f7f2deafeb8fb9593 (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
/*****************************************************************************
 * Copyright (c) 2012 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
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.nattable.provider;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

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.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.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 {

	/**
	 * 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;

	/**
	 * 
	 * Constructor.
	 * 
	 * @param manager
	 *        the table model manager to use to find selected elements
	 * @param selectionLayer
	 */
	public TableSelectionProvider(final INattableModelManager manager, final SelectionLayer selectionLayer) {
		this.selectionLayer = selectionLayer;
		this.selectionListener = new ILayerListener() {

			public void handleLayerEvent(final ILayerEvent event) {
				calculateAndStoreNewSelection(event);
			}
		};
		this.selectionLayer.addLayerListener(this.selectionListener);
		this.currentSelection = new StructuredSelection();
		this.listeners = new ArrayList<ISelectionChangedListener>();
		this.manager = manager;
	}

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

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

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

	/**
	 * 
	 * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection)
	 * 
	 * @param selection
	 */
	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);
		}
	}

	protected/* synchronized */void calculateAndStoreNewSelection(final ILayerEvent event) {
		//the list of the selected elements
		final List<Object> selection = new ArrayList<Object>();
		final ISelection newSelection;
		if(event instanceof ISelectionEvent) {
			//add the cell selection
			final Collection<PositionCoordinate> selectedCells = Arrays.asList(this.selectionLayer.getSelectedCellPositions());
			final TableSelectionWrapper wrapper = new TableSelectionWrapper(selectedCells);

			//we returns the contents of the last selected cell
			//we could returns the contents of all selected cells if its required
			if(event instanceof CellSelectionEvent) {
				for(final PositionCoordinate current1 : this.selectionLayer.getSelectedCellPositions()) {
					final int colPos = current1.getColumnPosition();
					final int rowPos = current1.getRowPosition();
					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 if(event instanceof RowSelectionEvent) {
				this.selectionLayer.getFullySelectedRowPositions();
				int[] rows = this.selectionLayer.getFullySelectedRowPositions();
				for(int i : rows) {
					Object el = this.manager.getRowElement(i);
					if(el != null) {
						el = AxisUtils.getRepresentedElement(el);
						selection.add(el);
					}
				}

			} else if(event instanceof ColumnSelectionEvent) {
				int[] selectedColumn = this.selectionLayer.getFullySelectedColumnPositions();
				for(int i : selectedColumn) {
					Object el = this.manager.getColumnElement(i);
					if(el != null) {
						el = AxisUtils.getRepresentedElement(el);
						selection.add(el);
					}
				}
			}
			newSelection = new TableStructuredSelection(selection, wrapper);
		} else {
			newSelection = new StructuredSelection();
		}
		setSelection(newSelection);
	}



	/**
	 * 
	 * @see org.eclipse.ui.services.IDisposable#dispose()
	 * 
	 */
	public void dispose() {
		this.selectionLayer.removeLayerListener(this.selectionListener);
		this.selectionLayer = null;
	}
}

Back to the top