Example usage for java.awt.dnd DnDConstants ACTION_COPY

List of usage examples for java.awt.dnd DnDConstants ACTION_COPY

Introduction

In this page you can find the example usage for java.awt.dnd DnDConstants ACTION_COPY.

Prototype

int ACTION_COPY

To view the source code for java.awt.dnd DnDConstants ACTION_COPY.

Click Source Link

Document

An int representing a "copy" action.

Usage

From source file:net.sf.nmedit.jtheme.component.JTModuleContainer.java

public void dropNewModule(DropTargetDropEvent dtde) {
    PModuleContainer mc = getModuleContainer();
    PModuleDescriptor md = PDragDrop.getModuleDescriptor(dtde.getTransferable());
    if (md == null || mc == null) {
        dtde.rejectDrop();/*from  w  w w.  j  a  va 2s  . co  m*/
        return;
    }

    Point l = dtde.getLocation();

    PModule module;
    try {
        module = mc.createModule(md);
        module.setScreenLocation(l.x, l.y);
    } catch (InvalidDescriptorException e) {
        if (log.isErrorEnabled()) {
            log.error("could not create module: " + md, e);
        }
        dtde.rejectDrop();
        return;
    }
    boolean moduleAdded = mc.add(module);

    if (!moduleAdded) {

        dtde.rejectDrop();
        return;
    }

    // TODO short after dropping a new module and then moving it
    // causes a NullPointerException in the next line
    PModuleContainer parent = module.getParentComponent();
    if (parent != null) {
        JTCableManager cm = getCableManager();
        try {
            cm.setAutoRepaintDisabled();
            MoveOperation move = parent.createMoveOperation();
            move.setScreenOffset(0, 0);
            move.add(module);
            move.move();
        } finally {
            cm.clearAutoRepaintDisabled();
        }
    } else {
        // XXX concurrency problems probably ?!
        throw new RuntimeException("Drop problem on illegal modules: for example 2 midi globals");
    }

    dtde.acceptDrop(DnDConstants.ACTION_COPY);

    // compute dimensions of container
    revalidate();
    repaint();
    dtde.dropComplete(true);
}

From source file:DragDropTreeExample.java

protected void transferFile(int action, File srcFile, File targetDirectory, FileTree.FileTreeNode targetNode) {
    DnDUtils.debugPrintln((action == DnDConstants.ACTION_COPY ? "Copy" : "Move") + " file "
            + srcFile.getAbsolutePath() + " to " + targetDirectory.getAbsolutePath());

    // Create a File entry for the target
    String name = srcFile.getName();
    File newFile = new File(targetDirectory, name);
    if (newFile.exists()) {
        // Already exists - is it the same file?
        if (newFile.equals(srcFile)) {
            // Exactly the same file - ignore
            return;
        }/*from w  ww.  ja  va  2 s  . co m*/
        // File of this name exists in this directory
        if (copyOverExistingFiles == false) {
            int res = JOptionPane.showOptionDialog(tree,
                    "A file called\n   " + name + "\nalready exists in the directory\n   "
                            + targetDirectory.getAbsolutePath() + "\nOverwrite it?",
                    "File Exists", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                    new String[] { "Yes", "Yes to All", "No", "Cancel" }, "No");
            switch (res) {
            case 1: // Yes to all
                copyOverExistingFiles = true;
            case 0: // Yes
                break;
            case 2: // No
                return;
            default: // Cancel
                throw new IllegalStateException("Cancelled");
            }
        }
    } else {
        // New file - create it
        try {
            newFile.createNewFile();
        } catch (IOException e) {
            JOptionPane.showMessageDialog(tree, "Failed to create new file\n  " + newFile.getAbsolutePath(),
                    "File Creation Failed", JOptionPane.ERROR_MESSAGE);
            return;
        }
    }

    // Copy the data and close file.
    BufferedInputStream is = null;
    BufferedOutputStream os = null;

    try {
        is = new BufferedInputStream(new FileInputStream(srcFile));
        os = new BufferedOutputStream(new FileOutputStream(newFile));
        int size = 4096;
        byte[] buffer = new byte[size];
        int len;
        while ((len = is.read(buffer, 0, size)) > 0) {
            os.write(buffer, 0, len);
        }
    } catch (IOException e) {
        JOptionPane.showMessageDialog(tree,
                "Failed to copy file\n  " + name + "\nto directory\n  " + targetDirectory.getAbsolutePath(),
                "File Copy Failed", JOptionPane.ERROR_MESSAGE);
        return;
    } finally {
        try {
            if (is != null) {
                is.close();
            }
            if (os != null) {
                os.close();
            }
        } catch (IOException e) {
        }
    }

    // Remove the source if this is a move operation.
    if (action == DnDConstants.ACTION_MOVE && System.getProperty("DnDExamples.allowRemove") != null) {
        srcFile.delete();
    }

    // Update the tree display
    if (targetNode != null) {
        tree.addNode(targetNode, name);
    }
}

