1 11 package org.eclipse.jdt.internal.ui.dnd; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.Assert; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IAdaptable; 20 import org.eclipse.core.runtime.IStatus; 21 import org.eclipse.core.runtime.MultiStatus; 22 23 import org.eclipse.core.resources.IResource; 24 25 import org.eclipse.swt.dnd.DND; 26 import org.eclipse.swt.dnd.DragSourceAdapter; 27 import org.eclipse.swt.dnd.DragSourceEvent; 28 import org.eclipse.swt.dnd.Transfer; 29 import org.eclipse.swt.widgets.Shell; 30 31 import org.eclipse.jface.dialogs.ErrorDialog; 32 import org.eclipse.jface.util.TransferDragSourceListener; 33 import org.eclipse.jface.viewers.ISelection; 34 import org.eclipse.jface.viewers.ISelectionProvider; 35 import org.eclipse.jface.viewers.IStructuredSelection; 36 37 import org.eclipse.ui.part.ResourceTransfer; 38 39 import org.eclipse.jdt.core.IJavaElement; 40 41 import org.eclipse.jdt.internal.ui.IJavaStatusConstants; 42 import org.eclipse.jdt.internal.ui.JavaPlugin; 43 import org.eclipse.jdt.internal.ui.JavaUIMessages; 44 import org.eclipse.jdt.internal.ui.util.SWTUtil; 45 46 52 public class ResourceTransferDragAdapter extends DragSourceAdapter implements TransferDragSourceListener { 53 54 private ISelectionProvider fProvider; 55 56 private static final List EMPTY_LIST= new ArrayList (0); 57 58 64 public ResourceTransferDragAdapter(ISelectionProvider provider) { 65 fProvider= provider; 66 Assert.isNotNull(fProvider); 67 } 68 69 public Transfer getTransfer() { 70 return ResourceTransfer.getInstance(); 71 } 72 73 public void dragStart(DragSourceEvent event) { 74 event.doit= convertSelection().size() > 0; 75 } 76 77 public void dragSetData(DragSourceEvent event) { 78 List resources= convertSelection(); 79 event.data= resources.toArray(new IResource[resources.size()]); 80 } 81 82 public void dragFinished(DragSourceEvent event) { 83 if (!event.doit) 84 return; 85 86 if (event.detail == DND.DROP_MOVE) { 87 handleFinishedDropMove(event); 88 } 89 } 90 91 private List convertSelection() { 92 ISelection s= fProvider.getSelection(); 93 if (!(s instanceof IStructuredSelection)) 94 return EMPTY_LIST; 95 IStructuredSelection selection= (IStructuredSelection)s; 96 List result= new ArrayList (selection.size()); 97 for (Iterator iter= selection.iterator(); iter.hasNext();) { 98 Object element= iter.next(); 99 IResource resource= null; 100 if (element instanceof IJavaElement) { 101 resource= ((IJavaElement) element).getResource(); 103 } else if (element instanceof IAdaptable) { 104 resource= (IResource) ((IAdaptable) element).getAdapter(IResource.class); 105 } 106 if (resource != null) 107 result.add(resource); 108 } 109 return result; 110 } 111 112 private void handleFinishedDropMove(DragSourceEvent event) { 113 MultiStatus status= new MultiStatus( 114 JavaPlugin.getPluginId(), 115 IJavaStatusConstants.INTERNAL_ERROR, 116 JavaUIMessages.ResourceTransferDragAdapter_cannot_delete_resource, 117 null); 118 List resources= convertSelection(); 119 for (Iterator iter= resources.iterator(); iter.hasNext();) { 120 IResource resource= (IResource) iter.next(); 121 try { 122 resource.delete(true, null); 123 } catch (CoreException e) { 124 status.add(e.getStatus()); 125 } 126 } 127 if (status.getChildren().length > 0) { 128 Shell parent= SWTUtil.getShell(event.widget); 129 ErrorDialog error= new ErrorDialog(parent, 130 JavaUIMessages.ResourceTransferDragAdapter_moving_resource, 131 JavaUIMessages.ResourceTransferDragAdapter_cannot_delete_files, 132 status, IStatus.ERROR); 133 error.open(); 134 } 135 } 136 } 137 138 | Popular Tags |