1 11 package org.eclipse.jdt.internal.ui.packageview; 12 13 import org.eclipse.core.resources.IContainer; 14 import org.eclipse.core.resources.IResource; 15 16 import org.eclipse.swt.dnd.DND; 17 import org.eclipse.swt.dnd.DropTargetEvent; 18 import org.eclipse.swt.dnd.FileTransfer; 19 import org.eclipse.swt.dnd.Transfer; 20 import org.eclipse.swt.widgets.Display; 21 import org.eclipse.swt.widgets.Shell; 22 23 import org.eclipse.jface.util.TransferDropTargetListener; 24 import org.eclipse.jface.viewers.AbstractTreeViewer; 25 26 import org.eclipse.ui.actions.CopyFilesAndFoldersOperation; 27 28 import org.eclipse.jdt.core.IJavaElement; 29 import org.eclipse.jdt.core.IJavaProject; 30 import org.eclipse.jdt.core.IPackageFragment; 31 import org.eclipse.jdt.core.IPackageFragmentRoot; 32 import org.eclipse.jdt.core.JavaModelException; 33 34 import org.eclipse.jdt.internal.corext.util.Resources; 35 36 import org.eclipse.jdt.internal.ui.dnd.JdtViewerDropAdapter; 37 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 38 39 42 class FileTransferDropAdapter extends JdtViewerDropAdapter implements TransferDropTargetListener { 43 44 FileTransferDropAdapter(AbstractTreeViewer viewer) { 45 super(viewer, DND.FEEDBACK_SCROLL | DND.FEEDBACK_EXPAND); 46 } 47 48 50 public Transfer getTransfer() { 51 return FileTransfer.getInstance(); 52 } 53 54 public boolean isEnabled(DropTargetEvent event) { 55 Object target= event.item != null ? event.item.getData() : null; 56 if (target == null) 57 return false; 58 return target instanceof IJavaElement || target instanceof IResource; 59 } 60 61 63 public void validateDrop(Object target, DropTargetEvent event, int operation) { 64 event.detail= DND.DROP_NONE; 65 66 boolean isPackageFragment= target instanceof IPackageFragment; 67 boolean isJavaProject= target instanceof IJavaProject; 68 boolean isPackageFragmentRoot= target instanceof IPackageFragmentRoot; 69 boolean isContainer= target instanceof IContainer; 70 71 if (!(isPackageFragment || isJavaProject || isPackageFragmentRoot || isContainer)) 72 return; 73 74 if (isContainer) { 75 IContainer container= (IContainer)target; 76 if (container.isAccessible() && !Resources.isReadOnly(container)) 77 event.detail= DND.DROP_COPY; 78 } else { 79 IJavaElement element= (IJavaElement)target; 80 if (!element.isReadOnly()) 81 event.detail= DND.DROP_COPY; 82 } 83 84 return; 85 } 86 87 public void drop(Object dropTarget, final DropTargetEvent event) { 88 try { 89 int operation= event.detail; 90 91 event.detail= DND.DROP_NONE; 92 final Object data= event.data; 93 if (data == null || !(data instanceof String []) || operation != DND.DROP_COPY) 94 return; 95 96 final IContainer target= getActualTarget(dropTarget); 97 if (target == null) 98 return; 99 100 Display.getCurrent().asyncExec(new Runnable () { 104 public void run() { 105 getShell().forceActive(); 106 new CopyFilesAndFoldersOperation(getShell()).copyFiles((String []) data, target); 107 event.detail= DND.DROP_COPY; 109 } 110 }); 111 } catch (JavaModelException e) { 112 String title= PackagesMessages.DropAdapter_errorTitle; 113 String message= PackagesMessages.DropAdapter_errorMessage; 114 ExceptionHandler.handle(e, getShell(), title, message); 115 } 116 } 117 118 private IContainer getActualTarget(Object dropTarget) throws JavaModelException{ 119 if (dropTarget instanceof IContainer) 120 return (IContainer)dropTarget; 121 else if (dropTarget instanceof IJavaElement) 122 return getActualTarget(((IJavaElement)dropTarget).getCorrespondingResource()); 123 return null; 124 } 125 126 private Shell getShell() { 127 return getViewer().getControl().getShell(); 128 } 129 } 130 | Popular Tags |