Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f72c1b8b78f74be6e434b6db11fca59caceed936 (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
/*******************************************************************************
 * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik
 * Rapperswil, University of applied sciences 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:
 *     Institute for Software - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URI;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.osgi.util.NLS;

/**
 * A Change for creating a new file with the given name, content and encoding at the specified path.
 *
 * @author Emanuel Graf
 */
public class CreateFileChange extends Change {
	private String name;
	private final IPath path;
	private final String source;
	private final String encoding;

	public CreateFileChange(String name, IPath path, String source, String encoding) {
		super();
		this.name = name;
		this.path = path;
		this.source = source;
		this.encoding = encoding;
	}

	public CreateFileChange(IPath path, String source, String encoding) {
		this(null, path, source, encoding);
	}

	@Override
	public Object getModifiedElement() {
		return ResourcesPlugin.getWorkspace().getRoot().getFile(path);
	}

	@Override
	public String getName() {
		if (name == null) {
			return NLS.bind(Messages.CreateFileChange_CreateFile, path.toOSString());
		}
		return name;
	}

	@Override
	public void initializeValidationData(IProgressMonitor pm) {
	}

	@Override
	public RefactoringStatus isValid(IProgressMonitor pm)
			throws CoreException, OperationCanceledException {
		RefactoringStatus result= new RefactoringStatus();
		IFile file= ResourcesPlugin.getWorkspace().getRoot().getFile(path);

		URI location= file.getLocationURI();
		if (location == null) {
			result.addFatalError(NLS.bind(Messages.CreateFileChange_UnknownLoc, file.getFullPath().toString()));
			return result;
		}

		if (file.exists()) {
			result.addFatalError(NLS.bind(Messages.CreateFileChange_FileExists, file.getFullPath().toString()));
			return result;
		}
		return result;
	}

	@Override
	public Change perform(IProgressMonitor pm) throws CoreException {
		IFile file= ResourcesPlugin.getWorkspace().getRoot().getFile(path);
		InputStream is = new ByteArrayInputStream(source.getBytes());
		file.create(is, false, new SubProgressMonitor(pm, 1));
		if (encoding != null) {
			file.setCharset(encoding, new SubProgressMonitor(pm,1));
		}
		return new DeleteFileChange(file.getFullPath());
	}

	public String getSource() {
		return source;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return getName();
	}
}

Back to the top