| package org.eclipse.epp.installer.internal.core.operations; |
| |
| import java.io.File; |
| import java.io.IOException; |
| import java.text.MessageFormat; |
| |
| import org.eclipse.core.runtime.IStatus; |
| import org.eclipse.core.runtime.Platform; |
| import org.eclipse.core.runtime.Status; |
| import org.eclipse.epp.installer.core.IActionConstants; |
| import org.eclipse.epp.installer.core.IErrorConstants; |
| import org.eclipse.epp.installer.core.model.Context; |
| import org.eclipse.epp.installer.core.model.InstallOperation; |
| import org.eclipse.epp.installer.internal.core.InstallLog; |
| import org.eclipse.epp.installer.internal.core.InstallLogEntry; |
| |
| /** |
| * Create a file if it does not already exist. If the file already exists, then ask the |
| * user for permission to overwrite the file but take no additional action. This operation |
| * is used as a prerequesite for other operations that actually write content to the file. |
| */ |
| public class CreateFileOperation extends InstallOperation { |
| |
| private File file; |
| private boolean doLogOperation; |
| |
| /** |
| * Constructor for CreateFileOperation. |
| * @param directory File |
| */ |
| public CreateFileOperation(File file) { |
| this(file, true); |
| } |
| |
| public CreateFileOperation(File file, boolean doLogOperation) { |
| this.file = file; |
| this.doLogOperation = doLogOperation; |
| } |
| |
| |
| /** |
| * Method getErrorStatus. |
| * @return IStatus |
| */ |
| private IStatus getErrorStatus() { |
| String message = MessageFormat.format( |
| "Can not create file: {0}", |
| new Object[] { file }); |
| return new Status(IStatus.ERROR, Context.PLUGIN_ID, |
| IErrorConstants.ERROR_CANNOT_CREATE_FILE, message, |
| null); |
| } |
| |
| |
| /** |
| * Method getErrorStatus. |
| * @return IStatus |
| */ |
| private IStatus getWarningStatus() { |
| String message = MessageFormat.format( |
| "Can not create file: {0}", |
| new Object[] { file }); |
| return new Status(IStatus.WARNING, Context.PLUGIN_ID, |
| IErrorConstants.ERROR_CANNOT_CREATE_FILE, message, |
| null); |
| } |
| /** |
| * Create empty file if it does not exists. |
| * |
| * @param installer |
| * Context |
| * @return IStatus |
| */ |
| protected IStatus run(Context installer) { |
| |
| if (file.exists()) { |
| if (!installer.confirm(IActionConstants.OVERWRITE_FILE, |
| "Overwrite file {0}?", new Object[] { file })) { |
| return getErrorStatus(); |
| } |
| |
| // If file already exists... then we are done |
| return Status.OK_STATUS; |
| } |
| |
| if (Platform.getOS()==Platform.OS_WIN32) |
| if (file.getAbsolutePath().length()>260) |
| { |
| |
| |
| if(installer.confirm(IActionConstants.CREATE_FILE,"The absolute path of file\n"+"{0}\nis more than 260 characters.\nDo you want to skip it?",new Object[]{file})) |
| return new Status(IStatus.WARNING, Context.PLUGIN_ID, |
| IErrorConstants.ERROR_USER_SKIPPED,MessageFormat.format("Skipped file {0}.", |
| new Object[] { file }) , null); |
| |
| } |
| |
| |
| try { |
| if (file.createNewFile()) { |
| if (doLogOperation) { |
| installer.getInstallLog().addEntry(new InstallLogEntry( |
| InstallLog.FILE_CREATED_ENTRY, file.getAbsolutePath())); |
| } |
| }else |
| { |
| return this.getWarningStatus(); |
| |
| |
| } |
| return Status.OK_STATUS; |
| } catch (IOException e) { |
| return getErrorStatus(); |
| } |
| } |
| |
| /** |
| * Method hashCode. |
| * |
| * @return int |
| */ |
| public int hashCode() { |
| return file.hashCode(); |
| } |
| |
| /** |
| * Method equals. |
| * @param o Object |
| * @return boolean |
| */ |
| public boolean equals(Object o) { |
| if (o instanceof CreateFileOperation) { |
| CreateFileOperation cfo = (CreateFileOperation) o; |
| return file.equals(cfo.file); |
| } |
| return false; |
| } |
| |
| /** |
| * Method toString. |
| * @return String |
| */ |
| public String toString() { |
| return MessageFormat.format("Creating file {0}.", |
| new Object[] { file.getAbsolutePath() }); |
| } |
| |
| } |