All Downloads are FREE. Search and download functionalities are using the official Maven repository.

VAqua.src.org.violetlib.aqua.fc.FileTransferHandler Maven / Gradle / Ivy

The newest version!
/*
 * @(#)FileTransferHandler.java
 *
 * Copyright (c) 2004-2013 Werner Randelshofer, Switzerland.
 * You may not use, copy or modify this file, except in compliance with the
 * accompanying license terms.
 */

package org.violetlib.aqua.fc;

import javax.swing.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.*;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * FileTransferHandler.
 *
 * @author  Werner Randelshofer
 * @version $Id$
 */
public class FileTransferHandler implements DropTargetListener {

    private JFileChooser fileChooser;

    /** Creates a new instance. */
    public FileTransferHandler(JFileChooser fileChooser) {
        this.fileChooser = fileChooser;
    }

    public void setFileChooser(JFileChooser fileChooser) {
        this.fileChooser = fileChooser;
    }

    /**
     * Called while a drag operation is ongoing, when the mouse pointer enters
     * the operable part of the drop site for the DropTarget
     * registered with this listener.
     *
     * @param evt the DropTargetDragEvent
     */
    public void dragEnter(DropTargetDragEvent evt) {
        handleDrag(evt);
    }

    private void handleDrag(DropTargetDragEvent evt) {
        // Reject if flavor not supported
        if (!evt.getCurrentDataFlavorsAsList().contains(DataFlavor.javaFileListFlavor)) {
            evt.rejectDrag();
        }
        // Change drag operation or reject if no suitable operation available
        if (evt.getDropAction() != DnDConstants.ACTION_COPY) {
            if ((evt.getSourceActions() & DnDConstants.ACTION_COPY) != 0) {
                evt.acceptDrag(DnDConstants.ACTION_COPY);
            } else {
                evt.rejectDrag();
            }
        }
    }

    /**
     * Called when a drag operation is ongoing, while the mouse pointer is still
     * over the operable part of the drop site for the DropTarget
     * registered with this listener.
     *
     * @param evt the DropTargetDragEvent
     */
    public void dragOver(DropTargetDragEvent evt) {
        handleDrag(evt);
    }

    /**
     * Called if the user has modified
     * the current drop gesture.
     *
     * @param evt the DropTargetDragEvent
     */
    public void dropActionChanged(DropTargetDragEvent evt) {
        handleDrag(evt);
    }

    /**
     * Called while a drag operation is ongoing, when the mouse pointer has
     * exited the operable part of the drop site for the
     * DropTarget registered with this listener.
     *
     * @param evt the DropTargetEvent
     */
    public void dragExit(DropTargetEvent evt) {
    }

    /**
     * Called when the drag operation has terminated with a drop on
     * the operable part of the drop site for the DropTarget
     * registered with this listener.
     * 

* This method is responsible for undertaking * the transfer of the data associated with the * gesture. The DropTargetDropEvent * provides a means to obtain a Transferable * object that represents the data object(s) to * be transfered.

* From this method, the DropTargetListener * shall accept or reject the drop via the * acceptDrop(int dropAction) or rejectDrop() methods of the * DropTargetDropEvent parameter. *

* Subsequent to acceptDrop(), but not before, * DropTargetDropEvent's getTransferable() * method may be invoked, and data transfer may be * performed via the returned Transferable's * getTransferData() method. *

* 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 * boolean to the DropTargetDropEvent's * dropComplete(boolean success) method. *

* Note: The data transfer should be completed before the call to the * DropTargetDropEvent's dropComplete(boolean success) method. * After that, a call to the getTransferData() method of the * Transferable returned by * DropTargetDropEvent.getTransferable() is guaranteed to * succeed only if the data transfer is local; that is, only if * DropTargetDropEvent.isLocalTransfer() returns * true. Otherwise, the behavior of the call is * implementation-dependent. *

* @param evt the DropTargetDropEvent */ public void drop(DropTargetDropEvent evt) { evt.acceptDrop(DnDConstants.ACTION_COPY); boolean success = false; try { if (fileChooser != null) { List files = (List) evt.getTransferable().getTransferData(DataFlavor.javaFileListFlavor); File[] fileArray = (File[]) files.toArray(new File[files.size()]); if (fileArray.length > 0) { if (fileChooser.getUI() instanceof SubtreeFileChooserUI) { // The file chooser has a settable root directory. // -> Determine which directory to set as the root. File dir = fileArray[0]; if (dir.isDirectory() && fileArray.length==1 // && fileChooser.getFileSelectionMode() == JFileChooser.FILES_ONLY) { // The user dropped a directory on a file chooser which // only selects files // -> Make the directory the current directory of the file chooser. ((SubtreeFileChooserUI) fileChooser.getUI()).selectDirectory(dir); } else { // The user dropped a file or directory on a file chooser which // selects files or directories (or both) // -> Make the parent directory the current directory of the file chooser. dir = dir.getParentFile(); if (dir != null) { ((SubtreeFileChooserUI) fileChooser.getUI()).selectDirectory(dir); } fileChooser.setSelectedFiles(fileArray); } } else { // The file chooser has not a settable root directory. // -> Just set the selected files. fileChooser.setSelectedFiles(fileArray); } } } success = true; } catch (UnsupportedFlavorException e) { } catch (IOException e) { } evt.dropComplete(success); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy