001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.packageview;
011:
012: import org.eclipse.core.resources.IContainer;
013: import org.eclipse.core.resources.IResource;
014:
015: import org.eclipse.swt.dnd.DND;
016: import org.eclipse.swt.dnd.DropTargetEvent;
017: import org.eclipse.swt.dnd.FileTransfer;
018: import org.eclipse.swt.dnd.Transfer;
019: import org.eclipse.swt.dnd.TransferData;
020: import org.eclipse.swt.widgets.Display;
021: import org.eclipse.swt.widgets.Shell;
022:
023: import org.eclipse.jface.util.TransferDropTargetListener;
024: import org.eclipse.jface.viewers.StructuredViewer;
025:
026: import org.eclipse.ui.actions.CopyFilesAndFoldersOperation;
027:
028: import org.eclipse.jdt.core.IJavaElement;
029: import org.eclipse.jdt.core.IJavaProject;
030: import org.eclipse.jdt.core.IPackageFragment;
031: import org.eclipse.jdt.core.IPackageFragmentRoot;
032: import org.eclipse.jdt.core.JavaModelException;
033:
034: import org.eclipse.jdt.internal.corext.util.Resources;
035:
036: import org.eclipse.jdt.internal.ui.dnd.JdtViewerDropAdapter;
037: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
038:
039: /**
040: * Adapter to handle file drop from other applications.
041: */
042: public class FileTransferDropAdapter extends JdtViewerDropAdapter
043: implements TransferDropTargetListener {
044:
045: public FileTransferDropAdapter(StructuredViewer viewer) {
046: super (viewer);
047:
048: setScrollEnabled(true);
049: setExpandEnabled(true);
050: setFeedbackEnabled(false);
051: }
052:
053: //---- TransferDropTargetListener interface ---------------------------------------
054:
055: /**
056: * {@inheritDoc}
057: */
058: public Transfer getTransfer() {
059: return FileTransfer.getInstance();
060: }
061:
062: /**
063: * {@inheritDoc}
064: */
065: public boolean isEnabled(DropTargetEvent event) {
066: Object target = event.item != null ? event.item.getData()
067: : null;
068: if (target == null)
069: return false;
070: return target instanceof IJavaElement
071: || target instanceof IResource;
072: }
073:
074: //---- Actual DND -----------------------------------------------------------------
075:
076: /**
077: * {@inheritDoc}
078: */
079: public boolean validateDrop(Object target, int operation,
080: TransferData transferType) {
081: return determineOperation(target, operation, transferType,
082: DND.DROP_MOVE | DND.DROP_LINK | DND.DROP_COPY) != DND.DROP_NONE;
083: }
084:
085: /**
086: * {@inheritDoc}
087: */
088: protected int determineOperation(Object target, int operation,
089: TransferData transferType, int operations) {
090:
091: boolean isPackageFragment = target instanceof IPackageFragment;
092: boolean isJavaProject = target instanceof IJavaProject;
093: boolean isPackageFragmentRoot = target instanceof IPackageFragmentRoot;
094: boolean isContainer = target instanceof IContainer;
095:
096: if (!(isPackageFragment || isJavaProject
097: || isPackageFragmentRoot || isContainer))
098: return DND.DROP_NONE;
099:
100: if (isContainer) {
101: IContainer container = (IContainer) target;
102: if (container.isAccessible()
103: && !Resources.isReadOnly(container))
104: return DND.DROP_COPY;
105: } else {
106: IJavaElement element = (IJavaElement) target;
107: if (!element.isReadOnly())
108: return DND.DROP_COPY;
109: }
110:
111: return DND.DROP_NONE;
112: }
113:
114: /**
115: * {@inheritDoc}
116: */
117: public boolean performDrop(final Object data) {
118: try {
119: int operation = getCurrentOperation();
120:
121: if (data == null || !(data instanceof String[])
122: || operation != DND.DROP_COPY)
123: return false;
124:
125: final IContainer target = getActualTarget(getCurrentTarget());
126: if (target == null)
127: return false;
128:
129: // Run the import operation asynchronously.
130: // Otherwise the drag source (e.g., Windows Explorer) will be blocked
131: // while the operation executes. Fixes bug 35796.
132: Display.getCurrent().asyncExec(new Runnable() {
133: public void run() {
134: getShell().forceActive();
135: new CopyFilesAndFoldersOperation(getShell())
136: .copyFiles((String[]) data, target);
137: }
138: });
139:
140: return false;
141: } catch (JavaModelException e) {
142: String title = PackagesMessages.DropAdapter_errorTitle;
143: String message = PackagesMessages.DropAdapter_errorMessage;
144: ExceptionHandler.handle(e, getShell(), title, message);
145: }
146:
147: return true;
148: }
149:
150: private IContainer getActualTarget(Object dropTarget)
151: throws JavaModelException {
152: if (dropTarget instanceof IContainer)
153: return (IContainer) dropTarget;
154: else if (dropTarget instanceof IJavaElement)
155: return getActualTarget(((IJavaElement) dropTarget)
156: .getCorrespondingResource());
157: return null;
158: }
159:
160: private Shell getShell() {
161: return getViewer().getControl().getShell();
162: }
163: }
|