From source file:org.jets3t.apps.uploader.Uploader.java

/**
 * Initialise the application's File drop targets for drag and drop copying of local files
 * to S3.//from  w  ww .  j av  a 2 s.c o  m
 *
 * @param dropTargetComponents
 * the components files can be dropped on to transfer them to S3
 */
private void initDropTarget(Component[] dropTargetComponents) {
    DropTargetListener dropTargetListener = new DropTargetListener() {

        private Border originalBorder = appContentPanel.getBorder();
        private Border dragOverBorder = BorderFactory.createBevelBorder(1);

        private boolean checkValidDrag(DropTargetDragEvent dtde) {
            if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)
                    && (DnDConstants.ACTION_COPY == dtde.getDropAction()
                            || DnDConstants.ACTION_MOVE == dtde.getDropAction())) {
                dtde.acceptDrag(dtde.getDropAction());
                return true;
            } else {
                dtde.rejectDrag();
                return false;
            }
        }

        public void dragEnter(DropTargetDragEvent dtde) {
            if (checkValidDrag(dtde)) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        appContentPanel.setBorder(dragOverBorder);
                    };
                });
            }
        }

        public void dragOver(DropTargetDragEvent dtde) {
            checkValidDrag(dtde);
        }

        public void dropActionChanged(DropTargetDragEvent dtde) {
            checkValidDrag(dtde);
        }

        public void dragExit(DropTargetEvent dte) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    appContentPanel.setBorder(originalBorder);
                };
            });
        }

        public void drop(DropTargetDropEvent dtde) {
            if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)
                    && (DnDConstants.ACTION_COPY == dtde.getDropAction()
                            || DnDConstants.ACTION_MOVE == dtde.getDropAction())) {
                dtde.acceptDrop(dtde.getDropAction());
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        appContentPanel.setBorder(originalBorder);
                    };
                });

                try {
                    final List fileList = (List) dtde.getTransferable()
                            .getTransferData(DataFlavor.javaFileListFlavor);
                    if (fileList != null && fileList.size() > 0) {
                        if (checkProposedUploadFiles(fileList)) {
                            wizardStepForward();
                        }
                    }
                } catch (Exception e) {
                    String errorMessage = "Unable to accept dropped item";
                    log.error(errorMessage, e);
                    ErrorDialog.showDialog(ownerFrame, null, uploaderProperties.getProperties(), errorMessage,
                            e);
                }
            } else {
                dtde.rejectDrop();
            }
        }
    };

    // Attach drop target listener to each target component.
    for (int i = 0; i < dropTargetComponents.length; i++) {
        new DropTarget(dropTargetComponents[i], DnDConstants.ACTION_COPY, dropTargetListener, true);
    }
}

From source file:DragDropTreeExample.java

