Example usage for java.awt.dnd DropTargetDropEvent getLocation

List of usage examples for java.awt.dnd DropTargetDropEvent getLocation

Introduction

In this page you can find the example usage for java.awt.dnd DropTargetDropEvent getLocation.

Prototype


public Point getLocation() 

Source Link

Document

This method returns a Point indicating the Cursor 's current location in the Component 's coordinates.

Usage

From source file:org.kuali.test.ui.dnd.RepositoryDropTargetAdapter.java

@Override
public void drop(DropTargetDropEvent dtde) {
    boolean success = false;
    try {// w  w w.  j a v  a2  s.  c  o m
        if (canDrop(dtde)) {
            Transferable t = dtde.getTransferable();
            Object data = t.getTransferData(t.getTransferDataFlavors()[0]);
            DefaultMutableTreeNode dropNode = getNodeAtPoint(dtde.getLocation());
            if ((dropNode != null) && (data != null) && (data instanceof RepositoryTransferData)) {
                repositoryTree.handleDataDrop(t.getTransferDataFlavors()[0], (RepositoryTransferData) data,
                        dropNode);
                success = true;
                dtde.dropComplete(true);
            }
        }
    }

    catch (Exception e) {
        LOG.error(e.toString(), e);
    }

    if (!success) {
        dtde.rejectDrop();
    }
}

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  .j av  a  2s.c  o  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:org.rdv.datapanel.DigitalTabularDataPanel.java

/**
 * an overridden method from/*w ww. j  a v  a  2s  .  c  o m*/
 * 
 * @see org.rdv.datapanel.AbstractDataPanel for the
 * @see DropTargetListener interface
 */
@SuppressWarnings("unchecked")
public void drop(DropTargetDropEvent e) {
    try {
        int dropAction = e.getDropAction();
        if (dropAction == DnDConstants.ACTION_LINK) {
            DataFlavor channelListDataFlavor = new ChannelListDataFlavor();
            Transferable tr = e.getTransferable();
            if (e.isDataFlavorSupported(channelListDataFlavor)) {
                e.acceptDrop(DnDConstants.ACTION_LINK);
                e.dropComplete(true);

                // calculate which table the x coordinate of the mouse
                // corresponds to
                double clickX = e.getLocation().getX(); // get the mouse x
                // coordinate
                int compWidth = dataComponent.getWidth(); // gets the
                // width of this
                // component
                final int tableNum = (int) (clickX * columnGroupCount / compWidth);

                final List<String> channels = (List) tr.getTransferData(channelListDataFlavor);

                new Thread() {
                    public void run() {
                        for (int i = 0; i < channels.size(); i++) {
                            String channel = channels.get(i);
                            boolean status;
                            if (supportsMultipleChannels()) {
                                status = addChannel(channel, tableNum);
                            } else {
                                status = setChannel(channel);
                            }

                            if (!status) {
                                // TODO display an error in the UI
                            }
                        }
                    }
                }.start();
            } else {
                e.rejectDrop();
            }
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (UnsupportedFlavorException ufe) {
        ufe.printStackTrace();
    }
}