📄 copyfilesandfoldersoperation.java
字号:
* @param originalName
* the full path of the resource
* @param workspace
* the workspace
* @return the new full path for the copy, or <code>null</code> if the
* resource should not be copied
*/
private IPath getNewNameFor(final IPath originalName,
final IWorkspace workspace) {
final IResource resource = workspace.getRoot().findMember(originalName);
final IPath prefix = resource.getFullPath().removeLastSegments(1);
final String returnValue[] = { "" }; //$NON-NLS-1$
messageShell.getDisplay().syncExec(new Runnable() {
public void run() {
IInputValidator validator = new IInputValidator() {
public String isValid(String string) {
if (resource.getName().equals(string)) {
return IDEWorkbenchMessages.CopyFilesAndFoldersOperation_nameMustBeDifferent;
}
IStatus status = workspace.validateName(string,
resource.getType());
if (!status.isOK()) {
return status.getMessage();
}
if (workspace.getRoot().exists(prefix.append(string))) {
return IDEWorkbenchMessages.CopyFilesAndFoldersOperation_nameExists;
}
return null;
}
};
InputDialog dialog = new InputDialog(
messageShell,
IDEWorkbenchMessages.CopyFilesAndFoldersOperation_inputDialogTitle,
NLS
.bind(
IDEWorkbenchMessages.CopyFilesAndFoldersOperation_inputDialogMessage,
resource.getName()), getAutoNewNameFor(
originalName, workspace).lastSegment()
.toString(), validator);
dialog.setBlockOnOpen(true);
dialog.open();
if (dialog.getReturnCode() == Window.CANCEL) {
returnValue[0] = null;
} else {
returnValue[0] = dialog.getValue();
}
}
});
if (returnValue[0] == null) {
throw new OperationCanceledException();
}
return prefix.append(returnValue[0]);
}
/**
* Returns the task title for this operation's progress dialog.
*
* @return the task title
*/
protected String getOperationTitle() {
return IDEWorkbenchMessages.CopyFilesAndFoldersOperation_operationTitle;
}
/**
* Returns the message for this operation's problems dialog.
*
* @return the problems message
*/
protected String getProblemsMessage() {
return IDEWorkbenchMessages.CopyFilesAndFoldersOperation_problemMessage;
}
/**
* Returns the title for this operation's problems dialog.
*
* @return the problems dialog title
*/
protected String getProblemsTitle() {
return IDEWorkbenchMessages.CopyFilesAndFoldersOperation_copyFailedTitle;
}
/**
* Returns whether the source file in a destination collision will be
* validateEdited together with the collision itself. Returns false. Should
* return true if the source file is to be deleted after the operation.
*
* @return boolean <code>true</code> if the source file in a destination
* collision should be validateEdited. <code>false</code> if only
* the destination should be validated.
*/
protected boolean getValidateConflictSource() {
return false;
}
/**
* Returns whether the given resources are either both linked or both
* unlinked.
*
* @param source
* source resource
* @param destination
* destination resource
* @return boolean <code>true</code> if both resources are either linked
* or unlinked. <code>false</code> otherwise.
*/
protected boolean homogenousResources(IResource source,
IResource destination) {
boolean isSourceLinked = source.isLinked();
boolean isDestinationLinked = destination.isLinked();
return (isSourceLinked && isDestinationLinked || isSourceLinked == false
&& isDestinationLinked == false);
}
/**
* Returns whether the given resource is accessible. Files and folders are
* always considered accessible and a project is accessible if it is open.
*
* @param resource
* the resource
* @return <code>true</code> if the resource is accessible, and
* <code>false</code> if it is not
*/
private boolean isAccessible(IResource resource) {
switch (resource.getType()) {
case IResource.FILE:
return true;
case IResource.FOLDER:
return true;
case IResource.PROJECT:
return ((IProject) resource).isOpen();
default:
return false;
}
}
/**
* Returns whether any of the given source resources are being recopied to
* their current container.
*
* @param sourceResources
* the source resources
* @param destination
* the destination container
* @return <code>true</code> if at least one of the given source
* resource's parent container is the same as the destination
*/
boolean isDestinationSameAsSource(IResource[] sourceResources,
IContainer destination) {
IPath destinationLocation = destination.getLocation();
for (int i = 0; i < sourceResources.length; i++) {
IResource sourceResource = sourceResources[i];
if (sourceResource.getParent().equals(destination)) {
return true;
} else if (destinationLocation != null) {
// do thorough check to catch linked resources. Fixes bug 29913.
IPath sourceLocation = sourceResource.getLocation();
IPath destinationResource = destinationLocation
.append(sourceResource.getName());
if (sourceLocation != null
&& sourceLocation.isPrefixOf(destinationResource)) {
return true;
}
}
}
return false;
}
/**
* Copies the given resources to the destination container with the given
* name.
* <p>
* Note: the destination container may need to be created prior to copying
* the resources.
* </p>
*
* @param resources
* the resources to copy
* @param destination
* the path of the destination container
* @param monitor
* a progress monitor for showing progress and for cancelation
* @return <code>true</code> if the copy operation completed without
* errors
*/
private boolean performCopy(IResource[] resources, IPath destination,
IProgressMonitor monitor) {
try {
ContainerGenerator generator = new ContainerGenerator(destination);
generator.generateContainer(new SubProgressMonitor(monitor, 10));
IProgressMonitor subMonitor = new SubProgressMonitor(monitor, 75);
copy(resources, destination, subMonitor);
} catch (CoreException e) {
recordError(e); // log error
return false;
} finally {
monitor.done();
}
return true;
}
/**
* Individually copies the given resources to the specified destination
* container checking for name collisions. If a collision is detected, it is
* saved with a new name.
* <p>
* Note: the destination container may need to be created prior to copying
* the resources.
* </p>
*
* @param resources
* the resources to copy
* @param destination
* the path of the destination container
* @return <code>true</code> if the copy operation completed without
* errors.
*/
private boolean performCopyWithAutoRename(IResource[] resources,
IPath destination, IProgressMonitor monitor) {
IWorkspace workspace = resources[0].getWorkspace();
try {
ContainerGenerator generator = new ContainerGenerator(destination);
generator.generateContainer(new SubProgressMonitor(monitor, 10));
IProgressMonitor subMonitor = new SubProgressMonitor(monitor, 75);
subMonitor.beginTask(getOperationTitle(), resources.length);
for (int i = 0; i < resources.length; i++) {
IResource source = resources[i];
IPath destinationPath = destination.append(source.getName());
if (workspace.getRoot().exists(destinationPath)) {
destinationPath = getNewNameFor(destinationPath, workspace);
}
if (destinationPath != null) {
try {
source.copy(destinationPath, IResource.SHALLOW,
new SubProgressMonitor(subMonitor, 0));
} catch (CoreException e) {
recordError(e); // log error
return false;
}
}
subMonitor.worked(1);
if (subMonitor.isCanceled()) {
throw new OperationCanceledException();
}
}
} catch (CoreException e) {
recordError(e); // log error
return false;
} finally {
monitor.done();
}
return true;
}
/**
* Performs an import of the given stores into the provided container.
* Returns a status indicating if the import was successful.
*
* @param stores
* stores that are to be imported
* @param target
* container to which the import will be done
* @param monitor
* a progress monitor for showing progress and for cancelation
*/
private void performFileImport(IFileStore[] stores, IContainer target,
IProgressMonitor monitor) {
IOverwriteQuery query = new IOverwriteQuery() {
public String queryOverwrite(String pathString) {
if (alwaysOverwrite) {
return ALL;
}
final String returnCode[] = { CANCEL };
final String msg = NLS
.bind(
IDEWorkbenchMessages.CopyFilesAndFoldersOperation_overwriteQuestion,
pathString);
final String[] options = { IDialogConstants.YES_LABEL,
IDialogConstants.YES_TO_ALL_LABEL,
IDialogConstants.NO_LABEL,
IDialogConstants.CANCEL_LABEL };
messageShell.getDisplay().syncExec(new Runnable() {
public void run() {
MessageDialog dialog = new MessageDialog(
messageShell,
IDEWorkbenchMessages.CopyFilesAndFoldersOperation_question,
null, msg, MessageDialog.QUESTION, options, 0);
dialog.open();
int returnVal = dialog.getReturnCode();
String[] returnCodes = { YES, ALL, NO, CANCEL };
returnCode[0] = returnVal == -1 ? CANCEL
: returnCodes[returnVal];
}
});
if (returnCode[0] == ALL) {
alwaysOverwrite = true;
} else if (returnCode[0] == CANCEL) {
canceled = true;
}
return returnCode[0];
}
};
ImportOperation op = new ImportOperation(target.getFullPath(),
stores[0].getParent(), FileStoreStructureProvider.INSTANCE,
query, Arrays.asList(stores));
op.setContext(messageShell);
op.setCreateContainerStructure(false);
try {
op.run(monitor);
} catch (InterruptedException e) {
return;
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof CoreException) {
displayError(((CoreException) e.getTargetException())
.getStatus());
} else {
display(e);
}
return;
}
// Special case since ImportOperation doesn't throw a CoreException on
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -