Example usage for java.awt.dnd DropTarget DropTarget

List of usage examples for java.awt.dnd DropTarget DropTarget

Introduction

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

Prototype

public DropTarget() throws HeadlessException 

Source Link

Document

Creates a DropTarget .

Usage

From source file:view.MainWindow.java

private void setupDropListener() {
    this.setDropTarget(new DropTarget() {
        @Override//from   w  ww. ja  v a2s .co  m
        public synchronized void drop(DropTargetDropEvent evt) {
            try {
                evt.acceptDrop(DnDConstants.ACTION_COPY);
                List<File> droppedFiles = (List<File>) evt.getTransferable()
                        .getTransferData(DataFlavor.javaFileListFlavor);
                if (!droppedFiles.isEmpty()) {
                    for (File file : droppedFiles) {
                        if (file.exists() && file.isFile() && file.getAbsolutePath().endsWith(".pdf")) {
                            if (loadPdf(file, false)) {
                                ctrl.openDocument(file.getAbsolutePath());
                                workspacePanel.setDocument(ctrl.getDocument());
                            } else {
                                errorList.add(file.getAbsolutePath());
                            }
                        } else if (file.exists() && file.isDirectory()) {
                            loadFolder(file, true);
                        }
                    }
                    showErrors();
                }
            } catch (UnsupportedFlavorException | IOException ex) {
                controller.Logger.getLogger().addEntry(ex);
            }
        }
    });
}