Example usage for java.awt.dnd DropTargetDropEvent rejectDrop

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

Introduction

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

Prototype


public void rejectDrop() 

Source Link

Document

reject the Drop.

Usage

From source file:pkgrenamer.Main.java

static void onDropFiles(DropTargetDropEvent dtde, FileDropListener onDrop) {
    try {/*  w w  w.j a  va  2  s .c  o  m*/
        Transferable transferable = dtde.getTransferable();

        if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            dtde.acceptDrop(DnDConstants.ACTION_MOVE);
            java.util.List<?> files = (java.util.List<?>) transferable
                    .getTransferData(DataFlavor.javaFileListFlavor);
            File[] fa = new File[files.size()];
            for (int i = 0; i < fa.length; i++) {
                fa[i] = (File) files.get(i);
            }
            onDrop.onDropFile(fa);
            dtde.getDropTargetContext().dropComplete(true);

        } else {
            if (sNixFileDataFlavor == null) {
                sNixFileDataFlavor = new DataFlavor("text/uri-list;class=java.lang.String");
            }
            dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
            String data = (String) transferable.getTransferData(sNixFileDataFlavor);
            if (data != null) {
                ArrayList<File> fs = new ArrayList<>();
                for (StringTokenizer st = new StringTokenizer(data, "\r\n"); st.hasMoreTokens();) {
                    String token = st.nextToken().trim();
                    if (token.startsWith("#") || token.isEmpty()) {
                        continue;
                    }
                    try {
                        fs.add(new File(new URI(token)));
                    } catch (Exception e) {
                    }
                }
                onDrop.onDropFile(fs.toArray(new File[0]));
                dtde.getDropTargetContext().dropComplete(true);
            } else {
                dtde.rejectDrop();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:tk.tomby.tedit.core.Workspace.java

/**
 * Creates a new WorkSpace object./*from   ww w  .  j av a2  s  .co  m*/
 */
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);
}

From source file:tvbrowser.ui.mainframe.MainFrame.java

@Override
public void drop(DropTargetDropEvent dtde) {
    dtde.acceptDrop(dtde.getDropAction());
    File[] files = getDragDropPlugins(dtde.getCurrentDataFlavors(), dtde.getTransferable());

    try {//  ww w .ja va2 s . com
        File tmpFile = File.createTempFile("plugins", ".txt");
        StringBuilder alreadyInstalled = new StringBuilder();
        StringBuilder notCompatiblePlugins = new StringBuilder();

        for (File jarFile : files) {
            ClassLoader classLoader = null;

            try {
                URL[] urls = new URL[] { jarFile.toURI().toURL() };
                classLoader = URLClassLoader.newInstance(urls, ClassLoader.getSystemClassLoader());
            } catch (MalformedURLException exc) {

            }

            if (classLoader != null) {
                // Get the plugin name
                String pluginName = jarFile.getName();
                pluginName = pluginName.substring(0, pluginName.length() - 4);

                try {
                    String pluginId = "java." + pluginName.toLowerCase() + "." + pluginName;

                    PluginProxy installedPlugin = PluginProxyManager.getInstance().getPluginForId(pluginId);
                    TvDataServiceProxy service = TvDataServiceProxyManager.getInstance()
                            .findDataServiceById(pluginName.toLowerCase() + '.' + pluginName);

                    Class<?> pluginClass = classLoader.loadClass(pluginName.toLowerCase() + '.' + pluginName);

                    Method getVersion = pluginClass.getMethod("getVersion", new Class[0]);

                    Version version1 = null;
                    try {
                        version1 = (Version) getVersion.invoke(pluginClass, new Object[0]);
                    } catch (Throwable t1) {
                        t1.printStackTrace();
                    }

                    if (installedPlugin != null
                            && (installedPlugin.getInfo().getVersion().compareTo(version1) > 0
                                    || (installedPlugin.getInfo().getVersion().compareTo(version1) == 0
                                            && version1.isStable()))) {
                        alreadyInstalled.append(installedPlugin.getInfo().getName()).append('\n');
                    } else if (service != null && (service.getInfo().getVersion().compareTo(version1) > 0
                            || (service.getInfo().getVersion().compareTo(version1) == 0
                                    && version1.isStable()))) {
                        alreadyInstalled.append(service.getInfo().getName()).append('\n');
                    } else {
                        RandomAccessFile write = new RandomAccessFile(tmpFile, "rw");

                        String versionString = Integer.toString(version1.getMajor()) + '.'
                                + (version1.getMinor() / 10) + (version1.getMinor() % 10) + '.'
                                + version1.getSubMinor();

                        write.seek(write.length());

                        write.writeBytes("[plugin:" + pluginName + "]\n");
                        write.writeBytes("name_en=" + pluginName + "\n");
                        write.writeBytes("filename=" + jarFile.getName() + "\n");
                        write.writeBytes("version=" + versionString + "\n");
                        write.writeBytes("stable=" + version1.isStable() + "\n");
                        write.writeBytes("download=" + jarFile.toURI().toURL() + "\n");
                        write.writeBytes("category=unknown\n");

                        write.close();

                    }
                } catch (Exception e) {
                    notCompatiblePlugins.append(jarFile.getName()).append("\n");
                }
            }
        }

        if (alreadyInstalled.length() > 0) {
            showInfoTextMessage(
                    mLocalizer.msg("update.alreadyInstalled",
                            "The following Plugin in current version are already installed:"),
                    alreadyInstalled.toString(), 400);
        }

        if (notCompatiblePlugins.length() > 0) {
            showInfoTextMessage(
                    mLocalizer.msg("update.noTVBPlugin", "This following files are not TV-Browser Plugins:"),
                    notCompatiblePlugins.toString(), 400);
        }

        if (tmpFile.length() > 0) {
            java.net.URL url = tmpFile.toURI().toURL();
            SoftwareUpdater softwareUpdater = new SoftwareUpdater(url, false, true);
            mSoftwareUpdateItems = softwareUpdater.getAvailableSoftwareUpdateItems();
            dtde.dropComplete(true);

            SoftwareUpdateDlg updateDlg = new SoftwareUpdateDlg(this, false, mSoftwareUpdateItems);
            updateDlg.setVisible(true);
        } else {
            dtde.rejectDrop();
            dtde.dropComplete(false);
        }

        if (!tmpFile.delete()) {
            tmpFile.deleteOnExit();
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:unikn.dbis.univis.visualization.pivottable.VPivotTable.java

/**
 * Called when the drag operation has terminated with a drop on
 * the operable part of the drop site for the <code>DropTarget</code>
 * registered with this listener.//w  ww . j a  v  a  2s.c o  m
 * <p/>
 * This method is responsible for undertaking
 * the transfer of the data associated with the
 * gesture. The <code>DropTargetDropEvent</code>
 * provides a means to obtain a <code>Transferable</code>
 * object that represents the data object(s) to
 * be transfered.<P>
 * From this method, the <code>DropTargetListener</code>
 * shall accept or reject the drop via the
 * acceptDrop(int dropAction) or rejectDrop() methods of the
 * <code>DropTargetDropEvent</code> parameter.
 * <p/>
 * Subsequent to acceptDrop(), but not before,
 * <code>DropTargetDropEvent</code>'s getTransferable()
 * method may be invoked, and data transfer may be
 * performed via the returned <code>Transferable</code>'s
 * getTransferData() method.
 * <p/>
 * At the completion of a drop, an implementation
 * of this method is required to signal the success/failure
 * of the drop by passing an appropriate
 * <code>boolean</code> to the <code>DropTargetDropEvent</code>'s
 * dropComplete(boolean success) method.
 * <p/>
 * Note: The data transfer should be completed before the call  to the
 * <code>DropTargetDropEvent</code>'s dropComplete(boolean success) method.
 * After that, a call to the getTransferData() method of the
 * <code>Transferable</code> returned by
 * <code>DropTargetDropEvent.getTransferable()</code> is guaranteed to
 * succeed only if the data transfer is local; that is, only if
 * <code>DropTargetDropEvent.isLocalTransfer()</code> returns
 * <code>true</code>. Otherwise, the behavior of the call is
 * implementation-dependent.
 * <p/>
 *
 * @param dtde the <code>DropTargetDropEvent</code>
 */

public void drop(DropTargetDropEvent dtde) {
    Object o = null;
    try {
        if (dtde.getTransferable().isDataFlavorSupported(VDataReferenceFlavor.COMBINATION_FLAVOR)) {
            o = dtde.getTransferable().getTransferData(VDataReferenceFlavor.COMBINATION_FLAVOR);
        }

        if (o == null) {
            dtde.rejectDrop();
            dtde.dropComplete(false);
        }
    } catch (UnsupportedFlavorException ufe) {
        dtde.rejectDrop();
        dtde.dropComplete(false);
        /*if (LOG.isErrorEnabled()) {
           LOG.error(ufe.getMessage(), ufe);
        } */
    } catch (IOException ioe) {
        dtde.rejectDrop();
        dtde.dropComplete(false);
        /*if (LOG.isErrorEnabled()) {
           LOG.error(ioe.getMessage(), ioe);
        } */
    }

    if (o instanceof VCombination) {
        VCombination combination = (VCombination) o;

        VDimension dimension = combination.getDimension();

        if (cube == null) {
            Set<VCube> supportedCubes = dimension.getSupportedCubes();
            if (supportedCubes.size() > 1) {
                cube = CubeChooser.showCubeChooser(VPivotTable.this, supportedCubes);
            } else {
                cube = supportedCubes.iterator().next();
            }

            // Check whether a cube exists or a cube has been selected on the
            // cube chooser. The chooser maybe interrupted with window close
            // and then return null.
            if (cube == null) {
                return;
            }
        }

        if (measure == null) {
            measure = combination.getMeasure();
        }

        if (function == null) {
            function = combination.getFunction();
        }

        addDimension(dimension, dtde);
        setAncestorsDropped(dimension, true);
        dtde.dropComplete(true);
    }
}