use of org.eclipse.swt.dnd.DragSource in project eclipse.platform.swt by eclipse.
the class Bug35783_DnDMouseExit method main.
public static void main(String[] args) {
final Display display = Display.getDefault();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.addListener(SWT.MouseExit, event -> System.out.println("exit"));
shell.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
System.out.println("down");
}
@Override
public void mouseUp(MouseEvent e) {
System.out.println("up");
}
});
DragSource ds = new DragSource(shell, DND.DROP_COPY);
ds.setTransfer(new Transfer[] { TextTransfer.getInstance() });
ds.addDragListener(new DragSourceAdapter() {
@Override
public void dragStart(DragSourceEvent event) {
System.out.println("Drag Start");
}
});
shell.open();
while (!shell.isDisposed()) if (!display.readAndDispatch())
display.sleep();
}
use of org.eclipse.swt.dnd.DragSource in project eclipse.platform.swt by eclipse.
the class Snippet319 method go.
public void go() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(10, 10, 600, 200);
/* Create SWT controls and add drag source */
final Label swtLabel = new Label(shell, SWT.BORDER);
swtLabel.setBounds(10, 10, 580, 50);
swtLabel.setText("SWT drag source");
DragSource dragSource = new DragSource(swtLabel, DND.DROP_COPY);
dragSource.setTransfer(new MyTypeTransfer());
dragSource.addDragListener(new DragSourceAdapter() {
@Override
public void dragSetData(DragSourceEvent event) {
MyType object = new MyType();
object.name = "content dragged from SWT";
object.time = System.currentTimeMillis();
event.data = object;
}
});
/* Create AWT/Swing controls */
Composite embeddedComposite = new Composite(shell, SWT.EMBEDDED);
embeddedComposite.setBounds(10, 100, 580, 50);
embeddedComposite.setLayout(new FillLayout());
Frame frame = SWT_AWT.new_Frame(embeddedComposite);
final JLabel jLabel = new JLabel("AWT/Swing drop target");
frame.add(jLabel);
/* Register the custom data flavour */
final DataFlavor flavor = new DataFlavor(MIME_TYPE, "MyType custom flavor");
/*
* Note that according to jre/lib/flavormap.properties, the preferred way to
* augment the default system flavor map is to specify the AWT.DnD.flavorMapFileURL
* property in an awt.properties file.
*
* This snippet uses the alternate approach below in order to provide a simple
* stand-alone snippet that demonstrates the functionality. This implementation
* works well, but if the instanceof check below fails for some reason when used
* in a different context then the drop will not be accepted.
*/
FlavorMap map = SystemFlavorMap.getDefaultFlavorMap();
if (map instanceof SystemFlavorMap) {
SystemFlavorMap systemMap = (SystemFlavorMap) map;
systemMap.addFlavorForUnencodedNative(MIME_TYPE, flavor);
}
/* add drop target */
DropTargetListener dropTargetListener = new DropTargetAdapter() {
@Override
public void drop(DropTargetDropEvent dropTargetDropEvent) {
try {
dropTargetDropEvent.acceptDrop(DnDConstants.ACTION_COPY);
ByteArrayInputStream inStream = (ByteArrayInputStream) dropTargetDropEvent.getTransferable().getTransferData(flavor);
int available = inStream.available();
byte[] bytes = new byte[available];
inStream.read(bytes);
MyType object = restoreFromByteArray(bytes);
String string = object.name + ": " + new Date(object.time).toString();
jLabel.setText(string);
} catch (Exception e) {
e.printStackTrace();
}
}
};
new DropTarget(jLabel, dropTargetListener);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
use of org.eclipse.swt.dnd.DragSource in project eclipse.platform.swt by eclipse.
the class Bug186038_DNDActivateEvent method main.
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
shell.setLayout(layout);
// create the drop down widget.shell
final Shell dropDownShell = new Shell(shell, SWT.ON_TOP | SWT.DROP_DOWN);
dropDownShell.setLayout(new RowLayout());
dropDownShell.setVisible(false);
dropDownShell.addListener(SWT.Activate, event -> System.out.println("dropDownShell gets Activate event!"));
dropDownShell.addListener(SWT.Deactivate, event -> {
System.out.println("dropDownShell entering Deactivate event handler and will hide the dropdown widget.shell");
hideDropDown(dropDownShell);
});
dropDownShell.addListener(SWT.Close, event -> hideDropDown(dropDownShell));
// create the button1 and when it is hovered, display the dropdown
final Button button1 = new Button(shell, SWT.PUSH);
button1.setText("Drop target");
button1.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if (!dropDownShell.isVisible()) {
showDropDown(button1, dropDownShell);
}
}
});
int operations = DND.DROP_COPY | DND.DROP_DEFAULT;
DropTarget target = new DropTarget(button1, operations);
// Provide data in Text format
Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
target.setTransfer(types);
target.addDropListener(new DropTargetListener() {
@Override
public void dragEnter(DropTargetEvent event) {
if (event.detail == DND.DROP_DEFAULT) {
if ((event.operations & DND.DROP_COPY) != 0) {
event.detail = DND.DROP_COPY;
} else {
event.detail = DND.DROP_NONE;
}
}
for (int i = 0; i < event.dataTypes.length; i++) {
if (TextTransfer.getInstance().isSupportedType(event.dataTypes[i])) {
event.currentDataType = event.dataTypes[i];
if (event.detail != DND.DROP_COPY) {
event.detail = DND.DROP_NONE;
}
break;
}
}
}
@Override
public void dragOver(DropTargetEvent event) {
event.feedback = DND.FEEDBACK_SELECT;
if (!dropDownShell.isVisible()) {
showDropDown(button1, dropDownShell);
}
}
@Override
public void dragOperationChanged(DropTargetEvent event) {
}
@Override
public void dragLeave(DropTargetEvent event) {
}
@Override
public void dropAccept(DropTargetEvent event) {
}
@Override
public void drop(DropTargetEvent event) {
if (TextTransfer.getInstance().isSupportedType(event.currentDataType)) {
String text = (String) event.data;
System.out.println(text);
}
}
});
// create the button2 as the drag source
final Button button2 = new Button(shell, SWT.PUSH);
button2.setText("Drag source");
operations = DND.DROP_COPY;
DragSource source = new DragSource(button2, operations);
// Provide data in Text format
source.setTransfer(types);
source.addDragListener(new DragSourceListener() {
@Override
public void dragStart(DragSourceEvent event) {
if (button2.getText().length() == 0) {
event.doit = false;
}
}
@Override
public void dragSetData(DragSourceEvent event) {
// Provide the data of the requested type.
if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
event.data = button2.getText();
}
}
@Override
public void dragFinished(DragSourceEvent event) {
}
});
shell.setSize(300, 300);
shell.addDisposeListener(e -> {
if (dropDownShell != null && !dropDownShell.isDisposed()) {
dropDownShell.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
use of org.eclipse.swt.dnd.DragSource in project cogtool by cogtool.
the class ProjectUI method setUpDragAndDrop.
// See http://www.eclipse.org/articles/Article-SWT-DND/DND-in-SWT.html
// for more documentation of SWT drag-and-drop support.
protected void setUpDragAndDrop() {
DragSource treeAsSource = new DragSource(tree, DND.DROP_MOVE | DND.DROP_COPY);
TaskDnDTransfer taskTransfer = TaskDnDTransfer.getInstance();
TaskAppDnDTransfer taskAppTransfer = TaskAppDnDTransfer.getInstance();
Transfer[] types = new Transfer[] { taskTransfer, taskAppTransfer };
treeAsSource.setTransfer(types);
// DropSourceEvent fields:
// dataType:
// the Transfer type of the data the target prefers to receive;
// useful in dragSetData
// detail:
// the operation the target performed; one of:
// DROP_MOVE - move from source to target; remove from source
// DROP_COPY - copy the source to target; leave the source
// DROP_LINK - create a link of the source at the target
// useful in dragFinished in case the source needs to be removed
// doit:
// in dragStart, determines if the operation should proceed
// in dragFinished, may be set to indicate if the operation succeeded
// image:
// may be set to the Image displayed during drag
// x, y: position within the Tree
DragSourceListener srcListener = new TreeDragSourceEffect(tree) {
@Override
public void dragStart(DragSourceEvent evt) {
// If the Transfer type cannot be determined until the drag
// starts, the setTransfer() call can be invoked here.
// Set evt.doit to false here if action is inappropriate.
// Reset, just in case no drag-and-drop should happen
currentDnDSource = null;
// Must be in first column!
TreeColumn column = findColumn(evt.x);
TreeItem row = tree.getItem(new Point(evt.x, evt.y));
if ((column != null) && (column.getData() == null)) {
if ((row != null) && (row.getData() != null)) {
if (((AUndertaking) row.getData()).isSpawned()) {
evt.doit = false;
return;
}
}
if (selection.getSelectedTaskCount() == 0) {
if (row != null) {
selection.setSelectedItem(row);
currentDnDSource = tree;
currentDnDColumn = 0;
}
} else {
currentDnDSource = tree;
currentDnDColumn = 0;
}
} else {
// Must be in cell with a valid TaskApplication!
if ((column != null) && (column.getData() != null)) {
if ((row != null) && (row.getData() != null)) {
Design design = (Design) column.getData();
AUndertaking task = (AUndertaking) row.getData();
TaskApplication taskApp = project.getTaskApplication(task, design);
if (taskApp != null) {
if (!taskApp.getDemonstration().isEditable()) {
evt.doit = false;
return;
}
// set some highlighting of the source cell
selection.setSelectedCell(row, column);
contextSelection.setSelectedDesign(design);
contextSelection.addSelectedTask(task);
currentDnDRow = row;
currentDnDSource = tree;
currentDnDColumn = tree.indexOf(column);
// do not do superclass work!
return;
}
}
}
evt.doit = false;
}
super.dragStart(evt);
}
@Override
public void dragSetData(DragSourceEvent evt) {
// Based on the requested Transfer data type, set evt.data
// if (taskTransfer.isSupportedType(evt.dataType)) {
// evt.data = "This is the requested data";
// }
super.dragSetData(evt);
}
@Override
public void dragFinished(DragSourceEvent evt) {
// Operation was performed by the drop target; clean up
// If needed, evt.detail should be the operation performed.
super.dragFinished(evt);
currentDnDSource = null;
currentDnDColumn = -1;
currentDnDRow = null;
currentDndTaskAppDropRow = null;
}
};
treeAsSource.addDragListener(srcListener);
DropTarget treeAsTarget = new DropTarget(tree, DND.DROP_MOVE | DND.DROP_COPY);
treeAsTarget.setTransfer(types);
// DropTargetEvent fields:
// currentDataType:
// the Transfer type of the data the target prefers to receive;
// can be set -- see the method comments below
// dataTypes:
// the array of Transfer types the source can "send"
// detail:
// the operation the user is trying to perform; one of:
// DROP_MOVE - move from source to target; remove from source
// DROP_COPY - copy the source to target; leave the source
// DROP_LINK - create a link of the source at the target
// DROP_DEFAULT - indicator that target must choose operation
// DROP_NONE - indicator that user is trying an unsupported op
// may be set to the operation the target feels is correct
// (thus, if initially DEFAULT, then the operation that would be
// performed; if initially DEFAULT and not changed, it will appear
// to the user as a MOVE -- also, set to NONE if target determines
// operation is not permitted)
// feedback:
// bitwise OR'ing of feedback effects displayed to the user;
// can be set using the following constants:
// FEEDBACK_SELECT - item under cursor is selected
// FEEDBACK_SCROLL - allows scrolling to make items visible
// FEEDBACK_EXPAND - allows tree items to be expanded
// FEEDBACK_INSERT_BEFORE - insertion mark before item under cursor
// FEEDBACK_INSERT_AFTER - insertion mark after item under cursor
// FEEDBACK_NONE - no feedback
// item:
// TreeItem or TableItem under the cursor, if applicable
// operations:
// bitwise OR'ing of the operations that the DragSource can support
treeAsTarget.addDropListener(new TreeDropTargetEffect(tree) {
protected static final int DRAG_FEEDBACK = DND.FEEDBACK_EXPAND | DND.FEEDBACK_INSERT_BEFORE | DND.FEEDBACK_SCROLL;
protected static final int DRAG_APP_FEEDBACK = DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL;
protected int requestedOp = DND.DROP_MOVE;
@Override
public void dragEnter(DropTargetEvent evt) {
// Set evt.detail to DND.DROP_NONE when the operation is a no-op
// or if the presented type is unacceptable. Other choices
// that make sense: DND.DROP_MOVE, DND.DROP_COPY
// evt.currentDataType is the type preferred by the target.
// evt.dataTypes contains types provided by the source.
super.dragEnter(evt);
if (currentDnDSource != getControl()) {
evt.detail = DND.DROP_NONE;
} else {
requestedOp = evt.detail;
}
}
@Override
public void dragLeave(DropTargetEvent evt) {
if (currentDndTaskAppDropRow != null) {
currentDndTaskAppDropRow.setBackground(currentDnDColumn, ProjectUIModel.unselectedTaskBackgroundColor);
}
super.dragLeave(evt);
}
@Override
public void dragOperationChanged(DropTargetEvent evt) {
// change evt.currentDataType if desired here.
if ((evt.detail != DND.DROP_MOVE) && (evt.detail != DND.DROP_COPY)) {
evt.detail = DND.DROP_NONE;
}
requestedOp = evt.detail;
super.dragOperationChanged(evt);
}
@Override
public void dragOver(DropTargetEvent evt) {
if (currentDndTaskAppDropRow != null) {
currentDndTaskAppDropRow.setBackground(currentDnDColumn, ProjectUIModel.unselectedTaskBackgroundColor);
}
Point toTreeEvtLoc = tree.toControl(evt.x, evt.y);
//System.out.println("dragOver; set feedback here?");
if (currentDnDSource != getControl()) {
evt.detail = DND.DROP_NONE;
evt.feedback = DND.FEEDBACK_NONE;
} else if (currentDnDColumn == 0) {
// Moving tasks
evt.feedback = DRAG_FEEDBACK;
evt.detail = requestedOp;
TreeItem row = tree.getItem(toTreeEvtLoc);
if ((row != null) && (row.getData() != null)) {
if (((AUndertaking) row.getData()).isSpawned()) {
evt.detail = DND.DROP_NONE;
evt.feedback = DND.FEEDBACK_NONE;
}
}
} else {
// Moving task applications
evt.feedback = DRAG_APP_FEEDBACK;
TreeColumn column = findColumn(toTreeEvtLoc.x);
if (column == null) {
evt.detail = DND.DROP_NONE;
} else {
Design design = (Design) column.getData();
if (design != contextSelection.getSelectedDesign()) {
evt.detail = DND.DROP_NONE;
} else {
TreeItem row = tree.getItem(toTreeEvtLoc);
if ((row == null) || (row.getData() == null)) {
evt.detail = DND.DROP_NONE;
} else {
AUndertaking task = (AUndertaking) row.getData();
if (task.isTaskGroup() || task.isSpawned() || contextSelection.isTaskSelected(task)) {
evt.detail = DND.DROP_NONE;
} else {
evt.detail = requestedOp;
currentDndTaskAppDropRow = row;
currentDndTaskAppDropRow.setBackground(currentDnDColumn, CONTEXT_COLOR);
}
}
}
}
}
super.dragOver(evt);
}
@Override
public void dropAccept(DropTargetEvent evt) {
// Can change evt.detail if desired here.
// Provide one last chance to define the type of data that
// will be returned in the drop event; thus, change
// evt.currentDataType if desired here
super.dropAccept(evt);
}
@Override
public void drop(DropTargetEvent evt) {
// When the drop operation is completed, update the
// evt.detail field with the operation performed.
// Do the operation!
AUndertaking beforeTask = null;
if (evt.item != null) {
beforeTask = (AUndertaking) evt.item.getData();
}
if (requestedOp == DND.DROP_COPY) {
if (currentDnDColumn == 0) {
ProjectUI.ChangeTaskPositionParms parms = new ProjectUI.ChangeTaskPositionParms(selection, beforeTask, true);
if (performAction(ProjectLID.DuplicateTaskFull, parms, true)) {
evt.detail = DND.DROP_COPY;
}
} else {
AUndertaking fromTask = (AUndertaking) currentDnDRow.getData();
AUndertaking toTask = (AUndertaking) currentDndTaskAppDropRow.getData();
TreeColumn column = tree.getColumn(currentDnDColumn);
Design design = (Design) column.getData();
ProjectUI.MoveCopyTaskApplicationParms parms = new ProjectUI.MoveCopyTaskApplicationParms(fromTask, toTask, design);
selection.setSelectedCell(currentDndTaskAppDropRow, column);
if (performAction(ProjectLID.DuplicateTaskApplication, parms, true)) {
uiModel.redisplayAllResults();
evt.detail = DND.DROP_COPY;
}
}
} else if (requestedOp == DND.DROP_MOVE) {
if (currentDnDColumn == 0) {
ProjectUI.ChangeTaskPositionParms parms = new ProjectUI.ChangeTaskPositionParms(selection, beforeTask, false);
if (performAction(ProjectLID.ChangeTaskPosition, parms, true)) {
evt.detail = DND.DROP_MOVE;
}
} else {
AUndertaking fromTask = (AUndertaking) currentDnDRow.getData();
AUndertaking toTask = (AUndertaking) currentDndTaskAppDropRow.getData();
TreeColumn column = tree.getColumn(currentDnDColumn);
Design design = (Design) column.getData();
ProjectUI.MoveCopyTaskApplicationParms parms = new ProjectUI.MoveCopyTaskApplicationParms(fromTask, toTask, design);
selection.setSelectedCell(currentDndTaskAppDropRow, column);
if (performAction(ProjectLID.MoveTaskApplication, parms, true)) {
uiModel.redisplayAllResults();
evt.detail = DND.DROP_MOVE;
}
}
}
super.drop(evt);
}
});
}
use of org.eclipse.swt.dnd.DragSource in project tdi-studio-se by Talend.
the class DragAndDropForWebService method createDragSource.
/**
*
* DOC amaumont Comment method "createDragSource".
*
* @param sourceListener
*/
private void createDragSource(DragSourceListener sourceListener) {
if (dragSource != null) {
dragSource.dispose();
}
dragSource = new DragSource(draggableControl, DND.DROP_DEFAULT | DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK);
dragSource.setTransfer(new Transfer[] { TableEntriesTransfer.getInstance() });
dragSource.addDragListener(sourceListener);
}
Aggregations