📄 pyresourcedropadapterassistant.java
字号:
public IStatus handlePluginTransferDrop(IStructuredSelection aDragSelection, Object aDropTarget) {
aDropTarget = getActual(aDropTarget);
IContainer target = getActualTarget((IResource) aDropTarget);
IResource[] resources = getSelectedResources(aDragSelection);
MoveFilesAndFoldersOperation operation = new MoveFilesAndFoldersOperation(getShell());
operation.copyResources(resources, target);
if (target != null && target.isAccessible()) {
try {
target.refreshLocal(IResource.DEPTH_ONE, null);
} catch (CoreException e) {
}
}
return Status.OK_STATUS;
}
/**
* Returns the actual target of the drop, given the resource under the
* mouse. If the mouse target is a file, then the drop actually occurs in
* its parent. If the drop location is before or after the mouse target and
* feedback is enabled, the target is also the parent.
*/
private IContainer getActualTarget(IResource mouseTarget) {
/* if cursor is on a file, return the parent */
if (mouseTarget.getType() == IResource.FILE) {
return mouseTarget.getParent();
}
/* otherwise the mouseTarget is the real target */
return (IContainer) mouseTarget;
}
/**
* Returns the resource selection from the LocalSelectionTransfer.
*
* @return the resource selection from the LocalSelectionTransfer
*/
private IResource[] getSelectedResources() {
ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();
if (selection instanceof IStructuredSelection) {
return getSelectedResources((IStructuredSelection) selection);
}
return NO_RESOURCES;
}
/**
* Returns the resource selection from the LocalSelectionTransfer.
*
* @return the resource selection from the LocalSelectionTransfer
*/
private IResource[] getSelectedResources(IStructuredSelection selection) {
ArrayList selectedResources = new ArrayList();
for (Iterator i = selection.iterator(); i.hasNext();) {
Object o = i.next();
if (o instanceof IResource) {
selectedResources.add(o);
} else if (o instanceof IAdaptable) {
IAdaptable a = (IAdaptable) o;
IResource r = (IResource) a.getAdapter(IResource.class);
if (r != null) {
selectedResources.add(r);
}
}
}
return (IResource[]) selectedResources.toArray(new IResource[selectedResources.size()]);
}
/**
* Performs a resource copy
*/
private IStatus performResourceCopy(CommonDropAdapter dropAdapter, Shell shell, IResource[] sources) {
MultiStatus problems = new MultiStatus(PlatformUI.PLUGIN_ID, 1, WorkbenchNavigatorMessages.DropAdapter_problemsMoving, null);
mergeStatus(problems, validateTarget(getCurrentTarget(dropAdapter), dropAdapter.getCurrentTransfer(), dropAdapter
.getCurrentOperation()));
IContainer target = getActualTarget((IResource) getCurrentTarget(dropAdapter));
CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation(shell);
operation.copyResources(sources, target);
return problems;
}
/**
* Performs a resource move
*/
private IStatus performResourceMove(CommonDropAdapter dropAdapter, IResource[] sources) {
MultiStatus problems = new MultiStatus(PlatformUI.PLUGIN_ID, 1, WorkbenchNavigatorMessages.DropAdapter_problemsMoving, null);
mergeStatus(problems, validateTarget(getCurrentTarget(dropAdapter), dropAdapter.getCurrentTransfer(), dropAdapter
.getCurrentOperation()));
IContainer target = getActualTarget((IResource) getCurrentTarget(dropAdapter));
ReadOnlyStateChecker checker = new ReadOnlyStateChecker(getShell(), WorkbenchNavigatorMessages.MoveResourceAction_title,
WorkbenchNavigatorMessages.MoveResourceAction_checkMoveMessage);
sources = checker.checkReadOnlyResources(sources);
MoveFilesAndFoldersOperation operation = new MoveFilesAndFoldersOperation(getShell());
operation.copyResources(sources, target);
return problems;
}
private Object getCurrentTarget(CommonDropAdapter dropAdapter) {
return getActual(dropAdapter.getCurrentTarget());
}
/**
* Performs a drop using the FileTransfer transfer type.
*/
private IStatus performFileDrop(CommonDropAdapter anAdapter, Object data) {
data = getActual(data);
MultiStatus problems = new MultiStatus(PlatformUI.PLUGIN_ID, 0, WorkbenchNavigatorMessages.DropAdapter_problemImporting, null);
mergeStatus(problems, validateTarget(getCurrentTarget(anAdapter), anAdapter.getCurrentTransfer(), anAdapter.getCurrentOperation()));
final IContainer target = getActualTarget((IResource) getCurrentTarget(anAdapter));
final String[] names = (String[]) data;
// Run the import operation asynchronously.
// Otherwise the drag source (e.g., Windows Explorer) will be blocked
// while the operation executes. Fixes bug 16478.
Display.getCurrent().asyncExec(new Runnable() {
public void run() {
getShell().forceActive();
CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation(getShell());
operation.copyFiles(names, target);
}
});
return problems;
}
/**
* Ensures that the drop target meets certain criteria
*/
private IStatus validateTarget(Object target, TransferData transferType, int dropOperation) {
target = getActual(target);
if (!(target instanceof IResource)) {
return WorkbenchNavigatorPlugin.createInfoStatus(WorkbenchNavigatorMessages.DropAdapter_targetMustBeResource);
}
IResource resource = (IResource) target;
if (!resource.isAccessible()) {
return WorkbenchNavigatorPlugin.createErrorStatus(WorkbenchNavigatorMessages.DropAdapter_canNotDropIntoClosedProject);
}
IContainer destination = getActualTarget(resource);
if (destination.getType() == IResource.ROOT) {
return WorkbenchNavigatorPlugin.createErrorStatus(WorkbenchNavigatorMessages.DropAdapter_resourcesCanNotBeSiblings);
}
String message = null;
// drag within Eclipse?
if (LocalSelectionTransfer.getTransfer().isSupportedType(transferType)) {
IResource[] selectedResources = getSelectedResources();
if (selectedResources.length == 0) {
message = WorkbenchNavigatorMessages.DropAdapter_dropOperationErrorOther;
} else {
CopyFilesAndFoldersOperation operation;
if (dropOperation == DND.DROP_COPY) {
operation = new CopyFilesAndFoldersOperation(getShell());
} else {
operation = new MoveFilesAndFoldersOperation(getShell());
}
message = operation.validateDestination(destination, selectedResources);
}
} // file import?
else if (FileTransfer.getInstance().isSupportedType(transferType)) {
String[] sourceNames = (String[]) FileTransfer.getInstance().nativeToJava(transferType);
if (sourceNames == null) {
// source names will be null on Linux. Use empty names to do
// destination validation.
// Fixes bug 29778
sourceNames = new String[0];
}
CopyFilesAndFoldersOperation copyOperation = new CopyFilesAndFoldersOperation(getShell());
message = copyOperation.validateImportDestination(destination, sourceNames);
}
if (message != null) {
return WorkbenchNavigatorPlugin.createErrorStatus(message);
}
return Status.OK_STATUS;
}
/**
* Adds the given status to the list of problems. Discards OK statuses. If
* the status is a multi-status, only its children are added.
*/
private void mergeStatus(MultiStatus status, IStatus toMerge) {
if (!toMerge.isOK()) {
status.merge(toMerge);
}
}
/**
* Opens an error dialog if necessary. Takes care of complex rules necessary
* for making the error dialog look nice.
*/
private void openError(IStatus status) {
if (status == null) {
return;
}
String genericTitle = WorkbenchNavigatorMessages.DropAdapter_title;
int codes = IStatus.ERROR | IStatus.WARNING;
// simple case: one error, not a multistatus
if (!status.isMultiStatus()) {
ErrorDialog.openError(getShell(), genericTitle, null, status, codes);
return;
}
// one error, single child of multistatus
IStatus[] children = status.getChildren();
if (children.length == 1) {
ErrorDialog.openError(getShell(), status.getMessage(), null, children[0], codes);
return;
}
// several problems
ErrorDialog.openError(getShell(), genericTitle, null, status, codes);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -