Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0fdc7a770e71f671b134a161db62416c1daac060 (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
/*******************************************************************************
 * Copyright (c) 2008, 2010 Andrew Gvozdev.
 * 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:
 *    Andrew Gvozdev (Quoin Inc.) - Initial implementation
 *******************************************************************************/

package org.eclipse.cdt.make.internal.ui.dnd;

import org.eclipse.cdt.make.core.IMakeTarget;
import org.eclipse.cdt.make.ui.TargetSourceContainer;
import org.eclipse.core.resources.IContainer;
import org.eclipse.jface.util.TransferDropTargetListener;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.DropTargetListener;
import org.eclipse.swt.dnd.TransferData;

/**
 * This abstract class provides a frame for {@link DropTargetListener} able to accept a
 * drop into a container area. A container area includes the container itself and
 * the items directly belonging to the container. A drop into the container area is
 * treated as a drop on the container itself. Also, some flexibility for adjusting
 * drop operation is provided.
 *
 * @see org.eclipse.swt.dnd.DropTargetListener
 *
 */
public abstract class AbstractContainerAreaDropAdapter implements TransferDropTargetListener {

	private int originallyRequestedOperation = DND.DROP_NONE;
	private Object lastDragOverTarget = null;
	private int lastDragOverOperation = DND.DROP_NONE;

	/**
	 * This method lets changing initial drag operation. The operation will be
	 * indicated by mouse cursor. This operation is passed in {@code event.detail}
	 * to {@code dragEnter} method.
	 *
	 * @param operation - incoming operation.
	 * @return changed operation. The return must be one of
	 *         {@link org.eclipse.swt.dnd.DND} operations.
	 *
	 * @see DropTargetListener#dragEnter(DropTargetEvent)
	 * @see DND#DROP_NONE
	 * @see DND#DROP_COPY
	 * @see DND#DROP_MOVE
	 * @see DND#DROP_LINK
	 * @see DND#DROP_DEFAULT
	 */
	protected abstract int dragEnterOperation(int operation);

	/**
	 * This method lets adjust drag operation over container area. The operation
	 * will be indicated by mouse cursor. This operation is passed in {@code
	 * event.detail} to {@code dragOver} method.
	 *
	 * @param operation - incoming operation.
	 * @param dropContainer - container where drop is going to be.
	 * @param dropTarget - drop target.
	 * @return changed operation. The return must be one of
	 *         {@link org.eclipse.swt.dnd.DND} operations.
	 *
	 *
	 * @see DropTargetListener#dragOver(DropTargetEvent)
	 * @see DND#DROP_NONE
	 * @see DND#DROP_COPY
	 * @see DND#DROP_MOVE
	 * @see DND#DROP_LINK
	 * @see DND#DROP_DEFAULT
	 */
	protected abstract int dragOverOperation(int operation, IContainer dropContainer, Object dropTarget);

	/**
	 * Implementation of the actual drop of {@code dropObject} to {@code dropContainer}.
	 *
	 * @param dropObject - object to drop.
	 * @param dropContainer - container where to drop the object.
	 * @param operation - drop operation.
	 *
	 * @see DropTargetListener#drop(DropTargetEvent)
	 */
	protected abstract void dropToContainer(Object dropObject, IContainer dropContainer,
		int operation);

	/**
	 * Defines if a transfer data is supported by the implementer.
	 *
	 * @see org.eclipse.swt.dnd.Transfer#isSupportedType
	 *
	 * @param transferData - data type to examine
	 * @return whether the given data type is supported by this listener
	 */
	public boolean isSupportedType(TransferData transferData) {
		return getTransfer().isSupportedType(transferData);
	}

	/**
	 * This function is called only the listener is able to handle provided data type.
	 * By default {@code isEnabled} returns {@code true} but subclasses are free to
	 * override.
	 *
	 * @param event - the information associated with the drag event.
	 * @return {@code true}
	 *
	 * @see DropTargetEvent
	 */
	@Override
	public boolean isEnabled(DropTargetEvent event) {
		return true;
	}

	/**
	 * Implementation of {@code DropTargetListener#dragEnter} to support
	 * dropping into container area.
	 *
	 * @param event - the information associated with the drag event
	 *
	 * @see DropTargetEvent
	 */
	@Override
	public void dragEnter(DropTargetEvent event) {
		lastDragOverTarget = null;
		lastDragOverOperation = DND.DROP_NONE;

		if (isSupportedType(event.currentDataType)) {
			originallyRequestedOperation = event.detail;
			event.detail = dragEnterOperation(originallyRequestedOperation);
		} else {
			event.detail = DND.DROP_NONE;
			originallyRequestedOperation = DND.DROP_NONE;
		}
	}

	/**
	 * Implementation of {@code DropTargetListener#dragOperationChanged} to support
	 * dropping into container area.
	 *
	 * @param event - the information associated with the drag event
	 *
	 * @see DropTargetEvent
	 */
	@Override
	public void dragOperationChanged(DropTargetEvent event) {
		originallyRequestedOperation = event.detail;
		event.detail = dragOverOperationCached(originallyRequestedOperation,
			determineDropContainer(event), determineDropTarget(event));
	}

	/**
	 * Implementation of {@code DropTargetListener#dragOver} to support
	 * dropping into container area.
	 *
	 * @param event - the information associated with the drag event
	 *
	 * @see DropTargetEvent
	 */
	@Override
	public void dragOver(DropTargetEvent event) {
		event.detail = dragOverOperationCached(originallyRequestedOperation,
			determineDropContainer(event), determineDropTarget(event));

		if (originallyRequestedOperation != DND.DROP_NONE) {
			// let user discover items even if event.detail is DND.DROP_NONE
			// since the original operation could be applicable to other items
			event.feedback = DND.FEEDBACK_SCROLL | DND.FEEDBACK_EXPAND;
			if (event.detail != DND.DROP_NONE) {
				event.feedback = event.feedback | DND.FEEDBACK_SELECT;
			}
		} else {
			event.feedback = DND.FEEDBACK_NONE;
		}
	}

	/**
	 * Implementation of {@code DropTargetListener#dragLeave} to support
	 * dropping into container area.
	 *
	 * @param event - the information associated with the drag event
	 *
	 * @see DropTargetEvent
	 */
	@Override
	public void dragLeave(DropTargetEvent event) {
		// no action
	}

	/**
	 * Implementation of {@code DropTargetListener#dropAccept} to support
	 * dropping into container area.
	 *
	 * @param event - the information associated with the drag event
	 *
	 * @see DropTargetEvent
	 */
	@Override
	public void dropAccept(DropTargetEvent event) {
		// no action
	}

	/**
	 * Implementation of {@code DropTargetListener#drop} to support
	 * dropping into container area.
	 *
	 * @param event - the information associated with the drag event
	 *
	 * @see DropTargetEvent
	 */
	@Override
	public void drop(DropTargetEvent event) {
		IContainer dropContainer = determineDropContainer(event);
		if (dropContainer != null) {
			event.detail = dragOverOperationCached(event.detail, dropContainer, determineDropTarget(event));
			dropToContainer(event.data, dropContainer, event.detail);
		} else {
			event.detail = DND.DROP_NONE;
		}
	}

	/**
	 * This method provides caching of potentially long running and called on each
	 * mouse move {@link #dragOverOperation}.
	 *
	 * @param operation - incoming operation.
	 * @param dropContainer - container where drop is going to be.
	 * @param dropTarget - drop target.
	 * @return changed operation. The return must be one of
	 *         org.eclipse.swt.dnd.DND operations such as {@link DND#DROP_NONE},
	 *         {@link DND#DROP_COPY}, {@link DND#DROP_MOVE},
	 *         {@link DND#DROP_LINK}
	 *
	 *
	 * @see DropTargetListener#dragOver(DropTargetEvent)
	 */
	private int dragOverOperationCached(int operation, IContainer dropContainer, Object dropTarget) {
		if (dropTarget != lastDragOverTarget || operation != lastDragOverOperation) {
			lastDragOverOperation = dragOverOperation(operation, dropContainer, dropTarget);
			lastDragOverTarget = dropTarget;
		}
		return lastDragOverOperation;
	}

	/**
	 * Returns the drop target passed by {@code event.item}.
	 *
	 * @param event - the information associated with the drop event
	 */
	private static Object determineDropTarget(DropTargetEvent event) {
		return event.item == null ? null : event.item.getData();
	}

	/**
	 * Find which container area the mouse cursor is currently in.
	 *
	 * @param event - the information associated with the drop event
	 * @return the container of the current mouse location.
	 */
	public static IContainer determineDropContainer(DropTargetEvent event) {
		Object dropTarget = determineDropTarget(event);
		if (dropTarget instanceof IMakeTarget) {
			return ((IMakeTarget) dropTarget).getContainer();
		} else if (dropTarget instanceof IContainer) {
			return (IContainer) dropTarget;
		} else if (dropTarget instanceof TargetSourceContainer) {
			IContainer dropContainer = ((TargetSourceContainer) dropTarget).getContainer();
			return dropContainer;
		}
		return null;
	}

}

Back to the top