1 11 package org.eclipse.jdt.internal.ui.packageview; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Set ; 19 20 import org.eclipse.core.runtime.Assert; 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IAdaptable; 23 import org.eclipse.core.runtime.IProgressMonitor; 24 import org.eclipse.core.runtime.IStatus; 25 import org.eclipse.core.runtime.MultiStatus; 26 import org.eclipse.core.runtime.SubProgressMonitor; 27 28 import org.eclipse.core.resources.IResource; 29 30 import org.eclipse.swt.dnd.DND; 31 import org.eclipse.swt.dnd.DragSourceAdapter; 32 import org.eclipse.swt.dnd.DragSourceEvent; 33 import org.eclipse.swt.dnd.FileTransfer; 34 import org.eclipse.swt.dnd.Transfer; 35 import org.eclipse.swt.widgets.Shell; 36 37 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 38 import org.eclipse.jface.operation.IRunnableWithProgress; 39 import org.eclipse.jface.util.TransferDragSourceListener; 40 import org.eclipse.jface.viewers.ISelection; 41 import org.eclipse.jface.viewers.ISelectionProvider; 42 import org.eclipse.jface.viewers.IStructuredSelection; 43 44 import org.eclipse.ui.actions.WorkspaceModifyOperation; 45 46 import org.eclipse.jdt.core.IJavaElement; 47 import org.eclipse.jdt.core.IPackageFragmentRoot; 48 49 import org.eclipse.jdt.internal.corext.util.Resources; 50 51 import org.eclipse.jdt.internal.ui.JavaPlugin; 52 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 53 54 58 class FileTransferDragAdapter extends DragSourceAdapter implements TransferDragSourceListener { 59 60 private ISelectionProvider fProvider; 61 62 FileTransferDragAdapter(ISelectionProvider provider) { 63 fProvider= provider; 64 Assert.isNotNull(fProvider); 65 } 66 67 public Transfer getTransfer() { 68 return FileTransfer.getInstance(); 69 } 70 71 public void dragStart(DragSourceEvent event) { 72 event.doit= isDragable(fProvider.getSelection()); 73 } 74 75 private boolean isDragable(ISelection s) { 76 if (!(s instanceof IStructuredSelection)) 77 return false; 78 IStructuredSelection selection= (IStructuredSelection)s; 79 for (Iterator iter= selection.iterator(); iter.hasNext();) { 80 Object element= iter.next(); 81 if (element instanceof IJavaElement) { 82 IJavaElement jElement= (IJavaElement)element; 83 int type= jElement.getElementType(); 84 if (type != IJavaElement.PACKAGE_FRAGMENT_ROOT && 87 type != IJavaElement.COMPILATION_UNIT && type != IJavaElement.TYPE) 88 return false; 89 IPackageFragmentRoot root= (IPackageFragmentRoot)jElement.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); 90 if (root != null && root.isArchive()) 91 return false; 92 } 93 } 94 List resources= convertIntoResources(selection); 95 return resources.size() == selection.size(); 96 } 97 98 public void dragSetData(DragSourceEvent event){ 99 List elements= getResources(); 100 if (elements == null || elements.size() == 0) { 101 event.data= null; 102 return; 103 } 104 105 event.data= getResourceLocations(elements); 106 } 107 108 private static String [] getResourceLocations(List resources) { 109 return Resources.getLocationOSStrings((IResource[]) resources.toArray(new IResource[resources.size()])); 110 } 111 112 public void dragFinished(DragSourceEvent event) { 113 if (!event.doit) 114 return; 115 116 if (event.detail == DND.DROP_MOVE) { 117 } else if (event.detail == DND.DROP_NONE || event.detail == DND.DROP_TARGET_MOVE) { 120 handleRefresh(event); 121 } 122 } 123 124 void handleDropMove(DragSourceEvent event) { 125 final List elements= getResources(); 126 if (elements == null || elements.size() == 0) 127 return; 128 129 WorkspaceModifyOperation op= new WorkspaceModifyOperation() { 130 public void execute(IProgressMonitor monitor) throws CoreException { 131 try { 132 monitor.beginTask(PackagesMessages.DragAdapter_deleting, elements.size()); 133 MultiStatus status= createMultiStatus(); 134 Iterator iter= elements.iterator(); 135 while(iter.hasNext()) { 136 IResource resource= (IResource)iter.next(); 137 try { 138 monitor.subTask(resource.getFullPath().toOSString()); 139 resource.delete(true, null); 140 141 } catch (CoreException e) { 142 status.add(e.getStatus()); 143 } finally { 144 monitor.worked(1); 145 } 146 } 147 if (!status.isOK()) { 148 throw new CoreException(status); 149 } 150 } finally { 151 monitor.done(); 152 } 153 } 154 }; 155 runOperation(op, true, false); 156 } 157 158 private void handleRefresh(DragSourceEvent event) { 159 final Set roots= collectRoots(getResources()); 160 161 WorkspaceModifyOperation op= new WorkspaceModifyOperation() { 162 public void execute(IProgressMonitor monitor) throws CoreException { 163 try { 164 monitor.beginTask(PackagesMessages.DragAdapter_refreshing, roots.size()); 165 MultiStatus status= createMultiStatus(); 166 Iterator iter= roots.iterator(); 167 while (iter.hasNext()) { 168 IResource r= (IResource)iter.next(); 169 try { 170 r.refreshLocal(IResource.DEPTH_ONE, new SubProgressMonitor(monitor, 1)); 171 } catch (CoreException e) { 172 status.add(e.getStatus()); 173 } 174 } 175 if (!status.isOK()) { 176 throw new CoreException(status); 177 } 178 } finally { 179 monitor.done(); 180 } 181 } 182 }; 183 184 runOperation(op, true, false); 185 } 186 187 protected Set collectRoots(final List elements) { 188 final Set roots= new HashSet (10); 189 190 Iterator iter= elements.iterator(); 191 while (iter.hasNext()) { 192 IResource resource= (IResource)iter.next(); 193 IResource parent= resource.getParent(); 194 if (parent == null) { 195 roots.add(resource); 196 } else { 197 roots.add(parent); 198 } 199 } 200 return roots; 201 } 202 203 private List getResources() { 204 ISelection s= fProvider.getSelection(); 205 if (!(s instanceof IStructuredSelection)) 206 return null; 207 208 return convertIntoResources((IStructuredSelection)s); 209 } 210 211 private List convertIntoResources(IStructuredSelection selection) { 212 List result= new ArrayList (selection.size()); 213 for (Iterator iter= selection.iterator(); iter.hasNext();) { 214 Object o= iter.next(); 215 IResource r= null; 216 if (o instanceof IResource) { 217 r= (IResource)o; 218 } else if (o instanceof IAdaptable) { 219 r= (IResource)((IAdaptable)o).getAdapter(IResource.class); 220 } 221 if (r != null && r.getLocation() != null) { 224 result.add(r); 225 } 226 } 227 return result; 228 } 229 230 private MultiStatus createMultiStatus() { 231 return new MultiStatus(JavaPlugin.getPluginId(), 232 IStatus.OK, PackagesMessages.DragAdapter_problem, null); 233 } 234 235 private void runOperation(IRunnableWithProgress op, boolean fork, boolean cancelable) { 236 try { 237 Shell parent= JavaPlugin.getActiveWorkbenchShell(); 238 new ProgressMonitorDialog(parent).run(fork, cancelable, op); 239 } catch (InvocationTargetException e) { 240 String message= PackagesMessages.DragAdapter_problem; 241 String title= PackagesMessages.DragAdapter_problemTitle; 242 ExceptionHandler.handle(e, title, message); 243 } catch (InterruptedException e) { 244 } 246 } 247 } 248 | Popular Tags |