1 17 package org.eclipse.emf.edit.ui.dnd; 18 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Collections ; 23 import java.util.Iterator ; 24 25 import org.eclipse.jface.viewers.IStructuredSelection; 26 import org.eclipse.jface.viewers.Viewer; 27 import org.eclipse.swt.SWT; 28 import org.eclipse.swt.dnd.DND; 29 import org.eclipse.swt.dnd.DropTargetAdapter; 30 import org.eclipse.swt.dnd.DropTargetEvent; 31 import org.eclipse.swt.dnd.TransferData; 32 import org.eclipse.swt.graphics.Point; 33 import org.eclipse.swt.graphics.Rectangle; 34 import org.eclipse.swt.widgets.Control; 35 import org.eclipse.swt.widgets.Item; 36 import org.eclipse.swt.widgets.TableItem; 37 import org.eclipse.swt.widgets.TreeItem; 38 import org.eclipse.swt.widgets.Widget; 39 40 import org.eclipse.emf.common.command.Command; 41 import org.eclipse.emf.common.ui.viewer.ExtendedTableTreeViewer; 42 import org.eclipse.emf.edit.command.DragAndDropCommand; 43 import org.eclipse.emf.edit.command.DragAndDropFeedback; 44 import org.eclipse.emf.edit.domain.EditingDomain; 45 46 47 78 public class EditingDomainViewerDropAdapter extends DropTargetAdapter 79 { 80 85 protected final static boolean IS_MOTIF = "motif".equals(SWT.getPlatform()); 86 87 90 protected Viewer viewer; 91 92 95 protected EditingDomain domain; 96 97 100 protected Collection source; 101 102 105 protected Command command; 106 107 110 protected Object commandTarget; 111 112 117 protected int originalOperation; 118 119 124 protected DragAndDropCommandInformation dragAndDropCommandInformation; 125 126 129 public EditingDomainViewerDropAdapter(EditingDomain domain, Viewer viewer) 130 { 131 this.viewer = viewer; 132 this.domain = domain; 133 } 134 135 138 public void dragEnter(DropTargetEvent event) 139 { 140 originalOperation = event.detail; 142 143 helper(event); 144 } 145 146 153 public void dragLeave(DropTargetEvent event) 154 { 155 if (command != null) 159 { 160 command.dispose(); 161 command = null; 162 commandTarget = null; 163 } 164 165 source = null; 169 } 170 171 174 public void dragOperationChanged(DropTargetEvent event) 175 { 176 originalOperation = event.detail; 178 179 helper(event); 180 } 181 182 185 public void dragOver(DropTargetEvent event) 186 { 187 helper(event); 188 } 189 190 194 public void dropAccept(DropTargetEvent event) 195 { 196 helper(event); 197 } 198 199 202 public void drop(DropTargetEvent event) 203 { 204 if (dragAndDropCommandInformation != null) 208 { 209 command = dragAndDropCommandInformation.createCommand(); 212 } 213 else 214 { 215 source = extractDragSource(event.data); 219 Object target = extractDropTarget(event.item); 220 command = DragAndDropCommand.create(domain, target, getLocation(event), event.operations, originalOperation, source); 221 } 222 223 if (command.canExecute()) 226 { 227 domain.getCommandStack().execute(command); 230 } 231 else 232 { 233 event.detail = DND.DROP_NONE; 236 command.dispose(); 237 } 238 239 command = null; 242 commandTarget = null; 243 source = null; 244 } 245 246 253 protected void helper(DropTargetEvent event) 254 { 255 event.feedback = DND.FEEDBACK_SELECT | getAutoFeedback(); 258 259 if (source == null) 263 { 264 source = getDragSource(event); 265 if (source == null) return; 266 } 267 268 Object target = extractDropTarget(event.item); 271 float location = getLocation(event); 272 273 boolean valid = false; 276 277 if (command == null) 280 { 281 dragAndDropCommandInformation = new DragAndDropCommandInformation(domain, target, location, event.operations, originalOperation, source); 284 285 commandTarget = target; 288 command = dragAndDropCommandInformation.createCommand(); 289 valid = command.canExecute(); 290 } 291 else 292 { 293 if (target == commandTarget && command instanceof DragAndDropFeedback) 296 { 297 valid = ((DragAndDropFeedback)command).validate(target, location, event.operations, originalOperation, source); 300 301 dragAndDropCommandInformation = new DragAndDropCommandInformation(domain, target, location, event.operations, originalOperation, source); 303 } 304 else 305 { 306 dragAndDropCommandInformation = new DragAndDropCommandInformation(domain, target, location, event.operations, originalOperation, source); 309 commandTarget = target; 310 command.dispose(); 311 command = dragAndDropCommandInformation.createCommand(); 312 valid = command.canExecute(); 313 } 314 } 315 316 if (command instanceof DragAndDropFeedback) 319 { 320 DragAndDropFeedback dragAndDropFeedback = (DragAndDropFeedback)command; 323 event.detail = dragAndDropFeedback.getOperation(); 324 event.feedback = dragAndDropFeedback.getFeedback() | getAutoFeedback(); 325 } 326 else if (!valid) 327 { 328 event.detail = DND.DROP_NONE; 331 } 332 } 333 334 340 protected int getAutoFeedback() 341 { 342 return DND.FEEDBACK_SCROLL | DND.FEEDBACK_EXPAND; 343 } 344 345 351 protected Collection getDragSource(DropTargetEvent event) 352 { 353 LocalTransfer localTransfer = LocalTransfer.getInstance(); 356 if (!localTransfer.isSupportedType(event.currentDataType)) 357 { 358 TransferData [] dataTypes = event.dataTypes; 361 for (int i = 0; i < dataTypes.length; ++i) 362 { 363 TransferData transferData = dataTypes[i]; 364 365 if (localTransfer.isSupportedType(transferData)) 368 { 369 event.currentDataType = transferData; 370 } 371 } 372 373 return null; 374 } 375 else 376 { 377 if (IS_MOTIF) return null; 380 381 Object object = localTransfer.nativeToJava(event.currentDataType); 384 return object == null ? null : extractDragSource(object); 385 } 386 } 387 388 392 protected Collection extractDragSource(Object object) 393 { 394 if (object instanceof IStructuredSelection) 397 { 398 Collection result = new ArrayList (); 399 for (Iterator elements = ((IStructuredSelection)object).iterator(); elements.hasNext(); ) 400 { 401 result.add(elements.next()); 402 } 403 return result; 404 } 405 else 406 { 407 return Collections.EMPTY_LIST; 408 } 409 } 410 411 416 protected static Object extractDropTarget(Widget item) 417 { 418 if (item == null) return null; 419 return item.getData(ExtendedTableTreeViewer.ITEM_ID) instanceof Item ? 420 ((Item)item.getData(ExtendedTableTreeViewer.ITEM_ID)).getData() : 421 item.getData(); 422 } 423 424 425 429 protected static float getLocation(DropTargetEvent event) 430 { 431 if (event.item instanceof TreeItem) 432 { 433 TreeItem treeItem = (TreeItem)event.item; 434 Control control = treeItem.getParent(); 435 Point point = control.toControl(new Point(event.x, event.y)); 436 Rectangle bounds = treeItem.getBounds(); 437 return (float)(point.y - bounds.y) / (float)bounds.height; 438 } 439 else if (event.item instanceof TableItem) 440 { 441 TableItem tableItem = (TableItem)event.item; 442 Control control = tableItem.getParent(); 443 Point point = control.toControl(new Point(event.x, event.y)); 444 Rectangle bounds = tableItem.getBounds(0); 445 return (float)(point.y - bounds.y) / (float)bounds.height; 446 } 447 else 448 { 449 return 0.0F; 450 } 451 } 452 453 457 protected static class DragAndDropCommandInformation 458 { 459 protected EditingDomain domain; 460 protected Object target; 461 protected float location; 462 protected int operations; 463 protected int operation; 464 protected Collection source; 465 public DragAndDropCommandInformation 466 (EditingDomain domain, Object target, float location, int operations, int operation, Collection source) 467 { 468 this.domain = domain; 469 this.target = target; 470 this.location = location; 471 this.operations = operations; 472 this.operation = operation; 473 this.source = new ArrayList (source); 474 } 475 476 public Command createCommand() 477 { 478 return DragAndDropCommand.create(domain, target, location, operations, operation, source); 479 } 480 } 481 } 482 | Popular Tags |