List of usage examples for java.awt.dnd DropTargetAdapter DropTargetAdapter
DropTargetAdapter
From source file:org.eclipse.swt.snippets.Snippet319.java
public void go() { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Snippet 319"); shell.setBounds(10, 10, 600, 200);/*from www . j a v a2s .com*/ /* 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(); }
From source file:org.pmedv.blackboard.EditorUtils.java
/** * Configures the drop target for new symbols to be dragged from the symbol * table to the editor.// w w w . ja v a 2 s . co m */ public static DropTarget configureDropTarget(final BoardEditor editor, final int raster) { DropTarget dt = new DropTarget(editor, new DropTargetAdapter() { final DataFlavor flavors[] = { DataFlavor.imageFlavor }; @Override public void drop(DropTargetDropEvent e) { int xloc = BoardUtil.snap((int) e.getLocation().getX(), raster); int yloc = BoardUtil.snap((int) e.getLocation().getY(), raster); SymbolBean symbol = null; try { symbol = (SymbolBean) e.getTransferable().getTransferData(flavors[0]); } catch (Exception e1) { e1.printStackTrace(); } Symbol sym = new Symbol(symbol); sym.setXLoc(xloc); sym.setYLoc(yloc); for (Item subItem : sym.getItems()) { if (subItem instanceof Line) { Line line = (Line) subItem; line.setStart(new Point((int) line.getStart().getX() + xloc, (int) line.getStart().getY() + yloc)); line.setEnd( new Point((int) line.getEnd().getX() + +xloc, (int) line.getEnd().getY() + yloc)); line.setOldstart(new Point(line.getStart())); line.setOldEnd(new Point(line.getEnd())); } else { subItem.setXLoc(subItem.getXLoc() + xloc); subItem.setYLoc(subItem.getYLoc() + yloc); subItem.setOldXLoc(subItem.getXLoc()); subItem.setOldYLoc(subItem.getYLoc()); subItem.setOldWidth(subItem.getWidth()); subItem.setOldHeight(subItem.getHeight()); } } editor.getModel().getCurrentLayer().getItems().add(sym); sym.setLayer(editor.getModel().getCurrentLayer().getIndex()); editor.refresh(); e.dropComplete(true); e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); // Switch to select mode after drop ctx.getBean(SetSelectModeCommand.class).execute(null); if (!editor.getUndoManager().addEdit(new AddItemEdit(sym))) { log.info("Could not add edit " + this.getClass()); } editor.setFileState(FileState.DIRTY); ctx.getBean(RedoCommand.class).setEnabled(editor.getUndoManager().canRedo()); ctx.getBean(UndoCommand.class).setEnabled(editor.getUndoManager().canUndo()); } }); return dt; }
From source file:tk.tomby.tedit.core.Workspace.java
/** * Creates a new WorkSpace object./*from w w w . ja va 2 s.c om*/ */ public Workspace() { super(); setLayout(new BorderLayout()); setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); MessageManager.addMessageListener(MessageManager.BUFFER_GROUP_NAME, new IMessageListener<BufferMessage>() { public void receiveMessage(BufferMessage message) { Workspace.this.receiveMessage(message); } }); MessageManager.addMessageListener(MessageManager.PREFERENCE_GROUP_NAME, new IMessageListener<PreferenceMessage>() { public void receiveMessage(PreferenceMessage message) { Workspace.this.receiveMessage(message); } }); bufferPane = new JTabbedPane(); bufferPane.setMinimumSize(new Dimension(600, 400)); bufferPane.setPreferredSize(new Dimension(800, 600)); bufferPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); bottomPort = createDockingPort(0, 80); rightPort = createDockingPort(80, 0); leftPort = createDockingPort(80, 0); splitPaneRight = createSplitPane(JSplitPane.HORIZONTAL_SPLIT, bufferPane, rightPort); splitPaneLeft = createSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPort, splitPaneRight); splitPaneBottom = createSplitPane(JSplitPane.VERTICAL_SPLIT, splitPaneLeft, bottomPort); bufferPane.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent evt) { MessageManager .sendMessage(new WorkspaceMessage(evt.getSource(), bufferPane.getSelectedComponent())); } }); bufferPane.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { if (evt.getClickCount() == 2) { splitPaneLeft.setDividerLocation(0.0d); splitPaneBottom.setDividerLocation(1.0d); splitPaneRight.setDividerLocation(1.0d); } } }); DropTarget dropTarget = new DropTarget(bufferPane, new DropTargetAdapter() { public void drop(DropTargetDropEvent dtde) { if (log.isDebugEnabled()) { log.debug("drop start"); } try { if (log.isDebugEnabled()) { log.debug(dtde.getSource()); } Transferable tr = dtde.getTransferable(); DataFlavor[] flavors = tr.getTransferDataFlavors(); for (int i = 0; i < flavors.length; i++) { DataFlavor flavor = flavors[i]; if (log.isDebugEnabled()) { log.debug("mime-type:" + flavor.getMimeType()); } if (flavor.isMimeTypeEqual("text/plain")) { final Object obj = tr.getTransferData(flavor); if (log.isDebugEnabled()) { log.debug(obj); } if (obj instanceof String) { TaskManager.execute(new Runnable() { public void run() { BufferFactory factory = new BufferFactory(); IBuffer buffer = factory.createBuffer(); buffer.open((String) obj); addBuffer(buffer); } }); } dtde.dropComplete(true); return; } } } catch (UnsupportedFlavorException e) { log.warn(e.getMessage(), e); } catch (IOException e) { log.warn(e.getMessage(), e); } dtde.rejectDrop(); if (log.isDebugEnabled()) { log.debug("drop end"); } } }); bufferPane.setDropTarget(dropTarget); this.add(BorderLayout.CENTER, splitPaneBottom); }