Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b409714621a56fd6645f57faa31df46013a837c8 (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
/*******************************************************************************
 * Copyright (c) 2011, 2015 Wind River Systems, Inc. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.tcf.filesystem.ui.internal.dnd;

import static java.util.Arrays.asList;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.util.LocalSelectionTransfer;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.swt.dnd.TransferData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.tcf.te.tcf.filesystem.core.interfaces.IConfirmCallback;
import org.eclipse.tcf.te.tcf.filesystem.core.interfaces.IOperation;
import org.eclipse.tcf.te.tcf.filesystem.core.interfaces.runtime.IFSTreeNode;
import org.eclipse.tcf.te.tcf.filesystem.ui.activator.UIPlugin;
import org.eclipse.tcf.te.tcf.filesystem.ui.internal.ImageConsts;
import org.eclipse.tcf.te.tcf.filesystem.ui.internal.handlers.MoveCopyCallback;
import org.eclipse.tcf.te.tcf.filesystem.ui.internal.operations.UiExecutor;
import org.eclipse.tcf.te.tcf.filesystem.ui.nls.Messages;
import org.eclipse.ui.PlatformUI;
import org.osgi.framework.Bundle;
/**
 * Common DnD operations shared by File Explorer and Target Explorer.
 */
public class CommonDnD implements IConfirmCallback {

	/**
	 * If the current selection is draggable.
	 *
	 * @param selection The currently selected nodes.
	 * @return true if it is draggable.
	 */
	public boolean isDraggable(IStructuredSelection selection) {
		if (selection.isEmpty()) {
			return false;
		}
		Object[] objects = selection.toArray();
		for (Object object : objects) {
			if (!isDraggableObject(object)) {
				return false;
			}
		}
		return true;
	}

	/**
	 * If the specified object is a draggable element.
	 *
	 * @param object The object to be dragged.
	 * @return true if it is draggable.
	 */
	private boolean isDraggableObject(Object object) {
		if (object instanceof IFSTreeNode) {
			IFSTreeNode node = (IFSTreeNode) object;
			if (node.isRootDirectory())
				return false;
			if (node.isWindowsNode())
				return !node.isReadOnly();
			return node.isWritable();
		}
		return false;
	}

	/**
	 * Perform the drop operation over dragged files to the specified target folder.
	 *
	 * @param viewer the tree viewer to be refreshed after dragging.
	 * @param files The files being dropped.
	 * @param operations the current dnd operations.
	 * @param target the target folder the files to be dropped to.
	 * @return true if the dropping is successful.
	 */
	public boolean dropFiles(TreeViewer viewer, String[] files, int operations, IFSTreeNode target) {
		boolean move = (operations & DND.DROP_MOVE) != 0;
		if (move) {
			String question;
			if (files.length == 1) {
				question = NLS.bind(Messages.FSDropTargetListener_MovingWarningSingle, files[0]);
			} else {
				question = NLS.bind(Messages.FSDropTargetListener_MovingWarningMultiple, Integer.valueOf(files.length));
			}
			Shell parent = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
			if (!MessageDialog.openQuestion(parent, Messages.FSDropTargetListener_ConfirmMoveTitle, question)) {
				return false;
			}
		} else if ((operations & DND.DROP_COPY) == 0) {
			return false;
		}
		IStatus status = UiExecutor.execute(target.operationDropFiles(asList(files), this));
		if (move && status.isOK()) {
			for (String path : files) {
				File file = new File(path);
				if (!file.delete()) {
				}
			}
		}
		return status.isOK();
	}

	/**
	 * Perform the drop operation over dragged selection.
	 *
	 * @param target the target Object to be moved to.
	 * @param operations the current dnd operations.
	 * @param selection The local selection being dropped.
	 * @return true if the dropping is successful.
	 */
	public boolean dropLocalSelection(IFSTreeNode target, int operations, IStructuredSelection selection) {
		List<IFSTreeNode> nodes = toNodes(selection);
		IOperation operation;
		boolean move = (operations & DND.DROP_MOVE) != 0;
		if (move && nodes != null) {
			operation = target.operationDropMove(nodes, new MoveCopyCallback());
		} else if ((operations & DND.DROP_COPY) != 0) {
			if (nodes != null) {
				IFSTreeNode dest = getCopyDestination(target, nodes);
				boolean cpPerm = UIPlugin.isCopyPermission();
				boolean cpOwn = UIPlugin.isCopyOwnership();
				operation = dest.operationDropCopy(nodes, cpPerm, cpOwn, new MoveCopyCallback());
			} else {
				List<String> files = toFiles(selection);
				if (files != null) {
					operation = target.operationDropFiles(files, new MoveCopyCallback());
				} else {
					return false;
				}
			}
		} else {
			return false;
		}
		IStatus status = UiExecutor.execute(operation);
		if (move) {
			UIPlugin.getClipboard().clear();
		}
		return status.isOK();
	}

	/**
	 * Return an appropriate destination directory for copying according to the specified hovered
	 * node. If the hovered node is a file, then return its parent directory. If the hovered node is
	 * a directory, then return its self if it is not a node being copied. Return its parent
	 * directory if it is a node being copied.
	 *
	 * @param hovered
	 * @param nodes
	 * @return
	 */
	private IFSTreeNode getCopyDestination(IFSTreeNode hovered, List<IFSTreeNode> nodes) {
		if (hovered.isFile()) {
			return hovered.getParent();
		}
		else if (hovered.isDirectory()) {
			for (IFSTreeNode node : nodes) {
				if (node == hovered) {
					return hovered.getParent();
				}
			}
		}
		return hovered;
	}

	/**
	 * Validate dropping when the elements being dragged are files.
	 *
	 * @param target The target object.
	 * @param operation The DnD operation.
	 * @param transferType The transfered data simulator.
	 * @return true if it is valid for dropping.
	 */
	public boolean validateFilesDrop(Object target, int operation, TransferData transferType) {
		FileTransfer transfer = FileTransfer.getInstance();
		String[] elements = (String[]) transfer.nativeToJava(transferType);
		if (elements.length > 0) {
			boolean moving = (operation & DND.DROP_MOVE) != 0;
			boolean copying = (operation & DND.DROP_COPY) != 0;
			IFSTreeNode hovered = (IFSTreeNode) target;
			if (hovered.isFile() && copying) {
				hovered = hovered.getParent();
			}
			return hovered.isDirectory() && hovered.isWritable() && (moving || copying);
		}
		return false;
	}

	/**
	 * Validate dropping when the elements being dragged are local selection.
	 *
	 * @param target The target object.
	 * @param operation The DnD operation.
	 * @param transferType The transfered data simulator.
	 * @return true if it is valid for dropping.
	 */
	public int validateLocalSelectionDrop(Object target, int operation, TransferData transferType) {
		IFSTreeNode hovered = (IFSTreeNode) target;
		LocalSelectionTransfer transfer = LocalSelectionTransfer.getTransfer();
		IStructuredSelection selection = (IStructuredSelection) transfer.getSelection();
		boolean moving = (operation & DND.DROP_MOVE) != 0;
		boolean copying = (operation & DND.DROP_COPY) != 0;
		if (!moving && !copying)
			return 0;

		List<IFSTreeNode> nodes = toNodes(selection);
		if (nodes != null) {
			if (hovered.isDirectory() && hovered.isWritable() && (moving || copying)) {
				IFSTreeNode head = nodes.get(0);
				String hid = head.getPeerNode().getPeerId();
				String tid = hovered.getPeerNode().getPeerId();
				if (hid.equals(tid)) {
					for (IFSTreeNode node : nodes) {
						if (moving && node == hovered || node.getParent() == hovered || node.isAncestorOf(hovered)) {
							return 0;
						}
					}
					return operation;
				}
			} else if (hovered.isFile() && (copying || moving)) {
				hovered = hovered.getParent();
				return validateLocalSelectionDrop(hovered, operation, transferType);
			}
			return 0;
		}
		List<String> files = toFiles(selection);
		if (files != null) {
			if (hovered.isDirectory() && hovered.isWritable()) {
				return DND.DROP_COPY;
			}
			return 0;
		}
		return 0;
	}

	private List<IFSTreeNode> toNodes(IStructuredSelection selection) {
	    List<IFSTreeNode> nodes = new ArrayList<IFSTreeNode>();
	    for (Object o : selection.toList()) {
			if (!(o instanceof IFSTreeNode))
				return null;
			nodes.add((IFSTreeNode) o);
		}
	    return nodes;
    }

	private List<String> toFiles(IStructuredSelection selection) {
		// Dependency to org.eclipse.core.resources is optional
		Bundle rb = Platform.getBundle("org.eclipse.core.resources"); //$NON-NLS-1$
		if (rb == null || rb.getState() != Bundle.ACTIVE)
			return null;

		List<String> files = new ArrayList<String>();
		for (Object o : selection.toList()) {
			IResource res = (IResource) Platform.getAdapterManager().getAdapter(o, IResource.class);
			if (res == null)
				return null;
			IPath location = res.getLocation();
			if (location == null)
				return null;
			files.add(location.toFile().getAbsolutePath());
		}
		return files;
    }

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.filesystem.interfaces.IConfirmCallback#requires(java.lang.Object)
	 */
	@Override
    public boolean requires(Object object) {
		return true;
    }

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.filesystem.interfaces.IConfirmCallback#confirms(java.lang.Object)
	 */
	@Override
    public int confirms(final Object object) {
		final int[] results = new int[1];
		Display display = PlatformUI.getWorkbench().getDisplay();
		display.syncExec(new Runnable() {
			@Override
			public void run() {
				Shell parent = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
				String title = Messages.FSUpload_OverwriteTitle;
				String message = NLS.bind(Messages.FSUpload_OverwriteConfirmation, getName(object));
				final Image titleImage = UIPlugin.getImage(ImageConsts.DELETE_READONLY_CONFIRM);
				MessageDialog qDialog = new MessageDialog(parent, title, null, message,
								MessageDialog.QUESTION, new String[] {Messages.FSUpload_Yes,
								Messages.FSUpload_YesToAll, Messages.FSUpload_No, Messages.FSUpload_Cancel}, 0) {
					@Override
					public Image getQuestionImage() {
						return titleImage;
					}
				};
				results[0] = qDialog.open();
			}
		});
		return results[0];
    }

	protected String getName(Object object) {
		if (object instanceof File) {
			return ((File) object).getName();
		}
		if (object instanceof IFSTreeNode) {
			return ((IFSTreeNode) object).getName();
		}
		return String.valueOf(object);
    }

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.swt.dnd.DragSourceListener#dragSetData(org.eclipse.swt.dnd.DragSourceEvent)
	 */
	public boolean setDragData(DragSourceEvent anEvent) {
	    if (LocalSelectionTransfer.getTransfer().isSupportedType(anEvent.dataType)) {
			anEvent.data = LocalSelectionTransfer.getTransfer().getSelection();
			return true;
		}
		return false;
    }
}

Back to the top