Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b7f62ac59f4fd7ee321c8f4317e89b7911f2960f (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
/*******************************************************************************
 * Copyright (c) 2011, 2012 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.handlers;

import java.util.List;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.tcf.te.runtime.callback.Callback;
import org.eclipse.tcf.te.tcf.filesystem.core.interfaces.IOperation;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.operations.IOpExecutor;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.operations.OpCopy;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.operations.OpMove;
import org.eclipse.tcf.te.tcf.filesystem.core.model.FSTreeNode;
import org.eclipse.tcf.te.tcf.filesystem.ui.activator.UIPlugin;
import org.eclipse.tcf.te.tcf.filesystem.ui.internal.dnd.CommonDnD;
import org.eclipse.tcf.te.tcf.filesystem.ui.internal.operations.FsClipboard;
import org.eclipse.tcf.te.tcf.filesystem.ui.internal.operations.UiExecutor;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * The handler that pastes the files or folders in the clip board.
 */
public class PasteFilesHandler extends AbstractHandler {

	/*
	 * (non-Javadoc)
	 * @see
	 * org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
	 */
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		FsClipboard cb = UIPlugin.getClipboard();
		if (!cb.isEmpty()) {
			// Get the files/folders from the clip board.
			IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelectionChecked(event);
			List<FSTreeNode> nodes = cb.getFiles();
			IOpExecutor executor = null;
			IOperation operation = null;
			if (cb.isCutOp()) {
				FSTreeNode dest = (FSTreeNode) selection.getFirstElement();
				operation = new OpMove(nodes, dest, new MoveCopyCallback());
				executor = new UiExecutor(new Callback(){
					@Override
	                protected void internalDone(Object caller, IStatus status) {
						UIPlugin.getClipboard().clear();
	                }
				});
			}
			else if (cb.isCopyOp()) {
				FSTreeNode hovered = (FSTreeNode) selection.getFirstElement();
				FSTreeNode dest = getCopyDestination(hovered, nodes);
				boolean cpPerm = UIPlugin.isCopyPermission();
				boolean cpOwn = UIPlugin.isCopyOwnership();
				operation = new OpCopy(nodes, dest, cpPerm, cpOwn, new MoveCopyCallback());
				executor = new UiExecutor();
			}
			if (executor != null && operation != null) {
				executor.execute(operation);
			}
		}
		else {
			Clipboard clipboard = cb.getSystemClipboard();
			Object contents = clipboard.getContents(FileTransfer.getInstance());
			if (contents != null) {
				String[] files = (String[]) contents;
				// Get the files/folders from the clip board.
				IStructuredSelection selection = (IStructuredSelection) HandlerUtil
				                .getCurrentSelectionChecked(event);
				FSTreeNode hovered = (FSTreeNode) selection.getFirstElement();
				CommonDnD dnd = new CommonDnD();
				dnd.dropFiles(null, files, DND.DROP_COPY, hovered);
			}
		}
		return null;
	}

	/**
	 * 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 FSTreeNode getCopyDestination(FSTreeNode hovered, List<FSTreeNode> nodes) {
		if (hovered.isFile()) {
			return hovered.getParent();
		}
		else if (hovered.isDirectory()) {
			for (FSTreeNode node : nodes) {
				if (node == hovered) {
					return hovered.getParent();
				}
			}
		}
		return hovered;
	}
}

Back to the top