import java.awt.Canvas; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Frame; import java.awt.Graphics; import java.awt.Point; import java.awt.dnd.DropTarget; import java.awt.dnd.DropTargetDragEvent; import java.awt.dnd.DropTargetDropEvent; import java.awt.dnd.DropTargetEvent; import java.awt.dnd.DropTargetListener; import java.awt.geom.Rectangle2D; import javax.swing.SwingUtilities; import org.eclipse.swt.SWT; import org.eclipse.swt.awt.SWT_AWT; import org.eclipse.swt.dnd.DND; import org.eclipse.swt.dnd.DragSource; import org.eclipse.swt.dnd.DragSourceEffect; import org.eclipse.swt.dnd.DragSourceEvent; import org.eclipse.swt.dnd.DragSourceListener; import org.eclipse.swt.dnd.TextTransfer; import org.eclipse.swt.dnd.Transfer; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class MacDNDTestSnippet { public static void main(String[] args) { new MacDNDTestSnippet().run(); } private void run() { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new GridLayout()); Composite composite = new Composite(shell, SWT.NONE); composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); composite.setLayout(new GridLayout()); createHeaderPanel(composite, new GridData(SWT.FILL, SWT.FILL, true, false)); // to reproduce menu and toolbar space createDndPanels(composite, new GridData(SWT.FILL, SWT.FILL, true, true)); // to // reproduce // drop // target // panel // and // drag // source setLocation(shell); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } private void setLocation(Shell shell) { Rectangle monitorSize = shell.getMonitor().getClientArea(); int width = 2 * monitorSize.width / 3; int height = 2 * monitorSize.height / 3; shell.setBounds((monitorSize.width + width) / 2, (monitorSize.height + height) / 2, width, height); } private void createHeaderPanel(Composite parent, Object layoutData) { Text header = new Text(parent, SWT.READ_ONLY | SWT.MULTI); header.setText("Menu 1 | Menu 2 | Menu 3\nToolbar buttons..."); header.setLayoutData(layoutData); } private void createDndPanels(Composite parent, Object layoutData) { Composite composite = new Composite(parent, SWT.NONE); composite.setLayoutData(layoutData); GridLayout gridLayout = new GridLayout(); gridLayout.marginWidth = gridLayout.marginWidth = 0; composite.setLayout(gridLayout); DropZonePanel panel = createAWTComp(composite, new GridData(SWT.FILL, SWT.FILL, true, true)); installDrop(panel); Label label = createSWTComp(composite, new GridData(SWT.FILL, SWT.FILL, true, false)); installDrag(label, panel); } private void installDrag(Label label, final DropZonePanel panel) { new DragSourceEffect(label) { @Override public void dragStart(DragSourceEvent event) { System.out.println("Drag start on SWT"); super.dragStart(event); } @Override public void dragSetData(DragSourceEvent event) { System.out.println("Drag setData on SWT"); super.dragSetData(event); } @Override public void dragFinished(DragSourceEvent event) { System.out.println("Drag finished"); super.dragFinished(event); } }; final DragSource dragSource = new DragSource(label, DND.DROP_COPY | DND.DROP_MOVE); Transfer[] dragTranferTypes = new Transfer[] { TextTransfer .getInstance() }; dragSource.setTransfer(dragTranferTypes); dragSource.addDragListener(new DragSourceListener() { @Override public void dragStart(DragSourceEvent event) { System.out.println("Drag start on SWT Listener"); panel.init(); } @Override public void dragSetData(DragSourceEvent event) { System.out.println("Drag set data on SWT Listener"); event.data = "test"; event.doit = true; } @Override public void dragFinished(DragSourceEvent event) { System.out.println("Drag finished on SWT Listener"); } }); } private void installDrop(final DropZonePanel panel) { DropTarget dropTarget = new DropTarget(panel, new DropTargetListener() { @Override public void dropActionChanged(DropTargetDragEvent dtde) { System.out.println("drop Action changed on AWT"); } @Override public void drop(DropTargetDropEvent dtde) { System.out.println("drop on AWT"); dtde.rejectDrop(); } @Override public void dragOver(DropTargetDragEvent dtde) { System.out.println("drag over on AWT - Relative to component Mouse location / Target bounds: " + dtde.getLocation() + " / " + dtde.getDropTargetContext().getComponent().getBounds()); System.out.println("drag over on AWT - Relative to screen Mouse location / Target bounds: " + getRTSLocation(dtde) + " / " + getRTSBounds(dtde)); System.out.println("drag over on AWT - Relative to screen Frame bounds: " + getFrameBounds(dtde)); panel.update(dtde.getLocation()); } private java.awt.Rectangle getFrameBounds(DropTargetDragEvent dtde) { Component component = dtde.getDropTargetContext().getComponent(); Container frame = SwingUtilities.getAncestorOfClass(Frame.class, component); java.awt.Rectangle bounds = frame.getBounds(); bounds.setLocation(frame.getLocationOnScreen()); return bounds; } private java.awt.Rectangle getRTSBounds(DropTargetDragEvent dtde) { Component component = dtde.getDropTargetContext().getComponent(); java.awt.Rectangle bounds = component.getBounds(); bounds.setLocation(component.getLocationOnScreen()); return bounds; } private Point getRTSLocation(DropTargetDragEvent dtde) { Point p = dtde.getLocation(); SwingUtilities.convertPointToScreen(p, dtde.getDropTargetContext().getComponent()); return p; } @Override public void dragExit(DropTargetEvent dte) { System.out.println("drag exit on AWT - Target bounds: " + dte.getDropTargetContext().getComponent().getBounds() ); } @Override public void dragEnter(DropTargetDragEvent dtde) { System.out.println("drag enter on AWT - Mouse location: " + dtde.getLocation() + " - Target bounds: " + dtde.getDropTargetContext().getComponent().getBounds() ); } }); panel.setDropTarget(dropTarget); } private DropZonePanel createAWTComp(final Composite parent, Object layoutData) { final Composite composite = new Composite(parent, SWT.EMBEDDED | SWT.NO_BACKGROUND); composite.setLayoutData(layoutData); final DropZonePanel panel = new DropZonePanel(); panel.setBackground(Color.GREEN); SWT_AWT.embeddedFrameClass = "sun.lwawt.macosx.CViewEmbeddedFrame"; final Frame frame = SWT_AWT.new_Frame(composite); frame.add(panel); return panel; } private Label createSWTComp(Composite parent, Object layoutData) { Label label = new Label(parent, SWT.NONE); label.setLayoutData(layoutData); label.setText("Some text... (Drag me to green area)"); return label; } private static class DropZonePanel extends Canvas { private static final long serialVersionUID = 1L; private java.awt.Rectangle rectangle; public DropZonePanel() { } public void init() { if (rectangle != null) { rectangle = null; repaint(); } } public void update(Point location) { if (rectangle == null) { rectangle = new java.awt.Rectangle(location.x, location.y, 1, 1); } else { Rectangle2D.union(rectangle, new java.awt.Rectangle(location.x, location.y, 1, 1), rectangle); } repaint(); } @Override public void paint(Graphics g) { super.paint(g); if (rectangle != null) { g.setColor(Color.RED); g.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height); } } } }