View | Details | Raw Unified | Return to bug 224959 | Differences between
and this patch

Collapse All | Expand All

(-)a/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/core/refactoring/resource/CreateFileChange.java (+164 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2013 Zend Technologies Ltd and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Zend Technologies Ltd - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.ltk.core.refactoring.resource;
12
13
import java.io.IOException;
14
import java.io.InputStream;
15
import java.net.URI;
16
17
import org.eclipse.core.filesystem.EFS;
18
import org.eclipse.core.filesystem.IFileInfo;
19
20
import org.eclipse.core.runtime.Assert;
21
import org.eclipse.core.runtime.CoreException;
22
import org.eclipse.core.runtime.IPath;
23
import org.eclipse.core.runtime.IProgressMonitor;
24
import org.eclipse.core.runtime.IStatus;
25
import org.eclipse.core.runtime.NullProgressMonitor;
26
import org.eclipse.core.runtime.Status;
27
import org.eclipse.core.runtime.SubProgressMonitor;
28
29
import org.eclipse.core.resources.IFile;
30
import org.eclipse.core.resources.IResource;
31
import org.eclipse.core.resources.ResourcesPlugin;
32
33
import org.eclipse.ltk.core.refactoring.Change;
34
import org.eclipse.ltk.core.refactoring.CompositeChange;
35
import org.eclipse.ltk.core.refactoring.RefactoringCore;
36
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
37
import org.eclipse.ltk.internal.core.refactoring.BasicElementLabels;
38
import org.eclipse.ltk.internal.core.refactoring.Messages;
39
import org.eclipse.ltk.internal.core.refactoring.RefactoringCoreMessages;
40
41
/**
42
 * {@link Change} that creates a file.
43
 * 
44
 * @since 3.7
45
 */
46
public class CreateFileChange extends ResourceChange {
47
48
	private IPath fPath;
49
50
	private InputStream fInputStream;
51
52
	private boolean fOverwrite;
53
54
	/**
55
	 * Create new file.
56
	 * 
57
	 * <p>
58
	 * If there is an existing file on the specified path then it will be replaced with the new
59
	 * file. Calling this constructor is the same as calling
60
	 * <code>CreateFileChange(path, inputStream, true)</code>.
61
	 * </p>
62
	 * 
63
	 * @param path path where the new file will be created
64
	 * @param inputStream content for the new file
65
	 */
66
	public CreateFileChange(IPath path, InputStream inputStream) {
67
		this(path, inputStream, true);
68
	}
69
70
	/**
71
	 * Create new file.
72
	 * 
73
	 * @param path path where the new file will be created
74
	 * @param inputStream content for the new file
75
	 * @param overwrite whether an existing file on the specified path should be replaced, or the
76
	 *            change should fail
77
	 */
78
79
	public CreateFileChange(IPath path, InputStream inputStream, boolean overwrite) {
80
		Assert.isNotNull(path, "path"); //$NON-NLS-1$
81
		Assert.isNotNull(inputStream, "inputStream"); //$NON-NLS-1$
82
		fPath= path;
83
		fInputStream= inputStream;
84
		fOverwrite= overwrite;
85
	}
86
87
	/* (non-Javadoc)
88
	 * @see org.eclipse.ltk.core.refactoring.resource.ResourceChange#getModifiedResource()
89
	 */
90
	protected IResource getModifiedResource() {
91
		return ResourcesPlugin.getWorkspace().getRoot().getFile(fPath);
92
	}
93
94
	/* (non-Javadoc)
95
	 * @see org.eclipse.ltk.core.refactoring.Change#getName()
96
	 */
97
	public String getName() {
98
		String message= (getModifiedResource().exists()) ? RefactoringCoreMessages.CreateFileChange_update_file
99
				: RefactoringCoreMessages.CreateFileChange_create_file;
100
		return Messages.format(message, BasicElementLabels.getPathLabel(fPath, false));
101
	}
102
103
	/*
104
	 * (non-Javadoc)
105
	 * @see org.eclipse.ltk.core.refactoring.resource.ResourceChange#isValid(org.eclipse.core.runtime.IProgressMonitor)
106
	 */
107
	public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
108
		RefactoringStatus result= new RefactoringStatus();
109
		IFile file= (IFile)getModifiedResource();
110
111
		URI location= file.getLocationURI();
112
		if (location == null) {
113
			result.addFatalError(Messages.format(
114
					RefactoringCoreMessages.CreateFileChange_error_unknownLocation,
115
					BasicElementLabels.getPathLabel(file.getFullPath(), false)));
116
			return result;
117
		}
118
119
		if (!fOverwrite) {
120
			IFileInfo jFile= EFS.getStore(location).fetchInfo();
121
			if (jFile.exists()) {
122
				result.addFatalError(Messages.format(
123
						RefactoringCoreMessages.CreateFileChange_error_exists,
124
						file.getFullPath().toString()));
125
				return result;
126
			}
127
		}
128
129
		return result;
130
	}
131
132
	/* (non-Javadoc)
133
	 * @see org.eclipse.ltk.core.refactoring.Change#perform(org.eclipse.core.runtime.IProgressMonitor)
134
	 */
135
	public Change perform(IProgressMonitor pm) throws CoreException {
136
		if (pm == null)
137
			pm= new NullProgressMonitor();
138
139
		try {
140
			pm.beginTask(RefactoringCoreMessages.CreateFileChange_creating_resource, 2);
141
142
			IFile file= (IFile)getModifiedResource();
143
			if (file.exists()) {
144
				CompositeChange composite= new CompositeChange(getName());
145
				composite.add(new DeleteResourceChange(fPath, true));
146
				composite.add(new CreateFileChange(fPath, fInputStream));
147
				return composite.perform(new SubProgressMonitor(pm, 1));
148
			} else {
149
				file.create(fInputStream, false, new SubProgressMonitor(pm, 1));
150
				return new DeleteResourceChange(file.getFullPath(), true);
151
			}
152
		} finally {
153
			try {
154
				fInputStream.close();
155
			} catch (IOException e) {
156
				throw new CoreException(new Status(IStatus.ERROR,
157
						RefactoringCore.ID_PLUGIN, e.getMessage(), e));
158
			} finally {
159
				pm.done();
160
			}
161
		}
162
	}
163
164
}
(-)a/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/internal/core/refactoring/RefactoringCoreMessages.java (+10 lines)
Lines 266-271 Link Here
266
266
267
	public static String ValidateEditChecker_failed;
