DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

DragAndDrop SWT Example

08.30.2006
| 10956 views |
  • submit to reddit
        // This code implements a very basic Drag'n'Drop
// you can drag the labels from the top into the canvas
// and the text will be drawn there. The positioning isn't exact
// but who cares...

import java.util.HashMap;

import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DragSourceListener;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.DropTargetListener;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class DragAndDropTool {

	private Display display;
	private Shell shell;
	private HashMap<String, Image> hashImages;
	private Label drag1;
	private Label drag2;
	private Label drag3;
	private Canvas text;

	public DragAndDropTool() {
		// Display & Shell holen
		display = new Display();
		shell = new Shell(display);

		shell.setText("DnD Toolbar");
		shell.setSize(500, 300);
		
		init();
		createGUI();
		
		// Shell öffnen
		shell.open();
		
		// Event-Schleife starten
		while(!shell.isDisposed()) {
			if(!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
	
	private void init() {
		hashImages = new HashMap<String, Image>();
		hashImages.put("action", new Image(display, "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\aktion.png"));
		hashImages.put("help", new Image(display, "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\help.png"));
		hashImages.put("about", new Image(display, "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\about_kde.png"));		
	}

	private void createGUI() {
		shell.setLayout(new GridLayout(3, false));

		drag1 = new Label(shell, SWT.BORDER);
		drag1.setText("drag source #1");
		drag1.setLayoutData(new GridData());
		DragSource dragSource1 = new DragSource(drag1, DND.DROP_COPY);
		Transfer[] transfers1 = new Transfer[] {TextTransfer.getInstance()};
		dragSource1.setTransfer(transfers1);
		dragSource1.addDragListener(new DragSourceListener() {
			public void dragStart(DragSourceEvent event) {
				if(drag1.getText().length() == 0) {
					event.doit = false;
				}
			}

			public void dragSetData(DragSourceEvent event) {
				if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
					event.data = drag1.getText();
				}
			}

			public void dragFinished(DragSourceEvent event) {}
		});

		drag2 = new Label(shell, SWT.BORDER);
		drag2.setText("drag source #2");
		drag2.setLayoutData(new GridData());
		DragSource dragSource2 = new DragSource(drag2, DND.DROP_COPY);
		Transfer[] transfers2 = new Transfer[] {TextTransfer.getInstance()};
		dragSource2.setTransfer(transfers2);
		dragSource2.addDragListener(new DragSourceListener() {
			public void dragStart(DragSourceEvent event) {
				if(drag2.getText().length() == 0) {
					event.doit = false;
				}
			}

			public void dragSetData(DragSourceEvent event) {
				if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
					event.data = drag2.getText();
				}
			}

			public void dragFinished(DragSourceEvent event) {}
		});

		drag3 = new Label(shell, SWT.BORDER);
		drag3.setText("drag source #3");
		drag3.setLayoutData(new GridData());
		DragSource dragSource3 = new DragSource(drag3, DND.DROP_COPY | DND.DROP_DEFAULT);
		Transfer[] transfers3 = new Transfer[] {TextTransfer.getInstance()};
		dragSource3.setTransfer(transfers3);
		dragSource3.addDragListener(new DragSourceListener() {
			public void dragStart(DragSourceEvent event) {
				if(drag3.getText().length() == 0) {
					event.doit = false;
				}
			}

			public void dragSetData(DragSourceEvent event) {
				if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
					event.data = drag3.getText();
				}
			}

			public void dragFinished(DragSourceEvent event) {}
		});
		
		
//		text = new Text(shell, SWT.BORDER | SWT.MULTI);
		text = new Canvas(shell, SWT.BORDER);
		GridData gd = new GridData(GridData.FILL_BOTH);
		gd.horizontalSpan = 3;
		text.setLayoutData(gd);
		DropTarget dropTarget = new DropTarget(text, DND.DROP_COPY | DND.DROP_DEFAULT);
		Transfer[] types = new Transfer[] {TextTransfer.getInstance()};
		dropTarget.setTransfer(types);
		dropTarget.addDropListener(new DropTargetListener() {
			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;
					}
				}
			}

			public void dragLeave(DropTargetEvent event) {}

			public void dragOperationChanged(DropTargetEvent event) {}

			public void dragOver(DropTargetEvent event) {
			}

			public void drop(DropTargetEvent event) {}

			public void dropAccept(DropTargetEvent event) {
				if (TextTransfer.getInstance().isSupportedType(event.currentDataType)) {
					String d = (String)TextTransfer.getInstance().nativeToJava(event.currentDataType);
//					text.append(d + '\n');
					GC gc = new GC(text);
					int x = event.x - shell.getBounds().x - text.getBounds().x;
					int y = event.y - shell.getBounds().y - text.getBounds().y;
//					System.err.println("drop x=" + (event.x - shell.getBounds().x - text.getBounds().x) + ",y=" + (event.y - shell.getBounds().y - text.getBounds().y));
					gc.drawString(d, x, y);
					gc.dispose();
				}
			}
		});
	}

	public static void main(String[] args) {
		new DragAndDropTool();
	}

}