protected void transferDirectory(int action, File srcDir, File targetDirectory,
        FileTree.FileTreeNode targetNode) {
    DnDUtils.debugPrintln((action == DnDConstants.ACTION_COPY ? "Copy" : "Move") + " directory "
            + srcDir.getAbsolutePath() + " to " + targetDirectory.getAbsolutePath());

    // Do not copy a directory into itself or
    // a subdirectory of itself.
    File parentDir = targetDirectory;
    while (parentDir != null) {
        if (parentDir.equals(srcDir)) {
            DnDUtils.debugPrintln("-- SUPPRESSED");
            return;
        }/*www.  j  a v  a  2  s . co  m*/
        parentDir = parentDir.getParentFile();
    }

    // Copy the directory itself, then its contents

    // Create a File entry for the target
    String name = srcDir.getName();
    File newDir = new File(targetDirectory, name);
    if (newDir.exists()) {
        // Already exists - is it the same directory?
        if (newDir.equals(srcDir)) {
            // Exactly the same file - ignore
            return;
        }
    } else {
        // Directory does not exist - create it
        if (newDir.mkdir() == false) {
            // Failed to create - abandon this directory
            JOptionPane.showMessageDialog(tree,
                    "Failed to create target directory\n  " + newDir.getAbsolutePath(),
                    "Directory creation Failed", JOptionPane.ERROR_MESSAGE);
            return;
        }
    }

    // Add a node for the new directory
    if (targetNode != null) {
        targetNode = tree.addNode(targetNode, name);
    }

    // Now copy the directory content.
    File[] files = srcDir.listFiles();
    for (int i = 0; i < files.length; i++) {
        File f = files[i];
        if (f.isFile()) {
            transferFile(action, f, newDir, targetNode);
        } else if (f.isDirectory()) {
            transferDirectory(action, f, newDir, targetNode);
        }
    }

    // Remove the source directory after moving
    if (action == DnDConstants.ACTION_MOVE && System.getProperty("DnDExamples.allowRemove") != null) {
        srcDir.delete();
    }
}

From source file:org.jets3t.apps.cockpit.Cockpit.java

/**
 * Initialise the application's File drop targets for drag and drop copying of local files
 * to S3./*from  www .j av a2  s  .c o  m*/
 *
 * @param dropTargetComponents
 * the components files can be dropped on to transfer them to S3
 */
private void initDropTarget(JComponent[] dropTargetComponents) {
    DropTargetListener dropTargetListener = new DropTargetListener() {

        private boolean checkValidDrag(DropTargetDragEvent dtde) {
            if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)
                    && (DnDConstants.ACTION_COPY == dtde.getDropAction()
                            || DnDConstants.ACTION_MOVE == dtde.getDropAction())) {
                dtde.acceptDrag(dtde.getDropAction());
                return true;
            } else {
                dtde.rejectDrag();
                return false;
            }
        }

        public void dragEnter(DropTargetDragEvent dtde) {
            if (checkValidDrag(dtde)) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        objectsTable.requestFocusInWindow();
                    };
                });
            }
        }

        public void dragOver(DropTargetDragEvent dtde) {
            checkValidDrag(dtde);
        }

        public void dropActionChanged(DropTargetDragEvent dtde) {
            if (checkValidDrag(dtde)) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        objectsTable.requestFocusInWindow();
                    };
                });
            } else {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        ownerFrame.requestFocusInWindow();
                    };
                });
            }
        }

        public void dragExit(DropTargetEvent dte) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    ownerFrame.requestFocusInWindow();
                };
            });
        }

        public void drop(DropTargetDropEvent dtde) {
            if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)
                    && (DnDConstants.ACTION_COPY == dtde.getDropAction()
                            || DnDConstants.ACTION_MOVE == dtde.getDropAction())) {
                dtde.acceptDrop(dtde.getDropAction());
                try {
                    final List fileList = (List) dtde.getTransferable()
                            .getTransferData(DataFlavor.javaFileListFlavor);
                    if (fileList != null && fileList.size() > 0) {
                        uploadFiles((File[]) fileList.toArray(new File[fileList.size()]));
                    }
                } catch (Exception e) {
                    String message = "Unable to start accept dropped items";
                    log.error(message, e);
                    ErrorDialog.showDialog(ownerFrame, null, message, e);
                }
            } else {
                dtde.rejectDrop();
            }
        }
    };

    // Attach drop target listener to each target component.
    for (int i = 0; i < dropTargetComponents.length; i++) {
        new DropTarget(dropTargetComponents[i], DnDConstants.ACTION_COPY, dropTargetListener, true);
    }
}

From source file:javazoom.jlgui.player.amp.Player.java

/**
 * DnD : Drop implementation.//from   ww  w  .j  av a 2s .  co  m
 * Adds all dropped files to the playlist.
 */
