1 11 package org.eclipse.ui.views.navigator; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.core.resources.IFile; 18 import org.eclipse.core.resources.IFolder; 19 import org.eclipse.core.resources.IResource; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IPath; 22 import org.eclipse.jface.viewers.ISelection; 23 import org.eclipse.jface.viewers.ISelectionProvider; 24 import org.eclipse.jface.viewers.IStructuredSelection; 25 import org.eclipse.swt.dnd.DND; 26 import org.eclipse.swt.dnd.DragSource; 27 import org.eclipse.swt.dnd.DragSourceAdapter; 28 import org.eclipse.swt.dnd.DragSourceEvent; 29 import org.eclipse.swt.dnd.DragSourceListener; 30 import org.eclipse.swt.dnd.FileTransfer; 31 import org.eclipse.swt.dnd.TransferData; 32 import org.eclipse.swt.widgets.Control; 33 import org.eclipse.swt.widgets.Shell; 34 import org.eclipse.ui.actions.ReadOnlyStateChecker; 35 import org.eclipse.ui.internal.views.navigator.ResourceNavigatorMessages; 36 import org.eclipse.ui.part.ResourceTransfer; 37 38 44 public class NavigatorDragAdapter extends DragSourceAdapter { 45 private static final String CHECK_MOVE_TITLE = ResourceNavigatorMessages.DragAdapter_title; 46 47 private static final String CHECK_DELETE_MESSAGE = ResourceNavigatorMessages.DragAdapter_checkDeleteMessage; 48 49 ISelectionProvider selectionProvider; 50 51 private TransferData lastDataType; 52 53 57 public NavigatorDragAdapter(ISelectionProvider provider) { 58 selectionProvider = provider; 59 } 60 61 66 public void dragFinished(DragSourceEvent event) { 67 LocalSelectionTransfer.getInstance().setSelection(null); 68 69 if (event.doit == false) { 70 return; 71 } 72 73 final int typeMask = IResource.FOLDER | IResource.FILE; 74 if (event.detail == DND.DROP_MOVE) { 75 if (lastDataType != null 78 && FileTransfer.getInstance().isSupportedType(lastDataType)) { 79 return; 80 } 81 82 IResource[] resources = getSelectedResources(typeMask); 83 DragSource dragSource = (DragSource) event.widget; 84 Control control = dragSource.getControl(); 85 Shell shell = control.getShell(); 86 ReadOnlyStateChecker checker; 87 88 if (resources == null || resources.length == 0) { 89 return; 90 } 91 92 checker = new ReadOnlyStateChecker(shell, CHECK_MOVE_TITLE, 93 CHECK_DELETE_MESSAGE); 94 resources = checker.checkReadOnlyResources(resources); 95 for (int i = 0; i < resources.length; i++) { 97 try { 98 resources[i].delete(IResource.KEEP_HISTORY 99 | IResource.FORCE, null); 100 } catch (CoreException e) { 101 e.printStackTrace(); 102 } 103 } 104 } else if (event.detail == DND.DROP_TARGET_MOVE) { 105 IResource[] resources = getSelectedResources(typeMask); 106 107 if (resources == null) { 110 return; 111 } 112 for (int i = 0; i < resources.length; i++) { 113 try { 114 resources[i].refreshLocal(IResource.DEPTH_INFINITE, null); 115 } catch (CoreException e) { 116 e.printStackTrace(); 117 } 118 } 119 } 120 } 121 122 126 public void dragSetData(DragSourceEvent event) { 127 final int typeMask = IResource.FILE | IResource.FOLDER; 128 IResource[] resources = getSelectedResources(typeMask); 129 130 if (resources == null || resources.length == 0) { 131 return; 132 } 133 134 lastDataType = event.dataType; 135 if (LocalSelectionTransfer.getInstance() 137 .isSupportedType(event.dataType)) { 138 event.data = LocalSelectionTransfer.getInstance().getSelection(); 139 return; 140 } 141 if (ResourceTransfer.getInstance().isSupportedType(event.dataType)) { 143 event.data = resources; 144 return; 145 } 146 if (!FileTransfer.getInstance().isSupportedType(event.dataType)) { 148 return; 149 } 150 151 final int length = resources.length; 153 int actualLength = 0; 154 String [] fileNames = new String [length]; 155 for (int i = 0; i < length; i++) { 156 IPath location = resources[i].getLocation(); 157 if (location != null) { 159 fileNames[actualLength++] = location.toOSString(); 160 } 161 } 162 if (actualLength == 0) { 163 return; 164 } 165 if (actualLength < length) { 167 String [] tempFileNames = fileNames; 168 fileNames = new String [actualLength]; 169 for (int i = 0; i < actualLength; i++) { 170 fileNames[i] = tempFileNames[i]; 171 } 172 } 173 event.data = fileNames; 174 } 175 176 181 public void dragStart(DragSourceEvent event) { 182 lastDataType = null; 183 DragSource dragSource = (DragSource) event.widget; 185 Control control = dragSource.getControl(); 186 if (control != control.getDisplay().getFocusControl()) { 187 event.doit = false; 188 return; 189 } 190 191 IStructuredSelection selection = (IStructuredSelection) selectionProvider 192 .getSelection(); 193 for (Iterator i = selection.iterator(); i.hasNext();) { 194 Object next = i.next(); 195 if (!(next instanceof IFile || next instanceof IFolder)) { 196 event.doit = false; 197 return; 198 } 199 } 200 if (selection.isEmpty()) { 201 event.doit = false; 202 return; 203 } 204 LocalSelectionTransfer.getInstance().setSelection(selection); 205 event.doit = true; 206 } 207 208 private IResource[] getSelectedResources(int resourceTypes) { 209 List resources = new ArrayList (); 210 IResource[] result = new IResource[0]; 211 212 ISelection selection = selectionProvider.getSelection(); 213 if (!(selection instanceof IStructuredSelection) || selection.isEmpty()) { 214 return null; 215 } 216 IStructuredSelection structuredSelection = (IStructuredSelection) selection; 217 218 Iterator itr = structuredSelection.iterator(); 220 while (itr.hasNext()) { 221 Object obj = itr.next(); 222 if (obj instanceof IResource) { 223 IResource res = (IResource) obj; 224 if ((res.getType() & resourceTypes) == res.getType()) { 225 resources.add(res); 226 } 227 } 228 } 229 result = new IResource[resources.size()]; 230 resources.toArray(result); 231 return result; 232 } 233 } 234 | Popular Tags |