copyfilesandfoldersoperation.java
来自「Python Development Environment (Python I」· Java 代码 · 共 1,653 行 · 第 1/5 页
JAVA
1,653 行
for (int i = 0; i < copyResources.length; i++) {
IResource source = copyResources[i];
IPath newDestinationPath = destinationPath.append(source.getName());
IResource newDestination = workspaceRoot
.findMember(newDestinationPath);
IFolder folder;
if (newDestination == null) {
continue;
}
folder = getFolder(newDestination);
if (folder != null) {
IFolder sourceFolder = getFolder(source);
if (sourceFolder != null) {
try {
collectExistingReadonlyFiles(newDestinationPath,
sourceFolder.members(), existing);
} catch (CoreException exception) {
recordError(exception);
}
}
} else {
IFile file = getFile(newDestination);
if (file != null) {
if (file.isReadOnly()) {
existing.add(file);
}
if (getValidateConflictSource()) {
IFile sourceFile = getFile(source);
if (sourceFile != null) {
existing.add(sourceFile);
}
}
}
}
}
}
/**
* Copies the resources to the given destination. This method is called
* recursively to merge folders during folder copy.
*
* @param resources
* the resources to copy
* @param destination
* destination to which resources will be copied
* @param subMonitor
* a progress monitor for showing progress and for cancelation
*/
protected void copy(IResource[] resources, IPath destination,
IProgressMonitor subMonitor) throws CoreException {
subMonitor
.beginTask(
IDEWorkbenchMessages.CopyFilesAndFoldersOperation_CopyResourcesTask,
resources.length);
for (int i = 0; i < resources.length; i++) {
IResource source = resources[i];
IPath destinationPath = destination.append(source.getName());
IWorkspace workspace = source.getWorkspace();
IWorkspaceRoot workspaceRoot = workspace.getRoot();
IResource existing = workspaceRoot.findMember(destinationPath);
if (source.getType() == IResource.FOLDER && existing != null) {
// the resource is a folder and it exists in the destination,
// copy the
// children of the folder.
if (homogenousResources(source, existing)) {
IResource[] children = ((IContainer) source).members();
copy(children, destinationPath, new SubProgressMonitor(
subMonitor, 1));
} else {
// delete the destination folder, copying a linked folder
// over an unlinked one or vice versa. Fixes bug 28772.
delete(existing, new SubProgressMonitor(subMonitor, 0));
source.copy(destinationPath, IResource.SHALLOW,
new SubProgressMonitor(subMonitor, 1));
}
} else {
if (existing != null) {
if (homogenousResources(source, existing)) {
copyExisting(source, existing, new SubProgressMonitor(
subMonitor, 1));
} else {
// Copying a linked resource over unlinked or vice
// versa.
// Can't use setContents here. Fixes bug 28772.
delete(existing, new SubProgressMonitor(subMonitor, 0));
source.copy(destinationPath, IResource.SHALLOW,
new SubProgressMonitor(subMonitor, 1));
}
} else {
source.copy(destinationPath, IResource.SHALLOW,
new SubProgressMonitor(subMonitor, 1));
}
if (subMonitor.isCanceled()) {
throw new OperationCanceledException();
}
}
}
}
/**
* Sets the content of the existing file to the source file content.
*
* @param source
* source file to copy
* @param existing
* existing file to set the source content in
* @param subMonitor
* a progress monitor for showing progress and for cancelation
* @throws CoreException
* setContents failed
*/
private void copyExisting(IResource source, IResource existing,
IProgressMonitor subMonitor) throws CoreException {
IFile existingFile = getFile(existing);
if (existingFile != null) {
IFile sourceFile = getFile(source);
if (sourceFile != null) {
existingFile.setContents(sourceFile.getContents(),
IResource.KEEP_HISTORY, new SubProgressMonitor(
subMonitor, 0));
}
}
}
/**
* Copies the given resources to the destination. The current Thread is
* halted while the resources are copied using a WorkspaceModifyOperation.
* This method should be called from the UIThread.
*
* @param resources
* the resources to copy
* @param destination
* destination to which resources will be copied
* @return IResource[] the resulting {@link IResource}[]
* @see WorkspaceModifyOperation
* @see Display#getThread()
* @see Thread#currentThread()
*/
public IResource[] copyResources(final IResource[] resources,
IContainer destination) {
return copyResources(resources, destination, true, null);
}
/**
* Copies the given resources to the destination in the current Thread
* without forking a new Thread or blocking using a
* WorkspaceModifyOperation. It recommended that this method only be called
* from a {@link WorkspaceJob} to avoid possible deadlock.
*
* @param resources
* the resources to copy
* @param destination
* destination to which resources will be copied
* @param monitor
* the monitor that information will be sent to.
* @return IResource[] the resulting {@link IResource}[]
* @see WorkspaceModifyOperation
* @see WorkspaceJob
* @since 3.2
*/
public IResource[] copyResourcesInCurrentThread(
final IResource[] resources, IContainer destination,
IProgressMonitor monitor) {
return copyResources(resources, destination, false, monitor);
}
/**
* Copies the given resources to the destination.
*
* @param resources
* the resources to copy
* @param destination
* destination to which resources will be copied
* @return IResource[] the resulting {@link IResource}[]
*/
private IResource[] copyResources(final IResource[] resources,
IContainer destination, boolean fork, IProgressMonitor monitor) {
final IPath destinationPath = destination.getFullPath();
final IResource[][] copiedResources = new IResource[1][0];
// test resources for existence separate from validate API.
// Validate is performance critical and resource exists
// check is potentially slow. Fixes bugs 16129/28602.
IStatus resourceStatus = checkExist(resources);
if (resourceStatus.getSeverity() != IStatus.OK) {
displayError(resourceStatus);
return copiedResources[0];
}
String errorMsg = validateDestination(destination, resources);
if (errorMsg != null) {
displayError(errorMsg);
return copiedResources[0];
}
if (!validateOperation(resources, destinationPath)) {
return copiedResources[0];
}
if (fork) {
WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
public void execute(IProgressMonitor monitor) {
copyResources(resources, destinationPath, copiedResources,
monitor);
}
};
try {
PlatformUI.getWorkbench().getProgressService().run(true, true,
op);
} catch (InterruptedException e) {
return copiedResources[0];
} catch (InvocationTargetException e) {
display(e);
}
} else {
copyResources(resources, destinationPath, copiedResources, monitor);
}
// If errors occurred, open an Error dialog
if (errorStatus != null) {
displayError(errorStatus);
errorStatus = null;
}
return copiedResources[0];
}
/**
* Validates the copy or move operation.
*
* @param resources
* the resources being copied or moved
* @param destinationPath
* the destination of the copy or move
* @return whether the operation should proceed
* @since 3.2
*/
private boolean validateOperation(IResource[] resources,
IPath destinationPath) {
IResourceChangeDescriptionFactory factory = ResourceChangeValidator
.getValidator().createDeltaFactory();
for (int i = 0; i < resources.length; i++) {
IResource resource = resources[i];
if (isMove()) {
factory.move(resource, destinationPath.append(resource
.getName()));
} else {
factory.copy(resource, destinationPath.append(resource
.getName()));
}
}
String title;
String message;
if (isMove()) {
title = IDEWorkbenchMessages.CopyFilesAndFoldersOperation_confirmMove;
message = IDEWorkbenchMessages.CopyFilesAndFoldersOperation_warningMove;
} else {
title = IDEWorkbenchMessages.CopyFilesAndFoldersOperation_confirmCopy;
message = IDEWorkbenchMessages.CopyFilesAndFoldersOperation_warningCopy;
}
return IDE.promptToConfirm(messageShell, title, message, factory
.getDelta(), modelProviderIds, true /* syncExec */);
}
/**
* Return whether the operation is a move or a copy
*
* @return whether the operation is a move or a copy
* @since 3.2
*/
protected boolean isMove() {
return false;
}
private void display(InvocationTargetException e) {
// CoreExceptions are collected above, but unexpected runtime
// exceptions and errors may still occur.
IDEWorkbenchPlugin.getDefault().getLog().log(
StatusUtil.newStatus(IStatus.ERROR, MessageFormat.format(
"Exception in {0}.performCopy(): {1}", //$NON-NLS-1$
new Object[] { getClass().getName(),
e.getTargetException() }), null));
displayError(NLS
.bind(
IDEWorkbenchMessages.CopyFilesAndFoldersOperation_internalError,
e.getTargetException().getMessage()));
}
/**
* Copies the given URIS and folders to the destination. The current Thread
* is halted while the resources are copied using a
* WorkspaceModifyOperation. This method should be called from the UI
* Thread.
*
* @param uris
* the URIs to copy
* @param destination
* destination to which files will be copied
* @see WorkspaceModifyOperation
* @see Display#getThread()
* @see Thread#currentThread()
* @since 3.2
*/
public void copyFiles(URI[] uris, IContainer destination) {
IFileStore[] stores = buildFileStores(uris);
if (stores == null) {
return;
}
copyFileStores(destination, stores, true, null);
}
/**
* Copies the given files and folders to the destination without forking a
* new Thread or blocking using a WorkspaceModifyOperation. It is
* recommended that this method only be called from a {@link WorkspaceJob}
* to avoid possible deadlock.
*
* @param uris
* the URIs to copy
* @param destination
* destination to which URIS will be copied
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?