public void drop(DropTargetDropEvent e) {
    // Check DataFlavor
    DataFlavor[] dfs = e.getCurrentDataFlavors();
    DataFlavor tdf = null;
    for (int i = 0; i < dfs.length; i++) {
        if (DataFlavor.javaFileListFlavor.equals(dfs[i])) {
            tdf = dfs[i];
            break;
        }
    }
    // Is file list ?
    if (tdf != null) {
        // Accept COPY DnD only.
        if ((e.getSourceActions() & DnDConstants.ACTION_COPY) != 0) {
            e.acceptDrop(DnDConstants.ACTION_COPY);
        } else
            return;
        try {
            Transferable t = e.getTransferable();
            Object data = t.getTransferData(tdf);
            // How many files ?
            if (data instanceof java.util.List) {
                java.util.List al = (java.util.List) data;
                // Read the first File.
                if (al.size() > 0) {
                    File file = null;
                    // Stops the player if needed.
                    if ((playerState == PLAY) || (playerState == PAUSE)) {
                        theSoundPlayer.stop();
                        playerState = STOP;
                    }
                    // Clean the playlist.
                    playlist.removeAllItems();
                    // Add all dropped files to playlist.
                    ListIterator li = al.listIterator();
                    while (li.hasNext()) {
                        file = (File) li.next();
                        PlaylistItem pli = null;
                        if (file != null) {

                            pli = new PlaylistItem(file.getName(), file.getAbsolutePath(), -1, true);
                            if (pli != null)
                                playlist.appendItem(pli);
                        }
                    }
                    // Start the playlist from the top.
                    playlist.nextCursor();
                    fileList.initPlayList();
                    this.setCurrentSong(playlist.getCursor());
                }
            } else {
                log.info("Unknown dropped objects");
            }
        } catch (IOException ioe) {
            log.info("Drop error", ioe);
            e.dropComplete(false);
            return;
        } catch (UnsupportedFlavorException ufe) {
            log.info("Drop error", ufe);
            e.dropComplete(false);
            return;
        } catch (Exception ex) {
            log.info("Drop error", ex);
            e.dropComplete(false);
            return;
        }
        e.dropComplete(true);
    }
}

From source file:javazoom.jlgui.player.amp.Player.java

/**
 * Checks if Drag allowed./*from  www .  j  av  a2 s  .  com*/
 */
protected boolean isDragOk(DropTargetDragEvent e) {
    // Check DataFlavor
    DataFlavor[] dfs = e.getCurrentDataFlavors();
    DataFlavor tdf = null;
    for (int i = 0; i < dfs.length; i++) {
        if (DataFlavor.javaFileListFlavor.equals(dfs[i])) {
            tdf = dfs[i];
            break;
        }
    }
    // Only file list allowed.
    if (tdf != null) {
        // Only DnD COPY allowed.
        if ((e.getSourceActions() & DnDConstants.ACTION_COPY) != 0) {
            return true;
        } else
            return false;
    } else
        return false;
}

From source file:org.apache.cayenne.modeler.ProjectTreeView.java

public ProjectTreeView(ProjectController mediator) {
    super();/*from  w w w.j  ava  2  s . c  o  m*/
    this.mediator = mediator;

    initView();
    initController();
    initFromModel(Application.getInstance().getProject());
    this.tds = new TreeDragSource(this, DnDConstants.ACTION_COPY, mediator);
}

From source file:org.fanhongtao.filetools.HexViewPanel.java

public HexViewPanel() {
    super(new BorderLayout());

    textArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(textArea);
    add(scrollPane);//from  w  w  w . j a  v a 2  s  .  c  om

    textArea.setEditable(false);
    textArea.append("Drop a file to here\n");
    textArea.setFont(new Font("Consolas", Font.PLAIN, 16));
    //         textArea.setFont(new Font("Courier New", Font.PLAIN, 16));
    new DropTarget(textArea, DnDConstants.ACTION_COPY, new FileDropper(new FileDropListener() {

        @Override
        public void onDropFile(File file) {
            showHex(file);
        }
    }));
}

From source file:org.jas.dnd.FileDragSource.java

public static void addDragSource(Component c, FileSelection modelSelection) {
    DragSource dragSource = new DragSource();
    FileDragSource dgl = new FileDragSource(dragSource, modelSelection);
    // Do not change ACTION_COPY or else only pain and misery will follow you until the end of time. JAVA DnDrops sux.
    // Big Time// w  w  w .j a va  2 s.c o m
    dragSource.createDefaultDragGestureRecognizer(c, DnDConstants.ACTION_COPY, dgl);
}