1 11 package org.eclipse.ui.navigator; 12 13 import org.eclipse.core.runtime.IStatus; 14 import org.eclipse.jface.util.LocalSelectionTransfer; 15 import org.eclipse.jface.viewers.StructuredViewer; 16 import org.eclipse.swt.dnd.DND; 17 import org.eclipse.swt.dnd.DropTargetEvent; 18 import org.eclipse.swt.dnd.FileTransfer; 19 import org.eclipse.swt.dnd.Transfer; 20 import org.eclipse.swt.dnd.TransferData; 21 import org.eclipse.swt.graphics.Point; 22 import org.eclipse.swt.graphics.Rectangle; 23 import org.eclipse.swt.widgets.Item; 24 import org.eclipse.ui.internal.navigator.NavigatorPlugin; 25 import org.eclipse.ui.internal.navigator.dnd.NavigatorDnDService; 26 import org.eclipse.ui.internal.navigator.dnd.NavigatorPluginDropAction; 27 import org.eclipse.ui.part.PluginDropAdapter; 28 import org.eclipse.ui.part.PluginTransfer; 29 30 48 public final class CommonDropAdapter extends PluginDropAdapter { 49 50 private static final Transfer[] SUPPORTED_DROP_TRANSFERS = new Transfer[] { 51 LocalSelectionTransfer.getTransfer(), FileTransfer.getInstance(), 52 PluginTransfer.getInstance() }; 53 54 private static final boolean DEBUG = false; 55 56 private final INavigatorContentService contentService; 57 58 private final NavigatorDnDService dndService; 59 60 69 public CommonDropAdapter(INavigatorContentService aContentService, 70 StructuredViewer aStructuredViewer) { 71 super(aStructuredViewer); 72 contentService = aContentService; 73 dndService = (NavigatorDnDService) contentService.getDnDService(); 74 } 75 76 86 public Transfer[] getSupportedDropTransfers() { 87 return SUPPORTED_DROP_TRANSFERS; 88 } 89 90 95 public void dragEnter(DropTargetEvent event) { 96 super.dragEnter(event); 97 98 for (int i = 0; i < event.dataTypes.length; i++) { 99 if (LocalSelectionTransfer.getTransfer().isSupportedType( 100 event.dataTypes[i])) { 101 event.currentDataType = event.dataTypes[i]; 102 return; 103 } 104 } 105 106 for (int i = 0; i < event.dataTypes.length; i++) { 107 if (FileTransfer.getInstance().isSupportedType(event.dataTypes[i])) { 108 event.currentDataType = event.dataTypes[i]; 109 event.detail = DND.DROP_COPY; 110 return; 111 } 112 } 113 114 for (int i = 0; i < event.dataTypes.length; i++) { 115 if (PluginTransfer.getInstance() 116 .isSupportedType(event.dataTypes[i])) { 117 event.currentDataType = event.dataTypes[i]; 118 return; 119 } 120 } 121 122 event.detail = DND.DROP_NONE; 123 124 } 125 126 131 public void dragLeave(DropTargetEvent event) { 132 super.dragLeave(event); 133 if (LocalSelectionTransfer.getTransfer().isSupportedType( 134 event.currentDataType)) { 135 event.data = NavigatorPluginDropAction 136 .createTransferData(contentService); 137 } 138 } 139 140 145 public void drop(DropTargetEvent event) { 146 if (PluginTransfer.getInstance().isSupportedType(event.currentDataType)) { 147 super.drop(event); 148 } else { 149 150 Object target = getCurrentTarget() != null ? 151 getCurrentTarget() : getViewer().getInput(); 152 153 CommonDropAdapterAssistant[] assistants = dndService 154 .findCommonDropAdapterAssistants(target, getCurrentTransfer()); 155 156 IStatus valid = null; 157 for (int i = 0; i < assistants.length; i++) { 158 try { 159 160 valid = assistants[i].validateDrop(getCurrentTarget(), 161 getCurrentOperation(), getCurrentTransfer()); 162 if (valid != null && valid.isOK()) { 163 assistants[i].handleDrop(this, event, 164 getCurrentTarget()); 165 return; 166 } 167 } catch (Throwable t) { 168 NavigatorPlugin.logError(0, t.getMessage(), t); 169 } 170 } 171 } 172 } 173 174 180 public boolean validateDrop(Object aDropTarget, int theDropOperation, 181 TransferData theTransferData) { 182 183 if (DEBUG) { 184 System.out.println("CommonDropAdapter.validateDrop (begin)"); } 186 187 boolean result = false; 188 189 IStatus valid = null; 190 191 if (super.validateDrop(aDropTarget, theDropOperation, theTransferData)) { 192 result = true; 193 } else { 194 195 Object target = aDropTarget != null ? aDropTarget : getViewer().getInput(); 196 CommonDropAdapterAssistant[] assistants = dndService 197 .findCommonDropAdapterAssistants(target, 198 theTransferData); 199 if (DEBUG) { 200 System.out 201 .println("CommonDropAdapter.validateDrop found " + assistants.length + " drop assistants"); for(int i=0; i<assistants.length; i++) 203 System.out.println("CommonDropAdapter.validateDrop :" + assistants[i].getClass().getName()); 205 } 206 for (int i = 0; i < assistants.length; i++) { 207 try { 208 valid = assistants[i].validateDrop(target, 209 theDropOperation, theTransferData); 210 } catch (Throwable t) { 211 NavigatorPlugin.logError(0, t.getMessage(), t); 212 } 213 if (valid != null && valid.isOK()) { 214 result = true; 215 if (DEBUG) { 216 System.out 217 .println("CommonDropAdapter.validateDrop found \""+assistants[i].getClass().getName()+"\" would handle drop."); } 219 break; 220 } 221 } 222 } 223 224 if (DEBUG) { 225 System.out 226 .println("CommonDropAdapter.validateDrop (returning " + (valid != null ? valid.getSeverity() + ": " + valid.getMessage() : "" + result) + ")"); } 228 229 230 setScrollExpandEnabled(result); 231 return result; 232 233 } 234 235 239 240 245 public Rectangle getBounds(Item item) { 246 return super.getBounds(item); 247 } 248 249 254 public int getCurrentLocation() { 255 return super.getCurrentLocation(); 256 } 257 258 263 public int getCurrentOperation() { 264 return super.getCurrentOperation(); 265 } 266 267 272 public Object getCurrentTarget() { 273 return super.getCurrentTarget(); 274 } 275 276 281 public TransferData getCurrentTransfer() { 282 return super.getCurrentTransfer(); 283 } 284 285 293 protected int determineLocation(DropTargetEvent event) { 294 if (!(event.item instanceof Item)) { 295 return LOCATION_NONE; 296 } 297 Point coordinates = new Point(event.x, event.y); 299 coordinates = getViewer().getControl().toControl(coordinates); 300 return LOCATION_ON; 313 } 314 315 } 316 | Popular Tags |