1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.packageview;
13 import org.eclipse.swt.dnd.DND;
14 import org.eclipse.swt.dnd.DropTargetEvent;
15 import org.eclipse.swt.dnd.FileTransfer;
16 import org.eclipse.swt.dnd.Transfer;
17 import org.eclipse.swt.dnd.TransferData;
18 import org.eclipse.swt.widgets.Display;
19 import org.eclipse.swt.widgets.Shell;
21 import org.eclipse.core.resources.IContainer;
22 import org.eclipse.core.resources.IResource;
24 import org.eclipse.jface.util.TransferDropTargetListener;
25 import org.eclipse.jface.viewers.StructuredViewer;
27 import org.eclipse.ui.actions.CopyFilesAndFoldersOperation;
29 import org.eclipse.jdt.core.IJavaElement;
30 import org.eclipse.jdt.core.IJavaProject;
31 import org.eclipse.jdt.core.IPackageFragment;
32 import org.eclipse.jdt.core.IPackageFragmentRoot;
33 import org.eclipse.jdt.core.JavaModelException;
35 import org.eclipse.jdt.internal.corext.util.Resources;
37 import org.eclipse.jdt.internal.ui.dnd.JdtViewerDropAdapter;
38 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
41 * Adapter to handle file drop from other applications.
43 public class FileTransferDropAdapter extends JdtViewerDropAdapter implements TransferDropTargetListener {
45 public FileTransferDropAdapter(StructuredViewer viewer) {
48 setScrollEnabled(true);
49 setExpandEnabled(true);
50 setFeedbackEnabled(false);
53 //---- TransferDropTargetListener interface ---------------------------------------
58 public Transfer getTransfer() {
59 return FileTransfer.getInstance();
65 public boolean isEnabled(DropTargetEvent event) {
66 Object target= event.item != null ? event.item.getData() : null;
69 return target instanceof IJavaElement || target instanceof IResource;
72 //---- Actual DND -----------------------------------------------------------------
78 public boolean validateDrop(Object target, int operation, TransferData transferType) {
79 return determineOperation(target, operation, transferType, DND.DROP_MOVE | DND.DROP_LINK | DND.DROP_COPY) != DND.DROP_NONE;
86 protected int determineOperation(Object target, int operation, TransferData transferType, int operations) {
88 boolean isPackageFragment= target instanceof IPackageFragment;
89 boolean isJavaProject= target instanceof IJavaProject;
90 boolean isPackageFragmentRoot= target instanceof IPackageFragmentRoot;
91 boolean isContainer= target instanceof IContainer;
93 if (!(isPackageFragment || isJavaProject || isPackageFragmentRoot || isContainer))
97 IContainer container= (IContainer)target;
98 if (container.isAccessible() && !Resources.isReadOnly(container))
101 IJavaElement element= (IJavaElement)target;
102 if (!element.isReadOnly())
103 return DND.DROP_COPY;
106 return DND.DROP_NONE;
113 public boolean performDrop(final Object data) {
115 final int currentOperation= getCurrentOperation();
117 if (data == null || !(data instanceof String[]) || currentOperation != DND.DROP_COPY)
120 final IContainer target= getActualTarget(getCurrentTarget());
124 // Run the import operation asynchronously.
125 // Otherwise the drag source (e.g., Windows Explorer) will be blocked
126 // while the operation executes. Fixes bug 35796.
127 Display.getCurrent().asyncExec(new Runnable() {
129 getShell().forceActive();
130 new CopyFilesAndFoldersOperation(getShell()).copyOrLinkFiles((String[])data, target, currentOperation);
135 } catch (JavaModelException e) {
136 String title= PackagesMessages.DropAdapter_errorTitle;
137 String message= PackagesMessages.DropAdapter_errorMessage;
138 ExceptionHandler.handle(e, getShell(), title, message);
143 private IContainer getActualTarget(Object dropTarget) throws JavaModelException{
144 if (dropTarget instanceof IContainer)
145 return (IContainer)dropTarget;
146 else if (dropTarget instanceof IJavaElement)
147 return getActualTarget(((IJavaElement)dropTarget).getCorrespondingResource());
151 private Shell getShell() {
152 return getViewer().getControl().getShell();