diff --git a/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/core/refactoring/resource/CreateFileChange.java b/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/core/refactoring/resource/CreateFileChange.java new file mode 100644 index 0000000..a5b2f02 --- /dev/null +++ b/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/core/refactoring/resource/CreateFileChange.java @@ -0,0 +1,164 @@ +/******************************************************************************* + * Copyright (c) 2013 Zend Technologies Ltd 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: + * Zend Technologies Ltd - initial API and implementation + *******************************************************************************/ +package org.eclipse.ltk.core.refactoring.resource; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; + +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.filesystem.IFileInfo; + +import org.eclipse.core.runtime.Assert; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.SubProgressMonitor; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.ResourcesPlugin; + +import org.eclipse.ltk.core.refactoring.Change; +import org.eclipse.ltk.core.refactoring.CompositeChange; +import org.eclipse.ltk.core.refactoring.RefactoringCore; +import org.eclipse.ltk.core.refactoring.RefactoringStatus; +import org.eclipse.ltk.internal.core.refactoring.BasicElementLabels; +import org.eclipse.ltk.internal.core.refactoring.Messages; +import org.eclipse.ltk.internal.core.refactoring.RefactoringCoreMessages; + +/** + * {@link Change} that creates a file. + * + * @since 3.7 + */ +public class CreateFileChange extends ResourceChange { + + private IPath fPath; + + private InputStream fInputStream; + + private boolean fOverwrite; + + /** + * Create new file. + * + *

+ * If there is an existing file on the specified path then it will be replaced with the new + * file. Calling this constructor is the same as calling + * CreateFileChange(path, inputStream, true). + *

+ * + * @param path path where the new file will be created + * @param inputStream content for the new file + */ + public CreateFileChange(IPath path, InputStream inputStream) { + this(path, inputStream, true); + } + + /** + * Create new file. + * + * @param path path where the new file will be created + * @param inputStream content for the new file + * @param overwrite whether an existing file on the specified path should be replaced, or the + * change should fail + */ + + public CreateFileChange(IPath path, InputStream inputStream, boolean overwrite) { + Assert.isNotNull(path, "path"); //$NON-NLS-1$ + Assert.isNotNull(inputStream, "inputStream"); //$NON-NLS-1$ + fPath= path; + fInputStream= inputStream; + fOverwrite= overwrite; + } + + /* (non-Javadoc) + * @see org.eclipse.ltk.core.refactoring.resource.ResourceChange#getModifiedResource() + */ + protected IResource getModifiedResource() { + return ResourcesPlugin.getWorkspace().getRoot().getFile(fPath); + } + + /* (non-Javadoc) + * @see org.eclipse.ltk.core.refactoring.Change#getName() + */ + public String getName() { + String message= (getModifiedResource().exists()) ? RefactoringCoreMessages.CreateFileChange_update_file + : RefactoringCoreMessages.CreateFileChange_create_file; + return Messages.format(message, BasicElementLabels.getPathLabel(fPath, false)); + } + + /* + * (non-Javadoc) + * @see org.eclipse.ltk.core.refactoring.resource.ResourceChange#isValid(org.eclipse.core.runtime.IProgressMonitor) + */ + public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException { + RefactoringStatus result= new RefactoringStatus(); + IFile file= (IFile)getModifiedResource(); + + URI location= file.getLocationURI(); + if (location == null) { + result.addFatalError(Messages.format( + RefactoringCoreMessages.CreateFileChange_error_unknownLocation, + BasicElementLabels.getPathLabel(file.getFullPath(), false))); + return result; + } + + if (!fOverwrite) { + IFileInfo jFile= EFS.getStore(location).fetchInfo(); + if (jFile.exists()) { + result.addFatalError(Messages.format( + RefactoringCoreMessages.CreateFileChange_error_exists, + file.getFullPath().toString())); + return result; + } + } + + return result; + } + + /* (non-Javadoc) + * @see org.eclipse.ltk.core.refactoring.Change#perform(org.eclipse.core.runtime.IProgressMonitor) + */ + public Change perform(IProgressMonitor pm) throws CoreException { + if (pm == null) + pm= new NullProgressMonitor(); + + try { + pm.beginTask(RefactoringCoreMessages.CreateFileChange_creating_resource, 2); + + IFile file= (IFile)getModifiedResource(); + if (file.exists()) { + CompositeChange composite= new CompositeChange(getName()); + composite.add(new DeleteResourceChange(fPath, true)); + composite.add(new CreateFileChange(fPath, fInputStream)); + return composite.perform(new SubProgressMonitor(pm, 1)); + } else { + file.create(fInputStream, false, new SubProgressMonitor(pm, 1)); + return new DeleteResourceChange(file.getFullPath(), true); + } + } finally { + try { + fInputStream.close(); + } catch (IOException e) { + throw new CoreException(new Status(IStatus.ERROR, + RefactoringCore.ID_PLUGIN, e.getMessage(), e)); + } finally { + pm.done(); + } + } + } + +} diff --git a/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/internal/core/refactoring/RefactoringCoreMessages.java b/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/internal/core/refactoring/RefactoringCoreMessages.java index d733c3b..f218ae2 100644 --- a/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/internal/core/refactoring/RefactoringCoreMessages.java +++ b/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/internal/core/refactoring/RefactoringCoreMessages.java @@ -266,6 +266,16 @@ public static String ValidateEditChecker_failed; + public static String CreateFileChange_creating_resource; + + public static String CreateFileChange_create_file; + + public static String CreateFileChange_update_file; + + public static String CreateFileChange_error_exists; + + public static String CreateFileChange_error_unknownLocation; + static { NLS.initializeMessages(BUNDLE_NAME, RefactoringCoreMessages.class); } diff --git a/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/internal/core/refactoring/RefactoringCoreMessages.properties b/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/internal/core/refactoring/RefactoringCoreMessages.properties index 7be94b0..48c1357 100644 --- a/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/internal/core/refactoring/RefactoringCoreMessages.properties +++ b/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/internal/core/refactoring/RefactoringCoreMessages.properties @@ -156,3 +156,9 @@ MoveResourceProcessor_error_destination_not_exists=Destination does not exist MoveResourceProcessor_error_invalid_destination=Invalid parent MoveResourceProcessor_processor_name=Move Resources + +CreateFileChange_creating_resource=Creating file... +CreateFileChange_create_file=Create file ''{0}'' +CreateFileChange_update_file=Update file ''{0}'' +CreateFileChange_error_exists=File ''{0}'' already exists +CreateFileChange_error_unknownLocation=The location for file ''{0}'' is unknown