1 11 package org.eclipse.jdt.internal.ui.navigator; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.List ; 15 16 import org.eclipse.core.runtime.IStatus; 17 import org.eclipse.core.runtime.Status; 18 19 import org.eclipse.core.resources.IContainer; 20 import org.eclipse.core.resources.IResource; 21 22 import org.eclipse.swt.dnd.DND; 23 import org.eclipse.swt.dnd.DropTargetEvent; 24 import org.eclipse.swt.dnd.FileTransfer; 25 import org.eclipse.swt.dnd.TransferData; 26 27 import org.eclipse.jface.viewers.ISelection; 28 import org.eclipse.jface.viewers.IStructuredSelection; 29 30 import org.eclipse.ui.actions.CopyFilesAndFoldersOperation; 31 import org.eclipse.ui.navigator.CommonDropAdapter; 32 import org.eclipse.ui.navigator.CommonDropAdapterAssistant; 33 34 import org.eclipse.ui.views.navigator.LocalSelectionTransfer; 35 36 import org.eclipse.jdt.core.IJavaElement; 37 import org.eclipse.jdt.core.JavaModelException; 38 39 import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaCopyProcessor; 40 import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaMoveProcessor; 41 import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgPolicyFactory; 42 import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgUtils; 43 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.ICopyPolicy; 44 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.IMovePolicy; 45 46 import org.eclipse.jdt.internal.ui.packageview.PackagesMessages; 47 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 48 import org.eclipse.jdt.internal.ui.refactoring.reorg.ReorgCopyStarter; 49 import org.eclipse.jdt.internal.ui.refactoring.reorg.ReorgMoveStarter; 50 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 51 52 public class JavaDropAdapterAssistant extends CommonDropAdapterAssistant { 53 54 private List fElements; 55 private JavaMoveProcessor fMoveProcessor; 56 private int fCanMoveElements; 57 private JavaCopyProcessor fCopyProcessor; 58 private int fCanCopyElements; 59 60 public IStatus handleDrop(CommonDropAdapter dropAdapter, DropTargetEvent dropTargetEvent, Object target) { 61 if (LocalSelectionTransfer.getInstance().isSupportedType(dropAdapter.getCurrentTransfer())) { 62 try { 63 64 switch (dropAdapter.getCurrentOperation()) { 65 case DND.DROP_MOVE : 66 handleDropMove(target); 67 break; 68 case DND.DROP_COPY : 69 handleDropCopy(target); 70 break; 71 } 72 } catch (JavaModelException e) { 73 ExceptionHandler.handle(e, PackagesMessages.SelectionTransferDropAdapter_error_title, PackagesMessages.SelectionTransferDropAdapter_error_message); 74 } catch (InvocationTargetException e) { 75 ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception); 76 } catch (InterruptedException e) { 77 } finally { 79 } 84 clear(); 85 return Status.OK_STATUS; 86 } else if (FileTransfer.getInstance().isSupportedType(dropAdapter.getCurrentTransfer())) { 87 try { 88 89 final Object data = FileTransfer.getInstance().nativeToJava(dropAdapter.getCurrentTransfer()); 90 if (!(data instanceof String [])) 91 return Status.CANCEL_STATUS; 92 93 final IContainer targetContainer = getActualTarget(target); 94 if (targetContainer == null) 95 return Status.CANCEL_STATUS; 96 97 98 getShell().forceActive(); 99 new CopyFilesAndFoldersOperation(getShell()).copyFiles((String []) data, targetContainer); 100 } catch (JavaModelException e) { 101 String title = PackagesMessages.DropAdapter_errorTitle; 102 String message = PackagesMessages.DropAdapter_errorMessage; 103 ExceptionHandler.handle(e, getShell(), title, message); 104 } 105 return Status.OK_STATUS; 106 } 107 return Status.CANCEL_STATUS; 108 } 109 110 public IStatus validateDrop(Object target, int operation, TransferData transferType) { 111 IStatus result = Status.OK_STATUS; 112 if (LocalSelectionTransfer.getInstance().isSupportedType(transferType)) { 113 initializeSelection(); 114 try { 115 switch (operation) { 116 case DND.DROP_DEFAULT : 117 if (handleValidateDefault(target) != DND.DROP_NONE) { 118 result = Status.OK_STATUS; 119 } else { 120 result = Status.CANCEL_STATUS; 121 } 122 break; 123 case DND.DROP_COPY : 124 if (handleValidateCopy(target) != DND.DROP_NONE) { 125 result = Status.OK_STATUS; 126 } else { 127 result = Status.CANCEL_STATUS; 128 } 129 break; 130 case DND.DROP_MOVE : 131 if (handleValidateMove(target) != DND.DROP_NONE) { 132 result = Status.OK_STATUS; 133 } else { 134 result = Status.CANCEL_STATUS; 135 } 136 break; 137 } 138 } catch (JavaModelException e) { 139 ExceptionHandler.handle(e, PackagesMessages.SelectionTransferDropAdapter_error_title, PackagesMessages.SelectionTransferDropAdapter_error_message); 140 result = Status.CANCEL_STATUS; 142 } 143 } 144 return result; 145 } 146 147 public boolean isSupportedType(TransferData transferType) { 148 return super.isSupportedType(transferType) || FileTransfer.getInstance().isSupportedType(transferType); 149 } 150 151 private IContainer getActualTarget(Object dropTarget) throws JavaModelException { 152 if (dropTarget instanceof IContainer) 153 return (IContainer) dropTarget; 154 else if (dropTarget instanceof IJavaElement) 155 return getActualTarget(((IJavaElement) dropTarget).getCorrespondingResource()); 156 return null; 157 } 158 159 protected void initializeSelection() { 160 if (fElements != null) 161 return; 162 ISelection s = LocalSelectionTransfer.getInstance().getSelection(); 163 if (!(s instanceof IStructuredSelection)) 164 return; 165 fElements = ((IStructuredSelection) s).toList(); 166 } 167 168 private void handleDropMove(final Object target) throws JavaModelException, InvocationTargetException , InterruptedException { 169 IJavaElement[] javaElements = ReorgUtils.getJavaElements(fElements); 170 IResource[] resources = ReorgUtils.getResources(fElements); 171 ReorgMoveStarter starter = null; 172 if (target instanceof IResource) 173 starter = ReorgMoveStarter.create(javaElements, resources, (IResource) target); 174 else if (target instanceof IJavaElement) 175 starter = ReorgMoveStarter.create(javaElements, resources, (IJavaElement) target); 176 if (starter != null) 177 starter.run(getShell()); 178 } 179 180 private void handleDropCopy(final Object target) throws JavaModelException, InvocationTargetException , InterruptedException { 181 IJavaElement[] javaElements = ReorgUtils.getJavaElements(fElements); 182 IResource[] resources = ReorgUtils.getResources(fElements); 183 ReorgCopyStarter starter = null; 184 if (target instanceof IResource) 185 starter = ReorgCopyStarter.create(javaElements, resources, (IResource) target); 186 else if (target instanceof IJavaElement) 187 starter = ReorgCopyStarter.create(javaElements, resources, (IJavaElement) target); 188 if (starter != null) 189 starter.run(getShell()); 190 } 191 192 private int handleValidateCopy(Object target) throws JavaModelException { 193 194 final ICopyPolicy policy= ReorgPolicyFactory.createCopyPolicy(ReorgUtils.getResources(fElements), ReorgUtils.getJavaElements(fElements)); 195 fCopyProcessor= policy.canEnable() ? new JavaCopyProcessor(policy) : null; 196 197 if (!canCopyElements()) 198 return DND.DROP_NONE; 199 200 if (target instanceof IResource && fCopyProcessor != null && fCopyProcessor.setDestination((IResource)target).isOK()) 201 return DND.DROP_COPY; 202 else if (target instanceof IJavaElement && fCopyProcessor != null && fCopyProcessor.setDestination((IJavaElement)target).isOK()) 203 return DND.DROP_COPY; 204 else 205 return DND.DROP_NONE; 206 } 207 208 private int handleValidateDefault(Object target) throws JavaModelException { 209 if (target == null) 210 return DND.DROP_NONE; 211 212 return handleValidateMove(target); 213 } 214 215 private int handleValidateMove(Object target) throws JavaModelException { 216 if (target == null) 217 return DND.DROP_NONE; 218 219 IMovePolicy policy= ReorgPolicyFactory.createMovePolicy(ReorgUtils.getResources(fElements), ReorgUtils.getJavaElements(fElements)); 220 fMoveProcessor= (policy.canEnable()) ? new JavaMoveProcessor(policy) : null; 221 222 if (!canMoveElements()) 223 return DND.DROP_NONE; 224 225 if (target instanceof IResource && fMoveProcessor != null && fMoveProcessor.setDestination((IResource)target).isOK()) 226 return DND.DROP_MOVE; 227 else if (target instanceof IJavaElement && fMoveProcessor != null && fMoveProcessor.setDestination((IJavaElement)target).isOK()) 228 return DND.DROP_MOVE; 229 else 230 return DND.DROP_NONE; 231 } 232 233 234 private boolean canMoveElements() { 235 if (fCanMoveElements == 0) { 236 fCanMoveElements = 2; 237 if (fMoveProcessor == null) 238 fCanMoveElements = 1; 239 } 240 return fCanMoveElements == 2; 241 } 242 243 244 private boolean canCopyElements() { 245 if (fCanCopyElements == 0) { 246 fCanCopyElements = 2; 247 if (fCopyProcessor == null) 248 fCanCopyElements = 1; 249 } 250 return fCanCopyElements == 2; 251 } 252 253 private void clear() { 254 fElements = null; 255 fMoveProcessor = null; 256 fCanMoveElements = 0; 257 fCopyProcessor = null; 258 fCanCopyElements = 0; 259 } 260 } 261 | Popular Tags |