Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d1d0e50d18d3af4a85b082d6e2f0a8e346c6dc68 (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
/*******************************************************************************
 * 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.core.internal.operations;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.osgi.util.NLS;
import org.eclipse.tcf.protocol.IChannel;
import org.eclipse.tcf.protocol.IToken;
import org.eclipse.tcf.services.IFileSystem;
import org.eclipse.tcf.services.IFileSystem.DoneRename;
import org.eclipse.tcf.services.IFileSystem.FileSystemException;
import org.eclipse.tcf.te.tcf.core.Tcf;
import org.eclipse.tcf.te.tcf.filesystem.core.interfaces.IConfirmCallback;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.exceptions.TCFException;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.exceptions.TCFFileSystemException;
import org.eclipse.tcf.te.tcf.filesystem.core.model.FSTreeNode;
import org.eclipse.tcf.te.tcf.filesystem.core.nls.Messages;

/**
 * FSMove moves specified tree nodes to a destination folder.
 */
public class OpMove extends Operation {
	// The file/folder nodes to be moved.
	List<FSTreeNode> nodes;
	// The destination folder to be moved to.
	FSTreeNode dest;
	// The callback
	IConfirmCallback confirmCallback;

	/**
	 * Create a move operation to move the specified nodes to the destination folder.
	 *
	 * @param nodes The nodes to be moved.
	 * @param dest the destination folder to move to.
	 */
	public OpMove(List<FSTreeNode> nodes, FSTreeNode dest) {
		this(nodes, dest, null);
	}

	/**
	 * Create a move operation to move the specified nodes to the destination folder
	 * and a confirmation callback.
	 *
	 * @param nodes The nodes to be moved.
	 * @param dest the destination folder to move to.
	 * @param confirmCallback the confirmation callback.
	 */
	public OpMove(List<FSTreeNode> nodes, FSTreeNode dest, IConfirmCallback confirmCallback) {
		super();
		this.nodes = getAncestors(nodes);
		this.dest = dest;
		this.confirmCallback = confirmCallback;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.filesystem.core.internal.operations.Operation#run(org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
		super.run(monitor);
		// Remove its self from the clipped nodes.
		nodes.remove(dest);
		IChannel channel = null;
		try {
			if (!nodes.isEmpty()) {
				FSTreeNode head = nodes.get(0);
				channel = openChannel(head.peerNode.getPeer());
				if (channel != null) {
					IFileSystem service = getBlockingFileSystem(channel);
					if (service != null) {
						for (FSTreeNode node : nodes) {
							// Move each node.
							moveNode(service, node, dest);
						}
					}
					else {
						String message = NLS.bind(Messages.Operation_NoFileSystemError, head.peerNode.getPeerId());
						throw new TCFFileSystemException(IStatus.ERROR, message);
					}
				}
			}
		}
		catch (TCFException e) {
			throw new InvocationTargetException(e, e.getMessage());
		}
		finally {
			if (channel != null) Tcf.getChannelManager().closeChannel(channel);
			monitor.done();
		}
	}

	/**
	 * Move the file/folder to the destination folder using the specified file system service.
	 *
	 * @param monitor The monitor used to report the moving progress.
	 * @param service The file system service used to move the remote files.
	 * @param node The file/folder node to be moved.
	 * @param dest The destination folder.
	 * @throws TCFFileSystemException The exception thrown during moving.
	 * @throws InterruptedException Thrown when the operation is canceled.
	 */
	void moveNode(IFileSystem service, final FSTreeNode node, FSTreeNode dest) throws TCFFileSystemException, InterruptedException {
		if (monitor.isCanceled()) throw new InterruptedException();
		monitor.subTask(NLS.bind(Messages.OpMove_Moving, node.name));
		FSTreeNode copy = findChild(service, dest, node.name);
		if (copy == null || !copy.equals(node) && confirmReplace(node, confirmCallback)) {
			if (copy != null && copy.isDirectory() && node.isDirectory()) {
				List<FSTreeNode> children = getChildren(node, service);
				for (FSTreeNode child : children) {
					moveNode(service, child, copy);
				}
				removeFolder(node, service);
				monitor.worked(1);
			}
			else if (copy != null && copy.isFile() && node.isDirectory()) {
				String error = NLS.bind(Messages.OpMove_FileExistsError, copy.name);
				throw new TCFFileSystemException(IStatus.ERROR, error);
			}
			else if (copy != null && copy.isDirectory() && node.isFile()) {
				String error = NLS.bind(Messages.OpMove_FolderExistsError, copy.name);
				throw new TCFFileSystemException(IStatus.ERROR, error);
			}
			else {
				if (copy != null && copy.isFile() && node.isFile()) {
					removeFile(copy, service);
				}
				else if (copy == null) {
					copy = (FSTreeNode) node.clone();
				}
				addChild(service, dest, copy);
				String dst_path = copy.getLocation(true);
				String src_path = node.getLocation(true);
				final FSTreeNode copyNode = copy;
				final TCFFileSystemException[] errors = new TCFFileSystemException[1];
				service.rename(src_path, dst_path, new DoneRename() {
					@Override
					public void doneRename(IToken token, FileSystemException error) {
						if (error != null) {
							String message = NLS.bind(Messages.OpMove_CannotMove, node.name, error);
							errors[0] = new TCFFileSystemException(IStatus.ERROR, message, error);
						}
						else {
							cleanUpNode(node, copyNode);
						}
					}
				});
				if (errors[0] != null) {
					removeChild(service, dest, copy);
					throw errors[0];
				}
				monitor.worked(1);
			}
		}
	}

	/**
	 * Clean up the node after successful moving.
	 *
	 * @param node The node being moved.
	 * @param copyNode The target node that is moved to.
	 */
	void cleanUpNode(FSTreeNode node, FSTreeNode copyNode) {
		if (node.isFile()) {
			super.cleanUpFile(node);
		}
		else if (node.isDirectory()) {
			super.cleanUpFolder(node);
			List<FSTreeNode> children = node.getChildren();
			copyNode.addChidren(children);
			for (FSTreeNode child : children) {
				child.setParent(copyNode);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.filesystem.core.interfaces.IOperation#getName()
	 */
	@Override
    public String getName() {
	    return Messages.OpMove_MovingFile;
    }

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.filesystem.core.interfaces.IOperation#getTotalWork()
	 */
	@Override
    public int getTotalWork() {
	    return nodes == null ? IProgressMonitor.UNKNOWN : nodes.size();
    }
}

Back to the top