267
	public static String ValidateEditChecker_failed;
268
268
269
	public static String CreateFileChange_creating_resource;
270
271
	public static String CreateFileChange_create_file;
272
273
	public static String CreateFileChange_update_file;
274
275
	public static String CreateFileChange_error_exists;
276
277
	public static String CreateFileChange_error_unknownLocation;
278
269
	static {
279
	static {
270
		NLS.initializeMessages(BUNDLE_NAME, RefactoringCoreMessages.class);
280
		NLS.initializeMessages(BUNDLE_NAME, RefactoringCoreMessages.class);
271
	}
281
	}
(-)a/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/internal/core/refactoring/RefactoringCoreMessages.properties (+6 lines)
Lines 156-158 Link Here
156
MoveResourceProcessor_error_destination_not_exists=Destination does not exist
156
MoveResourceProcessor_error_destination_not_exists=Destination does not exist
157
MoveResourceProcessor_error_invalid_destination=Invalid parent
157
MoveResourceProcessor_error_invalid_destination=Invalid parent
158
MoveResourceProcessor_processor_name=Move Resources
158
MoveResourceProcessor_processor_name=Move Resources
159
160
CreateFileChange_creating_resource=Creating file...
161
CreateFileChange_create_file=Create file ''{0}''
162
CreateFileChange_update_file=Update file ''{0}''
163
CreateFileChange_error_exists=File ''{0}'' already exists
164
CreateFileChange_error_unknownLocation=The location for file ''{0}'' is unknown

Return to bug 224959