Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 36ef614415f009810d33b844c31bb4f0d84101b0 (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
/*****************************************************************************
 * Copyright (c) 2012, 2014 CEA LIST 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:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - bug 430880
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.nattable.listener;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.UnexecutableCommand;
import org.eclipse.emf.edit.ui.dnd.LocalTransfer;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.jface.util.LocalSelectionTransfer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForEObject;
import org.eclipse.papyrus.infra.nattable.Activator;
import org.eclipse.papyrus.infra.nattable.manager.cell.CellManagerFactory;
import org.eclipse.papyrus.infra.nattable.manager.table.INattableModelManager;
import org.eclipse.papyrus.infra.nattable.selection.ISelectionExtractor;
import org.eclipse.papyrus.infra.nattable.utils.LocationValue;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.DropTargetListener;
import org.eclipse.swt.graphics.Point;

/**
 *
 * This listener allow to manage the drop inside the table
 *
 */
public class NatTableDropListener implements DropTargetListener {

	/**
	 * the table manager
	 */
	private final INattableModelManager manager;

	private final ISelectionExtractor selectionExtractor;
	/**
	 * the location value to use to drop the elements
	 */
	private LocationValue dropKindValue;

	/**
	 *
	 * Constructor.
	 *
	 * @param manager
	 *            the table manager
	 * @param selectionExtractor
	 */
	public NatTableDropListener(final INattableModelManager manager, ISelectionExtractor selectionExtractor) {
		this.manager = manager;
		this.selectionExtractor = selectionExtractor;
	}

	/**
	 *
	 * @see org.eclipse.swt.dnd.DropTargetListener#dragEnter(org.eclipse.swt.dnd.DropTargetEvent)
	 *
	 * @param event
	 */
	@Override
	public void dragEnter(final DropTargetEvent event) {
		validateDropEvent(event);
	}

	protected void validateDropEvent(final DropTargetEvent event) {
		event.operations = DND.DROP_COPY | DND.DROP_MOVE;

		// Move and Link semantics don't make sense for tables which, like diagrams, are visualizations
		// of objects also visualized in other places (such as the Model Explorer)
		event.detail = DND.DROP_COPY;
	}

	/**
	 *
	 * @see org.eclipse.swt.dnd.DropTargetListener#dragLeave(org.eclipse.swt.dnd.DropTargetEvent)
	 *
	 * @param event
	 */
	@Override
	public void dragLeave(final DropTargetEvent event) {
		// nothing to do
	}

	/**
	 *
	 * @see org.eclipse.swt.dnd.DropTargetListener#dragOperationChanged(org.eclipse.swt.dnd.DropTargetEvent)
	 *
	 * @param event
	 */
	@Override
	public void dragOperationChanged(final DropTargetEvent event) {
		validateDropEvent(event);
	}

	/**
	 *
	 * @see org.eclipse.swt.dnd.DropTargetListener#dragOver(org.eclipse.swt.dnd.DropTargetEvent)
	 *
	 * @param event
	 */
	@Override
	public void dragOver(final DropTargetEvent event) {
		validateDropEvent(event);
		this.dropKindValue = null;
		final List<Object> droppedElements = getDroppedObjects(event);
		if (droppedElements.isEmpty()) {
			return; // Nothing to do
		}
		this.dropKindValue = this.manager.getLocationInTheTable(new Point(event.x, event.y));
		int drop = DND.DROP_NONE;
		switch (this.dropKindValue.getKind()) {
		case AFTER_COLUMN_HEADER:
			if (this.manager.canDropColumnsElement(droppedElements)) {
				drop = event.detail;
			}
			break;
		case AFTER_ROW_HEADER:
			if (this.manager.canDropRowElement(droppedElements)) {
				drop = event.detail;
			}
			break;
		case COLUMN_HEADER:
			if (this.manager.canInsertColumns(droppedElements, this.dropKindValue.getColumnIndex())) {
				drop = event.detail;
			}
			break;
		case ROW_HEADER:
			if (this.manager.canInsertRow(droppedElements, this.dropKindValue.getRowIndex())) {
				drop = event.detail;
			}
			break;
		case CELL:
			int rowIndex = this.dropKindValue.getRowIndex();
			int columnIndex = this.dropKindValue.getColumnIndex();
			final Object rowElement = this.manager.getRowElement(rowIndex);
			final Object columnElement = this.manager.getColumnElement(columnIndex);
			if (CellManagerFactory.INSTANCE.isCellEditable(columnElement, rowElement, this.manager)) {
				final TransactionalEditingDomain domain = getEditingDomain();
				final Command cmd = getDropSetValueCommand(domain, droppedElements);
				if (cmd!=null && cmd.canExecute()) {
					drop = event.detail;
				}
			}
			break;
		case UNKNOWN:
			drop = DND.DROP_NONE;
			break;
		default:
			drop = DND.DROP_NONE;
			break;
		}
		event.detail = drop;
	}

	protected List<Object> getDroppedObjects(DropTargetEvent event) {
		final LocalTransfer localTransfer = LocalTransfer.getInstance();
		final Object data = localTransfer.nativeToJava(event.currentDataType);
		IStructuredSelection structuredSelection = null;
		if (data instanceof IStructuredSelection) {
			structuredSelection = (IStructuredSelection) data;
		} else if (LocalSelectionTransfer.getTransfer().isSupportedType(event.currentDataType)) {
			// Try the local selection transfer
			ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();
			if (selection instanceof IStructuredSelection) {
				structuredSelection = (IStructuredSelection) selection;
			}
		}
		if (structuredSelection == null) {
			return Collections.emptyList();
		}
		return new ArrayList<Object>(extractSelectedObjects(structuredSelection));
	}

	protected Collection<?> extractSelectedObjects(IStructuredSelection structuredSelection) {
		return this.selectionExtractor.extractSelectedObjects(structuredSelection);
	}

	/**
	 *
	 * @param droppedElements
	 *            the dropped elements
	 * @return
	 *         the command to set the value in the selected cell
	 */
	private Command getDropSetValueCommand(final TransactionalEditingDomain domain, final List<Object> droppedElements) {
		int rowIndex = this.dropKindValue.getRowIndex();
		int columnIndex = this.dropKindValue.getColumnIndex();
		final Object rowElement = this.manager.getRowElement(rowIndex);
		final Object columnElement = this.manager.getColumnElement(columnIndex);
		if (CellManagerFactory.INSTANCE.isCellEditable(columnElement, rowElement, this.manager)) {
			Object newValue = null;
			final Object currentValue = CellManagerFactory.INSTANCE.getCrossValueIgnoringProblems(columnElement, rowElement, this.manager);
			if (currentValue instanceof Collection<?>) {
				// the dropped elements will be added to the current Value in case of multivalued cell
				final Collection<Object> tmpNewValue = new ArrayList<Object>();
				tmpNewValue.addAll((Collection<?>) currentValue);
				tmpNewValue.addAll(droppedElements);
				newValue = tmpNewValue;
			} else if (droppedElements.size() == 1) {
				newValue = droppedElements.get(0);
			} else {
				newValue = droppedElements;
			}
			final Command cmd = CellManagerFactory.INSTANCE.getSetCellValueCommand(domain, columnElement, rowElement, newValue, manager);
			return cmd;
		}
		return UnexecutableCommand.INSTANCE;
	}

	/**
	 *
	 * @see org.eclipse.swt.dnd.DropTargetListener#drop(org.eclipse.swt.dnd.DropTargetEvent)
	 *
	 * @param event
	 */
	@Override
	public void drop(final DropTargetEvent event) {
		// we drop the elements into the table
		final List<Object> droppedElements = getDroppedObjects(event);
		if (!droppedElements.isEmpty()) {
			if (this.dropKindValue != null) {
				switch (this.dropKindValue.getKind()) {
				case AFTER_COLUMN_HEADER:
					this.manager.addColumns(droppedElements);
					break;
				case AFTER_ROW_HEADER:
					this.manager.addRows(droppedElements);
					break;
				case COLUMN_HEADER:
					this.manager.insertColumns(droppedElements, this.dropKindValue.getColumnIndex());
					break;
				case ROW_HEADER:
					this.manager.insertRows(droppedElements, this.dropKindValue.getRowIndex());
					break;
				case CELL:
					final TransactionalEditingDomain domain = getEditingDomain();
					final Command cmd = getDropSetValueCommand(domain, droppedElements);
					if (cmd!=null && cmd.canExecute()) {
						domain.getCommandStack().execute(cmd);
					}
					break;
				case UNKNOWN:
					break;
				default:
					break;
				}
			}
		}
		this.dropKindValue = null;
	}


	@Override
	public void dropAccept(final DropTargetEvent event) {
		// nothing to do
	}

	/**
	 *
	 * @return
	 *         the Transactional Editing Domain to use to edit the model
	 */
	private TransactionalEditingDomain getEditingDomain() {
		TransactionalEditingDomain domain = null;
		ServicesRegistry registry = null;
		try {
			registry = ServiceUtilsForEObject.getInstance().getServiceRegistry(this.manager.getTable().getContext());
		} catch (ServiceException e) {
			Activator.log.error(e);
		}

		if (registry != null) {
			try {
				domain = registry.getService(TransactionalEditingDomain.class);
			} catch (ServiceException e) {
				Activator.log.error(e);
			}
		}
		return domain;
	}

}

Back to the top