|
1 | 1 | /******************************************************************************* |
2 | | - * Copyright (c) 2000, 2021 IBM Corporation and others. |
| 2 | + * Copyright (c) 2000, 2026 IBM Corporation and others. |
3 | 3 | * |
4 | 4 | * This program and the accompanying materials |
5 | 5 | * are made available under the terms of the Eclipse Public License 2.0 |
|
27 | 27 | import org.eclipse.core.commands.ExecutionException; |
28 | 28 | import org.eclipse.core.commands.IHandler; |
29 | 29 | import org.eclipse.core.commands.operations.IUndoContext; |
| 30 | +import org.eclipse.debug.core.DebugException; |
| 31 | +import org.eclipse.debug.core.model.IVariable; |
| 32 | +import org.eclipse.debug.core.model.IWatchExpression; |
| 33 | +import org.eclipse.debug.internal.ui.DebugUIPlugin; |
30 | 34 | import org.eclipse.debug.ui.DebugUITools; |
31 | 35 | import org.eclipse.debug.ui.IDebugUIConstants; |
32 | 36 | import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants; |
|
62 | 66 | import org.eclipse.jface.text.IUndoManagerExtension; |
63 | 67 | import org.eclipse.jface.text.source.ISourceViewer; |
64 | 68 | import org.eclipse.jface.util.IPropertyChangeListener; |
| 69 | +import org.eclipse.jface.util.LocalSelectionTransfer; |
65 | 70 | import org.eclipse.jface.util.PropertyChangeEvent; |
| 71 | +import org.eclipse.jface.viewers.ISelection; |
66 | 72 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
| 73 | +import org.eclipse.jface.viewers.IStructuredSelection; |
67 | 74 | import org.eclipse.jface.viewers.SelectionChangedEvent; |
68 | 75 | import org.eclipse.swt.SWT; |
69 | 76 | import org.eclipse.swt.custom.StyledText; |
70 | 77 | import org.eclipse.swt.dnd.DND; |
71 | 78 | import org.eclipse.swt.dnd.DragSource; |
72 | 79 | import org.eclipse.swt.dnd.DragSourceAdapter; |
| 80 | +import org.eclipse.swt.dnd.DropTarget; |
| 81 | +import org.eclipse.swt.dnd.DropTargetAdapter; |
| 82 | +import org.eclipse.swt.dnd.DropTargetEvent; |
73 | 83 | import org.eclipse.swt.dnd.TextTransfer; |
74 | 84 | import org.eclipse.swt.dnd.Transfer; |
75 | 85 | import org.eclipse.swt.widgets.Composite; |
@@ -488,17 +498,87 @@ public void init(IViewSite site, IMemento memento) throws PartInitException { |
488 | 498 | * |
489 | 499 | * @since 3.4 |
490 | 500 | */ |
491 | | - protected void initDragAndDrop() { |
492 | | - // Drag only |
493 | | - DragSource ds = new DragSource(fSourceViewer.getTextWidget(), DND.DROP_COPY | DND.DROP_MOVE); |
494 | | - ds.setTransfer(new Transfer[] {TextTransfer.getInstance()}); |
495 | | - ds.addDragListener(new DragSourceAdapter() { |
496 | | - @Override |
| 501 | + protected void initDragAndDrop() { |
| 502 | + StyledText text = fSourceViewer.getTextWidget(); |
| 503 | + // Drag & Drop |
| 504 | + DragSource ds = new DragSource(fSourceViewer.getTextWidget(), DND.DROP_COPY | DND.DROP_MOVE); |
| 505 | + ds.setTransfer(new Transfer[] { TextTransfer.getInstance() }); |
| 506 | + ds.addDragListener(new DragSourceAdapter() { |
| 507 | + @Override |
497 | 508 | public void dragSetData(org.eclipse.swt.dnd.DragSourceEvent event) { |
498 | | - event.data = fSourceViewer.getTextWidget().getSelectionText(); |
499 | | - } |
500 | | - }); |
501 | | - } |
| 509 | + event.data = fSourceViewer.getTextWidget().getSelectionText(); |
| 510 | + } |
| 511 | + }); |
| 512 | + |
| 513 | + DropTarget dropTarget = new DropTarget(text, DND.DROP_COPY | DND.DROP_MOVE); |
| 514 | + dropTarget.setTransfer(new Transfer[] { LocalSelectionTransfer.getTransfer() }); |
| 515 | + dropTarget.addDropListener(new DropTargetAdapter() { |
| 516 | + |
| 517 | + @Override |
| 518 | + public void dragEnter(DropTargetEvent event) { |
| 519 | + normalizeDrop(event); |
| 520 | + } |
| 521 | + |
| 522 | + @Override |
| 523 | + public void dragOver(DropTargetEvent event) { |
| 524 | + normalizeDrop(event); |
| 525 | + } |
| 526 | + |
| 527 | + @Override |
| 528 | + public void drop(DropTargetEvent event) { |
| 529 | + normalizeDrop(event); |
| 530 | + String dropped = null; |
| 531 | + if (TextTransfer.getInstance().isSupportedType(event.currentDataType)) { |
| 532 | + dropped = (String) event.data; |
| 533 | + } |
| 534 | + if (dropped == null && LocalSelectionTransfer.getTransfer().isSupportedType(event.currentDataType)) { |
| 535 | + ISelection selection = LocalSelectionTransfer.getTransfer().getSelection(); |
| 536 | + |
| 537 | + if (selection instanceof IStructuredSelection sel) { |
| 538 | + List<Object> selections = sel.toList(); |
| 539 | + StringBuilder build = new StringBuilder(); |
| 540 | + for (Object obj : selections) { |
| 541 | + if (obj instanceof IVariable var) { |
| 542 | + try { |
| 543 | + build.append(var.getName()); |
| 544 | + } catch (DebugException e) { |
| 545 | + DebugUIPlugin.log(e); |
| 546 | + } |
| 547 | + } else if (obj instanceof IWatchExpression exp) { |
| 548 | + build.append(exp.getExpressionText()); |
| 549 | + } |
| 550 | + if (selections.size() > 1) { |
| 551 | + build.append(System.lineSeparator()); |
| 552 | + } |
| 553 | + } |
| 554 | + dropped = build.toString(); |
| 555 | + } |
| 556 | + } |
| 557 | + |
| 558 | + if (dropped == null || dropped.isEmpty()) { |
| 559 | + return; |
| 560 | + } |
| 561 | + |
| 562 | + StyledText text = fSourceViewer.getTextWidget(); |
| 563 | + int start = text.getSelection().x; |
| 564 | + int len = text.getSelection().y - start; |
| 565 | + text.replaceTextRange(start, len, dropped); |
| 566 | + text.setCaretOffset(start + dropped.length()); |
| 567 | + text.setFocus(); |
| 568 | + } |
| 569 | + |
| 570 | + private void normalizeDrop(DropTargetEvent event) { |
| 571 | + if (event.detail == DND.DROP_NONE) { |
| 572 | + event.detail = DND.DROP_COPY; |
| 573 | + } |
| 574 | + |
| 575 | + if (LocalSelectionTransfer.getTransfer().isSupportedType(event.currentDataType)) { |
| 576 | + return; |
| 577 | + } |
| 578 | + event.detail = DND.DROP_NONE; |
| 579 | + } |
| 580 | + }); |
| 581 | + } |
502 | 582 |
|
503 | 583 | /** |
504 | 584 | * Returns the entire trimmed contents of the current document. |
|
0 commit comments