1 11 package org.eclipse.ui.views.navigator; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 16 import org.eclipse.core.resources.IContainer; 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.runtime.IAdaptable; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.MultiStatus; 21 import org.eclipse.core.runtime.Status; 22 import org.eclipse.jface.dialogs.ErrorDialog; 23 import org.eclipse.jface.dialogs.IDialogConstants; 24 import org.eclipse.jface.dialogs.MessageDialog; 25 import org.eclipse.jface.viewers.ISelection; 26 import org.eclipse.jface.viewers.IStructuredSelection; 27 import org.eclipse.jface.viewers.StructuredViewer; 28 import org.eclipse.osgi.util.NLS; 29 import org.eclipse.swt.dnd.DND; 30 import org.eclipse.swt.dnd.DropTargetEvent; 31 import org.eclipse.swt.dnd.FileTransfer; 32 import org.eclipse.swt.dnd.TransferData; 33 import org.eclipse.swt.widgets.Display; 34 import org.eclipse.swt.widgets.Shell; 35 import org.eclipse.ui.PlatformUI; 36 import org.eclipse.ui.actions.CopyFilesAndFoldersOperation; 37 import org.eclipse.ui.actions.MoveFilesAndFoldersOperation; 38 import org.eclipse.ui.actions.ReadOnlyStateChecker; 39 import org.eclipse.ui.dialogs.IOverwriteQuery; 40 import org.eclipse.ui.internal.views.navigator.ResourceNavigatorMessages; 41 import org.eclipse.ui.part.PluginDropAdapter; 42 import org.eclipse.ui.part.ResourceTransfer; 43 44 50 public class NavigatorDropAdapter extends PluginDropAdapter implements 51 IOverwriteQuery { 52 53 56 private boolean alwaysOverwrite = false; 57 58 61 private int lastValidOperation = DND.DROP_NONE; 62 63 68 public NavigatorDropAdapter(StructuredViewer viewer) { 69 super(viewer); 70 } 71 72 75 public void dragEnter(DropTargetEvent event) { 76 if (FileTransfer.getInstance().isSupportedType(event.currentDataType) 77 && event.detail == DND.DROP_DEFAULT) { 78 event.detail = DND.DROP_COPY; 80 } 81 super.dragEnter(event); 82 } 83 84 87 private IStatus error(String message) { 88 return error(message, null); 89 } 90 91 94 private IStatus error(String message, Throwable exception) { 95 return new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, 0, message, 96 exception); 97 } 98 99 105 private IContainer getActualTarget(IResource mouseTarget) { 106 107 if (getFeedbackEnabled()) { 108 if (getCurrentLocation() == LOCATION_BEFORE 109 || getCurrentLocation() == LOCATION_AFTER) { 110 return mouseTarget.getParent(); 111 } 112 } 113 114 if (mouseTarget.getType() == IResource.FILE) { 115 return mouseTarget.getParent(); 116 } 117 118 return (IContainer) mouseTarget; 119 } 120 121 124 private Display getDisplay() { 125 return getViewer().getControl().getDisplay(); 126 } 127 128 133 private IResource[] getSelectedResources() { 134 ArrayList selectedResources = new ArrayList (); 135 136 ISelection selection = LocalSelectionTransfer.getInstance() 137 .getSelection(); 138 if (selection instanceof IStructuredSelection) { 139 IStructuredSelection ssel = (IStructuredSelection) selection; 140 for (Iterator i = ssel.iterator(); i.hasNext();) { 141 Object o = i.next(); 142 if (o instanceof IResource) { 143 selectedResources.add(o); 144 } 145 else if (o instanceof IAdaptable) { 146 IAdaptable a = (IAdaptable) o; 147 IResource r = (IResource) a.getAdapter(IResource.class); 148 if (r != null) { 149 selectedResources.add(r); 150 } 151 } 152 } 153 } 154 return (IResource[]) selectedResources.toArray(new IResource[selectedResources.size()]); 155 } 156 157 160 private Shell getShell() { 161 return getViewer().getControl().getShell(); 162 } 163 164 167 private IStatus info(String message) { 168 return new Status(IStatus.INFO, PlatformUI.PLUGIN_ID, 0, message, null); 169 } 170 171 176 private void mergeStatus(MultiStatus status, IStatus toMerge) { 177 if (!toMerge.isOK()) { 178 status.merge(toMerge); 179 } 180 } 181 182 185 private IStatus ok() { 186 return new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 0, 187 ResourceNavigatorMessages.DropAdapter_ok, null); 188 } 189 190 194 private void openError(IStatus status) { 195 if (status == null) { 196 return; 197 } 198 199 String genericTitle = ResourceNavigatorMessages.DropAdapter_title; 200 int codes = IStatus.ERROR | IStatus.WARNING; 201 202 if (!status.isMultiStatus()) { 204 ErrorDialog 205 .openError(getShell(), genericTitle, null, status, codes); 206 return; 207 } 208 209 IStatus[] children = status.getChildren(); 211 if (children.length == 1) { 212 ErrorDialog.openError(getShell(), status.getMessage(), null, 213 children[0], codes); 214 return; 215 } 216 ErrorDialog.openError(getShell(), genericTitle, null, status, codes); 218 } 219 220 224 public boolean performDrop(final Object data) { 225 alwaysOverwrite = false; 226 if (getCurrentTarget() == null || data == null) { 227 return false; 228 } 229 boolean result = false; 230 IStatus status = null; 231 IResource[] resources = null; 232 TransferData currentTransfer = getCurrentTransfer(); 233 if (LocalSelectionTransfer.getInstance().isSupportedType( 234 currentTransfer)) { 235 resources = getSelectedResources(); 236 } else if (ResourceTransfer.getInstance().isSupportedType( 237 currentTransfer)) { 238 resources = (IResource[]) data; 239 } else if (FileTransfer.getInstance().isSupportedType(currentTransfer)) { 240 status = performFileDrop(data); 241 result = status.isOK(); 242 } else { 243 result = NavigatorDropAdapter.super.performDrop(data); 244 } 245 if (resources != null && resources.length > 0) { 246 if (getCurrentOperation() == DND.DROP_COPY) { 247 status = performResourceCopy(getShell(), resources); 248 } else { 249 status = performResourceMove(resources); 250 } 251 } 252 openError(status); 253 return result; 254 } 255 256 259 private IStatus performFileDrop(Object data) { 260 MultiStatus problems = new MultiStatus(PlatformUI.PLUGIN_ID, 0, 261 ResourceNavigatorMessages.DropAdapter_problemImporting, null); 262 mergeStatus(problems, validateTarget(getCurrentTarget(), 263 getCurrentTransfer())); 264 265 final IContainer target = getActualTarget((IResource) getCurrentTarget()); 266 final String [] names = (String []) data; 267 Display.getCurrent().asyncExec(new Runnable () { 271 public void run() { 272 getShell().forceActive(); 273 CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation( 274 getShell()); 275 operation.copyFiles(names, target); 276 } 277 }); 278 return problems; 279 } 280 281 284 private IStatus performResourceCopy(Shell shell, IResource[] sources) { 285 MultiStatus problems = new MultiStatus(PlatformUI.PLUGIN_ID, 1, 286 ResourceNavigatorMessages.DropAdapter_problemsMoving, null); 287 mergeStatus(problems, validateTarget(getCurrentTarget(), 288 getCurrentTransfer())); 289 290 IContainer target = getActualTarget((IResource) getCurrentTarget()); 291 CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation( 292 shell); 293 operation.copyResources(sources, target); 294 295 return problems; 296 } 297 298 301 private IStatus performResourceMove(IResource[] sources) { 302 MultiStatus problems = new MultiStatus(PlatformUI.PLUGIN_ID, 1, 303 ResourceNavigatorMessages.DropAdapter_problemsMoving, null); 304 mergeStatus(problems, validateTarget(getCurrentTarget(), 305 getCurrentTransfer())); 306 307 IContainer target = getActualTarget((IResource) getCurrentTarget()); 308 ReadOnlyStateChecker checker = new ReadOnlyStateChecker( 309 getShell(), 310 ResourceNavigatorMessages.MoveResourceAction_title, 311 ResourceNavigatorMessages.MoveResourceAction_checkMoveMessage); 312 sources = checker.checkReadOnlyResources(sources); 313 MoveFilesAndFoldersOperation operation = new MoveFilesAndFoldersOperation( 314 getShell()); 315 operation.copyResources(sources, target); 316 317 return problems; 318 } 319 320 323 public String queryOverwrite(String pathString) { 324 if (alwaysOverwrite) { 325 return ALL; 326 } 327 328 final String returnCode[] = { CANCEL }; 329 final String msg = NLS.bind(ResourceNavigatorMessages.DropAdapter_overwriteQuery, pathString); 330 final String [] options = { IDialogConstants.YES_LABEL, 331 IDialogConstants.YES_TO_ALL_LABEL, IDialogConstants.NO_LABEL, 332 IDialogConstants.CANCEL_LABEL }; 333 getDisplay().syncExec(new Runnable () { 334 public void run() { 335 MessageDialog dialog = new MessageDialog( 336 getShell(), 337 ResourceNavigatorMessages.DropAdapter_question, null, msg, MessageDialog.QUESTION, options, 0); 338 dialog.open(); 339 int returnVal = dialog.getReturnCode(); 340 String [] returnCodes = { YES, ALL, NO, CANCEL }; 341 returnCode[0] = returnVal < 0 ? CANCEL : returnCodes[returnVal]; 342 } 343 }); 344 if (returnCode[0] == ALL) { 345 alwaysOverwrite = true; 346 } 347 return returnCode[0]; 348 } 349 350 354 public boolean validateDrop(Object target, int dragOperation, 355 TransferData transferType) { 356 357 if (dragOperation != DND.DROP_NONE) { 358 lastValidOperation = dragOperation; 359 } 360 if (FileTransfer.getInstance().isSupportedType(transferType) 361 && lastValidOperation != DND.DROP_COPY) { 362 return false; 364 } 365 if (super.validateDrop(target, dragOperation, transferType)) { 366 return true; 367 } 368 return validateTarget(target, transferType).isOK(); 369 } 370 371 374 private IStatus validateTarget(Object target, TransferData transferType) { 375 if (!(target instanceof IResource)) { 376 return info(ResourceNavigatorMessages.DropAdapter_targetMustBeResource); 377 } 378 IResource resource = (IResource) target; 379 if (!resource.isAccessible()) { 380 return error(ResourceNavigatorMessages.DropAdapter_canNotDropIntoClosedProject); 381 } 382 IContainer destination = getActualTarget(resource); 383 if (destination.getType() == IResource.ROOT) { 384 return error(ResourceNavigatorMessages.DropAdapter_resourcesCanNotBeSiblings); 385 } 386 String message = null; 387 if (LocalSelectionTransfer.getInstance().isSupportedType(transferType)) { 389 IResource[] selectedResources = getSelectedResources(); 390 391 if (selectedResources.length == 0) { 392 message = ResourceNavigatorMessages.DropAdapter_dropOperationErrorOther; 393 } else { 394 CopyFilesAndFoldersOperation operation; 395 if (lastValidOperation == DND.DROP_COPY) { 396 operation = new CopyFilesAndFoldersOperation(getShell()); 397 } else { 398 operation = new MoveFilesAndFoldersOperation(getShell()); 399 } 400 message = operation.validateDestination(destination, 401 selectedResources); 402 } 403 } else if (FileTransfer.getInstance().isSupportedType(transferType)) { 405 String [] sourceNames = (String []) FileTransfer.getInstance() 406 .nativeToJava(transferType); 407 if (sourceNames == null) { 408 sourceNames = new String [0]; 411 } 412 CopyFilesAndFoldersOperation copyOperation = new CopyFilesAndFoldersOperation( 413 getShell()); 414 message = copyOperation.validateImportDestination(destination, 415 sourceNames); 416 } 417 if (message != null) { 418 return error(message); 419 } 420 return ok(); 421 } 422 } 423 | Popular Tags |