📄 copyfilesandfoldersoperation.java
字号:
package org.python.pydev.navigator.actions.copied;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileInfo;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.resources.WorkspaceJob;
import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory;
import org.eclipse.core.resources.mapping.ResourceChangeValidator;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.eclipse.ui.dialogs.ContainerGenerator;
import org.eclipse.ui.dialogs.IOverwriteQuery;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
import org.eclipse.ui.internal.ide.StatusUtil;
import org.eclipse.ui.internal.ide.dialogs.IDEResourceInfoUtils;
import org.eclipse.ui.wizards.datatransfer.FileStoreStructureProvider;
import org.eclipse.ui.wizards.datatransfer.ImportOperation;
/**
* Perform the copy of file and folder resources from the clipboard when paste
* action is invoked.
* <p>
* This class may be instantiated; it is not intended to be subclassed.
* </p>
*/
public class CopyFilesAndFoldersOperation {
/**
* Status containing the errors detected when running the operation or
* <code>null</code> if no errors detected.
*/
private MultiStatus errorStatus;
/**
* The parent shell used to show any dialogs.
*/
private Shell messageShell;
/**
* Whether or not the copy has been canceled by the user.
*/
private boolean canceled = false;
/**
* Overwrite all flag.
*/
private boolean alwaysOverwrite = false;
private String[] modelProviderIds;
/**
* Returns a new name for a copy of the resource at the given path in the
* given workspace. This name is determined automatically.
*
* @param originalName
* the full path of the resource
* @param workspace
* the workspace
* @return the new full path for the copy
*/
static IPath getAutoNewNameFor(IPath originalName, IWorkspace workspace) {
int counter = 1;
String resourceName = originalName.lastSegment();
IPath leadupSegment = originalName.removeLastSegments(1);
while (true) {
String nameSegment;
if (counter > 1) {
nameSegment = NLS
.bind(
IDEWorkbenchMessages.CopyFilesAndFoldersOperation_copyNameTwoArgs,
new Integer(counter), resourceName);
} else {
nameSegment = NLS
.bind(
IDEWorkbenchMessages.CopyFilesAndFoldersOperation_copyNameOneArg,
resourceName);
}
IPath pathToTry = leadupSegment.append(nameSegment);
if (!workspace.getRoot().exists(pathToTry)) {
return pathToTry;
}
counter++;
}
}
/**
* Creates a new operation initialized with a shell.
*
* @param shell
* parent shell for error dialogs
*/
public CopyFilesAndFoldersOperation(Shell shell) {
messageShell = shell;
}
/**
* Returns whether this operation is able to perform on-the-fly
* auto-renaming of resources with name collisions.
*
* @return <code>true</code> if auto-rename is supported, and
* <code>false</code> otherwise
*/
protected boolean canPerformAutoRename() {
return true;
}
/**
* Returns the message for querying deep copy/move of a linked resource.
*
* @param source
* resource the query is made for
* @return the deep query message
*/
protected String getDeepCheckQuestion(IResource source) {
return NLS
.bind(
IDEWorkbenchMessages.CopyFilesAndFoldersOperation_deepCopyQuestion,
source.getFullPath().makeRelative());
}
/**
* Checks whether the infos exist.
*
* @param stores
* the file infos to test
* @return Multi status with one error message for each missing file.
*/
IStatus checkExist(IFileStore[] stores) {
MultiStatus multiStatus = new MultiStatus(PlatformUI.PLUGIN_ID,
IStatus.OK, getProblemsMessage(), null);
for (int i = 0; i < stores.length; i++) {
if (stores[i].fetchInfo().exists() == false) {
String message = NLS
.bind(
IDEWorkbenchMessages.CopyFilesAndFoldersOperation_resourceDeleted,
stores[i].getName());
IStatus status = new Status(IStatus.ERROR,
PlatformUI.PLUGIN_ID, IStatus.OK, message, null);
multiStatus.add(status);
}
}
return multiStatus;
}
/**
* Checks whether the resources with the given names exist.
*
* @param resources
* IResources to checl
* @return Multi status with one error message for each missing file.
*/
IStatus checkExist(IResource[] resources) {
MultiStatus multiStatus = new MultiStatus(PlatformUI.PLUGIN_ID,
IStatus.OK, getProblemsMessage(), null);
for (int i = 0; i < resources.length; i++) {
IResource resource = resources[i];
if (resource != null) {
URI location = resource.getLocationURI();
String message = null;
if (location != null) {
IFileInfo info = IDEResourceInfoUtils.getFileInfo(location);
if (info == null || info.exists() == false) {
if (resource.isLinked()) {
message = NLS
.bind(
IDEWorkbenchMessages.CopyFilesAndFoldersOperation_missingLinkTarget,
resource.getName());
} else {
message = NLS
.bind(
IDEWorkbenchMessages.CopyFilesAndFoldersOperation_resourceDeleted,
resource.getName());
}
}
}
if (message != null) {
IStatus status = new Status(IStatus.ERROR,
PlatformUI.PLUGIN_ID, IStatus.OK, message, null);
multiStatus.add(status);
}
}
}
return multiStatus;
}
/**
* Check if the user wishes to overwrite the supplied resource or all
* resources.
*
* @param source
* the source resource
* @param destination
* the resource to be overwritten
* @return one of IDialogConstants.YES_ID, IDialogConstants.YES_TO_ALL_ID,
* IDialogConstants.NO_ID, IDialogConstants.CANCEL_ID indicating
* whether the current resource or all resources can be overwritten,
* or if the operation should be canceled.
*/
private int checkOverwrite(final IResource source,
final IResource destination) {
final int[] result = new int[1];
// Dialogs need to be created and opened in the UI thread
Runnable query = new Runnable() {
public void run() {
String message;
int resultId[] = { IDialogConstants.YES_ID,
IDialogConstants.YES_TO_ALL_ID, IDialogConstants.NO_ID,
IDialogConstants.CANCEL_ID };
String labels[] = new String[] { IDialogConstants.YES_LABEL,
IDialogConstants.YES_TO_ALL_LABEL,
IDialogConstants.NO_LABEL,
IDialogConstants.CANCEL_LABEL };
if (destination.getType() == IResource.FOLDER) {
if (homogenousResources(source, destination)) {
message = NLS
.bind(
IDEWorkbenchMessages.CopyFilesAndFoldersOperation_overwriteMergeQuestion,
destination.getFullPath()
.makeRelative());
} else {
if (destination.isLinked()) {
message = NLS
.bind(
IDEWorkbenchMessages.CopyFilesAndFoldersOperation_overwriteNoMergeLinkQuestion,
destination.getFullPath()
.makeRelative());
} else {
message = NLS
.bind(
IDEWorkbenchMessages.CopyFilesAndFoldersOperation_overwriteNoMergeNoLinkQuestion,
destination.getFullPath()
.makeRelative());
}
resultId = new int[] { IDialogConstants.YES_ID,
IDialogConstants.NO_ID,
IDialogConstants.CANCEL_ID };
labels = new String[] { IDialogConstants.YES_LABEL,
IDialogConstants.NO_LABEL,
IDialogConstants.CANCEL_LABEL };
}
} else {
String[] bindings = new String[] {
IDEResourceInfoUtils.getLocationText(destination),
IDEResourceInfoUtils
.getDateStringValue(destination),
IDEResourceInfoUtils.getLocationText(source),
IDEResourceInfoUtils.getDateStringValue(source) };
message = NLS
.bind(
IDEWorkbenchMessages.CopyFilesAndFoldersOperation_overwriteWithDetailsQuestion,
bindings);
}
MessageDialog dialog = new MessageDialog(
messageShell,
IDEWorkbenchMessages.CopyFilesAndFoldersOperation_resourceExists,
null, message, MessageDialog.QUESTION, labels, 0);
dialog.open();
if (dialog.getReturnCode() == SWT.DEFAULT) {
// A window close returns SWT.DEFAULT, which has to be
// mapped to a cancel
result[0] = IDialogConstants.CANCEL_ID;
} else {
result[0] = resultId[dialog.getReturnCode()];
}
}
};
messageShell.getDisplay().syncExec(query);
return result[0];
}
/**
* Recursively collects existing files in the specified destination path.
*
* @param destinationPath
* destination path to check for existing files
* @param copyResources
* resources that may exist in the destination
* @param existing
* holds the collected existing files
*/
private void collectExistingReadonlyFiles(IPath destinationPath,
IResource[] copyResources, ArrayList existing) {
IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -