use of org.eclipse.swt.dnd.DragSource in project eclipse.platform.text by eclipse.
the class AbstractTextEditor method installTextDragAndDrop.
/**
* Installs text drag and drop on the given source viewer.
*
* @param viewer the viewer
* @since 3.3
*/
protected void installTextDragAndDrop(final ISourceViewer viewer) {
if (viewer == null || fIsTextDragAndDropInstalled)
return;
final IDragAndDropService dndService = getSite().getService(IDragAndDropService.class);
if (dndService == null)
return;
final StyledText st = viewer.getTextWidget();
// Install drag source
final ISelectionProvider selectionProvider = viewer.getSelectionProvider();
final DragSource source = new DragSource(st, DND.DROP_COPY | DND.DROP_MOVE);
source.setTransfer(new Transfer[] { TextTransfer.getInstance() });
source.addDragListener(new DragSourceAdapter() {
String fSelectedText;
Point fSelection;
@Override
public void dragStart(DragSourceEvent event) {
fTextDragAndDropToken = null;
try {
fSelection = st.getSelection();
event.doit = isLocationSelected(new Point(event.x, event.y));
ISelection selection = selectionProvider.getSelection();
if (selection instanceof ITextSelection)
fSelectedText = ((ITextSelection) selection).getText();
else
// fallback to widget
fSelectedText = st.getSelectionText();
} catch (IllegalArgumentException ex) {
event.doit = false;
}
}
private boolean isLocationSelected(Point point) {
// FIXME: https://bugs.eclipse.org/bugs/show_bug.cgi?id=260922
if (isBlockSelectionModeEnabled())
return false;
int offset = st.getOffsetAtPoint(point);
Point p = st.getLocationAtOffset(offset);
if (p.x > point.x)
offset--;
return offset >= fSelection.x && offset < fSelection.y;
}
@Override
public void dragSetData(DragSourceEvent event) {
event.data = fSelectedText;
// Can be any non-null object
fTextDragAndDropToken = this;
}
@Override
public void dragFinished(DragSourceEvent event) {
try {
if (event.detail == DND.DROP_MOVE && validateEditorInputState()) {
Point newSelection = st.getSelection();
int length = fSelection.y - fSelection.x;
int delta = 0;
if (newSelection.x < fSelection.x)
delta = length;
// $NON-NLS-1$
st.replaceTextRange(fSelection.x + delta, length, "");
if (fTextDragAndDropToken == null) {
// Move in same editor - end compound change
IRewriteTarget target = getAdapter(IRewriteTarget.class);
if (target != null)
target.endCompoundChange();
}
}
} finally {
fTextDragAndDropToken = null;
}
}
});
// Install drag target
DropTargetListener dropTargetListener = new DropTargetAdapter() {
private Point fSelection;
@Override
public void dragEnter(DropTargetEvent event) {
fTextDragAndDropToken = null;
fSelection = st.getSelection();
if (event.detail == DND.DROP_DEFAULT) {
if ((event.operations & DND.DROP_MOVE) != 0) {
event.detail = DND.DROP_MOVE;
} else if ((event.operations & DND.DROP_COPY) != 0) {
event.detail = DND.DROP_COPY;
} else {
event.detail = DND.DROP_NONE;
}
}
}
@Override
public void dragOperationChanged(DropTargetEvent event) {
if (event.detail == DND.DROP_DEFAULT) {
if ((event.operations & DND.DROP_MOVE) != 0) {
event.detail = DND.DROP_MOVE;
} else if ((event.operations & DND.DROP_COPY) != 0) {
event.detail = DND.DROP_COPY;
} else {
event.detail = DND.DROP_NONE;
}
}
}
@Override
public void dragOver(DropTargetEvent event) {
event.feedback |= DND.FEEDBACK_SCROLL;
}
@Override
public void drop(DropTargetEvent event) {
try {
if (fTextDragAndDropToken != null && event.detail == DND.DROP_MOVE) {
// Move in same editor
int caretOffset = st.getCaretOffset();
if (fSelection.x <= caretOffset && caretOffset <= fSelection.y) {
event.detail = DND.DROP_NONE;
return;
}
// Start compound change
IRewriteTarget target = getAdapter(IRewriteTarget.class);
if (target != null)
target.beginCompoundChange();
}
if (!validateEditorInputState()) {
event.detail = DND.DROP_NONE;
return;
}
String text = (String) event.data;
if (isBlockSelectionModeEnabled()) {
// FIXME fix block selection and DND
// if (fTextDNDColumnSelection != null && fTextDragAndDropToken != null && event.detail == DND.DROP_MOVE) {
// // DND_MOVE within same editor - remove origin before inserting
// Rectangle newSelection= st.getColumnSelection();
// st.replaceColumnSelection(fTextDNDColumnSelection, ""); //$NON-NLS-1$
// st.replaceColumnSelection(newSelection, text);
// st.setColumnSelection(newSelection.x, newSelection.y, newSelection.x + fTextDNDColumnSelection.width - fTextDNDColumnSelection.x, newSelection.y + fTextDNDColumnSelection.height - fTextDNDColumnSelection.y);
// } else {
// Point newSelection= st.getSelection();
// st.insert(text);
// IDocument document= getDocumentProvider().getDocument(getEditorInput());
// int startLine= st.getLineAtOffset(newSelection.x);
// int startColumn= newSelection.x - st.getOffsetAtLine(startLine);
// int endLine= startLine + document.computeNumberOfLines(text);
// int endColumn= startColumn + TextUtilities.indexOf(document.getLegalLineDelimiters(), text, 0)[0];
// st.setColumnSelection(startColumn, startLine, endColumn, endLine);
// }
} else {
Point newSelection = st.getSelection();
try {
int modelOffset = widgetOffset2ModelOffset(viewer, newSelection.x);
viewer.getDocument().replace(modelOffset, 0, text);
} catch (BadLocationException e) {
return;
}
st.setSelectionRange(newSelection.x, text.length());
}
} finally {
fTextDragAndDropToken = null;
}
}
};
dndService.addMergedDropTarget(st, DND.DROP_MOVE | DND.DROP_COPY, new Transfer[] { TextTransfer.getInstance() }, dropTargetListener);
fIsTextDragAndDropInstalled = true;
}
use of org.eclipse.swt.dnd.DragSource in project eclipse.platform.swt by eclipse.
the class Bug497705_BrowserDragDetect method setDrag.
public static void setDrag(final Browser browser) {
Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK;
final DragSource source = new DragSource(browser, operations);
source.setTransfer(types);
source.addDragListener(new DragSourceListener() {
@Override
public void dragStart(DragSourceEvent event) {
event.doit = (browser.getText().length() != 0);
// not ran.
System.out.println("Drag started");
}
@Override
public void dragSetData(DragSourceEvent event) {
event.data = browser.getText();
}
@Override
public void dragFinished(DragSourceEvent event) {
if (event.detail == DND.DROP_MOVE)
browser.setText("");
}
});
}
use of org.eclipse.swt.dnd.DragSource in project eclipse.platform.swt by eclipse.
the class DNDExample method createDragWidget.
private void createDragWidget(Composite parent) {
parent.setLayout(new FormLayout());
Combo combo = new Combo(parent, SWT.READ_ONLY);
combo.setItems("Toggle Button", "Radio Button", "Checkbox", "Canvas", "Label", "List", "Table", "Tree", "Text", "StyledText", "Combo");
combo.select(LABEL);
dragControlType = combo.getSelectionIndex();
dragControl = createWidget(dragControlType, parent, "Drag Source");
combo.addSelectionListener(widgetSelectedAdapter(e -> {
Object data = dragControl.getLayoutData();
Composite dragParent = dragControl.getParent();
dragControl.dispose();
Combo c = (Combo) e.widget;
dragControlType = c.getSelectionIndex();
dragControl = createWidget(dragControlType, dragParent, "Drag Source");
dragControl.setLayoutData(data);
if (dragEnabled)
createDragSource();
dragParent.layout();
}));
Button b = new Button(parent, SWT.CHECK);
b.setText("DragSource");
b.addSelectionListener(widgetSelectedAdapter(e -> {
Button b1 = (Button) e.widget;
dragEnabled = b1.getSelection();
if (dragEnabled) {
createDragSource();
} else {
if (dragSource != null) {
dragSource.dispose();
}
dragSource = null;
}
}));
b.setSelection(true);
dragEnabled = true;
FormData data = new FormData();
data.top = new FormAttachment(0, 10);
data.bottom = new FormAttachment(combo, -10);
data.left = new FormAttachment(0, 10);
data.right = new FormAttachment(100, -10);
dragControl.setLayoutData(data);
data = new FormData();
data.bottom = new FormAttachment(100, -10);
data.left = new FormAttachment(0, 10);
combo.setLayoutData(data);
data = new FormData();
data.bottom = new FormAttachment(100, -10);
data.left = new FormAttachment(combo, 10);
b.setLayoutData(data);
}
use of org.eclipse.swt.dnd.DragSource in project eclipse.platform.swt by eclipse.
the class FileViewer method createTreeDragSource.
/**
* Creates the Drag & Drop DragSource for items being dragged from the tree.
*
* @return the DragSource for the tree
*/
private DragSource createTreeDragSource(final Tree tree) {
DragSource dragSource = new DragSource(tree, DND.DROP_MOVE | DND.DROP_COPY);
dragSource.setTransfer(FileTransfer.getInstance());
dragSource.addDragListener(new DragSourceListener() {
TreeItem[] dndSelection = null;
String[] sourceNames = null;
@Override
public void dragStart(DragSourceEvent event) {
dndSelection = tree.getSelection();
sourceNames = null;
event.doit = dndSelection.length > 0;
isDragging = true;
processedDropFiles = null;
}
@Override
public void dragFinished(DragSourceEvent event) {
dragSourceHandleDragFinished(event, sourceNames);
dndSelection = null;
sourceNames = null;
isDragging = false;
processedDropFiles = null;
handleDeferredRefresh();
}
@Override
public void dragSetData(DragSourceEvent event) {
if (dndSelection == null || dndSelection.length == 0)
return;
if (!FileTransfer.getInstance().isSupportedType(event.dataType))
return;
sourceNames = new String[dndSelection.length];
for (int i = 0; i < dndSelection.length; i++) {
File file = (File) dndSelection[i].getData(TREEITEMDATA_FILE);
sourceNames[i] = file.getAbsolutePath();
}
event.data = sourceNames;
}
});
return dragSource;
}
use of org.eclipse.swt.dnd.DragSource in project eclipse.platform.swt by eclipse.
the class Bug66356_DND_move_reported_to_drag_source_even_though_cancelled method main.
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Button source = new Button(shell, SWT.PUSH);
source.setText("Source");
DragSource dragSource = new DragSource(source, DND.DROP_MOVE);
dragSource.setTransfer(new Transfer[] { TextTransfer.getInstance() });
dragSource.addDragListener(new DragSourceAdapter() {
@Override
public void dragSetData(DragSourceEvent event) {
event.data = "hello";
}
@Override
public void dragFinished(DragSourceEvent event) {
System.out.println("Move = " + (event.detail == DND.DROP_MOVE));
}
});
Button dest = new Button(shell, SWT.PUSH);
dest.setText("Dest");
DropTarget dropTarget = new DropTarget(dest, DND.DROP_MOVE);
dropTarget.setTransfer(new Transfer[] { TextTransfer.getInstance() });
// dropTarget.addDropListener(new DropTargetAdapter() {
// public void drop(DropTargetEvent event) {
// event.detail = DND.DROP_NONE;
// }
// });
// dropTarget.addDropListener(new DropTargetListener() {
//
// @Override
// public void dropAccept(DropTargetEvent event) {
// // TD Auto-generated method stub
// event.detail = DND.DROP_NONE;
//
// }
//
// @Override
// public void drop(DropTargetEvent event) {
// // TD Auto-generated method stub
//
// }
//
// @Override
// public void dragOver(DropTargetEvent event) {
// // TD Auto-generated method stub
//
// }
//
// @Override
// public void dragOperationChanged(DropTargetEvent event) {
// // TD Auto-generated method stub
//
// }
//
// @Override
// public void dragLeave(DropTargetEvent event) {
// // TD Auto-generated method stub
//
// }
//
// @Override
// public void dragEnter(DropTargetEvent event) {
// // TD Auto-generated method stub
//
// }
// });
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
Aggregations