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

import static java.text.MessageFormat.format;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.osgi.util.NLS;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.te.tcf.core.concurrent.TCFOperationMonitor;
import org.eclipse.tcf.te.tcf.filesystem.core.interfaces.IResultOperation;
import org.eclipse.tcf.te.tcf.filesystem.core.interfaces.runtime.IFSTreeNode;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.FSTreeNode;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.utils.StatusHelper;
import org.eclipse.tcf.te.tcf.filesystem.core.nls.Messages;

/**
 * The base operation class for creating a file or a folder in the file system of Target
 * Explorer.
 */
public abstract class OpCreate extends AbstractOperation implements IResultOperation<IFSTreeNode> {
	final protected FSTreeNode fDestination;
	final protected String fName;
	protected FSTreeNode fResult;

	public OpCreate(FSTreeNode folder, String name) {
		Assert.isNotNull(folder);
		Assert.isNotNull(name);
		fDestination = folder;
		fName = name;
	}

	@Override
	protected IStatus doRun(IProgressMonitor monitor) {
		if (fDestination.getChildren() == null) {
			IStatus status = fDestination.operationRefresh(false).run(new SubProgressMonitor(monitor, 0));
			if (!status.isOK())
				return status;
		}
		FSTreeNode existing = fDestination.findChild(fName);
		if (existing != null) {
			return StatusHelper.createStatus(format(Messages.OpCreate_error_existingFile, existing.getLocation()), null);
		}

		final TCFOperationMonitor<FSTreeNode> result = new TCFOperationMonitor<FSTreeNode>();
		Protocol.invokeLater(new Runnable() {
			@Override
			public void run() {
				tcfCreate(fDestination, fName, result);
			}
		});

		IStatus status = result.waitDone(monitor);
		fResult = result.getValue();
		return status;
	}

	protected abstract void tcfCreate(FSTreeNode destination, String name, TCFOperationMonitor<FSTreeNode> result);

	@Override
    public String getName() {
	    return NLS.bind(Messages.OpCreate_TaskName, fName);
    }

	@Override
	public FSTreeNode getResult() {
		return fResult;
	}
}

Back to the top